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.

805 lines
29 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
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. if (!column.Independent)
  359. {
  360. if (column.PrimaryKey) type = type + " primary key";
  361. }
  362. sqlcolumns.Add(type);
  363. }
  364. var sql = TextUtility.Merge("create table [", structure.Name, "](", TextUtility.Join(", ", sqlcolumns), "); ");
  365. var execute = Execute(sql);
  366. if (execute.Success) return TextUtility.Empty;
  367. return execute.Message;
  368. }
  369. }
  370. /// <summary>插入记录。返回错误信息。</summary>
  371. public string Insert(object record, string table = null)
  372. {
  373. if (record == null) return "参数无效。";
  374. SourceUtility.FixProperties(record);
  375. // 解析模型,获取表名。
  376. var structure = TableStructure.Parse(record.GetType());
  377. if (structure == null) return "无法解析记录模型。";
  378. if (string.IsNullOrEmpty(table)) table = structure.Name;
  379. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  380. // 排除字段。
  381. var excluded = new List<string>();
  382. foreach (var ca in structure.Columns)
  383. {
  384. // 排除不使用 ORM 的属性。
  385. if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
  386. }
  387. // 准备参数。
  388. var ps = structure.CreateParameters(record, Parameter, excluded);
  389. var psc = ps.Length;
  390. if (psc < 1) return "数据模型不包含字段。";
  391. // 合成 SQL 语句。
  392. var sb = new StringBuilder();
  393. sb.Append("insert into [");
  394. sb.Append(table);
  395. sb.Append("](");
  396. for (var i = 0; i < psc; i++)
  397. {
  398. if (i > 0) sb.Append(", ");
  399. sb.Append(ps[i].ParameterName);
  400. }
  401. sb.Append(") values(");
  402. for (var i = 0; i < psc; i++)
  403. {
  404. if (i > 0) sb.Append(", ");
  405. sb.Append("@");
  406. sb.Append(ps[i].ParameterName);
  407. }
  408. sb.Append("); ");
  409. var sql = sb.ToString();
  410. // 执行。
  411. var execute = Execute(sql, ps);
  412. if (execute.Success && execute.Rows > 0) return TextUtility.Empty;
  413. return execute.Message;
  414. }
  415. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  416. /// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  417. public string Update(IRecord record, string table = null)
  418. {
  419. if (record == null) return "参数无效。";
  420. FixProperties(record);
  421. SetUpdated(record);
  422. // 解析模型,获取表名。
  423. var structure = TableStructure.Parse(record.GetType());
  424. if (structure == null) return "无法解析记录模型。";
  425. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  426. if (string.IsNullOrEmpty(table)) table = structure.Name;
  427. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  428. // 排除字段。
  429. var excluded = new List<string>();
  430. if (structure.Key != null) excluded.Add(structure.Key.Field);
  431. foreach (var ca in structure.Columns)
  432. {
  433. // 排除不使用 ORM 的属性、自增属性和主键属性。
  434. if (ca.Independent || ca.Incremental || ca.PrimaryKey) excluded.Add(ca.Field);
  435. }
  436. // 准备参数。
  437. var ps = structure.CreateParameters(record, Parameter, excluded);
  438. var psc = ps.Length;
  439. if (psc < 1) return "数据模型不包含字段。";
  440. // 合成 SQL 语句。
  441. var sb = new StringBuilder();
  442. sb.Append("update [");
  443. sb.Append(table);
  444. sb.Append("] set ");
  445. for (var i = 0; i < psc; i++)
  446. {
  447. if (i > 0) sb.Append(", ");
  448. sb.Append("[");
  449. sb.Append(ps[i].ParameterName);
  450. sb.Append("] = @");
  451. sb.Append(ps[i].ParameterName);
  452. }
  453. sb.Append(" where [_key] = '");
  454. sb.Append(record.Key.SafeKey());
  455. sb.Append("'; ");
  456. var sql = sb.ToString();
  457. // 执行。
  458. var execute = Execute(sql, ps);
  459. if (execute.Success && execute.Rows > 0) return TextUtility.Empty;
  460. return execute.Message;
  461. }
  462. /// <summary>获取按指定语句查询到的所有记录。</summary>
  463. public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters);
  464. /// <summary>获取按指定语句查询到的所有记录。</summary>
  465. public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new()
  466. {
  467. var query = Query(sql);
  468. if (!query.Success) return new Result<T[]>(query.Message);
  469. var records = query.Fill<T>();
  470. query.Dispose();
  471. var result = new Result<T[]>(records);
  472. return result;
  473. }
  474. /// <summary>查询多条记录。</summary>
  475. public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) =>
  476. {
  477. if (flag == 0) return $"select * from [{tn}]; ";
  478. return $"select * from [{tn}] where _flag={flag}; ";
  479. });
  480. /// <summary>查询多条记录。</summary>
  481. public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) =>
  482. {
  483. if (flag == 0) return $"select * from [{tn}]; ";
  484. return $"select * from [{tn}] where _flag={flag}; ";
  485. });
  486. /// <summary>获取具有指定 Key 的记录。</summary>
  487. public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) =>
  488. {
  489. if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; ";
  490. return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; ";
  491. });
  492. /// <summary>获取具有指定 Key 的记录。</summary>
  493. public Result<T> Get<T>(string key, long flag) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) =>
  494. {
  495. if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; ";
  496. return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; ";
  497. });
  498. /// <summary>获取指定类型的主键,按 Flag 属性筛选。</summary>
  499. public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) =>
  500. {
  501. if (flag == 0) return $"select _key from [{tn}] where _flag={flag}; ";
  502. return $"select _key from [{tn}]; ";
  503. });
  504. /// <summary>>获取指定类型的主键,按 Flag 属性筛选。</summary>
  505. public Result<string[]> Keys<T>(long flag = 0) where T : class, IRecord, new() => Keys(typeof(T), flag);
  506. #endregion
  507. #region static
  508. /// <summary>对文本转义,符合 SQL 安全性。可根据字段类型限制 UTF-8 字节数,默认为 0 时不限制字节数。</summary>
  509. public static string Escape(string text, int bytes = 0)
  510. {
  511. if (text.IsEmpty()) return "";
  512. var t = text ?? "";
  513. t = t.Replace("\\", "\\\\");
  514. t = t.Replace("'", "\\'");
  515. t = t.Replace("\n", "\\n");
  516. t = t.Replace("\r", "\\r");
  517. t = t.Replace("\b", "\\b");
  518. t = t.Replace("\t", "\\t");
  519. t = t.Replace("\f", "\\f");
  520. if (bytes > 5)
  521. {
  522. if (t.Bytes(Encoding.UTF8).Length > bytes)
  523. {
  524. while (true)
  525. {
  526. t = t.Substring(0, t.Length - 1);
  527. if (t.Bytes(Encoding.UTF8).Length <= (bytes - 4)) break;
  528. }
  529. t = t + " ...";
  530. }
  531. }
  532. return t;
  533. }
  534. private static string GetColumnTypeName(ColumnType type)
  535. {
  536. switch (type)
  537. {
  538. case ColumnType.Bytes:
  539. return "blob";
  540. case ColumnType.Integer:
  541. return "integer";
  542. case ColumnType.Float:
  543. return "float";
  544. case ColumnType.VarChar:
  545. case ColumnType.VarChar191:
  546. case ColumnType.VarCharMax:
  547. return "varchar";
  548. case ColumnType.Text:
  549. return "text";
  550. case ColumnType.NVarChar:
  551. case ColumnType.NVarChar191:
  552. case ColumnType.NVarCharMax:
  553. return "nvarchar";
  554. case ColumnType.NText:
  555. return "ntext";
  556. default:
  557. return null;
  558. }
  559. }
  560. private static string Declaration(ColumnAttribute column)
  561. {
  562. var type = TextUtility.Empty;
  563. var length = NumberUtility.Restrict(column.Length, 0, 255).ToString();
  564. switch (column.Type)
  565. {
  566. case ColumnType.Bytes:
  567. type = "blob";
  568. break;
  569. case ColumnType.Integer:
  570. type = "integer";
  571. break;
  572. case ColumnType.Float:
  573. type = "real";
  574. break;
  575. case ColumnType.DateTime:
  576. type = "datetime";
  577. break;
  578. case ColumnType.VarChar:
  579. type = TextUtility.Merge("varchar(", length, ")");
  580. break;
  581. case ColumnType.VarChar191:
  582. type = TextUtility.Merge("varchar(191)");
  583. break;
  584. case ColumnType.VarCharMax:
  585. type = TextUtility.Merge("varchar(255)");
  586. break;
  587. case ColumnType.Text:
  588. type = TextUtility.Merge("text");
  589. break;
  590. case ColumnType.NVarChar:
  591. type = TextUtility.Merge("nvarchar(", length, ")");
  592. break;
  593. case ColumnType.NVarChar191:
  594. type = TextUtility.Merge("nvarchar(191)");
  595. break;
  596. case ColumnType.NVarCharMax:
  597. type = TextUtility.Merge("nvarchar(255)");
  598. break;
  599. case ColumnType.NText:
  600. type = TextUtility.Merge("ntext");
  601. break;
  602. default:
  603. return TextUtility.Empty;
  604. }
  605. return TextUtility.Merge("[", column.Field, "] ", type);
  606. }
  607. /// <summary>创建参数。</summary>
  608. /// <exception cref="ArgumentNullException"></exception>
  609. /// <exception cref="InvalidOperationException"></exception>
  610. public static SQLiteParameter Parameter(Parameter parameter)
  611. {
  612. if (parameter == null) throw new InvalidOperationException("参数无效。");
  613. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  614. }
  615. /// <summary>创建参数。</summary>
  616. public static SQLiteParameter Parameter(string name, ColumnType type, int size, object value)
  617. {
  618. var n = TextUtility.Trim(name);
  619. if (TextUtility.IsBlank(n)) return null;
  620. var t = GetColumnTypeName(type);
  621. var s = size;
  622. switch (type)
  623. {
  624. case ColumnType.VarChar:
  625. s = NumberUtility.Restrict(s, 0, 8000);
  626. break;
  627. case ColumnType.NVarChar:
  628. s = NumberUtility.Restrict(s, 0, 4000);
  629. break;
  630. case ColumnType.VarChar191:
  631. case ColumnType.NVarChar191:
  632. s = NumberUtility.Restrict(s, 0, 191);
  633. break;
  634. case ColumnType.VarCharMax:
  635. case ColumnType.NVarCharMax:
  636. s = NumberUtility.Restrict(s, 0, 255);
  637. break;
  638. default:
  639. s = 0;
  640. break;
  641. }
  642. var v = value;
  643. if (v is string && v != null && s > 0)
  644. {
  645. v = TextUtility.Left((string)v, s);
  646. }
  647. var p = new SQLiteParameter();
  648. p.ParameterName = n;
  649. p.TypeName = t;
  650. p.Value = v;
  651. if (s > 0) p.Size = s;
  652. return p;
  653. }
  654. /// <summary>创建参数。</summary>
  655. public static IDbDataParameter Parameter(string field, DbType type, int size, object value)
  656. {
  657. var p = new SQLiteParameter();
  658. p.ParameterName = field;
  659. p.DbType = type;
  660. p.Size = size;
  661. p.Value = value;
  662. return p;
  663. }
  664. /// <summary>创建参数。</summary>
  665. public static IDbDataParameter Parameter(string field, DbType type, object value)
  666. {
  667. var p = new SQLiteParameter();
  668. p.ParameterName = field;
  669. p.DbType = type;
  670. p.Value = value;
  671. return p;
  672. }
  673. /// <summary>备份数据库,返回错误信息。</summary>
  674. public static string Backup(Sqlite source, Sqlite destination)
  675. {
  676. if (source == null) return "备份失败:源无效。";
  677. if (destination == null) return "备份失败:目标无效。";
  678. var sConnect = source.Connect();
  679. if (sConnect.NotEmpty()) return sConnect;
  680. var dConnect = source.Connect();
  681. if (dConnect.NotEmpty()) return dConnect;
  682. lock (source._locker)
  683. {
  684. lock (destination._locker)
  685. {
  686. try
  687. {
  688. source._db.BackupDatabase(destination._db, "main", "main", -1, null, 0);
  689. return "";
  690. }
  691. catch (Exception ex)
  692. {
  693. return "SQLite Load Failed: " + ex.Message;
  694. }
  695. }
  696. }
  697. }
  698. /// <summary>整理数据库,压缩未使用的空间。</summary>
  699. public const string Vacuum = "vacuum";
  700. /// <summary>内存数据库的地址。</summary>
  701. public const string Memory = ":memory:";
  702. #endregion
  703. }
  704. }