forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBTool.cs
744 lines (705 loc) · 28.6 KB
/
DBTool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
using System;
using CYQ.Data.Table;
using System.Data;
using CYQ.Data.SQL;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace CYQ.Data.Tool
{
/// <summary>
/// 数据库工具类[都是静态方法]
/// </summary>
public static class DBTool
{
private static StringBuilder _ErrorMsg = new StringBuilder();
/// <summary>
/// 获取异常的信息
/// </summary>
public static string ErrorMsg
{
get
{
return _ErrorMsg.ToString();
}
set
{
_ErrorMsg.Length = 0;
if (value != null)
{
_ErrorMsg.Append(value);
}
}
}
#region 库层面操作
/// <summary>
/// 获取数据库链接的数据库类型
/// </summary>
/// <param name="conn">链接配置Key或数据库链接语句</param>
/// <returns></returns>
public static DalType GetDalType(string conn)
{
return DalCreate.GetDalTypeByConn(conn);
}
/// <summary>
/// 获取指定数据库的数据类型
/// </summary>
/// <param name="ms">单元格结构</param>
/// <param name="dalType">数据库类型</param>
/// <param name="version">数据库版本号</param>
/// <returns></returns>
public static string GetDataType(MCellStruct ms, DalType dalType, string version)
{
return DataType.GetDataType(ms, dalType, version);
}
/// <summary>
/// 测试数据库链接语句
/// </summary>
/// <param name="conn">链接配置Key或数据库链接语句</param>
/// <returns></returns>
public static bool TestConn(string conn)
{
string msg;
return TestConn(conn, out msg);
}
public static bool TestConn(string conn, out string msg)
{
bool result = false;
try
{
DbBase helper = DalCreate.CreateDal(conn);
result = helper.TestConn();
if (result)
{
msg = helper.Version;
}
else
{
msg = helper.debugInfo.ToString();
}
helper.Dispose();
}
catch (Exception err)
{
msg = err.Message;
}
return result;
}
#endregion
#region 表的相关操作
#region 获取所有表
/// <summary>
/// 获取数据库表
/// </summary>
public static Dictionary<string, string> GetTables(string conn)
{
string dbName;
return GetTables(conn, out dbName);
}
/// <summary>
/// 获取数据库表
/// </summary>
public static Dictionary<string, string> GetTables(string conn, out string dbName)
{
string errInfo;
return GetTables(conn, out dbName, out errInfo);
}
/// <summary>
/// 获取所有表(表名+表说明)【链接错误时,抛异常】
/// </summary>
/// <param name="conn">数据库链接</param>
/// <param name="dbName">返回指定链接的数据库名称</param>
/// <param name="errInfo">链接错误时的信息信息</param>
public static Dictionary<string, string> GetTables(string conn, out string dbName, out string errInfo)
{
errInfo = string.Empty;
DbBase helper = DalCreate.CreateDal(conn);
helper.IsAllowRecordSql = false;
dbName = helper.DataBase;
if (!helper.TestConn())
{
errInfo = helper.debugInfo.ToString();
if (string.IsNullOrEmpty(errInfo))
{
errInfo = "Open database fail : " + dbName;
}
helper.Dispose();
return null;
}
Dictionary<string, string> tables = TableSchema.GetTables(ref helper);//内部有缓存
helper.Dispose();
return tables;
}
#endregion
#region 表是否存在
/// <summary>
/// 是否存在表
/// </summary>
public static bool ExistsTable(string tableName)
{
return ExistsTable(tableName, AppConfig.DB.DefaultConn);
}
/// <summary>
/// 是否存在指定的表
/// </summary>
/// <param name="tableName">表名[或文件名]</param>
/// <param name="conn">数据库链接</param>
public static bool ExistsTable(string tableName, string conn)
{
DalType dal;
return ExistsTable(tableName, conn, out dal);
}
/// <summary>
/// 检测表是否存在
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="conn">数据库链接</param>
/// <param name="dalType">数据库类型</param>
public static bool ExistsTable(string tableName, string conn, out DalType dalType)
{
string database;
return ExistsTable(tableName, conn, out dalType, out database);
}
/// <summary>
/// 检测表是否存在
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="conn">数据库链接</param>
/// <param name="dalType">数据库类型</param>
public static bool ExistsTable(string tableName, string conn, out DalType dalType, out string database)
{
dalType = DalType.None;
database = string.Empty;
if (string.IsNullOrEmpty(tableName) || tableName.Contains("(") && tableName.Contains(")"))
{
return false;
}
DbBase helper = DalCreate.CreateDal(conn);
dalType = helper.dalType;
database = helper.DataBase;
bool result = TableSchema.Exists("U", tableName, ref helper);
helper.Dispose();
return result;
}
#endregion
#region 创建表语句
/// <summary>
/// 为指定的表架构生成SQL(Create Table)语句
/// </summary>
public static bool CreateTable(string tableName, MDataColumn columns)
{
return CreateTable(tableName, columns, AppConfig.DB.DefaultConn);
}
/// <summary>
/// 为指定的表架构生成SQL(Create Table)语句
/// </summary>
public static bool CreateTable(string tableName, MDataColumn columns, string conn)
{
if (string.IsNullOrEmpty(tableName) || tableName.Contains("(") && tableName.Contains(")"))
{
return false;
}
bool result = false;
DalType dalType = GetDalType(conn);
string dataBase = string.Empty;
switch (dalType)
{
case DalType.Txt:
case DalType.Xml:
// string a, b, c;
conn = AppConfig.GetConn(conn);// CYQ.Data.DAL.DalCreate.GetConnString(conn, out a, out b, out c);
if (conn.ToLower().Contains(";ts=0"))//不写入表架构。
{
return true;
}
else
{
tableName = Path.GetFileNameWithoutExtension(tableName);
string fileName = NoSqlConnection.GetFilePath(conn) + tableName + ".ts";
result = columns.WriteSchema(fileName);
dataBase = new NoSqlConnection(conn).Database;
}
break;
default:
using (MProc proc = new MProc(null, conn))
{
dataBase = proc.DataBase;
try
{
proc.dalHelper.IsAllowRecordSql = false;
proc.SetAopState(Aop.AopOp.CloseAll);
proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DalType, proc.DalVersion));//.Replace("\n", string.Empty)
result = proc.ExeNonQuery() > -2;
//获取扩展说明
string descriptionSql = GetCreateTableDescriptionSql(tableName, columns, proc.DalType).Replace("\r\n", " ").Trim(' ', ';');
if (!string.IsNullOrEmpty(descriptionSql))
{
if (proc.DalType == DalType.Oracle)
{
foreach (string sql in descriptionSql.Split(';'))
{
proc.ResetProc(sql);
if (proc.ExeNonQuery() == -2)
{
break;
}
}
}
else
{
proc.ResetProc(descriptionSql);
proc.ExeNonQuery();
}
}
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
finally
{
if (proc.RecordsAffected == -2)
{
_ErrorMsg.AppendLine("CreateTable:" + proc.DebugInfo);
}
}
}
break;
}
if (result)
{
//处理表缓存
string key = TableSchema.GetTableCacheKey(dalType, dataBase, conn);
if (TableSchema.tableCache.ContainsKey(key))
{
Dictionary<string, string> tableDic = TableSchema.tableCache[key];
if (!tableDic.ContainsKey(tableName))
{
tableDic.Add(tableName, "");
}
}
}
return result;
}
/// <summary>
/// 获取指定的表架构生成的SQL(Create Table)的说明语句
/// </summary>
public static string GetCreateTableDescriptionSql(string tableName, MDataColumn columns, DalType dalType)
{
return SqlCreateForSchema.CreateTableDescriptionSql(tableName, columns, dalType);
}
/// <summary>
/// 获取指定的表架构生成的SQL(Create Table)的说明语句
/// </summary>
public static string GetCreateTableSql(string tableName, MDataColumn columns, DalType dalType, string version)
{
return SqlCreateForSchema.CreateTableSql(tableName, columns, dalType, version);
}
internal static void CheckAndCreateOracleSequence(string seqName, string conn, string primaryKey, string tableName)
{
seqName = seqName.ToUpper();
using (DbBase db = DalCreate.CreateDal(conn))
{
object o = db.ExeScalar(string.Format(TableSchema.ExistOracleSequence, seqName), false);
if (db.recordsAffected != -2 && (o == null || Convert.ToString(o) == "0"))
{
int startWith = 1;
if (!string.IsNullOrEmpty(primaryKey))
{
o = db.ExeScalar(string.Format(TableSchema.GetOracleMaxID, primaryKey, tableName), false);
if (db.recordsAffected != -2)
{
if (!int.TryParse(Convert.ToString(o), out startWith) || startWith < 1)
{
startWith = 1;
}
else
{
startWith++;
}
}
}
db.ExeNonQuery(string.Format(TableSchema.CreateOracleSequence, seqName, startWith), false);
}
if (db.recordsAffected == -2)
{
_ErrorMsg.AppendLine("CheckAndCreateOracleSequence:" + db.debugInfo.ToString());
}
}
}
#endregion
#region 修改表语句
/// <summary>
/// 获取指定的表架构生成的SQL(Alter Table)的说明语句
/// </summary>
public static string GetAlterTableSql(string tableName, MDataColumn columns)
{
return GetAlterTableSql(tableName, columns, AppConfig.DB.DefaultConn);
}
/// <summary>
/// 获取指定的表架构生成的SQL(Alter Table)的说明语句
/// </summary>
public static string GetAlterTableSql(string tableName, MDataColumn columns, string conn)
{
List<string> sqlItems = SqlCreateForSchema.AlterTableSql(tableName, columns, conn);
if (sqlItems.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (string sql in sqlItems)
{
sb.AppendLine(sql);
}
sqlItems = null;
return sb.ToString();
}
return string.Empty;
}
/// <summary>
/// 修改表的列结构
/// </summary>
public static bool AlterTable(string tableName, MDataColumn columns)
{
return AlterTable(tableName, columns, AppConfig.DB.DefaultConn);
}
/// <summary>
/// 修改表的列结构
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="columns">列结构</param>
/// <param name="conn">数据库链接</param>
/// <returns></returns>
public static bool AlterTable(string tableName, MDataColumn columns, string conn)
{
if (columns == null) { return false; }
List<string> sqls = SqlCreateForSchema.AlterTableSql(tableName, columns, conn);
if (sqls.Count > 0)
{
DalType dalType = DalType.None;
string database = string.Empty;
using (MProc proc = new MProc(null, conn))
{
dalType = proc.DalType;
database = proc.dalHelper.DataBase;
proc.SetAopState(Aop.AopOp.CloseAll);
if (proc.DalType == DalType.MsSql)
{
proc.BeginTransation();//仅对mssql有效。
}
foreach (string sql in sqls)
{
proc.ResetProc(sql);
if (proc.ExeNonQuery() == -2)
{
proc.RollBack();
_ErrorMsg.AppendLine("AlterTable:" + proc.DebugInfo);
Log.WriteLogToTxt(proc.DebugInfo);
return false;
}
}
proc.EndTransation();
}
RemoveCache(tableName, database, dalType);
return true;
}
return false;
}
#endregion
#region 删除表语句
/// <summary>
/// 移除一张表
/// </summary>
/// <returns></returns>
public static bool DropTable(string tableName)
{
return DropTable(tableName, AppConfig.DB.DefaultConn);
}
/// <summary>
/// 移除一张表
/// <param name="conn">数据库链接</param>
/// </summary>
public static bool DropTable(string tableName, string conn)
{
bool result = false;
string key = string.Empty;
using (DbBase helper = DalCreate.CreateDal(conn))
{
key = TableSchema.GetTableCacheKey(helper);
DalType dalType = helper.dalType;
switch (dalType)
{
case DalType.Txt:
case DalType.Xml:
string folder = helper.Con.DataSource + Path.GetFileNameWithoutExtension(tableName);
string path = folder + ".ts";
try
{
if (File.Exists(path))
{
result = IOHelper.Delete(path);
}
path = folder + (dalType == DalType.Txt ? ".txt" : ".xml");
if (File.Exists(path))
{
result = IOHelper.Delete(path);
}
}
catch
{
}
break;
default:
result = helper.ExeNonQuery("drop table " + Keyword(tableName, dalType), false) != -2;
if (result)
{
//处理表相关的元数据和数据缓存。
RemoveCache(tableName, helper.DataBase, dalType);
}
break;
}
if (helper.recordsAffected == -2)
{
_ErrorMsg.AppendLine(helper.debugInfo.ToString());
}
}
if (result)
{
//处理数据库表字典缓存
if (TableSchema.tableCache.ContainsKey(key))
{
Dictionary<string, string> tableDic = TableSchema.tableCache[key];
if (tableDic.ContainsKey(tableName))
{
tableDic.Remove(tableName);
}
}
}
return result;
}
#endregion
#endregion
#region 获取结构
/// <summary>
/// 获取表架构
/// </summary>
/// <param name="conn">数据库链接</param>
/// <param name="connectionName">指定要返回架构的名称</param>
/// <param name="restrictionValues">为指定的架构返回一组限制值</param>
public static DataTable GetSchema(string conn, string connectionName, string[] restrictionValues)
{
DbBase helper = DalCreate.CreateDal(conn);
if (!helper.TestConn())
{
return null;
}
helper.Con.Open();
DataTable dt = helper.Con.GetSchema(connectionName, restrictionValues);
helper.Con.Close();
helper.Dispose();
return dt;
}
/// <summary>
/// 获取表列架构
/// </summary>
public static MDataColumn GetColumns(Type typeInfo)
{
return TableSchema.GetColumns(typeInfo);
}
/// <summary>
/// 获取表列架构
/// </summary>
/// <param name="tableName">表名</param>
public static MDataColumn GetColumns(object tableName)
{
string dbName = StaticTool.GetDbName(ref tableName);
string conn = string.Empty;
string tName = tableName.ToString();
if (string.IsNullOrEmpty(dbName))
{
conn = AppConfig.DB.DefaultConn;
}
else
{
conn = dbName + "Conn";
if (tName.IndexOf('(') == -1)
{
tName = dbName + "." + tName;//单表
}
}
return GetColumns(tName, conn);
}
/// <summary>
/// 获取表列架构(链接错误时,抛异常)
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="conn">数据库链接</param>
/// <param name="errInfo">出错时的错误信息</param>
/// <returns></returns>
public static MDataColumn GetColumns(string tableName, string conn, out string errInfo)
{
errInfo = string.Empty;
DbBase helper = DalCreate.CreateDal(conn);
helper.ChangeDatabaseWithCheck(tableName);//检测dbname.dbo.tablename的情况
if (!helper.TestConn())
{
errInfo = helper.debugInfo.ToString();
if (string.IsNullOrEmpty(errInfo))
{
errInfo = "Open database fail : " + tableName;
}
helper.Dispose();
Error.Throw(errInfo);
return null;
}
if (!tableName.Contains(" "))
{
tableName = GetMapTableName(conn, tableName);
}
MDataColumn mdc = TableSchema.GetColumns(tableName, ref helper);
helper.Dispose();
return mdc;
}
/// <summary>
/// 获取表列架构
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="conn">数据库链接</param>
/// <returns></returns>
public static MDataColumn GetColumns(string tableName, string conn)
{
string err;
return GetColumns(tableName, conn, out err);
}
#endregion
#region 其它操作
private static List<string> flag = new List<string>(2);
internal static void CreateSelectBaseProc(DalType dal, string conn)
{
try
{
switch (dal)
{
//case DalType.Oracle:
// if (!flag.Contains("oracle"))
// {
// flag.Add("oracle");
// using (DbBase db = DalCreate.CreateDal(conn))
// {
// db.AllowRecordSql = false;
// object o = db.ExeScalar(string.Format(ExistOracle.Replace("TABLE", "PROCEDURE"), "MyPackage.SelectBase"), false);
// if (o != null && Convert.ToInt32(o) < 1)
// {
// db.ExeNonQuery(SqlPager.GetPackageHeadForOracle(), false);
// db.ExeNonQuery(SqlPager.GetPackageBodyForOracle(), false);
// }
// }
// }
// break;
case DalType.MsSql:
if (!flag.Contains("sql"))
{
flag.Add("sql");//考虑到一个应用不太可能同时使用mssql的不同版本,只使用一个标识。
using (DbBase db = DalCreate.CreateDal(conn))
{
db.IsAllowRecordSql = false;
object o = null;
if (!db.Version.StartsWith("08"))
{
// o = db.ExeScalar(string.Format(Exist2000.Replace("U", "P"), "SelectBase"), false);
// if (o != null && Convert.ToInt32(o) < 1)
// {
// db.ExeNonQuery(SqlPager.GetSelectBaseForSql2000(), false);
// }
//}
//else
//{
o = db.ExeScalar(string.Format(TableSchema.Exist2005, "SelectBase", "P"), false);
if (o != null && Convert.ToInt32(o) < 1)
{
db.ExeNonQuery(SqlCreateForPager.GetSelectBaseForSql2005(), false);
}
}
}
}
break;
}
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
}
/// <summary>
/// 为字段或表名添加关键字标签:如[],''等符号
/// </summary>
/// <param name="name">表名或字段名</param>
/// <param name="dalType">数据类型</param>
/// <returns></returns>
public static string Keyword(string name, DalType dalType)
{
return SqlFormat.Keyword(name, dalType);
}
/// <summary>
/// 取消字段或表名添加关键字标签:如[],''等符号
/// </summary>
/// <param name="name">表名或字段名</param>
public static string NotKeyword(string name)
{
return SqlFormat.NotKeyword(name);
}
/// <summary>
/// 将各数据库默认值格式化成标准值,将标准值还原成各数据库默认值
/// </summary>
/// <param name="flag">[0:转成标准值],[1:转成各数据库值]</param>
/// <returns></returns>
public static string FormatDefaultValue(DalType dalType, object value, int flag, SqlDbType sqlDbType)
{
return SqlFormat.FormatDefaultValue(dalType, value, flag, sqlDbType);
}
#endregion
/// <summary>
/// 映射的表名
/// </summary>
internal static string GetMapTableName(string conn, string tableName)
{
Dictionary<string, string> mapTable = null;
string key = "MapTalbe:" + conn.GetHashCode();
if (Cache.CacheManage.LocalInstance.Contains(key))
{
mapTable = Cache.CacheManage.LocalInstance.Get<Dictionary<string, string>>(key);
}
else
{
Dictionary<string, string> list = GetTables(conn);
if (list != null && list.Count > 0)
{
mapTable = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string mapName = string.Empty;
foreach (string item in list.Keys)
{
mapName = item.Replace("_", "").Replace("-", "").Replace(" ", "");//有奇葩用-符号和空格的。
//去掉原有的限制条件:(item != mapName 只存档带-," ",_等符号的表名 ),支持外部映射
if (!mapTable.ContainsKey(mapName))//
{
mapTable.Add(mapName.ToLower(), item);
}
}
Cache.CacheManage.LocalInstance.Set(key, mapTable, 1440);
}
}
key = tableName.Replace("_", "").Replace("-", "").Replace(" ", "").ToLower();
if (mapTable != null && mapTable.Count > 0 && mapTable.ContainsKey(key))
{
return mapTable[key];
}
return tableName;
}
private static void RemoveCache(string tableName, string database, DalType dalType)
{
//清缓存
string key = Cache.CacheManage.GetKey(Cache.CacheKeyType.Schema, tableName, database, dalType);
Cache.CacheManage.LocalInstance.Remove(key);
key = Cache.CacheManage.GetKey(Cache.CacheKeyType.AutoCache, tableName, database, dalType);
Cache.AutoCache.ReadyForRemove(key);
}
}
}