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.

963 lines
36 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
  1. /* 2021.12.07 */
  2. using Apewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Text;
  8. using static Apewer.Source.SourceUtility;
  9. using System.Data.SqlClient;
  10. using System.IO;
  11. #if NETFRAMEWORK
  12. using System.Data.Sql;
  13. #endif
  14. namespace Apewer.Source
  15. {
  16. /// <summary></summary>
  17. [Serializable]
  18. public class SqlClient : IDbClient
  19. {
  20. #region 变量、构造函数
  21. private Timeout _timeout = null;
  22. private string _connectionstring = "";
  23. /// <summary>获取或设置日志记录。</summary>
  24. public Logger Logger { get; set; }
  25. /// <summary>超时设定。</summary>
  26. public Timeout Timeout { get => _timeout; }
  27. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  28. public SqlClient(string connectionString, Timeout timeout = null)
  29. {
  30. _timeout = timeout ?? Timeout.Default;
  31. _connectionstring = connectionString ?? "";
  32. }
  33. /// <summary>使用连接凭据创建数据库连接实例。</summary>
  34. public SqlClient(string address, string store, string user, string pass, Timeout timeout = null)
  35. {
  36. if (timeout == null) timeout = Timeout.Default;
  37. var a = address ?? "";
  38. var s = store ?? "";
  39. var u = user ?? "";
  40. var p = pass ?? "";
  41. var cs = $"data source = {a}; initial catalog = {s}; ";
  42. if (string.IsNullOrEmpty(u)) cs += "integrated security = sspi; ";
  43. else
  44. {
  45. cs += $"user id = {u}; ";
  46. if (!string.IsNullOrEmpty(p)) cs += $"password = {p}; ";
  47. }
  48. cs += $"connection timeout = {timeout.Connect}; ";
  49. _timeout = timeout ?? Timeout.Default;
  50. _connectionstring = cs;
  51. }
  52. #endregion
  53. #region Ado - Connection
  54. private SqlConnection _db = null;
  55. /// <summary>连接字符串。</summary>
  56. public string ConnectionString { get => _connectionstring; }
  57. /// <summary>获取当前的 SqlConnection 对象。</summary>
  58. public IDbConnection Connection { get => _db; }
  59. /// <summary>数据库是否已经连接。</summary>
  60. public bool Online
  61. {
  62. get
  63. {
  64. if (_db == null) return false;
  65. return (_db.State == ConnectionState.Open);
  66. }
  67. }
  68. /// <summary>连接数据库,若未连接则尝试连接,获取连接成功的状态。</summary>
  69. public string Connect()
  70. {
  71. if (_db == null)
  72. {
  73. _db = new SqlConnection();
  74. _db.ConnectionString = _connectionstring;
  75. }
  76. else
  77. {
  78. if (_db.State == ConnectionState.Open) return null;
  79. }
  80. try
  81. {
  82. _db.Open();
  83. switch (_db.State)
  84. {
  85. case ConnectionState.Open: return null;
  86. default: return $"连接失败,当前处于 {_db.State} 状态。";
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. Logger.Error(nameof(SqlClient), "Connection", ex, _db.ConnectionString);
  92. Close();
  93. return ex.Message;
  94. }
  95. }
  96. /// <summary>改变</summary>
  97. /// <param name="store"></param>
  98. /// <returns></returns>
  99. public string Change(string store)
  100. {
  101. if (store.IsEmpty()) return "未指定数据名称。";
  102. var connect = Connect();
  103. if (connect.NotEmpty()) return connect;
  104. try
  105. {
  106. _db.ChangeDatabase(store);
  107. return null;
  108. }
  109. catch (Exception ex) { return ex.Message(); }
  110. }
  111. /// <summary>关闭连接,并释放对象所占用的系统资源。</summary>
  112. public void Close()
  113. {
  114. if (_db != null)
  115. {
  116. if (_transaction != null)
  117. {
  118. if (_autocommit) Commit();
  119. else Rollback();
  120. }
  121. _db.Close();
  122. _db.Dispose();
  123. _db = null;
  124. }
  125. }
  126. /// <summary>关闭连接,释放对象所占用的系统资源,并清除连接信息。</summary>
  127. public void Dispose() => Close();
  128. #endregion
  129. #region Ado - Transaction
  130. private IDbTransaction _transaction = null;
  131. private bool _autocommit = false;
  132. /// <summary>启动事务。</summary>
  133. public string Begin(bool commit = true) => Begin(commit, null);
  134. /// <summary>启动事务。</summary>
  135. public string Begin(bool commit, Class<IsolationLevel> isolation)
  136. {
  137. var connect = Connect();
  138. if (connect.NotEmpty()) return connect;
  139. if (_transaction != null) return "存在已启动的事务,无法再次启动。";
  140. try
  141. {
  142. _transaction = isolation ? _db.BeginTransaction(isolation.Value) : _db.BeginTransaction();
  143. _autocommit = commit;
  144. return null;
  145. }
  146. catch (Exception ex)
  147. {
  148. Logger.Error(nameof(SqlClient), "Begin", ex.Message());
  149. return ex.Message();
  150. }
  151. }
  152. /// <summary>提交事务。</summary>
  153. public string Commit()
  154. {
  155. if (_transaction == null) return "事务不存在。";
  156. try
  157. {
  158. _transaction.Commit();
  159. RuntimeUtility.Dispose(_transaction);
  160. _transaction = null;
  161. return null;
  162. }
  163. catch (Exception ex)
  164. {
  165. RuntimeUtility.Dispose(_transaction);
  166. _transaction = null;
  167. Logger.Error(nameof(SqlClient), "Commit", ex.Message());
  168. return ex.Message();
  169. }
  170. }
  171. /// <summary>从挂起状态回滚事务。</summary>
  172. public string Rollback()
  173. {
  174. if (_transaction == null) return "事务不存在。";
  175. try
  176. {
  177. _transaction.Rollback();
  178. RuntimeUtility.Dispose(_transaction);
  179. _transaction = null;
  180. return null;
  181. }
  182. catch (Exception ex)
  183. {
  184. RuntimeUtility.Dispose(_transaction);
  185. _transaction = null;
  186. Logger.Error(nameof(SqlClient), "Rollback", ex.Message);
  187. return ex.Message();
  188. }
  189. }
  190. #endregion
  191. #region Ado - SQL
  192. /// <summary>查询。</summary>
  193. public IQuery Query(string sql) => Query(sql, null);
  194. /// <summary>查询。</summary>
  195. public IQuery Query(string sql, IEnumerable<IDataParameter> parameters)
  196. {
  197. if (TextUtility.IsBlank(sql)) return Example.InvalidQueryStatement;
  198. var connected = Connect();
  199. if (connected.NotEmpty()) return new Query(false, connected);
  200. try
  201. {
  202. using (var command = new SqlCommand())
  203. {
  204. command.Connection = _db;
  205. command.CommandTimeout = _timeout.Query;
  206. command.CommandText = sql;
  207. if (parameters != null)
  208. {
  209. foreach (var p in parameters)
  210. {
  211. if (p != null) command.Parameters.Add(p);
  212. }
  213. }
  214. using (var dataset = new DataSet())
  215. {
  216. using (var da = new SqlDataAdapter(command))
  217. {
  218. var name = "table_" + Guid.NewGuid().ToString("n");
  219. da.Fill(dataset, name);
  220. var table = dataset.Tables[name];
  221. return new Query(table);
  222. }
  223. }
  224. }
  225. }
  226. catch (Exception exception)
  227. {
  228. Logger.Error(nameof(SqlClient), "Query", exception, sql);
  229. return new Query(exception);
  230. }
  231. }
  232. /// <summary>执行。</summary>
  233. public IExecute Execute(string sql) => Execute(sql, null, true);
  234. /// <summary>执行单条 Transact-SQL 语句,并加入参数。</summary>
  235. public IExecute Execute(string sql, IEnumerable<IDataParameter> parameters) => Execute(sql, parameters, true);
  236. /// <summary>执行单条 Transact-SQL 语句,并加入参数。</summary>
  237. IExecute Execute(string sql, IEnumerable<IDataParameter> parameters, bool requireTransaction)
  238. {
  239. if (TextUtility.IsBlank(sql)) return Example.InvalidExecuteStatement;
  240. var connected = Connect();
  241. if (connected.NotEmpty()) return new Execute(false, connected);
  242. var inTransaction = _transaction != null;
  243. if (requireTransaction && !inTransaction) Begin();
  244. try
  245. {
  246. using (var command = new SqlCommand())
  247. {
  248. command.Connection = _db;
  249. if (requireTransaction) command.Transaction = (SqlTransaction)_transaction;
  250. command.CommandTimeout = _timeout.Execute;
  251. command.CommandText = sql;
  252. if (parameters != null)
  253. {
  254. foreach (var parameter in parameters)
  255. {
  256. if (parameter != null) command.Parameters.Add(parameter);
  257. }
  258. }
  259. var rows = command.ExecuteNonQuery();
  260. if (requireTransaction && !inTransaction) Commit(); // todo 此处应该检查事务提交产生的错误。
  261. return new Execute(true, rows);
  262. }
  263. }
  264. catch (Exception exception)
  265. {
  266. Logger.Error(nameof(SqlClient), "Execute", exception, sql);
  267. if (requireTransaction && !inTransaction) Rollback();
  268. return new Execute(exception);
  269. }
  270. }
  271. /// <summary>批量插入,必须在 DataTable 中指定表名,或指定 tableName 参数。</summary>
  272. /// <exception cref="ArgumentNullException"></exception>
  273. /// <exception cref="ArgumentException"></exception>
  274. public void BulkCopy(DataTable table, string tableName = null)
  275. {
  276. // 检查 table 参数。
  277. if (table == null) throw new ArgumentNullException(nameof(table));
  278. if (table.Rows.Count < 1) return;
  279. // 检查 tableName 参数。
  280. if (tableName.IsEmpty()) tableName = table.TableName;
  281. if (tableName.IsEmpty()) throw new ArgumentException("未指定表名。");
  282. // 连接数据库。
  283. var connect = Connect();
  284. if (connect.NotEmpty()) throw new Exception(connect);
  285. // 批量插入。
  286. var bc = null as SqlBulkCopy;
  287. try
  288. {
  289. bc = new SqlBulkCopy(_db);
  290. bc.DestinationTableName = tableName;
  291. bc.BatchSize = table.Rows.Count;
  292. bc.WriteToServer(table);
  293. }
  294. catch (Exception ex)
  295. {
  296. try { bc.Close(); } catch { }
  297. throw ex;
  298. }
  299. }
  300. #endregion
  301. #region ORM
  302. /// <summary>查询数据库中的所有表名。</summary>
  303. public string[] TableNames()
  304. {
  305. var list = new List<string>();
  306. if (Connect().IsEmpty())
  307. {
  308. var sql = "select [name] from [sysobjects] where [type] = 'u' order by [name]; ";
  309. var query = (Query)Query(sql);
  310. for (int r = 0; r < query.Rows; r++)
  311. {
  312. var cell = query.Text(r, 0);
  313. if (TextUtility.IsBlank(cell)) continue;
  314. list.Add(cell);
  315. }
  316. query.Dispose();
  317. }
  318. return list.ToArray();
  319. }
  320. /// <summary>查询数据库实例中的所有数据库名。</summary>
  321. public string[] StoreNames()
  322. {
  323. var list = new List<string>();
  324. if (Connect().IsEmpty())
  325. {
  326. var sql = "select [name] from [master]..[sysdatabases] order by [name]; ";
  327. var query = (Query)Query(sql);
  328. for (int r = 0; r < query.Rows; r++)
  329. {
  330. var cell = query.Text(r, 0);
  331. if (TextUtility.IsBlank(cell)) continue;
  332. if (cell == "master") continue;
  333. if (cell == "model") continue;
  334. if (cell == "msdb") continue;
  335. if (cell == "tempdb") continue;
  336. list.Add(cell);
  337. }
  338. query.Dispose();
  339. }
  340. return list.ToArray();
  341. }
  342. /// <summary>查询表中的所有列名。</summary>
  343. public string[] ColumnNames(string tableName)
  344. {
  345. var list = new List<string>();
  346. if (Connect().IsEmpty())
  347. {
  348. var table = TextUtility.AntiInject(tableName);
  349. var sql = TextUtility.Merge("select [name] from [syscolumns] where [id] = object_id('", table, "'); ");
  350. var query = (Query)Query(sql);
  351. for (int r = 0; r < query.Rows; r++)
  352. {
  353. var cell = query.Text(r, 0);
  354. if (TextUtility.IsBlank(cell)) continue;
  355. list.Add(cell);
  356. }
  357. query.Dispose();
  358. }
  359. return list.ToArray();
  360. }
  361. /// <summary>创建数据库,返回错误信息。</summary>
  362. /// <param name="name">数据库名称。</param>
  363. /// <param name="logMaxSizeMB">日志文件的最大 MB 数,指定非正数将不限制增长。</param>
  364. /// <returns>成功时候返回 NULL 值,失败时返回错误信息。</returns>
  365. public string CreateStore(string name, int logMaxSizeMB = 1024)
  366. {
  367. var store = name.Escape().ToTrim();
  368. if (store.IsEmpty()) return "创建失败:未指定数据库名称。";
  369. if (ConnectionString.IsEmpty()) return "创建失败:未指定数据库连接方式。";
  370. using (var source = new SqlClient(ConnectionString))
  371. {
  372. var connect = source.Connect();
  373. if (connect.NotEmpty()) return "创建失败:" + connect;
  374. var schema = source.SimpleCell("select default_schema_name from sys.database_principals where type = 'S' and name=user_name()");
  375. if (schema.IsEmpty()) return "创建失败:无法获取默认模式名称。";
  376. var refPath = source.SimpleCell(@"select f.physical_name path from sys.filegroups g left join sys.database_files f on f.data_space_id = g.data_space_id where g.name = 'PRIMARY' and g.type = 'FG' and g.is_default = 1 and g.filegroup_guid is null");
  377. if (refPath.IsEmpty()) return "创建失败:无法获取文件路径。";
  378. var dir = Path.GetDirectoryName(refPath);
  379. var mdfPath = Path.Combine(dir, store) + ".mdf";
  380. var ldfPath = Path.Combine(dir, store) + "_log.ldf";
  381. // 创建库。
  382. var maxLog = logMaxSizeMB > 0 ? $"{logMaxSizeMB}MB" : "UNLIMITED";
  383. var sql1 = $@"
  384. CREATE DATABASE [{store}]
  385. ON PRIMARY
  386. (
  387. NAME = N'{store}',
  388. FILENAME = N'{mdfPath}',
  389. SIZE = 4MB,
  390. MAXSIZE = UNLIMITED,
  391. FILEGROWTH = 4MB
  392. )
  393. LOG ON
  394. (
  395. NAME = N'{store}_log',
  396. FILENAME = N'{ldfPath}',
  397. SIZE = 1MB,
  398. MAXSIZE = {maxLog},
  399. FILEGROWTH = 1MB
  400. )
  401. COLLATE Chinese_PRC_CI_AS
  402. ";
  403. var create = source.Execute(sql1, null, false);
  404. if (!create.Success) return TextUtility.Merge("创建失败:", create.Message);
  405. // 设置兼容性级别。
  406. var sql2 = $"alter database [{store}] set compatibility_level = 0";
  407. source.Execute(sql2, null, false);
  408. // 设置恢复默认为“简单”
  409. var sql3 = $"alter database [{store}] set recovery simple";
  410. source.Execute(sql3, null, false);
  411. return null;
  412. }
  413. }
  414. static string XType(int xtype)
  415. {
  416. switch (xtype)
  417. {
  418. case 34: return "image";
  419. case 35: return "text";
  420. case 36: return "uniqueidentifier";
  421. case 48: return "tinyint";
  422. case 52: return "smallint";
  423. case 56: return "int";
  424. case 58: return "smalldatetime";
  425. case 59: return "real";
  426. case 60: return "money";
  427. case 61: return "datetime";
  428. case 62: return "float";
  429. case 98: return "sql_variant";
  430. case 99: return "ntext";
  431. case 104: return "bit";
  432. case 106: return "decimal";
  433. case 108: return "numeric";
  434. case 122: return "smallmoney";
  435. case 127: return "bigint";
  436. case 165: return "varbinary";
  437. case 167: return "varchar";
  438. case 173: return "binary";
  439. case 175: return "char";
  440. case 189: return "timestamp";
  441. case 231: return "nvarchar";
  442. case 239: return "nchar";
  443. case 241: return "xml";
  444. }
  445. return null;
  446. }
  447. /// <summary>获取列信息。</summary>
  448. public ColumnInfo[] ColumnsInfo(string tableName)
  449. {
  450. if (tableName.IsEmpty()) throw new ArgumentNullException(nameof(tableName));
  451. var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') ";
  452. using (var query = Query(sql))
  453. {
  454. var ab = new ArrayBuilder<ColumnInfo>();
  455. for (var i = 0; i < query.Rows; i++)
  456. {
  457. var info = new ColumnInfo();
  458. info.Name = query.Text(i, "name");
  459. info.Type = XType(query.Int32(i, "xtype"));
  460. info.Length = query.Int32(i, "length");
  461. ab.Add(info);
  462. }
  463. return ab.Export();
  464. }
  465. }
  466. /// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  467. public string Initialize<T>() where T : class, new() => Initialize(typeof(T));
  468. /// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  469. public string Initialize(Type model)
  470. {
  471. var structure = TableStructure.Parse(model);
  472. if (structure == null) return "无法解析记录模型。";
  473. // 连接数据库。
  474. var connect = Connect();
  475. if (connect.NotEmpty()) return connect;
  476. // 检查现存表。
  477. var exists = false;
  478. var tables = TableNames();
  479. if (tables.Length > 0)
  480. {
  481. var lower = structure.Name.ToLower();
  482. foreach (var table in tables)
  483. {
  484. if (TextUtility.IsBlank(table)) continue;
  485. if (table.ToLower() == lower)
  486. {
  487. exists = true;
  488. break;
  489. }
  490. }
  491. }
  492. if (exists)
  493. {
  494. // 获取已存在的列名。
  495. var columns = ColumnNames(structure.Name);
  496. if (columns.Length > 0)
  497. {
  498. var lower = new List<string>();
  499. foreach (var column in columns)
  500. {
  501. if (TextUtility.IsBlank(column)) continue;
  502. lower.Add(column.ToLower());
  503. }
  504. columns = lower.ToArray();
  505. }
  506. // 增加列。
  507. foreach (var column in structure.Columns)
  508. {
  509. // 检查 Independent 特性。
  510. if (structure.Independent && column.Independent) continue;
  511. // 去重。
  512. var lower = column.Field.ToLower();
  513. if (columns.Contains(lower)) continue;
  514. var type = Declaration(column);
  515. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  516. var sql = TextUtility.Merge("alter table [", structure.Name, "] add ", type, "; ");
  517. var execute = Execute(sql);
  518. if (execute.Success == false) return execute.Message;
  519. }
  520. return TextUtility.Empty;
  521. }
  522. else
  523. {
  524. var sqlcolumns = new List<string>();
  525. foreach (var column in structure.Columns)
  526. {
  527. // 检查 Independent 特性。
  528. if (structure.Independent && column.Independent) continue;
  529. var type = Declaration(column);
  530. if (!column.Independent)
  531. {
  532. if (column.PrimaryKey) type = type + " primary key";
  533. if (column.Incremental) type += " identity";
  534. }
  535. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  536. sqlcolumns.Add(type);
  537. }
  538. if (sqlcolumns.Count < 1) return $"无法对类型 {model.FullName} 创建表:没有定义任何字段。";
  539. var sql = TextUtility.Merge("create table [", structure.Name, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  540. var execute = Execute(sql);
  541. if (execute.Success) return TextUtility.Empty;
  542. return execute.Message;
  543. }
  544. }
  545. /// <summary>插入记录。返回错误信息。</summary>
  546. public string Insert(object record, string table = null)
  547. {
  548. if (record == null) return "参数无效。";
  549. FixProperties(record);
  550. var structure = TableStructure.Parse(record.GetType());
  551. if (structure == null) return "无法解析记录模型。";
  552. if (string.IsNullOrEmpty(table)) table = structure.Name;
  553. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  554. // 排除字段。
  555. var excluded = new List<string>();
  556. foreach (var ca in structure.Columns)
  557. {
  558. // 排除不使用 ORM 的属性。
  559. if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
  560. }
  561. var ps = structure.CreateParameters(record, Parameter, excluded);
  562. var psc = ps.Length;
  563. if (psc < 1) return "数据模型不包含字段。";
  564. var names = new List<string>(psc);
  565. var values = new List<string>(psc);
  566. foreach (var column in ps)
  567. {
  568. //names.Add(TextGenerator.Merge("[", column, "]"));
  569. names.Add(TextUtility.Merge(column));
  570. values.Add("@" + column);
  571. }
  572. var sb = new StringBuilder();
  573. sb.Append("insert into [", table, "](", string.Join(", ", names.ToArray()), ") ");
  574. sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
  575. var sql = sb.ToString();
  576. var execute = Execute(sql, ps);
  577. if (execute.Success) return TextUtility.Empty;
  578. return execute.Message;
  579. }
  580. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  581. /// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  582. public string Update(IRecord record, string table = null)
  583. {
  584. if (record == null) return "参数无效。";
  585. FixProperties(record);
  586. SetUpdated(record);
  587. var structure = TableStructure.Parse(record.GetType());
  588. if (structure == null) return "无法解析记录模型。";
  589. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  590. if (string.IsNullOrEmpty(table)) table = structure.Name;
  591. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  592. // 排除字段。
  593. var excluded = new List<string>();
  594. if (structure.Key != null) excluded.Add(structure.Key.Field);
  595. foreach (var ca in structure.Columns)
  596. {
  597. // 排除不使用 ORM 的属性、自增属性和主键属性。
  598. if (ca.Independent || ca.Incremental || ca.PrimaryKey) excluded.Add(ca.Field);
  599. }
  600. var ps = structure.CreateParameters(record, Parameter, excluded);
  601. var psc = ps.Length;
  602. if (psc < 1) return "数据模型不包含字段。";
  603. var items = new List<string>();
  604. foreach (var p in ps)
  605. {
  606. var pn = p.ParameterName;
  607. items.Add(TextUtility.Merge("[", pn, "] = @", pn));
  608. }
  609. var key = record.Key.SafeKey();
  610. var sql = TextUtility.Merge("update [", table, "] set ", string.Join(", ", items.ToArray()), " where [_key]='", key, "'; ");
  611. var execute = Execute(sql, ps);
  612. if (execute.Success) return TextUtility.Empty;
  613. return execute.Message;
  614. }
  615. /// <summary>获取按指定语句查询到的所有记录。</summary>
  616. public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters);
  617. /// <summary>获取按指定语句查询到的所有记录。</summary>
  618. public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new()
  619. {
  620. var query = Query(sql, parameters);
  621. if (!query.Success) return new Result<T[]>(query.Message);
  622. var records = query.Fill<T>();
  623. query.Dispose();
  624. var result = new Result<T[]>(records);
  625. return result;
  626. }
  627. /// <summary>获取记录。</summary>
  628. public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) =>
  629. {
  630. if (flag == 0) return $"select * from [{tn}]; ";
  631. return $"select * from [{tn}] where _flag={flag}; ";
  632. });
  633. /// <summary>获取记录。</summary>
  634. public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) =>
  635. {
  636. if (flag == 0) return $"select * from [{tn}]; ";
  637. return $"select * from [{tn}] where _flag={flag}; ";
  638. });
  639. /// <summary>获取具有指定 Key 的记录。</summary>
  640. public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) =>
  641. {
  642. if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; ";
  643. return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; ";
  644. });
  645. /// <summary>获取具有指定 Key 的记录。</summary>
  646. public Result<T> Get<T>(string key, long flag = 0) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) =>
  647. {
  648. if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; ";
  649. return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; ";
  650. });
  651. /// <summary>查询有效的 Key 值。</summary>
  652. public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) =>
  653. {
  654. if (flag == 0) return $"select _key from [{tn}]; ";
  655. return $"select _key from [{tn}] where _flag={flag}; ";
  656. });
  657. /// <summary>查询有效的 Key 值。</summary>
  658. public Result<string[]> Keys<T>(long flag = 0) where T : class, IRecord, new() => Keys(typeof(T), flag);
  659. #endregion
  660. #region public static
  661. #if NET20 || NET40
  662. /// <summary>枚举本地网络中服务器的名称。</summary>
  663. public static SqlServerSource[] EnumerateServer()
  664. {
  665. var list = new List<SqlServerSource>();
  666. // 表中列名:ServerName、InstanceName、IsClustered、Version。
  667. using (var query = new Query(SqlDataSourceEnumerator.Instance.GetDataSources()))
  668. {
  669. for (int i = 0; i < query.Rows; i++)
  670. {
  671. var item = new SqlServerSource();
  672. item.ServerName = query.Text(i, "ServerName");
  673. list.Add(item);
  674. }
  675. }
  676. return list.ToArray();
  677. }
  678. #endif
  679. /// <summary>指定的连接凭据是否符合连接要求,默认指定 master 数据库。</summary>
  680. public static bool Proven(string address, string user, string pass) => Proven(address, "master", user, pass);
  681. /// <summary>指定的连接凭据是否符合连接要求。</summary>
  682. public static bool Proven(string address, string store, string user, string pass)
  683. {
  684. var a = string.IsNullOrEmpty(address);
  685. var s = string.IsNullOrEmpty(store);
  686. var u = string.IsNullOrEmpty(user);
  687. var p = string.IsNullOrEmpty(pass);
  688. if (a) return false;
  689. if (s) return false;
  690. if (u && !p) return false;
  691. return true;
  692. }
  693. /// <summary>创建参数。</summary>
  694. /// <exception cref="ArgumentNullException"></exception>
  695. /// <exception cref="InvalidOperationException"></exception>
  696. static SqlParameter Parameter(Parameter parameter)
  697. {
  698. if (parameter == null) throw new InvalidOperationException("参数无效。");
  699. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  700. }
  701. /// <summary>创建参数。</summary>
  702. public static SqlParameter Parameter(string name, ColumnType type, int size, object value)
  703. {
  704. var vname = TextUtility.Trim(name);
  705. if (TextUtility.IsBlank(vname)) return null;
  706. var vtype = SqlDbType.BigInt;
  707. switch (type)
  708. {
  709. case ColumnType.Boolean:
  710. vtype = SqlDbType.Bit;
  711. break;
  712. case ColumnType.Bytes:
  713. vtype = SqlDbType.Image;
  714. break;
  715. case ColumnType.Integer:
  716. vtype = SqlDbType.BigInt;
  717. break;
  718. case ColumnType.Float:
  719. vtype = SqlDbType.Float;
  720. break;
  721. case ColumnType.DateTime:
  722. vtype = SqlDbType.DateTime;
  723. break;
  724. case ColumnType.VarChar:
  725. case ColumnType.VarChar191:
  726. case ColumnType.VarCharMax:
  727. vtype = SqlDbType.VarChar;
  728. break;
  729. case ColumnType.NVarChar:
  730. case ColumnType.NVarChar191:
  731. case ColumnType.NVarCharMax:
  732. vtype = SqlDbType.NVarChar;
  733. break;
  734. case ColumnType.Text:
  735. vtype = SqlDbType.Text;
  736. break;
  737. case ColumnType.NText:
  738. vtype = SqlDbType.NText;
  739. break;
  740. default:
  741. throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
  742. }
  743. var vsize = size;
  744. switch (type)
  745. {
  746. case ColumnType.VarChar:
  747. vsize = NumberUtility.Restrict(vsize, 0, 8000);
  748. break;
  749. case ColumnType.NVarChar:
  750. vsize = NumberUtility.Restrict(vsize, 0, 4000);
  751. break;
  752. case ColumnType.VarChar191:
  753. case ColumnType.NVarChar191:
  754. vsize = NumberUtility.Restrict(vsize, 0, 191);
  755. break;
  756. default:
  757. vsize = 0;
  758. break;
  759. }
  760. var vvalue = value;
  761. if (vvalue is string && vvalue != null && vsize > 0)
  762. {
  763. vvalue = TextUtility.Left((string)vvalue, vsize);
  764. }
  765. var parameter = new SqlParameter();
  766. parameter.ParameterName = vname;
  767. parameter.SqlDbType = vtype;
  768. parameter.Value = vvalue;
  769. if (vsize > 0) parameter.Size = vsize;
  770. return parameter;
  771. }
  772. /// <summary>创建参数。</summary>
  773. public static SqlParameter Parameter(string name, SqlDbType type, int size, object value)
  774. {
  775. if (value is string && value != null && size > 0)
  776. {
  777. value = TextUtility.Left((string)value, (int)size);
  778. }
  779. var p = new SqlParameter();
  780. p.ParameterName = name ?? "";
  781. p.SqlDbType = type;
  782. p.Size = size;
  783. p.Value = value;
  784. return p;
  785. }
  786. /// <summary>创建参数。</summary>
  787. public static SqlParameter Parameter(string name, SqlDbType type, object value)
  788. {
  789. var p = new SqlParameter();
  790. p.ParameterName = name ?? "";
  791. p.SqlDbType = type;
  792. p.Value = value;
  793. return p;
  794. }
  795. static string Declaration(ColumnAttribute column)
  796. {
  797. var type = TextUtility.Empty;
  798. var vcolumn = column;
  799. var length = Math.Max(0, vcolumn.Length);
  800. switch (vcolumn.Type)
  801. {
  802. case ColumnType.Boolean:
  803. type = "bit";
  804. break;
  805. case ColumnType.Integer:
  806. type = "bigint";
  807. break;
  808. case ColumnType.Float:
  809. type = "float";
  810. break;
  811. case ColumnType.Bytes:
  812. type = "image";
  813. break;
  814. case ColumnType.DateTime:
  815. type = "datetime";
  816. break;
  817. case ColumnType.VarChar:
  818. type = TextUtility.Merge("varchar(", Math.Min(8000, length).ToString(), ")");
  819. break;
  820. case ColumnType.VarChar191:
  821. type = TextUtility.Merge("varchar(191)");
  822. break;
  823. case ColumnType.VarCharMax:
  824. type = TextUtility.Merge("varchar(max)");
  825. break;
  826. case ColumnType.Text:
  827. type = TextUtility.Merge("text");
  828. break;
  829. case ColumnType.NVarChar:
  830. type = TextUtility.Merge("nvarchar(", Math.Min(4000, length).ToString(), ")");
  831. break;
  832. case ColumnType.NVarChar191:
  833. type = TextUtility.Merge("nvarchar(191)");
  834. break;
  835. case ColumnType.NVarCharMax:
  836. type = TextUtility.Merge("nvarchar(max)");
  837. break;
  838. case ColumnType.NText:
  839. type = TextUtility.Merge("ntext");
  840. break;
  841. default:
  842. return TextUtility.Empty;
  843. }
  844. return TextUtility.Merge("[", vcolumn.Field, "] ", type);
  845. }
  846. #endregion
  847. }
  848. }