-
Notifications
You must be signed in to change notification settings - Fork 2
/
OleDbDescriptor.cs
683 lines (648 loc) · 29 KB
/
OleDbDescriptor.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
// Copyright (c) 2004-2010 Azavea, Inc.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Text;
using Azavea.Open.Common;
using Azavea.Open.DAO.Exceptions;
using Azavea.Open.DAO.SQL;
using Azavea.Open.DAO.Util;
namespace Azavea.Open.DAO.OleDb
{
/// <summary>
/// This class represents the info necessary to connect to an OleDb data source.
/// </summary>
public class OleDbDescriptor : AbstractSqlConnectionDescriptor, ITransactionalConnectionDescriptor
{
/// <summary>
/// These are the database types for which support has been implemented via OleDB.
/// </summary>
public enum DatabaseType
{
/// <summary>
/// Oracle (tested on 10g and 11g), uses the Oracle driver not the MS one.
/// This requires you to have installed the Oracle client drivers. You can
/// use the MS drivers (though not recommended) by explicitly passing a provider
/// string to the constructor / config.
/// </summary>
ORACLE,
/// <summary>
/// MS SQL Server (2000, 2005, 2008 tested).
/// </summary>
SQLSERVER,
/// <summary>
/// MS Access (most versions).
/// </summary>
ACCESS
}
/// <summary>
/// The type of database (Oracle, SQL Server, etc).
/// </summary>
public readonly DatabaseType Type;
/// <exclude/>
protected readonly string _connectionStr;
/// <exclude/>
protected readonly string _cleanConnStr;
/// <summary>
/// The "Provider", meaning the value for the provider field in the OleDB connection string.
/// </summary>
public readonly string Provider;
/// <summary>
/// The server name, meaningful for some databases (Oracle, SQL Server) but not others (Access).
/// May be null depending on the database.
/// </summary>
public readonly string Server;
/// <summary>
/// The database name, meaningful for some databases (Access (filename), SQL Server)
/// but not others (Oracle). May be null depending on the database.
/// </summary>
public readonly string Database;
/// <summary>
/// The user name, if necessary to log into the database. May be null.
/// </summary>
public readonly string User;
/// <summary>
/// The password for the User. May be null.
/// </summary>
public readonly string Password;
/// <summary>
/// The connection timeout, in seconds. May be null, meaning use the default.
/// </summary>
public readonly int? ConnectTimeout;
/// <summary>
/// This constructor reads all the appropriate values from a config file.
/// </summary>
/// <param name="config">Config to get params from.</param>
/// <param name="component">Section of the config XML to look in for db params.</param>
/// <param name="decryptionDelegate">Delegate to call to decrypt password fields.
/// May be null if passwords are in plain text.</param>
public OleDbDescriptor(Config config, string component,
ConnectionInfoDecryptionDelegate decryptionDelegate)
: this(GetTypeFromConfig(config, component),
config.GetParameter(component, "Provider", null),
config.GetParameter(component, "Server", null),
config.GetParameterWithSubstitution(component, "Database", true, null),
config.GetParameter(component, "User", null),
GetDecryptedConfigParameter(config, component, "Password", decryptionDelegate),
config.GetParameterAsInt(component, "Connect_Timeout", null)) {}
/// <summary>
/// Constructor that lets you pass everything as parameters rather than requiring a config.
/// </summary>
/// <param name="provider">Database provider string. May not be null.</param>
/// <param name="server">Server (or in the case of access, .mdb file) May be null, but you
/// probably won't be able to connect to anything.</param>
/// <param name="database">database name on that server, if applicable. May be null.</param>
/// <param name="user">Database user name, may be null.</param>
/// <param name="password">Password for the user. May be null.</param>
/// <param name="timeout">Connection timeout, in seconds. May be null.</param>
public OleDbDescriptor(string provider, string server, string database,
string user, string password, int? timeout)
: this(GuessTypeFromProvider(provider), provider, server, database, user, password, timeout) {}
/// <summary>
/// Constructor that lets you pass everything as parameters rather than requiring a config.
/// </summary>
/// <param name="type">Type of database to connect to. May not be null. This parameter
/// will be used to determine what OleDb provider to use.</param>
/// <param name="server">Server (or in the case of access, .mdb file) May be null, but you
/// probably won't be able to connect to anything.</param>
/// <param name="database">database name on that server, if applicable. May be null.</param>
/// <param name="user">Database user name, may be null.</param>
/// <param name="password">Password for the user. May be null.</param>
/// <param name="timeout">Connection timeout, in seconds. May be null.</param>
public OleDbDescriptor(DatabaseType type, string server, string database,
string user, string password, int? timeout)
: this(type, null, server, database, user, password, timeout) {}
/// <summary>
/// Constructor that lets you pass everything as parameters rather than requiring a config.
/// </summary>
/// <param name="provider">Database provider string. May be null. If null, we will use a
/// provider value based on the type, if not null, we will use this
/// provider.</param>
/// <param name="type">Type of database to connect to. May not be null. This parameter
/// will be used to determine what OleDb provider to use.</param>
/// <param name="server">Server (or in the case of access, .mdb file) May be null, but you
/// probably won't be able to connect to anything.</param>
/// <param name="database">database name on that server, if applicable. May be null.</param>
/// <param name="user">Database user name, may be null.</param>
/// <param name="password">Password for the user. May be null.</param>
/// <param name="timeout">Connection timeout, in seconds. May be null.</param>
public OleDbDescriptor(DatabaseType type, string provider, string server, string database,
string user, string password, int? timeout)
{
Type = type;
_connectionStr = (provider == null
? MakeConnectionString(type, server, database, user, password, timeout)
: MakeConnectionString(provider, server, database, user, password, timeout));
_cleanConnStr = (provider == null
? MakeConnectionString(type, server, database, user, null, timeout)
: MakeConnectionString(provider, server, database, user, null, timeout));
Provider = provider;
Server = server;
Database = database;
User = user;
Password = password;
ConnectTimeout = timeout;
}
/// <summary>
/// Returns the appropriate data access layer for this connection. The default
/// implementation returns a normal SQL data access layer, but this may be
/// overridden in particular DB connection descriptors.
/// </summary>
public override IDaLayer CreateDataAccessLayer()
{
switch (Type)
{
case DatabaseType.ORACLE:
return new OleDbOracleDaLayer(this);
case DatabaseType.SQLSERVER:
return new OleDbSqlServerDaLayer(this);
case DatabaseType.ACCESS:
return new OleDbAccessDaLayer(this);
default:
throw new NotImplementedException("No data access layer available for DB type " + Type);
}
}
/// <summary>
/// Begins the transaction. Returns a NEW ConnectionDescriptor that you should
/// use for operations you wish to be part of the transaction.
///
/// NOTE: You MUST call Commit or Rollback on the returned ITransaction when you are done.
/// </summary>
/// <returns>The ConnectionDescriptor object to pass to calls that you wish to have
/// happen as part of this transaction.</returns>
public ITransaction BeginTransaction()
{
return new SqlTransaction(this);
}
/// <summary>
/// Gets the type based on a couple optional parameters in the DB config file.
/// </summary>
/// <param name="config">Config to get params from.</param>
/// <param name="component">Section of the config XML to look in for db params.</param>
/// <returns>The type as specified in the config file, or throws an exception if
/// there is no type correctly specified.</returns>
private static DatabaseType GetTypeFromConfig(Config config, string component)
{
if (config.ParameterExists(component, "Type"))
{
return (DatabaseType)Enum.Parse(typeof(DatabaseType), config.GetParameter(component, "Type").Trim().ToUpper());
}
if (config.ParameterExists(component, "Provider"))
{
return GuessTypeFromProvider(config.GetParameter(component, "Provider").Trim());
}
throw new BadDaoConfigurationException(
"Database connection config for '" + component + "' is missing both type and provider.");
}
/// <summary>
/// Attempts to parse the provider string and determine what database type we're connecting to.
/// </summary>
/// <param name="provider">OleDB "provider" piece of the connection string.</param>
/// <returns>The DatabaseVendorName identifying the database (an exception will be thrown
/// if we cannot determine the type).</returns>
public static DatabaseType GuessTypeFromProvider(string provider)
{
if (provider == null)
{
throw new ArgumentNullException("provider", "Provider cannot be null.");
}
DatabaseType retVal;
switch (provider.ToUpper())
{
case "MICROSOFT.JET.OLEDB.4.0":
retVal = DatabaseType.ACCESS;
break;
case "SQLOLEDB":
retVal = DatabaseType.SQLSERVER;
break;
case "ORAOLEDB.ORACLE.1":
case "MSDAORA.1":
retVal = DatabaseType.ORACLE;
break;
default:
throw (new ArgumentException("This OleDbProvider (" + provider + ") is not supported.", "provider"));
}
return retVal;
}
/// <summary>
/// Assembles a OLEDB connection string that can be used to get a database connection.
/// All the parameters are optional for the purposes of this method, although obviously
/// it would be possible to create a useless connection string if you leave out important
/// parameters.
/// </summary>
/// <param name="providerType">Database type, will be used to determine provider string.</param>
/// <param name="server">Server name that is hosting the database</param>
/// <param name="database">Database name, if necessary to specify</param>
/// <param name="user">User name to use when accessing the db</param>
/// <param name="password">Password for above user.</param>
/// <param name="connectionTimeout">How long to wait before giving up on a command, in seconds.</param>
/// <returns>A connection string that can be used to create OleDbConnections.</returns>
public static string MakeConnectionString(DatabaseType providerType, string server,
string database, string user, string password, int? connectionTimeout)
{
string provider;
if (DatabaseType.SQLSERVER == providerType)
{
provider = "SQLOLEDB";
}
else if (DatabaseType.ORACLE == providerType)
{
provider = "OraOLEDB.Oracle.1";
// Oracle doesn't want this parameter.
database = null;
}
else if (DatabaseType.ACCESS == providerType)
{
provider = "Microsoft.Jet.OLEDB.4.0";
}
else
{
throw new ArgumentException(providerType + " is an unsupported type of database.", providerType.ToString());
}
return MakeConnectionString(provider, server, database, user, password, connectionTimeout);
}
/// <summary>
/// Assembles a OLEDB connection string that can be used to get a database connection.
/// All the parameters are optional for the purposes of this method, although obviously
/// it would be possible to create a useless connection string if you leave out important
/// parameters.
/// </summary>
/// <param name="provider">Database driver/type/something, for example "SQLOLEDB"</param>
/// <param name="server">Server name that is hosting the database</param>
/// <param name="database">Database name, if necessary to specify</param>
/// <param name="user">User name to use when accessing the db</param>
/// <param name="password">Password for above user.</param>
/// <param name="connectionTimeout">How long to wait before giving up on a command, in seconds.</param>
/// <returns>A connection string that can be used to create OleDbConnections.</returns>
public static string MakeConnectionString(string provider, string server,
string database, string user, string password,
int? connectionTimeout)
{
string connStr = "";
// Assemble the string. Only include parameters that have values.
if (StringHelper.IsNonBlank(provider))
{
connStr += "Provider=" + provider + ";";
}
if (StringHelper.IsNonBlank(server))
{
connStr += "Data Source=" + server + ";";
}
if (StringHelper.IsNonBlank(database))
{
connStr += "Initial Catalog=" + database + ";";
}
if (connectionTimeout != null)
{
connStr += "Connect Timeout=" + connectionTimeout + ";";
}
if (StringHelper.IsNonBlank(user))
{
connStr += "User ID=" + user + ";";
}
if (StringHelper.IsNonBlank(password))
{
connStr += "Password=" + password;
}
return connStr;
}
/// <exclude/>
public override string ToCleanString()
{
return _cleanConnStr;
}
/// <exclude/>
public override string ToCompleteString()
{
return _connectionStr;
}
/// <exclude/>
public override DbConnection CreateNewConnection()
{
return new OleDbConnection(_connectionStr);
}
/// <exclude/>
public override void SetParametersOnCommand(IDbCommand cmd, IEnumerable parameters)
{
int x = 0;
foreach (object param in parameters)
{
object addMe = param ?? DBNull.Value;
string paramName = DbCaches.ParamNames.Get(x);
// This is a hack for MS Access. For SQL Server and Oracle, OleDb
// correctly identifies the type when you add the parameter, but with
// Access it uses the wrong type and results in a type mismatch
// exception at runtime.
if ((Type == DatabaseType.ACCESS) && (addMe is DateTime))
{
OleDbParameter oleParam = new OleDbParameter(paramName, OleDbType.Date);
oleParam.Value = addMe;
cmd.Parameters.Add(oleParam);
}
else
{
cmd.Parameters.Add(new OleDbParameter(paramName, addMe));
}
x++;
}
}
/// <exclude/>
public override DbDataAdapter CreateNewAdapter(IDbCommand cmd)
{
return new OleDbDataAdapter((OleDbCommand)cmd);
}
/// <exclude/>
public override bool UsePooling()
{
return Type != DatabaseType.ACCESS;
}
/// <exclude/>
public override SqlClauseWithValue MakeModulusClause(string fieldName)
{
StringBuilder sb = DbCaches.StringBuilders.Get();
SqlClauseWithValue retVal = DbCaches.Clauses.Get();
switch (Type)
{
case DatabaseType.ORACLE:
sb.Append("MOD(");
sb.Append(fieldName);
sb.Append(", ");
retVal.PartBeforeValue = sb.ToString();
retVal.PartAfterValue = ")";
break;
case DatabaseType.SQLSERVER:
sb.Append("(");
sb.Append(fieldName);
sb.Append(" % ");
retVal.PartBeforeValue = sb.ToString();
retVal.PartAfterValue = ")";
break;
case DatabaseType.ACCESS:
sb.Append("(");
sb.Append(fieldName);
sb.Append(" MOD ");
retVal.PartBeforeValue = sb.ToString();
retVal.PartAfterValue = ")";
break;
default:
throw new NotImplementedException("Modulus clause not implemented for DB type " + Type);
}
DbCaches.StringBuilders.Return(sb);
return retVal;
}
/// <exclude/>
public override string MakeSequenceValueQuery(string sequenceName)
{
StringBuilder sb = DbCaches.StringBuilders.Get();
if (DatabaseType.ORACLE.Equals(Type))
{
sb.Append("SELECT ");
sb.Append(sequenceName);
sb.Append(".NEXTVAL FROM DUAL");
}
else
{
throw new NotImplementedException(
"Sequence ID generation is not supported for db type " + Type);
}
string retVal = sb.ToString();
DbCaches.StringBuilders.Return(sb);
return retVal;
}
/// <exclude/>
public override SqlClauseWithValue MakeBitwiseAndClause(string columnName)
{
StringBuilder sb = DbCaches.StringBuilders.Get();
SqlClauseWithValue retVal = DbCaches.Clauses.Get();
switch (Type)
{
case DatabaseType.SQLSERVER:
sb.Append(" (");
sb.Append(columnName);
sb.Append(" & ");
retVal.PartBeforeValue = sb.ToString();
retVal.PartAfterValue = ")";
break;
default:
throw new NotImplementedException("Bitwise and is not supported for this connection: " + this);
}
DbCaches.StringBuilders.Return(sb);
return retVal;
}
/// <exclude/>
public override string MakeCreateIndexCommand(string indexName,
bool isUnique, string tableName,
IEnumerable<string> columnNames)
{
string sql = base.MakeCreateIndexCommand(indexName, isUnique, tableName, columnNames);
if (Type == DatabaseType.ORACLE)
{
sql += " COMPUTE STATISTICS";
}
return sql;
}
/// <exclude/>
public override bool SupportsTruncate()
{
return (Type != DatabaseType.ACCESS);
}
/// <exclude/>
public override string TableAliasPrefix()
{
switch (Type)
{
case DatabaseType.ORACLE:
return "";
case DatabaseType.SQLSERVER:
return "";
case DatabaseType.ACCESS:
return "";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override string TableAliasSuffix()
{
switch (Type)
{
case DatabaseType.ORACLE:
return "";
case DatabaseType.SQLSERVER:
return "";
case DatabaseType.ACCESS:
return "";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override bool NeedToAliasColumns()
{
switch (Type)
{
case DatabaseType.ORACLE:
return true;
case DatabaseType.SQLSERVER:
return true;
case DatabaseType.ACCESS:
return true;
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override bool NeedAsForColumnAliases()
{
switch (Type)
{
case DatabaseType.ORACLE:
return false;
case DatabaseType.SQLSERVER:
return false;
case DatabaseType.ACCESS:
return true;
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override bool CanUseAliasInOrderClause()
{
switch (Type)
{
case DatabaseType.ORACLE:
return true;
case DatabaseType.SQLSERVER:
return true;
case DatabaseType.ACCESS:
return false;
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override bool HasCaseInsensitiveLikeOperator()
{
switch (Type)
{
case DatabaseType.ORACLE:
return false;
case DatabaseType.SQLSERVER:
return true;
case DatabaseType.ACCESS:
return true;
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override string CaseInsensitiveLikeOperator()
{
switch (Type)
{
case DatabaseType.SQLSERVER:
return "LIKE";
case DatabaseType.ACCESS:
return "LIKE";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override string ColumnAliasPrefix()
{
switch (Type)
{
case DatabaseType.ORACLE:
return "\"";
case DatabaseType.SQLSERVER:
return "[";
case DatabaseType.ACCESS:
return "[";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override string ColumnAliasSuffix()
{
switch (Type)
{
case DatabaseType.ORACLE:
return "\"";
case DatabaseType.SQLSERVER:
return "]";
case DatabaseType.ACCESS:
return "]";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override string FullOuterJoinKeyword()
{
switch (Type)
{
case DatabaseType.ORACLE:
return "FULL OUTER JOIN";
case DatabaseType.SQLSERVER:
return "FULL OUTER JOIN";
case DatabaseType.ACCESS:
return "OUTER JOIN";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
/// <exclude/>
public override string LowerCaseFunction()
{
switch (Type)
{
case DatabaseType.ORACLE:
return "LOWER";
case DatabaseType.SQLSERVER:
return "LOWER";
case DatabaseType.ACCESS:
return "LCase";
default:
throw new NotImplementedException("Support for type " + Type +
" is not yet fully implemented.");
}
}
}
}