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
50da94ad
Commit
50da94ad
authored
Jun 13, 2019
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1. 添加 ToStringOfSQLiteFieldType 类型转string
2. 添加 线程排队 等写入数据库
parent
7176fbd7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
176 additions
and
33 deletions
+176
-33
SQLiteHelper.cs
...OBJComponents/OBJComponents/Common/SQLite/SQLiteHelper.cs
+176
-33
No files found.
Project.FLY.OBJComponents/OBJComponents/Common/SQLite/SQLiteHelper.cs
View file @
50da94ad
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.ComponentModel.DataAnnotations
;
using
System.ComponentModel.DataAnnotations.Schema
;
...
...
@@ -8,6 +9,7 @@ using System.Linq;
using
System.Reflection
;
using
System.Text
;
using
System.Text.RegularExpressions
;
using
System.Threading.Tasks
;
namespace
FLY.OBJComponents.Common.SQLite
{
...
...
@@ -75,11 +77,7 @@ namespace FLY.OBJComponents.Common.SQLite
new
SQLiteFieldTypeInfo
(
"INTEGER"
,
typeof
(
Int64
)),
new
SQLiteFieldTypeInfo
(
"BOOLEAN"
,
typeof
(
bool
)),
new
SQLiteFieldTypeInfo
(
"DOUBLE"
,
typeof
(
double
),
(
obj
)=>{
if
(
double
.
IsNaN
((
double
)
obj
))
return
"NULL"
;
else
return
obj
.
ToString
();
},
(
obj
)=>((
double
)
obj
).
ToStringOfSQLiteFieldType
(),
(
obj
)=>{
if
(
obj
==
DBNull
.
Value
)
return
double
.
NaN
;
...
...
@@ -88,16 +86,28 @@ namespace FLY.OBJComponents.Common.SQLite
}
),
new
SQLiteFieldTypeInfo
(
"STRING"
,
typeof
(
string
),
(
obj
)=>
"'"
+
obj
+
"'"
,
(
obj
)=>
((
string
)
obj
).
ToStringOfSQLiteFieldType
()
,
SQLiteFieldTypeInfo
.
DefaultToP
),
new
SQLiteFieldTypeInfo
(
"DATETIME"
,
typeof
(
DateTime
),
(
obj
)=>
"'"
+
((
DateTime
)
obj
).
ToString
(
"yyyy-MM-dd HH:mm:ss.fff"
)
+
"'"
,
(
obj
)=>
((
DateTime
)
obj
).
ToStringOfSQLiteFieldType
()
,
SQLiteFieldTypeInfo
.
DefaultToP
)
};
}
public
static
SQLiteFieldTypeInfo
GetFieldTypeInfo
(
string
fieldtype
)
{
var
ftis
=
from
fti
in
FieldTypeInfo
where
fti
.
FieldType
==
fieldtype
select
fti
;
if
(
ftis
.
Count
()
>
0
)
{
return
ftis
.
First
();
}
else
{
return
null
;
}
}
public
class
ArrayFieldTypeInfo
{
/// <summary>
...
...
@@ -503,6 +513,7 @@ namespace FLY.OBJComponents.Common.SQLite
adapter
.
Fill
(
data
);
connection
.
Close
();
}
}
return
data
;
}
...
...
@@ -554,6 +565,13 @@ namespace FLY.OBJComponents.Common.SQLite
/// <param name="queryList"></param>
/// <returns></returns>
public
bool
QueryTran
(
IEnumerable
<
string
>
queryList
)
{
if
(
isHoldQueryTran
)
{
holdQueryList
.
AddRange
(
queryList
);
return
true
;
}
else
{
using
(
SQLiteConnection
connection
=
new
SQLiteConnection
(
ConnectionString
))
{
...
...
@@ -586,10 +604,70 @@ namespace FLY.OBJComponents.Common.SQLite
return
check
;
}
}
}
}
/// <summary>
/// 异步
/// </summary>
/// <param name="queryList"></param>
/// <returns></returns>
public
void
QueryTranAsync
(
IEnumerable
<
string
>
queryList
)
{
new
Task
((
obj
)
=>
{
var
_sqls
=
obj
as
IEnumerable
<
string
>;
QueryTran
(
_sqls
);
},
queryList
).
Start
(
StepByStepTaskScheduler
.
Current
);
}
List
<
string
>
holdQueryList
=
new
List
<
string
>();
bool
isHoldQueryTran
=
false
;
public
void
HoldQueryTran
()
{
isHoldQueryTran
=
true
;
}
public
bool
ReleaseQueryTran
()
{
if
(
holdQueryList
.
Count
()
>
0
)
{
using
(
SQLiteConnection
connection
=
new
SQLiteConnection
(
ConnectionString
))
{
using
(
SQLiteCommand
command
=
new
SQLiteCommand
(
connection
))
{
connection
.
Open
();
SQLiteTransaction
tran
=
connection
.
BeginTransaction
();
bool
check
=
false
;
try
{
foreach
(
string
item
in
holdQueryList
)
{
command
.
CommandText
=
item
;
command
.
ExecuteNonQuery
();
}
tran
.
Commit
();
check
=
true
;
}
catch
(
Exception
ex
)
{
tran
.
Rollback
();
check
=
false
;
throw
ex
;
}
finally
{
holdQueryList
.
Clear
();
connection
.
Close
();
isHoldQueryTran
=
false
;
}
return
check
;
}
}
}
isHoldQueryTran
=
false
;
return
true
;
}
/// <summary>
/// 出错代码
/// </summary>
...
...
@@ -599,7 +677,7 @@ namespace FLY.OBJComponents.Common.SQLite
/// </summary>
/// <param name="DDLs">key=tablename, value=DDL</param>
/// <returns></returns>
public
bool
IsTableValid
(
Dictionary
<
string
,
string
>
DDLs
)
public
bool
IsTableValid
(
Dictionary
<
string
,
string
>
DDLs
)
{
//检测 table 是否合法
...
...
@@ -752,4 +830,69 @@ namespace FLY.OBJComponents.Common.SQLite
return
s
;
}
}
public
static
class
SQLiteFieldTypeExtern
{
public
static
string
ToStringOfSQLiteFieldType
(
this
DateTime
dt
)
{
return
$"'
{
dt
:
yyyy
-
MM
-
dd
HH
:
mm
:
ss
.
fff
}
'"
;
}
public
static
string
ToStringOfSQLiteFieldType
(
this
string
str
)
{
return
$"'
{
str
}
'"
;
}
public
static
string
ToStringOfSQLiteFieldType
(
this
double
d
)
{
if
(
double
.
IsNaN
(
d
))
return
"NULL"
;
else
return
d
.
ToString
();
}
}
public
class
StepByStepTaskScheduler
:
TaskScheduler
{
public
static
new
TaskScheduler
Current
{
get
;
}
=
new
StepByStepTaskScheduler
();
public
static
new
TaskScheduler
Default
{
get
;
}
=
Current
;
public
static
StepByStepTaskScheduler
Instance
{
get
;
}
=
(
StepByStepTaskScheduler
)
Current
;
private
readonly
BlockingCollection
<
Task
>
m_queue
=
new
BlockingCollection
<
Task
>();
StepByStepTaskScheduler
()
{
//Thread thread = new Thread(Run);
//thread.IsBackground = true;//设为为后台线程,当主线程结束时线程自动结束
//thread.Start();
Task
.
Factory
.
StartNew
(
Run
);
}
private
void
Run
()
{
//Console.WriteLine($"MyTaskScheduler, ThreadID: {Thread.CurrentThread.ManagedThreadId}");
Task
t
;
while
(
m_queue
.
TryTake
(
out
t
,
System
.
Threading
.
Timeout
.
Infinite
))
{
TryExecuteTask
(
t
);
//在当前线程执行Task
}
}
protected
override
IEnumerable
<
Task
>
GetScheduledTasks
()
{
return
m_queue
;
}
public
BlockingCollection
<
Task
>
ScheduledTasks
{
get
{
return
m_queue
;
}
}
protected
override
void
QueueTask
(
Task
task
)
{
m_queue
.
Add
(
task
);
//t.Start(MyTaskScheduler.Current)时,将Task加入到队列中
}
//当执行该函数时,程序正在尝试以同步的方式执行Task代码
protected
override
bool
TryExecuteTaskInline
(
Task
task
,
bool
taskWasPreviouslyQueued
)
{
return
false
;
}
}
}
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