Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
8e287a49
Commit
8e287a49
authored
Aug 09, 2019
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IDBTable 添加 FreeID, 替代 SQLiteDbContext 中的 IDs
parent
406aa0b8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
52 additions
and
31 deletions
+52
-31
DBTable.cs
Project.SQLiteHelper/SQLiteHelper/DBTable.cs
+23
-7
IDBTable.cs
Project.SQLiteHelper/SQLiteHelper/IDBTable.cs
+7
-2
SQLiteDbContext.cs
Project.SQLiteHelper/SQLiteHelper/SQLiteDbContext.cs
+11
-12
SQLiteHelper.cs
Project.SQLiteHelper/SQLiteHelper/SQLiteHelper.cs
+3
-0
DBModel.cs
Project.SQLiteHelper/UnitTestProject1/Model/DBModel.cs
+1
-1
UnitTest1.cs
Project.SQLiteHelper/UnitTestProject1/UnitTest1.cs
+7
-9
No files found.
Project.SQLiteHelper/SQLiteHelper/DBTable.cs
View file @
8e287a49
...
...
@@ -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
()
...
...
Project.SQLiteHelper/SQLiteHelper/IDBTable.cs
View file @
8e287a49
...
...
@@ -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
);
}
}
Project.SQLiteHelper/SQLiteHelper/SQLiteDbContext.cs
View file @
8e287a49
...
...
@@ -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
++
)
for
each
(
var
table
in
DbSet
)
{
var
table
=
IDs
.
Keys
.
ElementAt
(
i
);
IDs
[
table
]
=
LoadID
(
table
.
TableName
);
table
.
FreeID
=
LoadID
(
table
.
TableName
);
}
}
long
LoadID
(
string
tablename
)
...
...
Project.SQLiteHelper/SQLiteHelper/SQLiteHelper.cs
View file @
8e287a49
...
...
@@ -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,
...
...
Project.SQLiteHelper/UnitTestProject1/Model/DBModel.cs
View file @
8e287a49
...
...
@@ -15,7 +15,7 @@ namespace UnitTestProject1.Model
public
DBTable
<
Book
>
Books
{
get
;
set
;
}
=
new
DBTable
<
Book
>();
public
DBModel
()
public
DBModel
(
string
dbPath
):
base
(
dbPath
)
{
}
...
...
Project.SQLiteHelper/UnitTestProject1/UnitTest1.cs
View file @
8e287a49
...
...
@@ -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"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment