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.

528 lines
20 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
  1. #if DEBUG
  2. /* 2021.11.28 */
  3. using Apewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.Common;
  8. using System.Text;
  9. using static Apewer.Source.SourceUtility;
  10. using System.Data.SqlClient;
  11. #if NETFRAMEWORK
  12. using System.Data.Sql;
  13. #endif
  14. namespace Apewer.Source
  15. {
  16. /// <summary></summary>
  17. [Serializable]
  18. public class SqlClientThin : DbClient, IDbClient
  19. {
  20. #region
  21. string _str = null;
  22. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  23. public SqlClientThin(string connectionString, Timeout timeout = null) : base(timeout)
  24. {
  25. _str = connectionString ?? "";
  26. }
  27. /// <summary>使用连接凭据创建数据库连接实例。</summary>
  28. public SqlClientThin(string address, string store, string user, string pass, Timeout timeout = null) : base(timeout)
  29. {
  30. if (timeout == null) timeout = Timeout.Default;
  31. var a = address ?? "";
  32. var s = store ?? "";
  33. var u = user ?? "";
  34. var p = pass ?? "";
  35. var cs = $"data source = {a}; initial catalog = {s}; ";
  36. if (string.IsNullOrEmpty(u)) cs += "integrated security = sspi; ";
  37. else
  38. {
  39. cs += $"user id = {u}; ";
  40. if (!string.IsNullOrEmpty(p)) cs += $"password = {p}; ";
  41. }
  42. cs += $"connection timeout = {timeout.Connect}; ";
  43. _str = cs;
  44. }
  45. /// <summary>为 Ado 创建连接字符串。</summary>
  46. protected override string NewConnectionString() => _str;
  47. /// <summary>为 Ado 创建 IDbConnection 对象。</summary>
  48. protected override IDbConnection NewConnection() => new SqlConnection();
  49. /// <summary>为 Ado 创建 IDbCommand 对象。</summary>
  50. protected override IDbCommand NewCommand() => new SqlCommand();
  51. /// <summary>为 Ado 创建 IDataAdapter 对象。</summary>
  52. protected override IDataAdapter NewDataAdapter(IDbCommand command) => new SqlDataAdapter((SqlCommand)command);
  53. #endregion
  54. #region ORM
  55. /// <summary>查询数据库中的所有表名。</summary>
  56. public override string[] TableNames() => TextColumn("select [name] from [sysobjects] where [type] = 'u' order by [name]; ");
  57. /// <summary>查询数据库实例中的所有数据库名。</summary>
  58. public override string[] StoreNames() => TextColumn("select [name] from [master]..[sysdatabases] order by [name]; ", new string[] { "master", "model", "msdb", "tempdb" });
  59. /// <summary>查询表中的所有列名。</summary>
  60. public override string[] ColumnNames(string tableName) => TextColumn($"select [name] from [syscolumns] where [id] = object_id('{tableName}'); ");
  61. /// <summary>获取列信息。</summary>
  62. public override ColumnInfo[] ColumnsInfo(string tableName)
  63. {
  64. if (tableName.IsEmpty()) throw new ArgumentNullException(nameof(tableName));
  65. var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') ";
  66. using (var query = Query(sql))
  67. {
  68. var ab = new ArrayBuilder<ColumnInfo>();
  69. for (var i = 0; i < query.Rows; i++)
  70. {
  71. var info = new ColumnInfo();
  72. info.Name = query.Text(i, "name");
  73. info.Type = XType(query.Int32(i, "xtype"));
  74. info.Length = query.Int32(i, "length");
  75. ab.Add(info);
  76. }
  77. return ab.Export();
  78. }
  79. }
  80. /// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  81. public override string Initialize(Type model)
  82. {
  83. var structure = TableStructure.Parse(model);
  84. if (structure == null) return "无法解析记录模型。";
  85. // 连接数据库。
  86. var connect = Connect();
  87. if (connect.NotEmpty()) return connect;
  88. // 检查现存表。
  89. var exists = false;
  90. var tables = TableNames();
  91. if (tables.Length > 0)
  92. {
  93. var lower = structure.Name.ToLower();
  94. foreach (var table in tables)
  95. {
  96. if (TextUtility.IsBlank(table)) continue;
  97. if (table.ToLower() == lower)
  98. {
  99. exists = true;
  100. break;
  101. }
  102. }
  103. }
  104. if (exists)
  105. {
  106. // 获取已存在的列名。
  107. var columns = ColumnNames(structure.Name);
  108. if (columns.Length > 0)
  109. {
  110. var lower = new List<string>();
  111. foreach (var column in columns)
  112. {
  113. if (TextUtility.IsBlank(column)) continue;
  114. lower.Add(column.ToLower());
  115. }
  116. columns = lower.ToArray();
  117. }
  118. // 增加列。
  119. foreach (var column in structure.Columns)
  120. {
  121. // 检查 Independent 特性。
  122. if (structure.Independent && column.Independent) continue;
  123. // 去重。
  124. var lower = column.Field.ToLower();
  125. if (columns.Contains(lower)) continue;
  126. var type = Declaration(column);
  127. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  128. var sql = TextUtility.Merge("alter table [", structure.Name, "] add ", type, "; ");
  129. var execute = Execute(sql);
  130. if (execute.Success == false) return execute.Message;
  131. }
  132. return TextUtility.Empty;
  133. }
  134. else
  135. {
  136. var sqlcolumns = new List<string>();
  137. foreach (var column in structure.Columns)
  138. {
  139. // 检查 Independent 特性。
  140. if (structure.Independent && column.Independent) continue;
  141. var type = Declaration(column);
  142. if (!column.Independent && column.Property.Name == "Key") type = type + " primary key";
  143. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  144. sqlcolumns.Add(type);
  145. }
  146. var sql = TextUtility.Merge("create table [", structure.Name, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  147. var execute = Execute(sql);
  148. if (execute.Success) return TextUtility.Empty;
  149. return execute.Message;
  150. }
  151. }
  152. /// <summary>插入记录。返回错误信息。</summary>
  153. public override string Insert(object record, string table = null)
  154. {
  155. if (record == null) return "参数无效。";
  156. FixProperties(record);
  157. var structure = TableStructure.Parse(record.GetType());
  158. if (structure == null) return "无法解析记录模型。";
  159. if (string.IsNullOrEmpty(table)) table = structure.Name;
  160. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  161. var ps = structure.CreateParameters(record, Parameter);
  162. var psc = ps.Length;
  163. if (psc < 1) return "数据模型不包含字段。";
  164. var names = new List<string>(psc);
  165. var values = new List<string>(psc);
  166. foreach (var column in ps)
  167. {
  168. //names.Add(TextGenerator.Merge("[", column, "]"));
  169. names.Add(TextUtility.Merge(column));
  170. values.Add("@" + column);
  171. }
  172. var sb = new StringBuilder();
  173. sb.Append("insert into [", table, "](", string.Join(", ", names.ToArray()), ") ");
  174. sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
  175. var sql = sb.ToString();
  176. var execute = Execute(sql, ps);
  177. if (execute.Success) return TextUtility.Empty;
  178. return execute.Message;
  179. }
  180. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  181. /// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  182. public override string Update(IRecord record, string table = null)
  183. {
  184. if (record == null) return "参数无效。";
  185. FixProperties(record);
  186. SetUpdated(record);
  187. var structure = TableStructure.Parse(record.GetType());
  188. if (structure == null) return "无法解析记录模型。";
  189. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  190. if (string.IsNullOrEmpty(table)) table = structure.Name;
  191. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  192. var ps = structure.CreateParameters(record, Parameter, "_key");
  193. var psc = ps.Length;
  194. if (psc < 1) return "数据模型不包含字段。";
  195. var items = new List<string>();
  196. foreach (var p in ps)
  197. {
  198. var pn = p.ParameterName;
  199. items.Add(TextUtility.Merge("[", pn, "] = @", pn));
  200. }
  201. var key = record.Key.SafeKey();
  202. var sql = TextUtility.Merge("update [", table, "] set ", string.Join(", ", items.ToArray()), " where [_key]='", key, "'; ");
  203. var execute = Execute(sql, ps);
  204. if (execute.Success) return TextUtility.Empty;
  205. return execute.Message;
  206. }
  207. /// <summary>获取记录。</summary>
  208. public override Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) =>
  209. {
  210. if (flag == 0) return $"select * from [{tn}]; ";
  211. return $"select * from [{tn}] where _flag={flag}; ";
  212. });
  213. /// <summary>获取记录。</summary>
  214. public override Result<T[]> Query<T>(long flag = 0) => SourceUtility.Query<T>(this, (tn) =>
  215. {
  216. if (flag == 0) return $"select * from [{tn}]; ";
  217. return $"select * from [{tn}] where _flag={flag}; ";
  218. });
  219. /// <summary>获取具有指定 Key 的记录。</summary>
  220. public override Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) =>
  221. {
  222. if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; ";
  223. return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; ";
  224. });
  225. /// <summary>获取具有指定 Key 的记录。</summary>
  226. public override Result<T> Get<T>(string key, long flag = 0) => SourceUtility.Get<T>(this, key, (tn, sk) =>
  227. {
  228. if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; ";
  229. return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; ";
  230. });
  231. /// <summary>查询有效的 Key 值。</summary>
  232. public override Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) =>
  233. {
  234. if (flag == 0) return $"select _key from [{tn}]; ";
  235. return $"select _key from [{tn}] where _flag={flag}; ";
  236. });
  237. /// <summary>查询有效的 Key 值。</summary>
  238. public override Result<string[]> Keys<T>(long flag = 0) => Keys(typeof(T), flag);
  239. #endregion
  240. #region public static
  241. #if NET20 || NET40
  242. /// <summary>枚举本地网络中服务器的名称。</summary>
  243. public static SqlServerSource[] EnumerateServer()
  244. {
  245. var list = new List<SqlServerSource>();
  246. // 表中列名:ServerName、InstanceName、IsClustered、Version。
  247. using (var query = new Query(SqlDataSourceEnumerator.Instance.GetDataSources()))
  248. {
  249. for (int i = 0; i < query.Rows; i++)
  250. {
  251. var item = new SqlServerSource();
  252. item.ServerName = query.Text(i, "ServerName");
  253. list.Add(item);
  254. }
  255. }
  256. return list.ToArray();
  257. }
  258. #endif
  259. /// <summary>指定的连接凭据是否符合连接要求,默认指定 master 数据库。</summary>
  260. public static bool Proven(string address, string user, string pass) => Proven(address, "master", user, pass);
  261. /// <summary>指定的连接凭据是否符合连接要求。</summary>
  262. public static bool Proven(string address, string store, string user, string pass)
  263. {
  264. var a = string.IsNullOrEmpty(address);
  265. var s = string.IsNullOrEmpty(store);
  266. var u = string.IsNullOrEmpty(user);
  267. var p = string.IsNullOrEmpty(pass);
  268. if (a) return false;
  269. if (s) return false;
  270. if (u && !p) return false;
  271. return true;
  272. }
  273. /// <summary>创建参数。</summary>
  274. /// <exception cref="ArgumentNullException"></exception>
  275. /// <exception cref="InvalidOperationException"></exception>
  276. static SqlParameter Parameter(Parameter parameter)
  277. {
  278. if (parameter == null) throw new InvalidOperationException("参数无效。");
  279. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  280. }
  281. /// <summary>创建参数。</summary>
  282. public static SqlParameter Parameter(string name, ColumnType type, int size, object value)
  283. {
  284. var vname = TextUtility.Trim(name);
  285. if (TextUtility.IsBlank(vname)) return null;
  286. var vtype = SqlDbType.BigInt;
  287. switch (type)
  288. {
  289. case ColumnType.Bytes:
  290. vtype = SqlDbType.Image;
  291. break;
  292. case ColumnType.Integer:
  293. vtype = SqlDbType.BigInt;
  294. break;
  295. case ColumnType.Float:
  296. vtype = SqlDbType.Float;
  297. break;
  298. case ColumnType.DateTime:
  299. vtype = SqlDbType.DateTime;
  300. break;
  301. case ColumnType.VarChar:
  302. case ColumnType.VarChar191:
  303. case ColumnType.VarCharMax:
  304. vtype = SqlDbType.VarChar;
  305. break;
  306. case ColumnType.NVarChar:
  307. case ColumnType.NVarChar191:
  308. case ColumnType.NVarCharMax:
  309. vtype = SqlDbType.NVarChar;
  310. break;
  311. case ColumnType.Text:
  312. vtype = SqlDbType.Text;
  313. break;
  314. case ColumnType.NText:
  315. vtype = SqlDbType.NText;
  316. break;
  317. default:
  318. throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
  319. }
  320. var vsize = size;
  321. switch (type)
  322. {
  323. case ColumnType.VarChar:
  324. vsize = NumberUtility.Restrict(vsize, 0, 8000);
  325. break;
  326. case ColumnType.NVarChar:
  327. vsize = NumberUtility.Restrict(vsize, 0, 4000);
  328. break;
  329. case ColumnType.VarChar191:
  330. case ColumnType.NVarChar191:
  331. vsize = NumberUtility.Restrict(vsize, 0, 191);
  332. break;
  333. default:
  334. vsize = 0;
  335. break;
  336. }
  337. var vvalue = value;
  338. if (vvalue is string && vvalue != null && vsize > 0)
  339. {
  340. vvalue = TextUtility.Left((string)vvalue, vsize);
  341. }
  342. var parameter = new SqlParameter();
  343. parameter.ParameterName = vname;
  344. parameter.SqlDbType = vtype;
  345. parameter.Value = vvalue;
  346. if (vsize > 0) parameter.Size = vsize;
  347. return parameter;
  348. }
  349. /// <summary>创建参数。</summary>
  350. public static SqlParameter Parameter(string name, SqlDbType type, int size, object value)
  351. {
  352. if (value is string && value != null && size > 0)
  353. {
  354. value = TextUtility.Left((string)value, (int)size);
  355. }
  356. var p = new SqlParameter();
  357. p.ParameterName = name ?? "";
  358. p.SqlDbType = type;
  359. p.Size = size;
  360. p.Value = value;
  361. return p;
  362. }
  363. /// <summary>创建参数。</summary>
  364. public static SqlParameter Parameter(string name, SqlDbType type, object value)
  365. {
  366. var p = new SqlParameter();
  367. p.ParameterName = name ?? "";
  368. p.SqlDbType = type;
  369. p.Value = value;
  370. return p;
  371. }
  372. static string Declaration(ColumnAttribute column)
  373. {
  374. var type = TextUtility.Empty;
  375. var vcolumn = column;
  376. var length = Math.Max(0, vcolumn.Length);
  377. switch (vcolumn.Type)
  378. {
  379. case ColumnType.Integer:
  380. type = "bigint";
  381. break;
  382. case ColumnType.Float:
  383. type = "float";
  384. break;
  385. case ColumnType.Bytes:
  386. type = "image";
  387. break;
  388. case ColumnType.DateTime:
  389. type = "datetime";
  390. break;
  391. case ColumnType.VarChar:
  392. type = TextUtility.Merge("varchar(", Math.Min(8000, length).ToString(), ")");
  393. break;
  394. case ColumnType.VarChar191:
  395. type = TextUtility.Merge("varchar(191)");
  396. break;
  397. case ColumnType.VarCharMax:
  398. type = TextUtility.Merge("varchar(max)");
  399. break;
  400. case ColumnType.Text:
  401. type = TextUtility.Merge("text");
  402. break;
  403. case ColumnType.NVarChar:
  404. type = TextUtility.Merge("nvarchar(", Math.Min(4000, length).ToString(), ")");
  405. break;
  406. case ColumnType.NVarChar191:
  407. type = TextUtility.Merge("nvarchar(191)");
  408. break;
  409. case ColumnType.NVarCharMax:
  410. type = TextUtility.Merge("nvarchar(max)");
  411. break;
  412. case ColumnType.NText:
  413. type = TextUtility.Merge("ntext");
  414. break;
  415. default:
  416. return TextUtility.Empty;
  417. }
  418. return TextUtility.Merge("[", vcolumn.Field, "] ", type);
  419. }
  420. static string XType(int xtype)
  421. {
  422. switch (xtype)
  423. {
  424. case 34: return "image";
  425. case 35: return "text";
  426. case 36: return "uniqueidentifier";
  427. case 48: return "tinyint";
  428. case 52: return "smallint";
  429. case 56: return "int";
  430. case 58: return "smalldatetime";
  431. case 59: return "real";
  432. case 60: return "money";
  433. case 61: return "datetime";
  434. case 62: return "float";
  435. case 98: return "sql_variant";
  436. case 99: return "ntext";
  437. case 104: return "bit";
  438. case 106: return "decimal";
  439. case 108: return "numeric";
  440. case 122: return "smallmoney";
  441. case 127: return "bigint";
  442. case 165: return "varbinary";
  443. case 167: return "varchar";
  444. case 173: return "binary";
  445. case 175: return "char";
  446. case 189: return "timestamp";
  447. case 231: return "nvarchar";
  448. case 239: return "nchar";
  449. case 241: return "xml";
  450. }
  451. return null;
  452. }
  453. #endregion
  454. }
  455. }
  456. #endif