You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
782 lines
28 KiB
782 lines
28 KiB
/* 2021.11.07 */
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Data.SQLite;
|
|
using System.Text;
|
|
//using Mono.Data.Sqlite;
|
|
|
|
using static Apewer.Source.OrmHelper;
|
|
|
|
namespace Apewer.Source
|
|
{
|
|
|
|
/// <summary>用于快速连接 SQLite 数据库的辅助。</summary>
|
|
public class Sqlite : IDbClient
|
|
{
|
|
|
|
#region 基础
|
|
|
|
private Timeout _timeout = null;
|
|
private string _connstring = "";
|
|
private string _path = "";
|
|
private string _pass = "";
|
|
|
|
private object _locker = new object();
|
|
|
|
/// <summary>获取或设置日志记录。</summary>
|
|
public Logger Logger { get; set; }
|
|
|
|
/// <summary>超时设定。</summary>
|
|
public Timeout Timeout { get => _timeout; }
|
|
|
|
/// <summary>创建连接实例。</summary>
|
|
/// <remarks>注意:<br />- 构造函数不会创建不存在的文件;<br />- 参数 path 为文件路径,指定为空时将使用 :memory: 作为路径连接内存。</remarks>
|
|
public Sqlite(string path = null, string pass = null, Timeout timeout = null)
|
|
{
|
|
_timeout = timeout ?? Timeout.Default;
|
|
_path = path.IsEmpty() ? Memory : path;
|
|
_pass = pass;
|
|
if (pass.IsEmpty()) _connstring = $"data source='{_path}'; version=3; ";
|
|
else _connstring = $"data source='{_path}'; password={_pass}; version=3; ";
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 连接
|
|
|
|
private SQLiteConnection _db = null;
|
|
|
|
/// <summary>数据库已经连接。</summary>
|
|
public bool Online { get => _db != null && _db.State == ConnectionState.Open; }
|
|
|
|
/// <summary>连接字符串。</summary>
|
|
public string ConnectionString { get => _connstring; }
|
|
|
|
/// <summary>获取当前的 SQLiteConnection 对象。</summary>
|
|
public IDbConnection Connection { get => _db; }
|
|
|
|
/// <summary>连接数据库,若未连接则尝试连接。</summary>
|
|
/// <returns>是否已连接。</returns>
|
|
public string Connect()
|
|
{
|
|
if (_db == null)
|
|
{
|
|
_db = new SQLiteConnection();
|
|
_db.ConnectionString = ConnectionString;
|
|
}
|
|
else
|
|
{
|
|
if (_db.State == ConnectionState.Open) return null;
|
|
}
|
|
try
|
|
{
|
|
_db.Open();
|
|
switch (_db.State)
|
|
{
|
|
case ConnectionState.Open: return null;
|
|
default: return $"连接失败,当前处于 {_db.State} 状态。";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error(nameof(Sqlite), "Connection", ex, _db.ConnectionString);
|
|
Close();
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
/// <summary>关闭连接,并释放对象所占用的系统资源。</summary>
|
|
public void Close()
|
|
{
|
|
if (_db != null)
|
|
{
|
|
if (_transaction != null)
|
|
{
|
|
if (_autocommit) Commit();
|
|
else Rollback();
|
|
}
|
|
lock (_locker)
|
|
{
|
|
_db.Dispose();
|
|
}
|
|
_db = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>关闭连接,释放对象所占用的系统资源,并清除连接信息。</summary>
|
|
public void Dispose() { Close(); }
|
|
|
|
#endregion
|
|
|
|
#region Transaction
|
|
|
|
private IDbTransaction _transaction = null;
|
|
private bool _autocommit = false;
|
|
|
|
/// <summary>启动事务。</summary>
|
|
public string Begin(bool commit = true) => Begin(commit, null);
|
|
|
|
/// <summary>启动事务。</summary>
|
|
public string Begin(bool commit, Class<IsolationLevel> isolation)
|
|
{
|
|
var connect = Connect();
|
|
if (connect.NotEmpty()) return connect;
|
|
if (_transaction != null) return "存在已启动的事务,无法再次启动。";
|
|
try
|
|
{
|
|
_transaction = isolation ? _db.BeginTransaction(isolation.Value) : _db.BeginTransaction();
|
|
_autocommit = commit;
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error(nameof(Sqlite), "Begin", ex.Message());
|
|
return ex.Message();
|
|
}
|
|
}
|
|
|
|
/// <summary>提交事务。</summary>
|
|
public string Commit()
|
|
{
|
|
if (_transaction == null) return "事务不存在。";
|
|
try
|
|
{
|
|
_transaction.Commit();
|
|
RuntimeUtility.Dispose(_transaction);
|
|
_transaction = null;
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RuntimeUtility.Dispose(_transaction);
|
|
_transaction = null;
|
|
Logger.Error(nameof(Sqlite), "Commit", ex.Message());
|
|
return ex.Message();
|
|
}
|
|
}
|
|
|
|
/// <summary>从挂起状态回滚事务。</summary>
|
|
public string Rollback()
|
|
{
|
|
if (_transaction == null) return "事务不存在。";
|
|
try
|
|
{
|
|
_transaction.Rollback();
|
|
RuntimeUtility.Dispose(_transaction);
|
|
_transaction = null;
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RuntimeUtility.Dispose(_transaction);
|
|
_transaction = null;
|
|
Logger.Error(nameof(Sqlite), "Rollback", ex.Message());
|
|
return ex.Message();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SQL
|
|
|
|
/// <summary>查询。</summary>
|
|
public IQuery Query(string sql) => Query(sql, null);
|
|
|
|
/// <summary>查询。</summary>
|
|
public IQuery Query(string sql, IEnumerable<IDataParameter> parameters)
|
|
{
|
|
if (string.IsNullOrEmpty(sql)) return Example.InvalidQueryStatement;
|
|
|
|
var connected = Connect();
|
|
if (connected.NotEmpty()) return new Query(false, connected);
|
|
|
|
var query = new Query();
|
|
try
|
|
{
|
|
using (var command = new SQLiteCommand())
|
|
{
|
|
command.Connection = _db;
|
|
command.CommandTimeout = _timeout.Query;
|
|
command.CommandText = sql;
|
|
if (parameters != null)
|
|
{
|
|
foreach (var p in parameters)
|
|
{
|
|
if (p != null) command.Parameters.Add(p);
|
|
}
|
|
}
|
|
using (var dataset = new DataSet())
|
|
{
|
|
using (var da = new SQLiteDataAdapter(sql, _db))
|
|
{
|
|
var name = "table_" + Guid.NewGuid().ToString("n");
|
|
da.Fill(dataset, name);
|
|
var table = dataset.Tables[name];
|
|
return new Query(table);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error(nameof(Sqlite), "Query", ex, sql);
|
|
return new Query(ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>执行单条 Transact-SQL 语句。</summary>
|
|
public IExecute Execute(string sql) => Execute(sql, null);
|
|
|
|
/// <summary>执行单条 Transact-SQL 语句,并加入参数。</summary>
|
|
public IExecute Execute(string sql, IEnumerable<IDataParameter> parameters)
|
|
{
|
|
if (string.IsNullOrEmpty(sql)) return Example.InvalidExecuteStatement;
|
|
|
|
var connected = Connect();
|
|
if (connected.NotEmpty()) return new Execute(false, connected);
|
|
|
|
lock (_locker)
|
|
{
|
|
var inTransaction = _transaction != null;
|
|
if (!inTransaction) Begin();
|
|
try
|
|
{
|
|
using (var command = new SQLiteCommand())
|
|
{
|
|
command.Connection = _db;
|
|
command.Transaction = (SQLiteTransaction)_transaction;
|
|
command.CommandTimeout = _timeout.Execute;
|
|
command.CommandText = sql;
|
|
if (parameters != null)
|
|
{
|
|
foreach (var p in parameters)
|
|
{
|
|
if (p != null) command.Parameters.Add(p);
|
|
}
|
|
}
|
|
var rows = command.ExecuteNonQuery();
|
|
if (!inTransaction) Commit(); // todo 此处应该检查事务提交产生的错误。
|
|
return new Execute(true, rows);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error(nameof(Sqlite), "Execute", ex, sql);
|
|
if (!inTransaction) Rollback();
|
|
return new Execute(ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 属性。
|
|
|
|
/// <summary>保存当前数据库到文件,若文件已存在则将重写文件。</summary>
|
|
public bool Save(string path, string pass = null)
|
|
{
|
|
if (!StorageUtility.CreateFile(path, 0, true))
|
|
{
|
|
Logger.Error(nameof(Sqlite), "Save", TextUtility.Merge("创建文件 ", path, " 失败。"));
|
|
return false;
|
|
}
|
|
|
|
using (var destination = new Sqlite(path, pass)) return Save(destination);
|
|
}
|
|
|
|
/// <summary>保存当前数据库到目标数据库。</summary>
|
|
public bool Save(Sqlite destination) => string.IsNullOrEmpty(Backup(this, destination));
|
|
|
|
/// <summary>加载文件到当前数据库。</summary>
|
|
public bool Load(string path, string pass = null)
|
|
{
|
|
using (var source = new Sqlite(path, pass)) return Load(source);
|
|
}
|
|
|
|
/// <summary>加载源数据库到当前数据库。</summary>
|
|
public bool Load(Sqlite source) => string.IsNullOrEmpty(Backup(source, this));
|
|
|
|
#endregion
|
|
|
|
#region ORM
|
|
|
|
/// <summary>查询数据库中的所有表名。</summary>
|
|
public string[] TableNames()
|
|
{
|
|
var list = new List<string>();
|
|
if (Connect().IsEmpty())
|
|
{
|
|
var sql = "select name from sqlite_master where type='table' order by name; ";
|
|
var query = (Query)Query(sql);
|
|
for (int r = 0; r < query.Rows; r++)
|
|
{
|
|
var cell = query.Text(r, 0);
|
|
if (TextUtility.IsBlank(cell)) continue;
|
|
list.Add(cell);
|
|
}
|
|
query.Dispose();
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
|
|
/// <summary>查询数据库中的所有视图名。</summary>
|
|
public string[] ViewNames()
|
|
{
|
|
var list = new List<string>();
|
|
if (Connect().IsEmpty())
|
|
{
|
|
var sql = "select name from sqlite_master where type='view' order by name; ";
|
|
var query = (Query)Query(sql);
|
|
for (int r = 0; r < query.Rows; r++)
|
|
{
|
|
var cell = query.Text(r, 0);
|
|
if (TextUtility.IsBlank(cell)) continue;
|
|
list.Add(cell);
|
|
}
|
|
query.Dispose();
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
|
|
/// <summary>查询表中的所有列名。</summary>
|
|
public string[] ColumnNames(string table)
|
|
{
|
|
var list = new List<string>();
|
|
if (Connect().IsEmpty())
|
|
{
|
|
var t = TextUtility.AntiInject(table);
|
|
var sql = TextUtility.Merge("pragma table_info('", TextUtility.AntiInject(t), "'); ");
|
|
using (var query = Query(sql) as Query)
|
|
{
|
|
for (int r = 0; r < query.Rows; r++)
|
|
{
|
|
var cell = query.Text(r, "name");
|
|
if (TextUtility.IsBlank(cell)) continue;
|
|
list.Add(cell);
|
|
}
|
|
}
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
|
|
/// <summary>创建表,不修改已存在表。成功时返回空字符串,发生异常时返回异常信息。</summary>
|
|
public string Initialize(Record model) => model == null ? "参数无效。" : Initialize(model.GetType());
|
|
|
|
/// <summary>创建表,不修改已存在表。成功时返回空字符串,发生异常时返回异常信息。</summary>
|
|
public string Initialize<T>() where T : class, new() => Initialize(typeof(T));
|
|
|
|
/// <summary>创建表,不修改已存在表。当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
|
|
public string Initialize(Type model)
|
|
{
|
|
var structure = TableStructure.Parse(model);
|
|
if (structure == null) return "无法解析记录模型。";
|
|
|
|
// 连接数据库。
|
|
var connect = Connect();
|
|
if (connect.NotEmpty()) return connect;
|
|
|
|
// 检查现存表。
|
|
var exists = false;
|
|
var tables = TableNames();
|
|
if (tables.Length > 0)
|
|
{
|
|
var lower = structure.Name.ToLower();
|
|
foreach (var table in tables)
|
|
{
|
|
if (TextUtility.IsBlank(table)) continue;
|
|
if (table.ToLower() == lower)
|
|
{
|
|
exists = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (exists)
|
|
{
|
|
return TextUtility.Merge("指定的表已经存在。");
|
|
}
|
|
else
|
|
{
|
|
var sqlcolumns = new List<string>();
|
|
foreach (var column in structure.Columns)
|
|
{
|
|
var type = Declaration(column);
|
|
if (string.IsNullOrEmpty(type))
|
|
{
|
|
return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
|
|
}
|
|
sqlcolumns.Add(type);
|
|
}
|
|
var sql = TextUtility.Merge("create table [", structure.Name, "](", TextUtility.Join(", ", sqlcolumns), "); ");
|
|
var execute = Execute(sql);
|
|
if (execute.Success) return TextUtility.Empty;
|
|
return execute.Message;
|
|
}
|
|
}
|
|
|
|
/// <summary>插入记录。返回错误信息。</summary>
|
|
public string Insert(object record, string table = null)
|
|
{
|
|
if (record == null) return "参数无效。";
|
|
OrmHelper.FixProperties(record);
|
|
|
|
// 解析模型,获取表名。
|
|
var structure = TableStructure.Parse(record.GetType());
|
|
if (structure == null) return "无法解析记录模型。";
|
|
if (string.IsNullOrEmpty(table)) table = structure.Name;
|
|
if (string.IsNullOrEmpty(table)) return "表名称无效。";
|
|
|
|
// 准备参数。
|
|
var ps = structure.CreateParameters(record, Parameter);
|
|
var psc = ps.Length;
|
|
if (psc < 1) return "数据模型不包含字段。";
|
|
|
|
// 合成 SQL 语句。
|
|
var sb = new StringBuilder();
|
|
sb.Append("insert into [");
|
|
sb.Append(table);
|
|
sb.Append("](");
|
|
for (var i = 0; i < psc; i++)
|
|
{
|
|
if (i > 0) sb.Append(", ");
|
|
sb.Append(ps[i].ParameterName);
|
|
}
|
|
sb.Append(") values(");
|
|
for (var i = 0; i < psc; i++)
|
|
{
|
|
if (i > 0) sb.Append(", ");
|
|
sb.Append("@");
|
|
sb.Append(ps[i].ParameterName);
|
|
}
|
|
sb.Append("); ");
|
|
var sql = sb.ToString();
|
|
|
|
// 执行。
|
|
var execute = Execute(sql, ps);
|
|
if (execute.Success && execute.Rows > 0) return TextUtility.Empty;
|
|
return execute.Message;
|
|
}
|
|
|
|
/// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
|
|
/// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
|
|
public string Update(IRecord record, string table = null)
|
|
{
|
|
if (record == null) return "参数无效。";
|
|
FixProperties(record);
|
|
SetUpdated(record);
|
|
|
|
// 解析模型,获取表名。
|
|
var structure = TableStructure.Parse(record.GetType());
|
|
if (structure == null) return "无法解析记录模型。";
|
|
if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
|
|
if (string.IsNullOrEmpty(table)) table = structure.Name;
|
|
if (string.IsNullOrEmpty(table)) return "表名称无效。";
|
|
|
|
// 准备参数。
|
|
var ps = structure.CreateParameters(record, Parameter, "_key");
|
|
var psc = ps.Length;
|
|
if (psc < 1) return "数据模型不包含字段。";
|
|
|
|
// 合成 SQL 语句。
|
|
var sb = new StringBuilder();
|
|
sb.Append("update [");
|
|
sb.Append(table);
|
|
sb.Append("] set ");
|
|
for (var i = 0; i < psc; i++)
|
|
{
|
|
if (i > 0) sb.Append(", ");
|
|
sb.Append("[");
|
|
sb.Append(ps[i].ParameterName);
|
|
sb.Append("] = @");
|
|
sb.Append(ps[i].ParameterName);
|
|
}
|
|
sb.Append(" where [_key] = '");
|
|
sb.Append(record.Key.SafeKey());
|
|
sb.Append("'; ");
|
|
var sql = sb.ToString();
|
|
|
|
// 执行。
|
|
var execute = Execute(sql, ps);
|
|
if (execute.Success && execute.Rows > 0) return TextUtility.Empty;
|
|
return execute.Message;
|
|
}
|
|
|
|
/// <summary>获取按指定语句查询到的所有记录。</summary>
|
|
public Result<object[]> Query(Type model, string sql) => OrmHelper.Query(this, model, sql);
|
|
|
|
/// <summary>获取按指定语句查询到的所有记录。</summary>
|
|
public Result<T[]> Query<T>(string sql) where T : class, new()
|
|
{
|
|
var query = Query(sql);
|
|
if (!query.Success) return new Result<T[]>(query.Message);
|
|
var records = query.Fill<T>();
|
|
query.Dispose();
|
|
|
|
var result = new Result<T[]>(records);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>查询多条记录。</summary>
|
|
public Result<object[]> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) =>
|
|
{
|
|
if (flag == 0) return $"select * from [{tn}]; ";
|
|
return $"select * from [{tn}] where _flag={flag}; ";
|
|
});
|
|
|
|
/// <summary>查询多条记录。</summary>
|
|
public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query<T>(this, (tn) =>
|
|
{
|
|
if (flag == 0) return $"select * from [{tn}]; ";
|
|
return $"select * from [{tn}] where _flag={flag}; ";
|
|
});
|
|
|
|
/// <summary>获取具有指定 Key 的记录。</summary>
|
|
public Result<object> Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) =>
|
|
{
|
|
if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; ";
|
|
return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; ";
|
|
});
|
|
|
|
/// <summary>获取具有指定 Key 的记录。</summary>
|
|
public Result<T> Get<T>(string key, long flag) where T : class, IRecord, new() => OrmHelper.Get<T>(this, key, (tn, sk) =>
|
|
{
|
|
if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; ";
|
|
return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; ";
|
|
});
|
|
|
|
/// <summary>获取指定类型的主键,按 Flag 属性筛选。</summary>
|
|
public Result<string[]> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) =>
|
|
{
|
|
if (flag == 0) return $"select _key from [{tn}] where _flag={flag}; ";
|
|
return $"select _key from [{tn}]; ";
|
|
});
|
|
|
|
/// <summary>>获取指定类型的主键,按 Flag 属性筛选。</summary>
|
|
public Result<string[]> Keys<T>(long flag = 0) where T : class, IRecord, new() => Keys(typeof(T), flag);
|
|
|
|
#endregion
|
|
|
|
#region static
|
|
|
|
/// <summary>对文本转义,符合 SQL 安全性。可根据字段类型限制 UTF-8 字节数,默认为 0 时不限制字节数。</summary>
|
|
public static string Escape(string text, int bytes = 0)
|
|
{
|
|
if (text.IsEmpty()) return "";
|
|
|
|
var t = text ?? "";
|
|
t = t.Replace("\\", "\\\\");
|
|
t = t.Replace("'", "\\'");
|
|
t = t.Replace("\n", "\\n");
|
|
t = t.Replace("\r", "\\r");
|
|
t = t.Replace("\b", "\\b");
|
|
t = t.Replace("\t", "\\t");
|
|
t = t.Replace("\f", "\\f");
|
|
|
|
if (bytes > 5)
|
|
{
|
|
if (t.Bytes(Encoding.UTF8).Length > bytes)
|
|
{
|
|
while (true)
|
|
{
|
|
t = t.Substring(0, t.Length - 1);
|
|
if (t.Bytes(Encoding.UTF8).Length <= (bytes - 4)) break;
|
|
}
|
|
t = t + " ...";
|
|
}
|
|
}
|
|
|
|
return t;
|
|
}
|
|
|
|
private static string GetColumnTypeName(ColumnType type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case ColumnType.Bytes:
|
|
return "blob";
|
|
case ColumnType.Integer:
|
|
return "integer";
|
|
case ColumnType.Float:
|
|
return "float";
|
|
case ColumnType.VarChar:
|
|
case ColumnType.VarChar191:
|
|
case ColumnType.VarCharMax:
|
|
return "varchar";
|
|
case ColumnType.Text:
|
|
return "text";
|
|
case ColumnType.NVarChar:
|
|
case ColumnType.NVarChar191:
|
|
case ColumnType.NVarCharMax:
|
|
return "nvarchar";
|
|
case ColumnType.NText:
|
|
return "ntext";
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static string Declaration(ColumnAttribute column)
|
|
{
|
|
var type = TextUtility.Empty;
|
|
var length = NumberUtility.Restrict(column.Length, 0, 255).ToString();
|
|
switch (column.Type)
|
|
{
|
|
case ColumnType.Bytes:
|
|
type = "blob";
|
|
break;
|
|
case ColumnType.Integer:
|
|
type = "integer";
|
|
break;
|
|
case ColumnType.Float:
|
|
type = "real";
|
|
break;
|
|
case ColumnType.DateTime:
|
|
type = "datetime";
|
|
break;
|
|
case ColumnType.VarChar:
|
|
type = TextUtility.Merge("varchar(", length, ")");
|
|
break;
|
|
case ColumnType.VarChar191:
|
|
type = TextUtility.Merge("varchar(191)");
|
|
break;
|
|
case ColumnType.VarCharMax:
|
|
type = TextUtility.Merge("varchar(255)");
|
|
break;
|
|
case ColumnType.Text:
|
|
type = TextUtility.Merge("text");
|
|
break;
|
|
case ColumnType.NVarChar:
|
|
type = TextUtility.Merge("nvarchar(", length, ")");
|
|
break;
|
|
case ColumnType.NVarChar191:
|
|
type = TextUtility.Merge("nvarchar(191)");
|
|
break;
|
|
case ColumnType.NVarCharMax:
|
|
type = TextUtility.Merge("nvarchar(255)");
|
|
break;
|
|
case ColumnType.NText:
|
|
type = TextUtility.Merge("ntext");
|
|
break;
|
|
default:
|
|
return TextUtility.Empty;
|
|
}
|
|
return TextUtility.Merge("[", column.Field, "] ", type);
|
|
}
|
|
|
|
/// <summary>创建参数。</summary>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public static SQLiteParameter Parameter(Parameter parameter)
|
|
{
|
|
if (parameter == null) throw new InvalidOperationException("参数无效。");
|
|
return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
|
|
}
|
|
|
|
/// <summary>创建参数。</summary>
|
|
public static SQLiteParameter Parameter(string name, ColumnType type, int size, object value)
|
|
{
|
|
var n = TextUtility.Trim(name);
|
|
if (TextUtility.IsBlank(n)) return null;
|
|
|
|
var t = GetColumnTypeName(type);
|
|
|
|
var s = size;
|
|
switch (type)
|
|
{
|
|
case ColumnType.VarChar:
|
|
s = NumberUtility.Restrict(s, 0, 8000);
|
|
break;
|
|
case ColumnType.NVarChar:
|
|
s = NumberUtility.Restrict(s, 0, 4000);
|
|
break;
|
|
case ColumnType.VarChar191:
|
|
case ColumnType.NVarChar191:
|
|
s = NumberUtility.Restrict(s, 0, 191);
|
|
break;
|
|
case ColumnType.VarCharMax:
|
|
case ColumnType.NVarCharMax:
|
|
s = NumberUtility.Restrict(s, 0, 255);
|
|
break;
|
|
default:
|
|
s = 0;
|
|
break;
|
|
}
|
|
|
|
var v = value;
|
|
if (v is string && v != null && s > 0)
|
|
{
|
|
v = TextUtility.Left((string)v, s);
|
|
}
|
|
|
|
var p = new SQLiteParameter();
|
|
p.ParameterName = n;
|
|
p.TypeName = t;
|
|
p.Value = v;
|
|
if (s > 0) p.Size = s;
|
|
return p;
|
|
}
|
|
|
|
/// <summary>创建参数。</summary>
|
|
public static IDbDataParameter Parameter(string field, DbType type, int size, object value)
|
|
{
|
|
var p = new SQLiteParameter();
|
|
p.ParameterName = field;
|
|
p.DbType = type;
|
|
p.Size = size;
|
|
p.Value = value;
|
|
return p;
|
|
}
|
|
|
|
/// <summary>创建参数。</summary>
|
|
public static IDbDataParameter Parameter(string field, DbType type, object value)
|
|
{
|
|
var p = new SQLiteParameter();
|
|
p.ParameterName = field;
|
|
p.DbType = type;
|
|
p.Value = value;
|
|
return p;
|
|
}
|
|
|
|
/// <summary>备份数据库,返回错误信息。</summary>
|
|
public static string Backup(Sqlite source, Sqlite destination)
|
|
{
|
|
if (source == null) return "备份失败:源无效。";
|
|
if (destination == null) return "备份失败:目标无效。";
|
|
var sConnect = source.Connect();
|
|
if (sConnect.NotEmpty()) return sConnect;
|
|
var dConnect = source.Connect();
|
|
if (dConnect.NotEmpty()) return dConnect;
|
|
|
|
lock (source._locker)
|
|
{
|
|
lock (destination._locker)
|
|
{
|
|
try
|
|
{
|
|
source._db.BackupDatabase(destination._db, "main", "main", -1, null, 0);
|
|
return "";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "SQLite Load Failed: " + ex.Message;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>整理数据库,压缩未使用的空间。</summary>
|
|
public const string Vacuum = "vacuum";
|
|
|
|
/// <summary>内存数据库的地址。</summary>
|
|
public const string Memory = ":memory:";
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|