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

1. 添加 ToStringOfSQLiteFieldType 类型转string

2. 添加 线程排队 等写入数据库
parent 7176fbd7
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>
......@@ -432,7 +442,7 @@ namespace FLY.OBJComponents.Common.SQLite
continue;
Type ptype = propertyInfo.PropertyType;
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
......@@ -503,6 +513,7 @@ namespace FLY.OBJComponents.Common.SQLite
adapter.Fill(data);
connection.Close();
}
}
return data;
}
......@@ -555,41 +566,108 @@ namespace FLY.OBJComponents.Common.SQLite
/// <returns></returns>
public bool QueryTran(IEnumerable<string> queryList)
{
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
if (isHoldQueryTran)
{
using (SQLiteCommand command = new SQLiteCommand(connection))
holdQueryList.AddRange(queryList);
return true;
}
else
{
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
connection.Open();
SQLiteTransaction tran = connection.BeginTransaction();
bool check = false;
try
using (SQLiteCommand command = new SQLiteCommand(connection))
{
foreach (string item in queryList)
connection.Open();
SQLiteTransaction tran = connection.BeginTransaction();
bool check = false;
try
{
command.CommandText = item;
command.ExecuteNonQuery();
foreach (string item in queryList)
{
command.CommandText = item;
command.ExecuteNonQuery();
}
tran.Commit();
check = true;
}
tran.Commit();
check = true;
}
catch (Exception ex)
{
tran.Rollback();
check = false;
throw ex;
}
finally
{
connection.Close();
catch (Exception ex)
{
tran.Rollback();
check = false;
throw ex;
}
finally
{
connection.Close();
}
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>
......@@ -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 是否合法
......@@ -611,7 +689,7 @@ namespace FLY.OBJComponents.Common.SQLite
{
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)
{
......@@ -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;
}
}
}
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