Commit 8e287a49 authored by 潘栩锋's avatar 潘栩锋 🚴

IDBTable 添加 FreeID, 替代 SQLiteDbContext 中的 IDs

parent 406aa0b8
......@@ -12,11 +12,30 @@ namespace SQLite
public List<SQLiteHelper.ArrayFieldTypeInfo> ArrayFieldTypeInfos = new List<SQLiteHelper.ArrayFieldTypeInfo>();
public string TableName { get; private set; }
private long freeID = 0;
/// <summary>
/// 每次获取,自动++
/// </summary>
public long FreeID
{
get
{
return freeID++;
}
set {
freeID = value;
}
}
private string ddl;
public string DDL
{
get
{
return SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray());
if (ArrayFieldTypeInfos.Count() == 0)
return ddl;
else
return SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray());
}
}
......@@ -25,14 +44,11 @@ namespace SQLite
public DBTable()
{
TableName = SQLiteHelper.GetTableName(typeof(T));
ddl = SQLiteHelper.GetCreateTableCommandText(typeof(T));
}
public void Init(string connectionString)
public void Init(SQLiteHelper sQLiteHelper)
{
sqliteHelper = new SQLiteHelper
{
ConnectionString = connectionString
};
sqliteHelper = sQLiteHelper;
}
public void Create()
......
......@@ -11,19 +11,24 @@ namespace SQLite
/// 表名
/// </summary>
string TableName { get; }
/// <summary>
/// 表的 Create SQL
/// </summary>
string DDL { get; }
/// <summary>
/// 创建表
/// </summary>
void Create();
long FreeID { get; set; }
/// <summary>
/// 初始化
/// </summary>
/// <param name="connnectionString"></param>
void Init(string connnectionString);
/// <param name="sQLiteHelper"></param>
void Init(SQLiteHelper sQLiteHelper);
}
}
......@@ -18,14 +18,11 @@ namespace SQLite
return string.Format("Data Source={0};Version=3;", DBPath);
}
}
public string DBPath { get; set; } = @"test.sqlite3";
/// <summary>
/// 当前全部表的ID, 与 TbTableID 同步
/// </summary>
public Dictionary<IDBTable, Int64> IDs = new Dictionary<IDBTable, Int64>();
public string DBPath { get; private set; } = @"test.sqlite3";
public SQLiteDbContext()
public SQLiteDbContext(string dbPath)
{
DBPath = dbPath;
Constructor();
}
void Constructor()
......@@ -43,11 +40,14 @@ namespace SQLite
}
foreach (IDBTable dBTable in DbSet)
{
dBTable.Init(ConnectionString);
IDs.Add(dBTable, 0);
dBTable.Init(sqliteHelper);
}
}
public void SetDBPath(string dbPath)
{
DBPath = dbPath;
sqliteHelper.ConnectionString = ConnectionString;
}
void Build()
{
......@@ -79,10 +79,9 @@ namespace SQLite
void Load()
{
for (int i = 0; i < IDs.Count(); i++)
foreach(var table in DbSet)
{
var table = IDs.Keys.ElementAt(i);
IDs[table] = LoadID(table.TableName);
table.FreeID = LoadID(table.TableName);
}
}
long LoadID(string tablename)
......
......@@ -228,6 +228,9 @@ namespace SQLite
}
public static string GetCreateTableCommandText(Type type, params ArrayFieldTypeInfo[] arrayFieldTypeInfos)
{
if (arrayFieldTypeInfos == null)
arrayFieldTypeInfos = new ArrayFieldTypeInfo[0];
//CREATE TABLE table_name(
//column1 datatype PRIMARY KEY,
//column2 datatype,
......
......@@ -15,7 +15,7 @@ namespace UnitTestProject1.Model
public DBTable<Book> Books { get; set; } = new DBTable<Book>();
public DBModel()
public DBModel(string dbPath):base(dbPath)
{
}
......
......@@ -22,22 +22,21 @@ namespace UnitTestProject1
cfg.AddProfiles("UnitTestProject1");
AutoMapper.Mapper.Initialize(cfg);
Model.DBModel dBModel = new Model.DBModel();
Model.DBModel dBModel = new Model.DBModel("test.sqlite3");
dBModel.Init();
var books_db = new List<Model.Book>
{
new Model.Book()
{
ID = dBModel.IDs[dBModel.Books]++,
ID = dBModel.Books.FreeID,
BookName = "ASP.NET MVC 5 高级编程(第5版)",
PageCount = 460,
PrintTime = new DateTime(2019,8,8)
},
new Model.Book()
{
ID = dBModel.IDs[dBModel.Books]++,
ID = dBModel.Books.FreeID,
BookName = "c# 入门经典(第7版)",
PageCount = 432,
PrintTime = new DateTime(2012,5,20)
......@@ -46,7 +45,7 @@ namespace UnitTestProject1
var users_lc = new List<Model.UserLC>
{
new Model.UserLC(){
ID = dBModel.IDs[dBModel.Users]++,
ID = dBModel.Users.FreeID,
Name = "潘栩锋",
BookIDs = new List<long>{
books_db[0].ID,books_db[1].ID
......@@ -77,14 +76,13 @@ namespace UnitTestProject1
Exception ex = null;
try
{
//var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
var cfg = new AutoMapper.Configuration.MapperConfigurationExpression();
cfg.AddDataReaderMapping();
cfg.AddProfiles("UnitTestProject1");
AutoMapper.Mapper.Initialize(cfg);
Model.DBModel dBModel = new Model.DBModel();
Model.DBModel dBModel = new Model.DBModel("test.sqlite3");
dBModel.Init();
string sql =
......@@ -99,7 +97,7 @@ namespace UnitTestProject1
Console.WriteLine($"SQLiteHelper.ToObjs<Model.User>(table) Elapsed={stopwatch.ElapsedMilliseconds}ms");
stopwatch.Restart();
var users_db2 = AutoMapper.Mapper.Map<IDataReader,List<Model.User>>(table.CreateDataReader());
var users_db2 = AutoMapper.Mapper.Map<IDataReader, List<Model.User>>(table.CreateDataReader());
stopwatch.Stop();
Console.WriteLine($"AutoMapper.Mapper.Map<IDataReader,List<Model.User>>(table.CreateDataReader()) Elapsed={stopwatch.ElapsedMilliseconds}ms");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment