forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDalCreate.cs
315 lines (294 loc) · 12.2 KB
/
DalCreate.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
using System;
using System.Collections.Generic;
using System.Text;
using CYQ.Data.Tool;
namespace CYQ.Data
{
/// <summary>
/// 数据库类型操作类
/// </summary>
internal class DalCreate
{
private const string SqlClient = "System.Data.SqlClient";
private const string OleDb = "System.Data.OleDb";
private const string OracleClient = "System.Data.OracleClient";
private const string SQLiteClient = "System.Data.SQLite";
private const string MySqlClient = "MySql.Data.MySqlClient";
private const string SybaseClient = "Sybase.Data.AseClient";
private const string TxtClient = "CYQ.Data.TxtClient";
private const string XmlClient = "CYQ.Data.XmlClient";
private const string XHtmlClient = "CYQ.Data.XHtmlClient";
public static DbBase CreateDal(string dbConn)
{
return GetDbBaseBy(GetConnObject(dbConn));
//ConnEntity cEntity = GetConnString(dbConn);
//DbBase db = GetDbBaseBy(cEntity.Conn, cEntity.ProviderName);
//db.connObject = cEntity;
//return db;
}
public static DalType GetDalTypeByConn(string conn)
{
return GetConnBean(conn).ConnDalType;
}
public static DalType GetDalTypeByReaderName(string typeName)
{
switch (typeName.Replace("DataReader", "").ToLower())
{
case "oracle":
return DalType.Oracle;
case "sql":
return DalType.MsSql;
case "sqlite":
return DalType.SQLite;
case "oledb":
return DalType.Access;
case "mysql":
return DalType.MySql;
case "odbc":
case "ase":
return DalType.Sybase;
default:
return DalType.None;
}
}
public static string GetProvider(string connString)
{
connString = connString.ToLower().Replace(" ", "");//去掉空格
if (connString.Contains("microsoft.jet.oledb.4.0") || connString.Contains("microsoft.ace.oledb") || connString.Contains(".mdb"))
{
return OleDb;
}
else if (connString.Contains("provider=msdaora") || connString.Contains("provider=oraoledb.oracle")
|| connString.Contains("description=") || connString.Contains("fororacle"))
{
return OracleClient;
}
else if (connString.Contains("failifmissing=") || (connString.StartsWith("datasource=") && connString.EndsWith(".db")))
{
return SQLiteClient;
}
else if (connString.Contains("convert zero datetime") || (connString.Contains("host=") && connString.Contains("port=") && connString.Contains("database=")))
{
return MySqlClient;
}
else if (connString.Contains("provider=ase") || (connString.Contains("datasource=") && connString.Contains("port=") && connString.Contains("database=")))
{
return SybaseClient;
}
else if (connString.Contains("txtpath="))
{
return TxtClient;
}
else if (connString.Contains("xmlpath="))
{
return XmlClient;
}
else
{
return SqlClient;
}
}
public static DalType GetDalType(string providerName)
{
switch (providerName)
{
case SqlClient:
return DalType.MsSql;
case OleDb:
return DalType.Access;
case OracleClient:
return DalType.Oracle;
case SQLiteClient:
return DalType.SQLite;
case MySqlClient:
return DalType.MySql;
case SybaseClient:
return DalType.Sybase;
case TxtClient:
return DalType.Txt;
case XmlClient:
return DalType.Xml;
}
return (DalType)Error.Throw(string.Format("GetDalType:{0} No Be Support Now!", providerName));
}
private static DbBase GetDbBaseBy(ConnObject co)
{
string providerName = co.Master.ProviderName;
//License.Check(providerName);//框架模块授权检测。
switch (providerName)
{
case SqlClient:
return new MsSqlDal(co);
case OleDb:
return new OleDbDal(co);
case OracleClient:
return new OracleDal(co);
case SQLiteClient:
return new SQLiteDal(co);
case MySqlClient:
return new MySQLDal(co);
case SybaseClient:
return new SybaseDal(co);
case TxtClient:
case XmlClient:
return new NoSqlDal(co);
}
return (DbBase)Error.Throw(string.Format("GetHelper:{0} No Be Support Now!", providerName));
}
#region 注释的代码
/*
private static MDictionary<string, ConnEntity> connCache = new MDictionary<string, ConnEntity>();
internal static ConnEntity GetConnString(string dbConn) // 思考备份链接的问题。=》下一步思考读写分离的问题。
{
if (connCache.ContainsKey(dbConn))
{
return connCache[dbConn];
}
ConnEntity cEntity = new ConnEntity();
cEntity.Conn = string.IsNullOrEmpty(dbConn) ? AppConfig.DB.DefaultConn : dbConn;
if (cEntity.Conn.Length < 32 && cEntity.Conn.Split(' ').Length == 1)//配置项
{
if (ConfigurationManager.ConnectionStrings[cEntity.Conn] == null && cEntity.Conn != AppConfig.DB.DefaultConn)
{
cEntity.Conn = AppConfig.DB.DefaultConn;//转取默认配置;
if (cEntity.Conn.Length >= 32 || cEntity.Conn.Trim().Contains(" "))
{
goto er;
}
}
if (ConfigurationManager.ConnectionStrings[cEntity.Conn] != null)
{
string p = string.Empty, c = string.Empty;
#region 获取备份链接
string bakKey = cEntity.Conn + "_Bak";
if (ConfigurationManager.ConnectionStrings[cEntity.Conn + "_Bak"] == null && cEntity.Conn == AppConfig.DB.DefaultConn && AppConfig.DB.DefaultConnBak != cEntity.Conn + "_Bak")
{
bakKey = AppConfig.DB.DefaultConnBak;
}
string connBak = AppConfig.GetConn(bakKey, out p);
if (connBak.Length >= 32 || connBak.Trim().Contains(" "))//如果有完整的数据库链接
{
cEntity.ConnBak = AppConfig.GetConn(bakKey, out p);
cEntity.ProviderNameBak = p;
}
#endregion
cEntity.Conn = AppConfig.GetConn(cEntity.Conn, out p);
cEntity.ProviderName = p;
}
else
{
Error.Throw(string.Format("Can't find the connection key '{0}' from web.config!", cEntity.Conn));
}
}
else if (cEntity.Conn == AppConfig.DB.DefaultConn && string.IsNullOrEmpty(cEntity.ConnBak))
{
string connBak = AppConfig.GetConn(AppConfig.DB.DefaultConnBak);//先赋默认备份值。
if (connBak.Length >= 32 || connBak.Trim().Contains(" "))//如果有完整的数据库链接
{
cEntity.ConnBak = connBak;
}
}
er:
if (string.IsNullOrEmpty(cEntity.ProviderName))
{
cEntity.ProviderName = GetProvider(cEntity.Conn);
cEntity.ConnDalType = GetDalType(cEntity.ProviderName);
}
if (string.IsNullOrEmpty(cEntity.ProviderNameBak) && !string.IsNullOrEmpty(cEntity.ConnBak))
{
cEntity.ProviderNameBak = DalCreate.GetProvider(cEntity.ConnBak);
cEntity.ConnBakDalType = GetDalType(cEntity.ProviderNameBak);
}
cEntity.Conn = string.Format(cEntity.Conn, AppConfig.WebRootPath);
cEntity.ConnBak = string.Format(cEntity.ConnBak ?? string.Empty, AppConfig.WebRootPath);
if (!connCache.ContainsKey(dbConn))
{
connCache.Set(dbConn, cEntity);
}
return cEntity;
}
*/
#endregion
internal static string FormatConn(DalType dal, string connString)
{
if (dal != DalType.Access)
{
string conn = connString.ToLower();
int index = conn.IndexOf("provider");
if (index > -1 && index < connString.Length - 5 && (connString[index + 8] == '=' || connString[index + 9] == '='))
{
int end = conn.IndexOf(';', index);
if (end > index)
{
connString = connString.Remove(index, end - index + 1);
}
}
}
return connString;
}
private static MDictionary<string, ConnObject> connDicCache = new MDictionary<string, ConnObject>(StringComparer.OrdinalIgnoreCase);
internal static ConnObject GetConnObject(string dbConn)
{
dbConn = string.IsNullOrEmpty(dbConn) ? AppConfig.DB.DefaultConn : dbConn;
if (connDicCache.ContainsKey(dbConn))
{
return connDicCache[dbConn];
}
ConnBean cbMaster = GetConnBean(dbConn);
if (cbMaster == null)
{
if (dbConn == AppConfig.DB.DefaultConn)
{
Error.Throw(string.Format("Can't find the connection key '{0}' from web.config or app.config!", dbConn));
}
else
{
ConnBean cb = GetConnBean(AppConfig.DB.DefaultConn);
cbMaster = cb.Clone();//获取默认的值。
}
}
ConnObject co = new ConnObject();
co.Master = cbMaster;
if (dbConn != null && dbConn.Length < 32 && !dbConn.Trim().Contains(" ")) // 为configKey
{
ConnBean coBak = GetConnBean(dbConn + "_Bak");
if (coBak != null && coBak.ProviderName == cbMaster.ProviderName)
{
co.BackUp = coBak;
}
for (int i = 1; i < 1000; i++)
{
ConnBean cbSlave = GetConnBean(dbConn + "_Slave" + i);
if (cbSlave == null)
{
break;
}
co.Slave.Add(cbSlave);
}
}
if (!connDicCache.ContainsKey(dbConn))
{
connDicCache.Set(dbConn, co);
}
return co;
}
private static ConnBean GetConnBean(string dbConn)
{
string provider;
string conn = string.Format(AppConfig.GetConn(dbConn, out provider), AppConfig.WebRootPath);
if (string.IsNullOrEmpty(conn))
{
return null;
}
ConnBean cb = new ConnBean();
cb.Conn = conn;
if (string.IsNullOrEmpty(provider))
{
provider = GetProvider(cb.Conn);
}
cb.ProviderName = provider;
cb.ConnDalType = GetDalType(provider);
return cb;
}
}
}