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.

567 lines
21 KiB

4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
  1. using Apewer.Internals;
  2. #if NETFRAMEWORK
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.IO;
  8. using System.Text;
  9. using static Apewer.Source.SourceUtility;
  10. #endif
  11. namespace Apewer.Source
  12. {
  13. /// <summary>连接 Access 数据库的客户端。</summary>
  14. public abstract partial class Access
  15. {
  16. /// <summary>尝试解析 Access 文件的密码。</summary>
  17. /// <remarks>当操作系统启用随机化内存分配时,此方法可能会产生异常。</remarks>
  18. public static string[] ParsePassword(string path) => AccessHelper.GetPassword(path);
  19. /// <summary>数据库文件路径。</summary>
  20. public virtual string Path { get; protected set; }
  21. }
  22. #if NETFRAMEWORK
  23. public abstract partial class Access : DbClient
  24. {
  25. #region 连接
  26. OleDbConnection _conn = null;
  27. string _connstr = null;
  28. /// <summary>构造函数。</summary>
  29. internal Access(string connectrionString, Timeout timeout = null) : base(timeout)
  30. {
  31. _connstr = connectrionString;
  32. }
  33. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  34. /// <exception cref="ArgumentNullException"></exception>
  35. /// <exception cref="ArgumentException"></exception>
  36. public Access(IDbConnection connection, Timeout timeout = null) : base(timeout)
  37. {
  38. if (connection == null) throw new ArgumentNullException(nameof(connection), "指定的连接无效。");
  39. var conn = connection as OleDbConnection;
  40. if (conn == null) throw new ArgumentException(nameof(connection), "指定的连接不是支持的类型。");
  41. _conn = conn;
  42. _connstr = conn.ConnectionString;
  43. }
  44. #endregion
  45. #region public
  46. /// <summary></summary>
  47. public override string[] ColumnNames(string tableName)
  48. {
  49. if (_conn == null) _conn = new OleDbConnection(_connstr);
  50. // OleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  51. var table = _conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, tableName.ToString(), null });
  52. using (var query = new Query(table))
  53. {
  54. var names = new ArrayBuilder<string>();
  55. for (var i = 0; i < query.Rows; i++)
  56. {
  57. names.Add(query.Text(i, "COLUMN_NAME"));
  58. }
  59. return names.Export();
  60. }
  61. }
  62. /// <summary></summary>
  63. public override string[] StoreNames() => throw new InvalidOperationException();
  64. /// <summary></summary>
  65. public override string[] TableNames()
  66. {
  67. // QueryStrings("select name from msysobjects where type=1 and flags = 0");
  68. Connect();
  69. var schema = _conn.GetSchema("Tables");
  70. using (var query = new Query(schema))
  71. {
  72. var json = query.ToJson();
  73. var names = new List<string>();
  74. for (var i = 0; i < query.Rows; i++)
  75. {
  76. var type = query.Text(i, "TABLE_TYPE");
  77. var name = query.Text(i, "TABLE_NAME");
  78. switch (type)
  79. {
  80. case "ACCESS TABLE":
  81. case "SYSTEM TABLE":
  82. break;
  83. case "TABLE":
  84. names.Add(name);
  85. break;
  86. }
  87. }
  88. names.Sort();
  89. return names.ToArray();
  90. }
  91. }
  92. /// <summary></summary>
  93. public override string Insert(object record, string table = null, bool adjust = true)
  94. {
  95. if (record == null) return "参数无效。";
  96. if (adjust) FixProperties(record);
  97. var structure = TableStructure.Parse(record.GetType());
  98. if (structure == null) return "无法解析记录模型。";
  99. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  100. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  101. // 排除字段。
  102. var excluded = new List<string>();
  103. foreach (var ca in structure.Columns)
  104. {
  105. // 排除不使用 ORM 的属性。
  106. if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
  107. }
  108. var ps = structure.CreateParameters(record, Parameter, excluded);
  109. var psc = ps.Length;
  110. if (psc < 1) return "数据模型不包含字段。";
  111. var names = new List<string>(psc);
  112. var values = new List<string>(psc);
  113. foreach (var column in ps)
  114. {
  115. //names.Add(TextGenerator.Merge("[", column, "]"));
  116. names.Add(TextUtility.Merge(column));
  117. values.Add("@" + column);
  118. }
  119. var sb = new StringBuilder();
  120. sb.Append("insert into ", table, "(", string.Join(", ", names.ToArray()), ") ");
  121. sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
  122. var sql = sb.ToString();
  123. var execute = Execute(sql, ps, false);
  124. if (execute.Success) return TextUtility.Empty;
  125. return execute.Message;
  126. }
  127. /// <summary></summary>
  128. public override string Update(IRecord record, string table = null, bool adjust = true)
  129. {
  130. if (record == null) return "参数无效。";
  131. if (adjust)
  132. {
  133. FixProperties(record);
  134. SetUpdated(record);
  135. }
  136. var structure = TableStructure.Parse(record.GetType());
  137. if (structure == null) return "无法解析记录模型。";
  138. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  139. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  140. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  141. // 排除字段。
  142. var excluded = new List<string>();
  143. if (structure.Key != null) excluded.Add(structure.Key.Field);
  144. foreach (var ca in structure.Columns)
  145. {
  146. // 排除不使用 ORM 的属性、自增属性和主键属性。
  147. if (ca.Independent || ca.Incremental || ca.PrimaryKey || ca.NoUpdate) excluded.Add(ca.Field);
  148. }
  149. var ps = structure.CreateParameters(record, Parameter, excluded);
  150. var psc = ps.Length;
  151. if (psc < 1) return "数据模型不包含字段。";
  152. var items = new List<string>();
  153. foreach (var p in ps)
  154. {
  155. var pn = p.ParameterName;
  156. items.Add(TextUtility.Merge("[", pn, "] = @", pn));
  157. }
  158. var key = record.Key.SafeKey();
  159. var sql = $"update {table} set {string.Join(", ", items.ToArray())} where [{structure.Key.Field}]='{key}'";
  160. var execute = Execute(sql, ps, false);
  161. if (execute.Success) return TextUtility.Empty;
  162. return execute.Message;
  163. }
  164. #endregion
  165. #region protected
  166. /// <summary></summary>
  167. public override string ConnectionString { get => _connstr; }
  168. /// <summary></summary>
  169. protected override IDbConnection GetConnection()
  170. {
  171. if (_conn == null) _conn = new OleDbConnection(_connstr);
  172. return _conn;
  173. }
  174. /// <summary></summary>
  175. protected override IDataAdapter CreateDataAdapter(IDbCommand command) => new OleDbDataAdapter((OleDbCommand)command);
  176. /// <summary></summary>
  177. protected override IDataParameter CreateParameter() => new OleDbParameter();
  178. /// <summary></summary>
  179. protected override string Initialize(TableStructure structure, string table)
  180. {
  181. var model = structure.Model;
  182. if (table.IsEmpty()) table = structure.TableName;
  183. // 检查现存表。
  184. var exists = false;
  185. var tables = TableNames();
  186. if (tables.Length > 0)
  187. {
  188. var lower = table.ToLower();
  189. foreach (var tn in tables)
  190. {
  191. if (TextUtility.IsEmpty(tn)) continue;
  192. if (tn.ToLower() == lower)
  193. {
  194. exists = true;
  195. break;
  196. }
  197. }
  198. }
  199. if (exists)
  200. {
  201. // 获取已存在的列名。
  202. var columns = ColumnNames(table);
  203. if (columns.Length > 0)
  204. {
  205. var lower = new List<string>();
  206. foreach (var column in columns)
  207. {
  208. if (TextUtility.IsBlank(column)) continue;
  209. lower.Add(column.ToLower());
  210. }
  211. columns = lower.ToArray();
  212. }
  213. // 增加列。
  214. foreach (var column in structure.Columns)
  215. {
  216. // 检查 Independent 特性。
  217. if (structure.Independent && column.Independent) continue;
  218. // 去重。
  219. var lower = column.Field.ToLower();
  220. if (columns.Contains(lower)) continue;
  221. var type = Declaration(column);
  222. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  223. var sql = TextUtility.Merge("alter table [", table, "] add ", type, "; ");
  224. var execute = Execute(sql, null, false);
  225. if (execute.Success == false) return execute.Message;
  226. }
  227. return TextUtility.Empty;
  228. }
  229. else
  230. {
  231. var sqlcolumns = new List<string>();
  232. foreach (var column in structure.Columns)
  233. {
  234. // 检查 Independent 特性。
  235. if (structure.Independent && column.Independent) continue;
  236. var type = Declaration(column);
  237. if (!column.Independent)
  238. {
  239. if (column.PrimaryKey) type = type + " primary key";
  240. if (column.Incremental) type += " identity";
  241. }
  242. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  243. sqlcolumns.Add(type);
  244. }
  245. if (sqlcolumns.Count < 1) return $"无法对类型 {model.FullName} 创建表:没有定义任何字段。";
  246. var sql = TextUtility.Merge("create table [", table, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  247. var execute = Execute(sql, null, false);
  248. if (execute.Success) return TextUtility.Empty;
  249. return execute.Message;
  250. }
  251. }
  252. /// <summary></summary>
  253. protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
  254. {
  255. if (flagValue == 0) return $"select [{keyField}] from [{tableName}]";
  256. else return $"select [{keyField}] from [{tableName}] where [{flagField}] = {flagValue}";
  257. }
  258. /// <summary></summary>
  259. protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
  260. {
  261. if (flagValue == 0) return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}'";
  262. else return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}' and [{flagField}] = {flagValue}";
  263. }
  264. /// <summary></summary>
  265. protected override string List(string tableName, string flagField, long flagValue)
  266. {
  267. if (flagValue == 0) return $"select * from [{tableName}]";
  268. else return $"select * from [{tableName}] where [{flagField}] = {flagValue}";
  269. }
  270. #endregion
  271. #region Parameter
  272. /// <summary>创建参数。</summary>
  273. /// <exception cref="ArgumentNullException"></exception>
  274. /// <exception cref="InvalidOperationException"></exception>
  275. static OleDbParameter Parameter(Parameter parameter)
  276. {
  277. if (parameter == null) throw new InvalidOperationException("参数无效。");
  278. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  279. }
  280. /// <summary>创建参数。</summary>
  281. public static OleDbParameter Parameter(string name, ColumnType type, int size, object value)
  282. {
  283. var vname = TextUtility.Trim(name);
  284. if (TextUtility.IsBlank(vname)) return null;
  285. var vtype = OleDbType.BigInt;
  286. switch (type)
  287. {
  288. case ColumnType.Boolean:
  289. vtype = OleDbType.Boolean;
  290. break;
  291. case ColumnType.Bytes:
  292. vtype = OleDbType.LongVarBinary;
  293. break;
  294. case ColumnType.Integer:
  295. vtype = OleDbType.Integer;
  296. break;
  297. case ColumnType.Float:
  298. vtype = OleDbType.Double;
  299. break;
  300. case ColumnType.DateTime:
  301. vtype = OleDbType.Date;
  302. break;
  303. case ColumnType.VarChar:
  304. case ColumnType.VarChar191:
  305. case ColumnType.VarCharMax:
  306. vtype = OleDbType.VarChar;
  307. break;
  308. case ColumnType.NVarChar:
  309. case ColumnType.NVarChar191:
  310. case ColumnType.NVarCharMax:
  311. vtype = OleDbType.VarWChar;
  312. break;
  313. case ColumnType.Text:
  314. vtype = OleDbType.LongVarChar;
  315. break;
  316. case ColumnType.NText:
  317. vtype = OleDbType.LongVarWChar;
  318. break;
  319. default:
  320. throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
  321. }
  322. var vsize = size;
  323. switch (type)
  324. {
  325. case ColumnType.VarChar:
  326. vsize = NumberUtility.Restrict(vsize, 0, 8000);
  327. break;
  328. case ColumnType.NVarChar:
  329. vsize = NumberUtility.Restrict(vsize, 0, 4000);
  330. break;
  331. case ColumnType.VarChar191:
  332. case ColumnType.NVarChar191:
  333. vsize = NumberUtility.Restrict(vsize, 0, 191);
  334. break;
  335. default:
  336. vsize = 0;
  337. break;
  338. }
  339. var vvalue = value;
  340. if (vvalue is string && vvalue != null && vsize > 0)
  341. {
  342. vvalue = TextUtility.Left((string)vvalue, vsize);
  343. }
  344. var parameter = new OleDbParameter();
  345. parameter.ParameterName = vname;
  346. parameter.OleDbType = vtype;
  347. parameter.Value = vvalue;
  348. if (vsize > 0) parameter.Size = vsize;
  349. return parameter;
  350. }
  351. /// <summary>创建参数。</summary>
  352. public static IDbDataParameter CreateParameter(String field, DbType type, Int32 size, Object value)
  353. {
  354. var p = new OleDbParameter();
  355. p.ParameterName = field;
  356. p.DbType = type;
  357. p.Size = size;
  358. p.Value = value;
  359. return p;
  360. }
  361. /// <summary>创建参数。</summary>
  362. public static IDbDataParameter CreateParameter(String field, DbType type, Object value)
  363. {
  364. var p = new OleDbParameter();
  365. p.ParameterName = field;
  366. p.DbType = type;
  367. p.Value = value;
  368. return p;
  369. }
  370. #endregion
  371. #region protected
  372. /// <summary>获取或设置连接字符串。</summary>
  373. /// <exception cref="FileNotFoundException"></exception>
  374. internal protected static string GenerateCS(string provider, string path, string pass, string jo)
  375. {
  376. if (!File.Exists(path)) throw new FileNotFoundException("文件不存在。", path);
  377. var sb = new StringBuilder();
  378. sb.Append("provider=", provider, "; ");
  379. if (!string.IsNullOrEmpty(path)) sb.Append("data source=", path, "; ");
  380. if (string.IsNullOrEmpty(pass)) sb.Append("persist security info=false; ");
  381. else sb.Append("jet oledb:database password=\"", pass, "\"; ");
  382. // Microsoft Access Workgroup Information
  383. if (!string.IsNullOrEmpty(jo)) sb.Append("jet oledb:system database=", jo, "; ");
  384. return sb.ToString();
  385. }
  386. #endregion
  387. #region static
  388. static string Declaration(ColumnAttribute column)
  389. {
  390. var type = TextUtility.Empty;
  391. var vcolumn = column;
  392. var length = Math.Max(0, vcolumn.Length);
  393. switch (vcolumn.Type)
  394. {
  395. case ColumnType.Boolean:
  396. type = "bit";
  397. break;
  398. case ColumnType.Integer:
  399. type = "money";
  400. break;
  401. case ColumnType.Float:
  402. type = "float";
  403. break;
  404. case ColumnType.Bytes:
  405. type = "binary";
  406. break;
  407. case ColumnType.DateTime:
  408. type = "datetime";
  409. break;
  410. case ColumnType.VarChar:
  411. type = TextUtility.Merge("varchar(", Math.Min(8000, length).ToString(), ")");
  412. break;
  413. case ColumnType.VarChar191:
  414. type = TextUtility.Merge("varchar(191)");
  415. break;
  416. case ColumnType.VarCharMax:
  417. type = TextUtility.Merge("varchar(max)");
  418. break;
  419. case ColumnType.Text:
  420. type = TextUtility.Merge("text");
  421. break;
  422. case ColumnType.NVarChar:
  423. type = TextUtility.Merge("nvarchar(", Math.Min(4000, length).ToString(), ")");
  424. break;
  425. case ColumnType.NVarChar191:
  426. type = TextUtility.Merge("nvarchar(191)");
  427. break;
  428. case ColumnType.NVarCharMax:
  429. type = TextUtility.Merge("nvarchar(max)");
  430. break;
  431. case ColumnType.NText:
  432. type = TextUtility.Merge("ntext");
  433. break;
  434. default:
  435. return TextUtility.Empty;
  436. }
  437. return TextUtility.Merge("[", vcolumn.Field, "] ", type);
  438. }
  439. #endregion
  440. }
  441. /// <summary>使用 Microsoft.Jet.OLEDB.4.0 访问 Access 97 - 2003 数据库文件。</summary>
  442. public class AccessJet4 : Access
  443. {
  444. const string JetOleDB4 = "microsoft.jet.oledb.4.0";
  445. /// <summary>创建 Access 类的新实例。</summary>
  446. /// <exception cref="FileNotFoundException"></exception>
  447. public AccessJet4(string path, string pass = null, string jo = null, Timeout timeout = null)
  448. : base(GenerateCS(JetOleDB4, path, pass, jo), timeout)
  449. {
  450. Path = path;
  451. }
  452. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  453. /// <exception cref="ArgumentNullException"></exception>
  454. /// <exception cref="ArgumentException"></exception>
  455. public AccessJet4(IDbConnection connection, Timeout timeout = null) : base(connection, timeout)
  456. {
  457. Path = (connection as Access)?.Path;
  458. }
  459. }
  460. /// <summary>使用 Microsoft.ACE.OLEDB.12.0 访问 Access 2007 数据库文件。</summary>
  461. public class AccessAce12 : Access
  462. {
  463. const string AceOleDB12 = "microsoft.ace.oledb.12.0";
  464. /// <summary>创建 Access 类的新实例。</summary>
  465. /// <exception cref="FileNotFoundException"></exception>
  466. public AccessAce12(string path, string pass = null, string jo = null, Timeout timeout = null)
  467. : base(GenerateCS(AceOleDB12, path, pass, jo), timeout)
  468. {
  469. Path = path;
  470. }
  471. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  472. /// <exception cref="ArgumentNullException"></exception>
  473. /// <exception cref="ArgumentException"></exception>
  474. public AccessAce12(IDbConnection connection, Timeout timeout = null) : base(connection, timeout)
  475. {
  476. Path = (connection as Access)?.Path;
  477. }
  478. }
  479. #endif
  480. }