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

IDBTable 添加 FreeID, 替代 SQLiteDbContext 中的 IDs

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