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
01c81adc
Commit
01c81adc
authored
Sep 17, 2019
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sqliteDbContext 加载数据库时,当格式不对,不再删除整个文件,而是哪个表错,加哪个
parent
55820bef
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
124 additions
and
8 deletions
+124
-8
DBTable.cs
Project.SQLiteHelper/SQLiteHelper/DBTable.cs
+1
-1
SQLiteDbContext.cs
Project.SQLiteHelper/SQLiteHelper/SQLiteDbContext.cs
+52
-7
SQLiteHelper.cs
Project.SQLiteHelper/SQLiteHelper/SQLiteHelper.cs
+68
-0
SQLiteHelper.csproj
Project.SQLiteHelper/SQLiteHelper/SQLiteHelper.csproj
+3
-0
No files found.
Project.SQLiteHelper/SQLiteHelper/DBTable.cs
View file @
01c81adc
...
...
@@ -53,7 +53,7 @@ namespace SQLite
public
void
Create
()
{
sqliteHelper
.
ExecuteNonQuery
(
SQLiteHelper
.
GetCreateTableCommandText
(
typeof
(
T
)
,
ArrayFieldTypeInfos
.
ToArray
()));
sqliteHelper
.
ExecuteNonQuery
(
ddl
);
// SQLiteHelper.GetCreateTableCommandText(typeof(T)));//
, ArrayFieldTypeInfos.ToArray()));
}
public
void
Add
(
T
t
)
...
...
Project.SQLiteHelper/SQLiteHelper/SQLiteDbContext.cs
View file @
01c81adc
...
...
@@ -9,6 +9,8 @@ namespace SQLite
{
public
abstract
class
SQLiteDbContext
{
public
List
<
IDBTable
>
DbSet
=
new
List
<
IDBTable
>();
public
SQLiteHelper
sqliteHelper
{
get
;
private
set
;
}
string
ConnectionString
...
...
@@ -115,20 +117,63 @@ namespace SQLite
{
ddls
.
Add
(
tb
.
TableName
,
tb
.
DDL
);
}
bool
isVaild
=
sqliteHelper
.
IsTableValid
(
ddls
);
bool
isVaild
=
sqliteHelper
.
IsTableValid
(
ddls
,
out
Dictionary
<
string
,
SQLiteHelper
.
IsTableValidResult
>
results
);
if
(!
isVaild
)
//不合法
{
//有表 不对
foreach
(
var
kv
in
results
)
{
switch
(
kv
.
Value
)
{
case
SQLiteHelper
.
IsTableValidResult
.
NotHere
:
{
//直接创建表
sqliteHelper
.
ExecuteNonQuery
(
ddls
[
kv
.
Key
]);
}
break
;
case
SQLiteHelper
.
IsTableValidResult
.
FormatErr
:
{
//先删除表,再创建
sqliteHelper
.
ExecuteNonQuery
(
$"DROP TABLE
{
kv
.
Key
}
"
);
sqliteHelper
.
ExecuteNonQuery
(
ddls
[
kv
.
Key
]);
}
break
;
}
}
}
//最后也要加载数据
Load
();
return
isVaild
;
}
/// <summary>
/// 当出错,不重建表
/// </summary>
/// <returns></returns>
public
bool
InitNoBuild
()
{
if
(!
System
.
IO
.
File
.
Exists
(
DBPath
))
{
Build
();
return
false
;
}
//TODO, 表不对删除就好。。。没必要重新创建数据库
if
(!
isVaild
)
//任意一个表不对,或者不存在,都必须重建
Dictionary
<
string
,
string
>
ddls
=
new
Dictionary
<
string
,
string
>();
foreach
(
IDBTable
tb
in
DbSet
)
{
Rebuild
(
);
ddls
.
Add
(
tb
.
TableName
,
tb
.
DDL
);
}
else
if
(
sqliteHelper
.
IsTableValid
(
ddls
))
{
//加载
Load
();
return
true
;
}
else
{
return
false
;
}
return
isVaild
;
}
}
...
...
Project.SQLiteHelper/SQLiteHelper/SQLiteHelper.cs
View file @
01c81adc
...
...
@@ -15,6 +15,7 @@ namespace SQLite
{
public
class
SQLiteHelper
{
static
NLog
.
Logger
logger
=
NLog
.
LogManager
.
GetLogger
(
"sqlite"
);
#
region
静态操作
public
class
SQLiteFieldTypeInfo
{
...
...
@@ -598,6 +599,7 @@ namespace SQLite
{
tran
.
Rollback
();
check
=
false
;
logger
.
Fatal
(
ex
,
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
queryList
));
throw
ex
;
}
finally
...
...
@@ -730,6 +732,72 @@ namespace SQLite
return
isVaild
;
}
public
enum
IsTableValidResult
{
OK
,
NotHere
,
FormatErr
}
/// <summary>
/// 输入DDLs 判断这些table都是否合法
/// </summary>
/// <param name="DDLs">key=tablename, value=DDL</param>
/// <returns></returns>
public
bool
IsTableValid
(
Dictionary
<
string
,
string
>
DDLs
,
out
Dictionary
<
string
,
IsTableValidResult
>
results
)
{
results
=
new
Dictionary
<
string
,
IsTableValidResult
>();
//检测 table 是否合法
DataTable
data
=
ExecuteReader
(
"SELECT name,sql FROM sqlite_master WHERE type = 'table'"
);
//任意一个表不对,或者不存在,都必须重建
foreach
(
var
kv
in
DDLs
)
{
string
tablename
=
kv
.
Key
;
string
createtable_sql
=
kv
.
Value
;
var
sqls
=
from
r
in
data
.
AsEnumerable
()
where
(
string
)
r
[
"name"
]
==
tablename
select
r
[
"sql"
];
if
(
sqls
.
Count
()
==
0
)
{
//不存在该表
ErrorMsg
=
$"sqlite_master 不能找到 name = '
{
tablename
}
' 的 sql"
;
results
.
Add
(
tablename
,
IsTableValidResult
.
NotHere
);
continue
;
}
string
sql
=
(
string
)
sqls
.
First
();
if
(
sql
==
createtable_sql
)
{
//完全一样
results
.
Add
(
tablename
,
IsTableValidResult
.
OK
);
continue
;
}
if
(!
GetTableInfoFromDDL
(
sql
,
out
SQLiteTableInfo
tableInfo0
))
{
ErrorMsg
=
$"sqlite_master 找到 name = '
{
tablename
}
' 的 sql 不能解析"
;
results
.
Add
(
tablename
,
IsTableValidResult
.
FormatErr
);
continue
;
}
if
(!
GetTableInfoFromDDL
(
createtable_sql
,
out
SQLiteTableInfo
tableInfo1
))
{
ErrorMsg
=
$"程序中 name = '
{
tablename
}
' 的 sql 不能解析"
;
results
.
Add
(
tablename
,
IsTableValidResult
.
FormatErr
);
continue
;
}
if
(!
tableInfo0
.
Equals
(
tableInfo1
))
{
ErrorMsg
=
$"sqlite_master 找到 name = '
{
tablename
}
' 的 sql 不符合要求, "
+
tableInfo0
.
ErrorMsg
;
continue
;
}
//虽然不一样,都也是合法的
results
.
Add
(
tablename
,
IsTableValidResult
.
OK
);
}
return
results
.
All
(
kv
=>
kv
.
Value
==
IsTableValidResult
.
OK
);
}
public
bool
GetTableInfoFromDDL
(
string
DDL
,
out
SQLiteTableInfo
tableInfo
)
{
tableInfo
=
null
;
...
...
Project.SQLiteHelper/SQLiteHelper/SQLiteHelper.csproj
View file @
01c81adc
...
...
@@ -58,6 +58,9 @@
<PackageReference
Include=
"Newtonsoft.Json"
>
<Version>
12.0.2
</Version>
</PackageReference>
<PackageReference
Include=
"NLog"
>
<Version>
4.6.7
</Version>
</PackageReference>
<PackageReference
Include=
"System.Data.SQLite"
>
<Version>
1.0.111
</Version>
</PackageReference>
...
...
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