📄 dbhelper.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client.Data;
using Imps.Client.Resource;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
internal class DbHelper : IDisposable
{
private SQLiteConnection _connection;
private string _dataSource = string.Empty;
private static DbHelper _instance = new DbHelper();
private bool _isFirstUse;
private Dictionary<int, SQLiteTransaction> _localTransactionCollection;
private static object _locker = new object();
private string _password = string.Empty;
private static int _refCount = 0;
private const string CONNECTION_STRING_FORMAT = "Data Source={0};Password={1}";
private const string HISTORY_DATABASE_NAME = "history.dat";
private DbHelper()
{
}
internal void CommitTransaction()
{
lock (_locker)
{
int num = Thread.CurrentThread.get_ManagedThreadId();
if (this.LocalTransactionCollection.ContainsKey(num))
{
this.LocalTransactionCollection.get_Item(num).Commit();
_refCount--;
this.LocalTransactionCollection.Remove(num);
if (_refCount == 0)
{
this._connection.Close();
}
}
}
}
internal SQLiteCommand CreateCommand(string sql, params object[] parameters)
{
SQLiteCommand command = null;
int num = Thread.CurrentThread.get_ManagedThreadId();
if (this.LocalTransactionCollection.ContainsKey(num) && (this.LocalTransactionCollection.get_Item(num) != null))
{
command = new SQLiteCommand(sql, this._connection, this.LocalTransactionCollection.get_Item(num));
}
else
{
command = new SQLiteCommand(sql, this._connection);
}
if (parameters != null)
{
List<SQLiteParameter>.Enumerator enumerator = this.DeriveParameters(sql, parameters).GetEnumerator();
try
{
while (enumerator.MoveNext())
{
SQLiteParameter parameter = enumerator.get_Current();
command.Parameters.Add(parameter);
}
}
finally
{
enumerator.Dispose();
}
}
return command;
}
internal List<SQLiteParameter> DeriveParameters(string commandText, object[] paramList)
{
if (paramList == null)
{
return null;
}
List<SQLiteParameter> list = new List<SQLiteParameter>();
string input = commandText.Substring(commandText.IndexOf("@")).Replace(",", " ,").Replace(")", " )");
string pattern = @"(@)\S*(.*?)\b";
MatchCollection matchs = new Regex(pattern, RegexOptions.IgnoreCase).Matches(input);
List<string> list2 = new List<string>();
foreach (Match match in matchs)
{
if (!list2.Contains(match.Value))
{
list2.Add(match.Value);
}
}
string[] textArray = list2.ToArray();
int index = 0;
Type type = null;
foreach (object obj2 in paramList)
{
if (obj2 == null)
{
SQLiteParameter parameter = new SQLiteParameter();
parameter.DbType = DbType.Object;
parameter.ParameterName = textArray[index];
parameter.Value = DBNull.Value;
list.Add(parameter);
}
else
{
type = obj2.GetType();
SQLiteParameter parameter2 = new SQLiteParameter();
switch (type.ToString())
{
case "System.String":
parameter2.DbType = DbType.String;
parameter2.ParameterName = textArray[index];
parameter2.Value = (string) paramList[index];
list.Add(parameter2);
goto Label_0408;
case "System.Byte[]":
parameter2.DbType = DbType.Binary;
parameter2.ParameterName = textArray[index];
parameter2.Value = (byte[]) paramList[index];
list.Add(parameter2);
goto Label_0408;
case "System.Int64":
parameter2.DbType = DbType.Int64;
parameter2.ParameterName = textArray[index];
parameter2.Value = (long) paramList[index];
list.Add(parameter2);
goto Label_0408;
case "System.Int32":
parameter2.DbType = DbType.Int32;
parameter2.ParameterName = textArray[index];
parameter2.Value = (int) paramList[index];
list.Add(parameter2);
goto Label_0408;
case "System.Boolean":
parameter2.DbType = DbType.Boolean;
parameter2.ParameterName = textArray[index];
parameter2.Value = (bool) paramList[index];
list.Add(parameter2);
goto Label_0408;
case "System.DateTime":
parameter2.DbType = DbType.DateTime;
parameter2.ParameterName = textArray[index];
parameter2.Value = Convert.ToDateTime(paramList[index]);
list.Add(parameter2);
goto Label_0408;
case "System.Double":
parameter2.DbType = DbType.Double;
parameter2.ParameterName = textArray[index];
parameter2.Value = Convert.ToDouble(paramList[index]);
list.Add(parameter2);
goto Label_0408;
case "System.Decimal":
parameter2.DbType = DbType.Decimal;
parameter2.ParameterName = textArray[index];
parameter2.Value = Convert.ToDecimal(paramList[index]);
goto Label_0408;
case "System.Guid":
parameter2.DbType = DbType.Guid;
parameter2.ParameterName = textArray[index];
parameter2.Value = (Guid) paramList[index];
goto Label_0408;
case "System.Object":
parameter2.DbType = DbType.Object;
parameter2.ParameterName = textArray[index];
parameter2.Value = paramList[index];
list.Add(parameter2);
goto Label_0408;
}
throw new SystemException("Value is of unknown data type");
}
Label_0408:
index++;
}
return list;
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposed)
{
try
{
if (disposed)
{
if (this._localTransactionCollection != null)
{
lock (_locker)
{
Dictionary<int, SQLiteTransaction>.ValueCollection.Enumerator enumerator = this._localTransactionCollection.get_Values().GetEnumerator();
try
{
while (enumerator.MoveNext())
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -