|
@@ -1336,6 +1336,13 @@ public class SQLiteProvider : IProvider
|
|
|
|
|
|
#region Non-Entity Table operations
|
|
|
|
|
|
+ public static readonly string RANDOMGUID =
|
|
|
+ "lower(hex(randomblob(4)) " +
|
|
|
+ "|| '-' || hex(randomblob(2)) " +
|
|
|
+ "|| '-' || '4' || substr(hex( randomblob(2)), 2) " +
|
|
|
+ "|| '-' || substr('AB89', 1 + (abs(random()) % 4) , 1) || substr(hex(randomblob(2)), 2) " +
|
|
|
+ "|| '-' || hex(randomblob(6)))";
|
|
|
+
|
|
|
internal int ExecuteSQL(SQLiteWriteAccessor writer, string statement)
|
|
|
{
|
|
|
var result = 0;
|
|
@@ -1421,18 +1428,41 @@ public class SQLiteProvider : IProvider
|
|
|
Logger.Send(LogType.Error,"",$"Exception in Query({sql})\nMessage: {e.Message}\nStackTrace: {e.StackTrace}");
|
|
|
}
|
|
|
return _result;
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int Update(string sql)
|
|
|
+ {
|
|
|
+ var _result = -1;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var access = GetWriteAccess())
|
|
|
+ {
|
|
|
+ using (var command = access.CreateCommand())
|
|
|
+ {
|
|
|
+ command.CommandText = sql;
|
|
|
+ _result = command.ExecuteNonQuery();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error,"",$"Exception in Query({sql})\nMessage: {e.Message}\nStackTrace: {e.StackTrace}");
|
|
|
+ }
|
|
|
+ return _result;
|
|
|
+ }
|
|
|
|
|
|
public bool TableExists<T>() => TableExists(typeof(T));
|
|
|
|
|
|
- public bool TableExists(Type t)
|
|
|
+ public bool TableExists(Type t) => TableExists(t.EntityName().Split('.').Last());
|
|
|
+
|
|
|
+ public bool TableExists(string name)
|
|
|
{
|
|
|
bool _result = false;
|
|
|
using (var access = GetReadAccess())
|
|
|
{
|
|
|
using (var _check = access.CreateCommand())
|
|
|
{
|
|
|
- _check.CommandText = $"select name from sqlite_master where type='table' and name='{t.Name.Split('.').Last()}';";
|
|
|
+ _check.CommandText = $"select name from sqlite_master where type='table' and name='{name}';";
|
|
|
using (var _reader = _check.ExecuteReader())
|
|
|
{
|
|
|
_result = _reader.HasRows;
|
|
@@ -1450,7 +1480,7 @@ public class SQLiteProvider : IProvider
|
|
|
if (!TableExists(t))
|
|
|
return null;
|
|
|
|
|
|
- var _tablename = t.Name.Split('.').Last();
|
|
|
+ var _tablename = t.EntityName().Split('.').Last();
|
|
|
|
|
|
CoreTable? _result = null;
|
|
|
var props = CoreUtils.PropertyInfoList(t, p => true, true);
|
|
@@ -1495,15 +1525,59 @@ public class SQLiteProvider : IProvider
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public CoreTable? GetTable(string name)
|
|
|
+ {
|
|
|
+ if (!TableExists(name))
|
|
|
+ return null;
|
|
|
+
|
|
|
+ CoreTable? _result = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var access = GetReadAccess())
|
|
|
+ {
|
|
|
+ using (var command = access.CreateCommand())
|
|
|
+ {
|
|
|
+ command.CommandText = $"select * from {name}";
|
|
|
+ using (var reader = command.ExecuteReader())
|
|
|
+ {
|
|
|
+ _result = new CoreTable();
|
|
|
+ var schema = reader.GetColumnSchema();
|
|
|
+ foreach (var column in schema)
|
|
|
+ _result.Columns.Add(new CoreColumn(column.DataType, column.ColumnName));
|
|
|
+
|
|
|
+
|
|
|
+ var _colCount = reader.GetColumnSchema().Count;
|
|
|
+
|
|
|
+ while (reader.Read())
|
|
|
+ {
|
|
|
+ var _row = _result.NewRow();
|
|
|
+ for (int i=0; i <_colCount; i++)
|
|
|
+ ReadAndDecodeValue(_result,reader,_row,i);
|
|
|
+ _result.Rows.Add(_row);
|
|
|
+ }
|
|
|
+ reader.Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error,"",$"Exception in GetTable: {name}\nMessage: {e.Message}\nStackTrace: {e.StackTrace}");
|
|
|
+ }
|
|
|
+ return _result;
|
|
|
+ }
|
|
|
+
|
|
|
public void DropTable<T>() => DropTable(typeof(T));
|
|
|
|
|
|
- public void DropTable(Type t)
|
|
|
+ public void DropTable(Type t) => DropTable(t.EntityName().Split('.').Last());
|
|
|
+
|
|
|
+ public void DropTable(string name)
|
|
|
{
|
|
|
- if (t.IsSubclassOf(typeof(Entity)) || !TableExists(t))
|
|
|
+ if (!TableExists(name))
|
|
|
return;
|
|
|
using (var _access = GetWriteAccess())
|
|
|
{
|
|
|
- ExecuteSQL(_access, $"DROP TABLE {t.EntityName().Split('.').Last()}");
|
|
|
+ ExecuteSQL(_access, $"DROP TABLE {name}");
|
|
|
}
|
|
|
}
|
|
|
|