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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /* 2021.11.07 */
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Data.SQLite;
  7. using System.Text;
  8. //using Mono.Data.Sqlite;
  9. using static Apewer.Source.SourceUtility;
  10. namespace Apewer.Source
  11. {
  12. /// <summary>用于快速连接 SQLite 数据库的辅助。</summary>
  13. public class Sqlite : IDbClient
  14. {
  15. #region 基础
  16. private Timeout _timeout = null;
  17. private string _connstring = "";
  18. private string _path = "";
  19. private string _pass = "";
  20. private object _locker = new object();
  21. /// <summary>获取或设置日志记录。</summary>
  22. public Logger Logger { get; set; }
  23. /// <summary>超时设定。</summary>
  24. public Timeout Timeout { get => _timeout; }
  25. /// <summary>创建连接实例。</summary>
  26. /// <remarks>注意:<br />- 构造函数不会创建不存在的文件;<br />- 参数 path 为文件路径,指定为空时将使用 :memory: 作为路径连接内存。</remarks>
  27. public Sqlite(string path = null, string pass = null, Timeout timeout = null)
  28. {
  29. _timeout = timeout ?? Timeout.Default;
  30. _path = path.IsEmpty() ? Memory : path;
  31. _pass = pass;
  32. if (pass.IsEmpty()) _connstring = $"data source='{_path}'; version=3; ";
  33. else _connstring = $"data source='{_path}'; password={_pass}; version=3; ";
  34. }
  35. #endregion
  36. #region 连接
  37. private SQLiteConnection _db = null;
  38. /// <summary>数据库已经连接。</summary>
  39. public bool Online { get => _db != null && _db.State == ConnectionState.Open; }
  40. /// <summary>连接字符串。</summary>
  41. public string ConnectionString { get => _connstring; }
  42. /// <summary>获取当前的 SQLiteConnection 对象。</summary>
  43. public IDbConnection Connection { get => _db; }
  44. /// <summary>连接数据库,若未连接则尝试连接。</summary>
  45. /// <returns>是否已连接。</returns>
  46. public string Connect()
  47. {
  48. if (_db == null)
  49. {
  50. _db = new SQLiteConnection();
  51. _db.ConnectionString = ConnectionString;
  52. }
  53. else
  54. {
  55. if (_db.State == ConnectionState.Open) return null;
  56. }
  57. try
  58. {
  59. _db.Open();
  60. switch (_db.State)
  61. {
  62. case ConnectionState.Open: return null;
  63. default: return $"连接失败,当前处于 {_db.State} 状态。";
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. Logger.Error(nameof(Sqlite), "Connection", ex, _db.ConnectionString);
  69. Close();
  70. return ex.Message;
  71. }
  72. }
  73. /// <summary>关闭连接,并释放对象所占用的系统资源。</summary>
  74. public void Close()
  75. {
  76. if (_db != null)
  77. {
  78. if (_transaction != null)
  79. {
  80. if (_autocommit) Commit();
  81. else Rollback();
  82. }
  83. lock (_locker)
  84. {
  85. _db.Dispose();
  86. }
  87. _db = null;
  88. }
  89. }
  90. /// <summary>关闭连接,释放对象所占用的系统资源,并清除连接信息。</summary>
  91. public void Dispose() { Close(); }
  92. #endregion
  93. #region Transaction
  94. private IDbTransaction _transaction = null;
  95. private bool _autocommit = false;
  96. /// <summary>启动事务。</summary>
  97. public string Begin(bool commit = true) => Begin(commit, null);
  98. /// <summary>启动事务。</summary>
  99. public string Begin(bool commit, Class<IsolationLevel> isolation)
  100. {
  101. var connect = Connect();
  102. if (connect.NotEmpty()) return connect;
  103. if (_transaction != null) return "存在已启动的事务,无法再次启动。";
  104. try
  105. {
  106. _transaction = isolation ? _db.BeginTransaction(isolation.Value) : _db.BeginTransaction();
  107. _autocommit = commit;
  108. return null;
  109. }
  110. catch (Exception ex)
  111. {
  112. Logger.Error(nameof(Sqlite), "Begin", ex.Message());
  113. return ex.Message();
  114. }
  115. }
  116. /// <summary>提交事务。</summary>
  117. public string Commit()
  118. {
  119. if (_transaction == null) return "事务不存在。";
  120. try
  121. {
  122. _transaction.Commit();
  123. RuntimeUtility.Dispose(_transaction);
  124. _transaction = null;
  125. return null;
  126. }
  127. catch (Exception ex)
  128. {
  129. RuntimeUtility.Dispose(_transaction);
  130. _transaction = null;
  131. Logger.Error(nameof(Sqlite), "Commit", ex.Message());
  132. return ex.Message();
  133. }
  134. }
  135. /// <summary>从挂起状态回滚事务。</summary>
  136. public string Rollback()
  137. {
  138. if (_transaction == null) return "事务不存在。";
  139. try
  140. {
  141. _transaction.Rollback();
  142. RuntimeUtility.Dispose(_transaction);
  143. _transaction = null;
  144. return null;
  145. }
  146. catch (Exception ex)
  147. {
  148. RuntimeUtility.Dispose(_transaction);
  149. _transaction = null;
  150. Logger.Error(nameof(Sqlite), "Rollback", ex.Message());
  151. return ex.Message();
  152. }
  153. }
  154. #endregion
  155. #region SQL
  156. /// <summary>查询。</summary>
  157. public IQuery Query(string sql) => Query(sql, null);
  158. /// <summary>查询。</summary>
  159. public IQuery Query(string sql, IEnumerable<IDataParameter> parameters)
  160. {
  161. if (string.IsNullOrEmpty(sql)) return Example.InvalidQueryStatement;
  162. var connected = Connect();
  163. if (connected.NotEmpty()) return new Query(false, connected);
  164. var query = new Query();
  165. try
  166. {
  167. using (var command = new SQLiteCommand())
  168. {
  169. command.Connection = _db;
  170. command.CommandTimeout = _timeout.Query;
  171. command.CommandText = sql;
  172. if (parameters != null)
  173. {
  174. foreach (var p in parameters)
  175. {
  176. if (p != null) command.Parameters.Add(p);
  177. }
  178. }
  179. using (var dataset = new DataSet())
  180. {
  181. using (var da = new SQLiteDataAdapter(command))
  182. {
  183. var name = "table_" + Guid.NewGuid().ToString("n");
  184. da.Fill(dataset, name);
  185. var table = dataset.Tables[name];
  186. return new Query(table);
  187. }
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. Logger.Error(nameof(Sqlite), "Query", ex, sql);
  194. return new Query(ex);
  195. }
  196. }
  197. /// <summary>执行单条 Transact-SQL 语句。</summary>
  198. public IExecute Execute(string sql) => Execute(sql, null);
  199. /// <summary>执行单条 Transact-SQL 语句,并加入参数。</summary>
  200. public IExecute Execute(string sql, IEnumerable<IDataParameter> parameters)
  201. {
  202. if (string.IsNullOrEmpty(sql)) return Example.InvalidExecuteStatement;
  203. var connected = Connect();
  204. if (connected.NotEmpty()) return new Execute(false, connected);
  205. lock (_locker)
  206. {
  207. var inTransaction = _transaction != null;
  208. if (!inTransaction) Begin();
  209. try
  210. {
  211. using (var command = new SQLiteCommand())
  212. {
  213. command.Connection = _db;
  214. command.Transaction = (SQLiteTransaction)_transaction;
  215. command.CommandTimeout = _timeout.Execute;
  216. command.CommandText = sql;
  217. if (parameters != null)
  218. {
  219. foreach (var p in parameters)
  220. {
  221. if (p != null) command.Parameters.Add(p);
  222. }
  223. }
  224. var rows = command.ExecuteNonQuery();
  225. if (!inTransaction) Commit(); // todo 此处应该检查事务提交产生的错误。
  226. return new Execute(true, rows);
  227. }
  228. }
  229. catch (Exception ex)
  230. {
  231. Logger.Error(nameof(Sqlite), "Execute", ex, sql);
  232. if (!inTransaction) Rollback();
  233. return new Execute(ex);
  234. }
  235. }
  236. }
  237. #endregion
  238. #region 属性。
  239. /// <summary>保存当前数据库到文件,若文件已存在则将重写文件。</summary>
  240. public bool Save(string path, string pass = null)
  241. {
  242. if (!StorageUtility.CreateFile(path, 0, true))
  243. {
  244. Logger.Error(nameof(Sqlite), "Save", TextUtility.Merge("创建文件 ", path, " 失败。"));
  245. return false;
  246. }
  247. using (var destination = new Sqlite(path, pass)) return Save(destination);
  248. }
  249. /// <summary>保存当前数据库到目标数据库。</summary>
  250. public bool Save(Sqlite destination) => string.IsNullOrEmpty(Backup(this, destination));
  251. /// <summary>加载文件到当前数据库。</summary>
  252. public bool Load(string path, string pass = null)
  253. {
  254. using (var source = new Sqlite(path, pass)) return Load(source);
  255. }
  256. /// <summary>加载源数据库到当前数据库。</summary>
  257. public bool Load(Sqlite source) => string.IsNullOrEmpty(Backup(source, this));
  258. #endregion
  259. #region ORM
  260. /// <summary>查询数据库中的所有表名。</summary>
  261. public string[] TableNames()
  262. {
  263. var list = new List<string>();
  264. if (Connect().IsEmpty())
  265. {
  266. var sql = "select name from sqlite_master where type='table' order by name; ";
  267. var query = (Query)Query(sql);
  268. for (int r = 0; r < query.Rows; r++)
  269. {
  270. var cell = query.Text(r, 0);
  271. if (TextUtility.IsBlank(cell)) continue;
  272. list.Add(cell);
  273. }
  274. query.Dispose();
  275. }
  276. return list.ToArray();
  277. }
  278. /// <summary>查询数据库中的所有视图名。</summary>
  279. public string[] ViewNames()
  280. {
  281. var list = new List<string>();
  282. if (Connect().IsEmpty())
  283. {
  284. var sql = "select name from sqlite_master where type='view' order by name; ";
  285. var query = (Query)Query(sql);
  286. for (int r = 0; r < query.Rows; r++)
  287. {
  288. var cell = query.Text(r, 0);
  289. if (TextUtility.IsBlank(cell)) continue;
  290. list.Add(cell);
  291. }
  292. query.Dispose();
  293. }
  294. return list.ToArray();
  295. }
  296. /// <summary>查询表中的所有列名。</summary>
  297. public string[] ColumnNames(string table)
  298. {
  299. var list = new List<string>();
  300. if (Connect().IsEmpty())
  301. {
  302. var t = TextUtility.AntiInject(table);
  303. var sql = TextUtility.Merge("pragma table_info('", TextUtility.AntiInject(t), "'); ");
  304. using (var query = Query(sql) as Query)
  305. {
  306. for (int r = 0; r < query.Rows; r++)
  307. {
  308. var cell = query.Text(r, "name");
  309. if (TextUtility.IsBlank(cell)) continue;
  310. list.Add(cell);
  311. }
  312. }
  313. }
  314. return list.ToArray();
  315. }
  316. /// <summary>创建表,不修改已存在表。成功时返回空字符串,发生异常时返回异常信息。</summary>
  317. public string Initialize(Record model) => model == null ? "参数无效。" : Initialize(model.GetType());
  318. /// <summary>创建表,不修改已存在表。成功时返回空字符串,发生异常时返回异常信息。</summary>
  319. public string Initialize<T>() where T : class, new() => Initialize(typeof(T));
  320. /// <summary>创建表,不修改已存在表。当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  321. public string Initialize(Type model)
  322. {
  323. var structure = TableStructure.Parse(model);
  324. if (structure == null) return "无法解析记录模型。";
  325. // 连接数据库。
  326. var connect = Connect();
  327. if (connect.NotEmpty()) return connect;
  328. // 检查现存表。
  329. var exists = false;
  330. var tables = TableNames();
  331. if (tables.Length > 0)
  332. {
  333. var lower = structure.Name.ToLower();
  334. foreach (var table in tables)
  335. {
  336. if (TextUtility.IsBlank(table)) continue;
  337. if (table.ToLower() == lower)
  338. {
  339. exists = true;
  340. break;
  341. }
  342. }
  343. }
  344. if (exists)
  345. {
  346. return TextUtility.Merge("指定的表已经存在。");
  347. }
  348. else
  349. {
  350. var sqlcolumns = new List<string>();
  351. foreach (var column in structure.Columns)
  352. {
  353. var type = Declaration(column);
  354. if (string.IsNullOrEmpty(type))
  355. {
  356. return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  357. }
  358. sqlcolumns.Add(type);
  359. }
  360. var sql = TextUtility.Merge("create table [", structure.Name, "](", TextUtility.Join(", ", sqlcolumns), "); ");
  361. var execute = Execute(sql);
  362. if (execute.Success) return TextUtility.Empty;
  363. return execute.Message;
  364. }
  365. }
  366. /// <summary>插入记录。返回错误信息。</summary>
  367. public string Insert(object record, string table = null)
  368. {
  369. if (record == null) return "参数无效。";
  370. SourceUtility.FixProperties(record);
  371. // 解析模型,获取表名。
  372. var structure = TableStructure.Parse(record.GetType());
  373. if (structure == null) return "无法解析记录模型。";
  374. if (string.IsNullOrEmpty(table)) table = structure.Name;
  375. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  376. // 准备参数。
  377. var ps = structure.CreateParameters(record, Parameter);
  378. var psc = ps.Length;
  379. if (psc < 1) return "数据模型不包含字段。";
  380. // 合成 SQL 语句。
  381. var sb = new StringBuilder();
  382. sb.Append("insert into [");
  383. sb.Append(table);
  384. sb.Append("](");
  385. for (var i = 0; i < psc; i++)
  386. {
  387. if (i > 0) sb.Append(", ");
  388. sb.Append(ps[i].ParameterName);
  389. }
  390. sb.Append(") values(");
  391. for (var i = 0; i < psc; i++)
  392. {
  393. if (i > 0) sb.Append(", ");
  394. sb.Append("@");
  395. sb.Append(ps[i].ParameterName);
  396. }
  397. sb.Append("); ");
  398. var sql = sb.ToString();
  399. // 执行。
  400. var execute = Execute(sql, ps);
  401. if (execute.Success && execute.Rows > 0) return TextUtility.Empty;
  402. return execute.Message;
  403. }
  404. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  405. /// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  406. public string Update(IRecord record, string table = null)
  407. {
  408. if (record == null) return "参数无效。";
  409. FixProperties(record);
  410. SetUpdated(record);
  411. // 解析模型,获取表名。
  412. var structure = TableStructure.Parse(record.GetType());
  413. if (structure == null) return "无法解析记录模型。";
  414. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  415. if (string.IsNullOrEmpty(table)) table = structure.Name;
  416. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  417. // 准备参数。
  418. var ps = structure.CreateParameters(record, Parameter, "_key");
  419. var psc = ps.Length;
  420. if (psc < 1) return "数据模型不包含字段。";
  421. // 合成 SQL 语句。
  422. var sb = new StringBuilder();
  423. sb.Append("update [");
  424. sb.Append(table);
  425. sb.Append("] set ");
  426. for (var i = 0; i < psc; i++)
  427. {
  428. if (i > 0) sb.Append(", ");
  429. sb.Append("[");
  430. sb.Append(ps[i].ParameterName);
  431. sb.Append("] = @");
  432. sb.Append(ps[i].ParameterName);
  433. }
  434. sb.Append(" where [_key] = '");
  435. sb.Append(record.Key.SafeKey());
  436. sb.Append("'; ");
  437. var sql = sb.ToString();
  438. // 执行。
  439. var execute = Execute(sql, ps);
  440. if (execute.Success && execute.Rows > 0) return TextUtility.Empty;
  441. return execute.Message;
  442. }
  443. /// <summary>获取按指定语句查询到的所有记录。</summary>
  444. public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters);
  445. /// <summary>获取按指定语句查询到的所有记录。</summary>
  446. public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new()
  447. {
  448. var query = Query(sql);
  449. if (!query.Success) return new Result<T[]>(query.Message);
  450. var records = query.Fill<T>();
  451. query.Dispose();
  452. var result = new Result<T[]>(records);
  453. return result;
  454. }
  455. /// <summary>查询多条记录。</summary>
  456. public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) =>
  457. {
  458. if (flag == 0) return $"select * from [{tn}]; ";
  459. return $"select * from [{tn}] where _flag={flag}; ";
  460. });
  461. /// <summary>查询多条记录。</summary>
  462. public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) =>
  463. {
  464. if (flag == 0) return $"select * from [{tn}]; ";
  465. return $"select * from [{tn}] where _flag={flag}; ";
  466. });
  467. /// <summary>获取具有指定 Key 的记录。</summary>
  468. public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) =>
  469. {
  470. if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; ";
  471. return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; ";
  472. });
  473. /// <summary>获取具有指定 Key 的记录。</summary>
  474. public Result<T> Get<T>(string key, long flag) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) =>
  475. {
  476. if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; ";
  477. return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; ";
  478. });
  479. /// <summary>获取指定类型的主键,按 Flag 属性筛选。</summary>
  480. public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) =>
  481. {
  482. if (flag == 0) return $"select _key from [{tn}] where _flag={flag}; ";
  483. return $"select _key from [{tn}]; ";
  484. });
  485. /// <summary>>获取指定类型的主键,按 Flag 属性筛选。</summary>
  486. public Result<string[]> Keys<T>(long flag = 0) where T : class, IRecord, new() => Keys(typeof(T), flag);
  487. #endregion
  488. #region static
  489. /// <summary>对文本转义,符合 SQL 安全性。可根据字段类型限制 UTF-8 字节数,默认为 0 时不限制字节数。</summary>
  490. public static string Escape(string text, int bytes = 0)
  491. {
  492. if (text.IsEmpty()) return "";
  493. var t = text ?? "";
  494. t = t.Replace("\\", "\\\\");
  495. t = t.Replace("'", "\\'");
  496. t = t.Replace("\n", "\\n");
  497. t = t.Replace("\r", "\\r");
  498. t = t.Replace("\b", "\\b");
  499. t = t.Replace("\t", "\\t");
  500. t = t.Replace("\f", "\\f");
  501. if (bytes > 5)
  502. {
  503. if (t.Bytes(Encoding.UTF8).Length > bytes)
  504. {
  505. while (true)
  506. {
  507. t = t.Substring(0, t.Length - 1);
  508. if (t.Bytes(Encoding.UTF8).Length <= (bytes - 4)) break;
  509. }
  510. t = t + " ...";
  511. }
  512. }
  513. return t;
  514. }
  515. private static string GetColumnTypeName(ColumnType type)
  516. {
  517. switch (type)
  518. {
  519. case ColumnType.Bytes:
  520. return "blob";
  521. case ColumnType.Integer:
  522. return "integer";
  523. case ColumnType.Float:
  524. return "float";
  525. case ColumnType.VarChar:
  526. case ColumnType.VarChar191:
  527. case ColumnType.VarCharMax:
  528. return "varchar";
  529. case ColumnType.Text:
  530. return "text";
  531. case ColumnType.NVarChar:
  532. case ColumnType.NVarChar191:
  533. case ColumnType.NVarCharMax:
  534. return "nvarchar";
  535. case ColumnType.NText:
  536. return "ntext";
  537. default:
  538. return null;
  539. }
  540. }
  541. private static string Declaration(ColumnAttribute column)
  542. {
  543. var type = TextUtility.Empty;
  544. var length = NumberUtility.Restrict(column.Length, 0, 255).ToString();
  545. switch (column.Type)
  546. {
  547. case ColumnType.Bytes:
  548. type = "blob";
  549. break;
  550. case ColumnType.Integer:
  551. type = "integer";
  552. break;
  553. case ColumnType.Float:
  554. type = "real";
  555. break;
  556. case ColumnType.DateTime:
  557. type = "datetime";
  558. break;
  559. case ColumnType.VarChar:
  560. type = TextUtility.Merge("varchar(", length, ")");
  561. break;
  562. case ColumnType.VarChar191:
  563. type = TextUtility.Merge("varchar(191)");
  564. break;
  565. case ColumnType.VarCharMax:
  566. type = TextUtility.Merge("varchar(255)");
  567. break;
  568. case ColumnType.Text:
  569. type = TextUtility.Merge("text");
  570. break;
  571. case ColumnType.NVarChar:
  572. type = TextUtility.Merge("nvarchar(", length, ")");
  573. break;
  574. case ColumnType.NVarChar191:
  575. type = TextUtility.Merge("nvarchar(191)");
  576. break;
  577. case ColumnType.NVarCharMax:
  578. type = TextUtility.Merge("nvarchar(255)");
  579. break;
  580. case ColumnType.NText:
  581. type = TextUtility.Merge("ntext");
  582. break;
  583. default:
  584. return TextUtility.Empty;
  585. }
  586. return TextUtility.Merge("[", column.Field, "] ", type);
  587. }
  588. /// <summary>创建参数。</summary>
  589. /// <exception cref="ArgumentNullException"></exception>
  590. /// <exception cref="InvalidOperationException"></exception>
  591. public static SQLiteParameter Parameter(Parameter parameter)
  592. {
  593. if (parameter == null) throw new InvalidOperationException("参数无效。");
  594. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  595. }
  596. /// <summary>创建参数。</summary>
  597. public static SQLiteParameter Parameter(string name, ColumnType type, int size, object value)
  598. {
  599. var n = TextUtility.Trim(name);
  600. if (TextUtility.IsBlank(n)) return null;
  601. var t = GetColumnTypeName(type);
  602. var s = size;
  603. switch (type)
  604. {
  605. case ColumnType.VarChar:
  606. s = NumberUtility.Restrict(s, 0, 8000);
  607. break;
  608. case ColumnType.NVarChar:
  609. s = NumberUtility.Restrict(s, 0, 4000);
  610. break;
  611. case ColumnType.VarChar191:
  612. case ColumnType.NVarChar191:
  613. s = NumberUtility.Restrict(s, 0, 191);
  614. break;
  615. case ColumnType.VarCharMax:
  616. case ColumnType.NVarCharMax:
  617. s = NumberUtility.Restrict(s, 0, 255);
  618. break;
  619. default:
  620. s = 0;
  621. break;
  622. }
  623. var v = value;
  624. if (v is string && v != null && s > 0)
  625. {
  626. v = TextUtility.Left((string)v, s);
  627. }
  628. var p = new SQLiteParameter();
  629. p.ParameterName = n;
  630. p.TypeName = t;
  631. p.Value = v;
  632. if (s > 0) p.Size = s;
  633. return p;
  634. }
  635. /// <summary>创建参数。</summary>
  636. public static IDbDataParameter Parameter(string field, DbType type, int size, object value)
  637. {
  638. var p = new SQLiteParameter();
  639. p.ParameterName = field;
  640. p.DbType = type;
  641. p.Size = size;
  642. p.Value = value;
  643. return p;
  644. }
  645. /// <summary>创建参数。</summary>
  646. public static IDbDataParameter Parameter(string field, DbType type, object value)
  647. {
  648. var p = new SQLiteParameter();
  649. p.ParameterName = field;
  650. p.DbType = type;
  651. p.Value = value;
  652. return p;
  653. }
  654. /// <summary>备份数据库,返回错误信息。</summary>
  655. public static string Backup(Sqlite source, Sqlite destination)
  656. {
  657. if (source == null) return "备份失败:源无效。";
  658. if (destination == null) return "备份失败:目标无效。";
  659. var sConnect = source.Connect();
  660. if (sConnect.NotEmpty()) return sConnect;
  661. var dConnect = source.Connect();
  662. if (dConnect.NotEmpty()) return dConnect;
  663. lock (source._locker)
  664. {
  665. lock (destination._locker)
  666. {
  667. try
  668. {
  669. source._db.BackupDatabase(destination._db, "main", "main", -1, null, 0);
  670. return "";
  671. }
  672. catch (Exception ex)
  673. {
  674. return "SQLite Load Failed: " + ex.Message;
  675. }
  676. }
  677. }
  678. }
  679. /// <summary>整理数据库,压缩未使用的空间。</summary>
  680. public const string Vacuum = "vacuum";
  681. /// <summary>内存数据库的地址。</summary>
  682. public const string Memory = ":memory:";
  683. #endregion
  684. }
  685. }