Commit 50da94ad authored by 潘栩锋's avatar 潘栩锋 🚴

1. 添加 ToStringOfSQLiteFieldType 类型转string

2. 添加 线程排队 等写入数据库
parent 7176fbd7
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
...@@ -8,6 +9,7 @@ using System.Linq; ...@@ -8,6 +9,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Common.SQLite namespace FLY.OBJComponents.Common.SQLite
{ {
...@@ -75,11 +77,7 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -75,11 +77,7 @@ namespace FLY.OBJComponents.Common.SQLite
new SQLiteFieldTypeInfo("INTEGER",typeof(Int64)), new SQLiteFieldTypeInfo("INTEGER",typeof(Int64)),
new SQLiteFieldTypeInfo("BOOLEAN",typeof(bool)), new SQLiteFieldTypeInfo("BOOLEAN",typeof(bool)),
new SQLiteFieldTypeInfo("DOUBLE",typeof(double), new SQLiteFieldTypeInfo("DOUBLE",typeof(double),
(obj)=>{ (obj)=>((double)obj).ToStringOfSQLiteFieldType(),
if(double.IsNaN((double)obj))
return "NULL";
else return obj.ToString();
},
(obj)=>{ (obj)=>{
if(obj == DBNull.Value) if(obj == DBNull.Value)
return double.NaN; return double.NaN;
...@@ -88,16 +86,28 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -88,16 +86,28 @@ namespace FLY.OBJComponents.Common.SQLite
} }
), ),
new SQLiteFieldTypeInfo("STRING",typeof(string), new SQLiteFieldTypeInfo("STRING",typeof(string),
(obj)=>"'" + obj + "'", (obj)=>((string)obj).ToStringOfSQLiteFieldType(),
SQLiteFieldTypeInfo.DefaultToP SQLiteFieldTypeInfo.DefaultToP
), ),
new SQLiteFieldTypeInfo("DATETIME",typeof(DateTime), new SQLiteFieldTypeInfo("DATETIME",typeof(DateTime),
(obj)=>"'" + ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss.fff") + "'", (obj)=>((DateTime)obj).ToStringOfSQLiteFieldType(),
SQLiteFieldTypeInfo.DefaultToP 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 public class ArrayFieldTypeInfo
{ {
/// <summary> /// <summary>
...@@ -503,6 +513,7 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -503,6 +513,7 @@ namespace FLY.OBJComponents.Common.SQLite
adapter.Fill(data); adapter.Fill(data);
connection.Close(); connection.Close();
} }
} }
return data; return data;
} }
...@@ -554,6 +565,13 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -554,6 +565,13 @@ namespace FLY.OBJComponents.Common.SQLite
/// <param name="queryList"></param> /// <param name="queryList"></param>
/// <returns></returns> /// <returns></returns>
public bool QueryTran(IEnumerable<string> queryList) public bool QueryTran(IEnumerable<string> queryList)
{
if (isHoldQueryTran)
{
holdQueryList.AddRange(queryList);
return true;
}
else
{ {
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{ {
...@@ -586,10 +604,70 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -586,10 +604,70 @@ namespace FLY.OBJComponents.Common.SQLite
return check; 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>
/// 出错代码 /// 出错代码
/// </summary> /// </summary>
...@@ -599,7 +677,7 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -599,7 +677,7 @@ namespace FLY.OBJComponents.Common.SQLite
/// </summary> /// </summary>
/// <param name="DDLs">key=tablename, value=DDL</param> /// <param name="DDLs">key=tablename, value=DDL</param>
/// <returns></returns> /// <returns></returns>
public bool IsTableValid(Dictionary<string,string> DDLs) public bool IsTableValid(Dictionary<string, string> DDLs)
{ {
//检测 table 是否合法 //检测 table 是否合法
...@@ -752,4 +830,69 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -752,4 +830,69 @@ namespace FLY.OBJComponents.Common.SQLite
return s; 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;
}
}
} }
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