1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Using
|
28
|
|
|
29
|
|
using System;
|
30
|
|
using System.Data;
|
31
|
|
using System.Reflection;
|
32
|
|
using System.Xml.Serialization;
|
33
|
|
using IBatisNet.Common.Exceptions;
|
34
|
|
using IBatisNet.Common.Utilities.TypesResolver;
|
35
|
|
|
36
|
|
|
37
|
|
#endregion
|
38
|
|
|
39
|
|
namespace IBatisNet.Common
|
40
|
|
{
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
[Serializable]
|
45
|
|
[XmlRoot("provider")]
|
46
|
|
public class Provider
|
47
|
|
{
|
48
|
|
#region Fields
|
49
|
|
[NonSerialized]
|
50
|
|
private string _assemblyName = string.Empty;
|
51
|
|
[NonSerialized]
|
52
|
|
private string _connectionClass = string.Empty;
|
53
|
|
[NonSerialized]
|
54
|
|
private string _commandClass = string.Empty;
|
55
|
|
[NonSerialized]
|
56
|
|
private string _dataParameter = string.Empty;
|
57
|
|
[NonSerialized]
|
58
|
|
private Type _dataParameterType = null;
|
59
|
|
|
60
|
|
[NonSerialized]
|
61
|
|
private string _parameterDbTypeClass = string.Empty;
|
62
|
|
[NonSerialized]
|
63
|
|
private Type _parameterDbType = null;
|
64
|
|
|
65
|
|
[NonSerialized]
|
66
|
|
private string _parameterDbTypeProperty = string.Empty;
|
67
|
|
[NonSerialized]
|
68
|
|
private string _dataAdapterClass = string.Empty;
|
69
|
|
[NonSerialized]
|
70
|
|
private string _commandBuilderClass = string.Empty;
|
71
|
|
|
72
|
|
[NonSerialized]
|
73
|
|
private string _name = string.Empty;
|
74
|
|
[NonSerialized]
|
75
|
|
private string _description = string.Empty;
|
76
|
|
[NonSerialized]
|
77
|
|
private bool _isDefault = false;
|
78
|
|
[NonSerialized]
|
79
|
|
private bool _isEnabled = true;
|
80
|
|
[NonSerialized]
|
81
|
|
private IDbConnection _templateConnection = null;
|
82
|
|
[NonSerialized]
|
83
|
|
private IDbCommand _templateCommand= null;
|
84
|
|
[NonSerialized]
|
85
|
|
private IDbDataAdapter _templateDataAdapter= null;
|
86
|
|
[NonSerialized]
|
87
|
|
private Type _commandBuilderType = null;
|
88
|
|
[NonSerialized]
|
89
|
|
private string _parameterPrefix = string.Empty;
|
90
|
|
[NonSerialized]
|
91
|
|
private bool _useParameterPrefixInSql = true;
|
92
|
|
[NonSerialized]
|
93
|
|
private bool _useParameterPrefixInParameter = true;
|
94
|
|
[NonSerialized]
|
95
|
|
private bool _usePositionalParameters = false;
|
96
|
|
[NonSerialized]
|
97
|
|
private bool _templateConnectionIsICloneable = false;
|
98
|
|
[NonSerialized]
|
99
|
|
private bool _templateCommandIsICloneable = false;
|
100
|
|
[NonSerialized]
|
101
|
|
private bool _templateDataAdapterIsICloneable = false;
|
102
|
|
[NonSerialized]
|
103
|
|
private bool _setDbParameterSize = true;
|
104
|
|
[NonSerialized]
|
105
|
|
private bool _setDbParameterPrecision = true;
|
106
|
|
[NonSerialized]
|
107
|
|
private bool _setDbParameterScale = true;
|
108
|
|
[NonSerialized]
|
109
|
|
private bool _useDeriveParameters = true;
|
110
|
|
|
111
|
|
|
112
|
|
|
113
|
|
#endregion
|
114
|
|
|
115
|
|
#region Properties
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
[XmlAttribute("assemblyName")]
|
121
|
|
public string AssemblyName
|
122
|
|
{
|
123
|
0
|
get { return _assemblyName; }
|
124
|
2040
|
set
|
125
|
|
{
|
126
|
2040
|
if ((value == null) || (value.Length < 1))
|
127
|
0
|
throw new ArgumentNullException("AssemblyName");
|
128
|
2040
|
_assemblyName = value;
|
129
|
|
}
|
130
|
|
}
|
131
|
|
|
132
|
|
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
[XmlAttribute("default")]
|
138
|
|
public bool IsDefault
|
139
|
|
{
|
140
|
1360
|
get { return _isDefault; }
|
141
|
340
|
set {_isDefault = value;}
|
142
|
|
}
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
|
|
148
|
|
|
149
|
|
[XmlAttribute("enabled")]
|
150
|
|
public bool IsEnabled
|
151
|
|
{
|
152
|
2040
|
get { return _isEnabled; }
|
153
|
1870
|
set {_isEnabled = value;}
|
154
|
|
}
|
155
|
|
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
|
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
[XmlAttribute("connectionClass")]
|
166
|
|
public string ConnectionClass
|
167
|
|
{
|
168
|
0
|
get { return _connectionClass; }
|
169
|
2040
|
set
|
170
|
|
{
|
171
|
2040
|
if ((value == null) || (value.Length < 1))
|
172
|
0
|
throw new ArgumentNullException("The connectionClass attribute is mandatory in the provider " + _name);
|
173
|
2040
|
_connectionClass = value;
|
174
|
|
}
|
175
|
|
}
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
|
|
188
|
|
|
189
|
|
|
190
|
|
|
191
|
|
[XmlAttribute("useParameterPrefixInSql")]
|
192
|
|
public bool UseParameterPrefixInSql
|
193
|
|
{
|
194
|
0
|
get { return _useParameterPrefixInSql; }
|
195
|
2040
|
set { _useParameterPrefixInSql = value;}
|
196
|
|
}
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
|
[XmlAttribute("useParameterPrefixInParameter")]
|
208
|
|
public bool UseParameterPrefixInParameter
|
209
|
|
{
|
210
|
64909
|
get { return _useParameterPrefixInParameter; }
|
211
|
2040
|
set { _useParameterPrefixInParameter = value; }
|
212
|
|
}
|
213
|
|
|
214
|
|
|
215
|
|
|
216
|
|
|
217
|
|
|
218
|
|
[XmlAttribute("usePositionalParameters")]
|
219
|
|
public bool UsePositionalParameters
|
220
|
|
{
|
221
|
122732
|
get { return _usePositionalParameters; }
|
222
|
2040
|
set { _usePositionalParameters = value; }
|
223
|
|
}
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
|
|
228
|
|
|
229
|
|
|
230
|
|
|
231
|
|
|
232
|
|
[XmlAttribute("setDbParameterSize")]
|
233
|
|
public bool SetDbParameterSize
|
234
|
|
{
|
235
|
32683
|
get { return _setDbParameterSize; }
|
236
|
170
|
set { _setDbParameterSize = value; }
|
237
|
|
}
|
238
|
|
|
239
|
|
|
240
|
|
|
241
|
|
|
242
|
|
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
|
[XmlAttribute("setDbParameterPrecision")]
|
247
|
|
public bool SetDbParameterPrecision
|
248
|
|
{
|
249
|
32683
|
get { return _setDbParameterPrecision; }
|
250
|
170
|
set { _setDbParameterPrecision = value; }
|
251
|
|
}
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
|
|
259
|
|
|
260
|
|
[XmlAttribute("setDbParameterScale")]
|
261
|
|
public bool SetDbParameterScale
|
262
|
|
{
|
263
|
32683
|
get { return _setDbParameterScale; }
|
264
|
170
|
set { _setDbParameterScale = value; }
|
265
|
|
}
|
266
|
|
|
267
|
|
|
268
|
|
|
269
|
|
|
270
|
|
|
271
|
|
[XmlAttribute("useDeriveParameters")]
|
272
|
|
public bool UseDeriveParameters
|
273
|
|
{
|
274
|
6
|
get { return _useDeriveParameters; }
|
275
|
340
|
set { _useDeriveParameters = value; }
|
276
|
|
}
|
277
|
|
|
278
|
|
|
279
|
|
|
280
|
|
|
281
|
|
|
282
|
|
|
283
|
|
|
284
|
|
[XmlAttribute("commandClass")]
|
285
|
|
public string CommandClass
|
286
|
|
{
|
287
|
0
|
get { return _commandClass; }
|
288
|
2040
|
set
|
289
|
|
{
|
290
|
2040
|
if ((value == null) || (value.Length < 1))
|
291
|
0
|
throw new ArgumentNullException("The commandClass attribute is mandatory in the provider " + _name);
|
292
|
2040
|
_commandClass = value;
|
293
|
|
}
|
294
|
|
}
|
295
|
|
|
296
|
|
|
297
|
|
|
298
|
|
|
299
|
|
|
300
|
|
|
301
|
|
|
302
|
|
|
303
|
|
[XmlAttribute("parameterClass")]
|
304
|
|
public string ParameterClass
|
305
|
|
{
|
306
|
0
|
get { return _dataParameter; }
|
307
|
2040
|
set
|
308
|
|
{
|
309
|
2040
|
if ((value == null) || (value.Length < 1))
|
310
|
0
|
throw new ArgumentNullException("The parameterClass attribute is mandatory in the provider " + _name);
|
311
|
2040
|
_dataParameter = value;
|
312
|
|
}
|
313
|
|
}
|
314
|
|
|
315
|
|
|
316
|
|
|
317
|
|
|
318
|
|
|
319
|
|
|
320
|
|
|
321
|
|
|
322
|
|
[XmlAttribute("parameterDbTypeClass")]
|
323
|
|
public string ParameterDbTypeClass
|
324
|
|
{
|
325
|
0
|
get { return _parameterDbTypeClass; }
|
326
|
2040
|
set
|
327
|
|
{
|
328
|
2040
|
if ((value == null) || (value.Length < 1))
|
329
|
0
|
throw new ArgumentNullException("The parameterDbTypeClass attribute is mandatory in the provider " + _name);
|
330
|
2040
|
_parameterDbTypeClass = value;
|
331
|
|
}
|
332
|
|
}
|
333
|
|
|
334
|
|
|
335
|
|
|
336
|
|
|
337
|
|
|
338
|
|
|
339
|
|
|
340
|
|
|
341
|
|
|
342
|
|
[XmlAttribute("parameterDbTypeProperty")]
|
343
|
|
public string ParameterDbTypeProperty
|
344
|
|
{
|
345
|
16263
|
get { return _parameterDbTypeProperty; }
|
346
|
2040
|
set
|
347
|
|
{
|
348
|
2040
|
if ((value == null) || (value.Length < 1))
|
349
|
0
|
throw new ArgumentNullException("The parameterDbTypeProperty attribute is mandatory in the provider " + _name);
|
350
|
2040
|
_parameterDbTypeProperty = value;
|
351
|
|
}
|
352
|
|
}
|
353
|
|
|
354
|
|
|
355
|
|
|
356
|
|
|
357
|
|
|
358
|
|
|
359
|
|
|
360
|
|
[XmlAttribute("dataAdapterClass")]
|
361
|
|
public string DataAdapterClass
|
362
|
|
{
|
363
|
0
|
get { return _dataAdapterClass; }
|
364
|
2040
|
set
|
365
|
|
{
|
366
|
2040
|
if ((value == null) || (value.Length < 1))
|
367
|
0
|
throw new ArgumentNullException("The dataAdapterClass attribute is mandatory in the provider " + _name);
|
368
|
2040
|
_dataAdapterClass = value;
|
369
|
|
}
|
370
|
|
}
|
371
|
|
|
372
|
|
|
373
|
|
|
374
|
|
|
375
|
|
|
376
|
|
|
377
|
|
|
378
|
|
|
379
|
|
|
380
|
|
[XmlAttribute("commandBuilderClass")]
|
381
|
|
public string CommandBuilderClass
|
382
|
|
{
|
383
|
8
|
get { return _commandBuilderClass; }
|
384
|
2040
|
set
|
385
|
|
{
|
386
|
2040
|
if ((value == null) || (value.Length < 1))
|
387
|
0
|
throw new ArgumentNullException("The commandBuilderClass attribute is mandatory in the provider " + _name);
|
388
|
2040
|
_commandBuilderClass = value;
|
389
|
|
}
|
390
|
|
}
|
391
|
|
|
392
|
|
|
393
|
|
|
394
|
|
|
395
|
|
|
396
|
|
[XmlAttribute("name")]
|
397
|
|
public string Name
|
398
|
|
{
|
399
|
36467
|
get { return _name; }
|
400
|
2040
|
set
|
401
|
|
{
|
402
|
2040
|
if ((value == null) || (value.Length < 1))
|
403
|
0
|
throw new ArgumentNullException("The name attribute is mandatory in the provider.");
|
404
|
|
|
405
|
2040
|
_name = value;
|
406
|
|
}
|
407
|
|
}
|
408
|
|
|
409
|
|
|
410
|
|
|
411
|
|
|
412
|
|
[XmlAttribute("description")]
|
413
|
|
public string Description
|
414
|
|
{
|
415
|
566
|
get { return _description; }
|
416
|
2040
|
set { _description = value;}
|
417
|
|
}
|
418
|
|
|
419
|
|
|
420
|
|
|
421
|
|
|
422
|
|
|
423
|
|
[XmlAttribute("parameterPrefix")]
|
424
|
|
public string ParameterPrefix
|
425
|
|
{
|
426
|
20471
|
get { return _parameterPrefix; }
|
427
|
2040
|
set
|
428
|
|
{
|
429
|
2040
|
if ((value == null) || (value.Length < 1))
|
430
|
|
{
|
431
|
340
|
_parameterPrefix = "";
|
432
|
|
|
433
|
|
}
|
434
|
|
else
|
435
|
|
{
|
436
|
1700
|
_parameterPrefix = value;
|
437
|
|
}
|
438
|
|
}
|
439
|
|
}
|
440
|
|
|
441
|
|
|
442
|
|
|
443
|
|
|
444
|
|
[XmlIgnore]
|
445
|
|
public bool IsObdc
|
446
|
|
{
|
447
|
6
|
get
|
448
|
|
{
|
449
|
6
|
return (_connectionClass.IndexOf(".Odbc.")>0);
|
450
|
|
}
|
451
|
|
}
|
452
|
|
#endregion
|
453
|
|
|
454
|
|
#region Constructor (s) / Destructor
|
455
|
|
|
456
|
|
|
457
|
|
|
458
|
2040
|
[Obsolete("This public constructor with no parameter is not really obsolete, but is reserved for serialization.", false)]
|
459
|
|
public Provider()
|
460
|
|
{
|
461
|
|
}
|
462
|
|
#endregion
|
463
|
|
|
464
|
|
#region Methods
|
465
|
|
|
466
|
|
|
467
|
|
|
468
|
1360
|
public void Initialisation()
|
469
|
|
{
|
470
|
1360
|
Assembly assembly;
|
471
|
1360
|
Type type;
|
472
|
1360
|
CachedTypeResolver cachedTypeResolver = new CachedTypeResolver();
|
473
|
|
|
474
|
1360
|
try
|
475
|
|
{
|
476
|
1360
|
assembly = Assembly.Load(_assemblyName);
|
477
|
|
|
478
|
|
|
479
|
1360
|
type = assembly.GetType(_commandClass, true);
|
480
|
1360
|
_templateCommand = (IDbCommand)type.GetConstructor(Type.EmptyTypes).Invoke(null);
|
481
|
|
|
482
|
1360
|
type = assembly.GetType(_dataAdapterClass, true);
|
483
|
1360
|
_templateDataAdapter = (IDbDataAdapter)type.GetConstructor(Type.EmptyTypes).Invoke(null);
|
484
|
|
|
485
|
1360
|
type = assembly.GetType(_connectionClass, true);
|
486
|
1360
|
_templateConnection = (IDbConnection)type.GetConstructor(Type.EmptyTypes).Invoke(null);
|
487
|
|
|
488
|
1360
|
_dataParameterType = assembly.GetType(_dataParameter, true);
|
489
|
|
|
490
|
1360
|
_commandBuilderType = assembly.GetType(_commandBuilderClass, true);
|
491
|
1360
|
if (_parameterDbTypeClass.IndexOf(',')>0)
|
492
|
|
{
|
493
|
0
|
_parameterDbType = cachedTypeResolver.Resolve(_parameterDbTypeClass);
|
494
|
|
}
|
495
|
|
else
|
496
|
|
{
|
497
|
1360
|
_parameterDbType = assembly.GetType(_parameterDbTypeClass, true);
|
498
|
|
}
|
499
|
|
|
500
|
1360
|
_templateConnectionIsICloneable = _templateConnection is ICloneable;
|
501
|
1360
|
_templateCommandIsICloneable = _templateCommand is ICloneable;
|
502
|
1360
|
_templateDataAdapterIsICloneable = _templateDataAdapter is ICloneable;
|
503
|
|
}
|
504
|
|
catch(Exception e)
|
505
|
|
{
|
506
|
0
|
throw new ConfigurationException(
|
507
|
|
string.Format("Could not configure providers. Provider named \"{0}\" not found, failed. Cause: {1}", _name, e.Message)
|
508
|
|
);
|
509
|
|
}
|
510
|
|
}
|
511
|
|
|
512
|
|
|
513
|
|
|
514
|
|
|
515
|
|
|
516
|
995
|
public IDbConnection GetConnection()
|
517
|
|
{
|
518
|
|
|
519
|
|
|
520
|
|
|
521
|
|
|
522
|
|
|
523
|
|
|
524
|
|
|
525
|
|
|
526
|
995
|
if (_templateConnectionIsICloneable)
|
527
|
|
{
|
528
|
995
|
return (IDbConnection) ((ICloneable)_templateConnection).Clone();
|
529
|
|
}
|
530
|
|
else
|
531
|
|
{
|
532
|
0
|
return (IDbConnection) Activator.CreateInstance(_templateConnection.GetType());
|
533
|
|
}
|
534
|
|
}
|
535
|
|
|
536
|
|
|
537
|
|
|
538
|
|
|
539
|
|
|
540
|
33193
|
public IDbCommand GetCommand()
|
541
|
|
{
|
542
|
33193
|
if (_templateCommandIsICloneable)
|
543
|
|
{
|
544
|
33193
|
return (IDbCommand) ((ICloneable)_templateCommand).Clone();
|
545
|
|
}
|
546
|
|
else
|
547
|
|
{
|
548
|
0
|
return (IDbCommand) Activator.CreateInstance(_templateCommand.GetType());
|
549
|
|
}
|
550
|
|
}
|
551
|
|
|
552
|
|
|
553
|
|
|
554
|
|
|
555
|
|
|
556
|
0
|
public IDbDataAdapter GetDataAdapter()
|
557
|
|
{
|
558
|
|
if (_templateDataAdapterIsICloneable)
|
559
|
|
{
|
560
|
|
return (IDbDataAdapter) ((ICloneable)_templateDataAdapter).Clone();
|
561
|
|
}
|
562
|
|
else
|
563
|
|
{
|
564
|
|
return (IDbDataAdapter) Activator.CreateInstance(_templateDataAdapter.GetType());
|
565
|
|
}
|
566
|
|
}
|
567
|
|
|
568
|
|
|
569
|
|
|
570
|
|
|
571
|
|
|
572
|
0
|
public IDataParameter GetDataParameter()
|
573
|
|
{
|
574
|
|
return (IDataParameter) Activator.CreateInstance(_dataParameterType);
|
575
|
|
}
|
576
|
|
|
577
|
|
|
578
|
|
|
579
|
|
|
580
|
|
|
581
|
4
|
public Type GetCommandBuilderType()
|
582
|
|
{
|
583
|
4
|
return _commandBuilderType;
|
584
|
|
}
|
585
|
|
|
586
|
|
|
587
|
|
|
588
|
|
|
589
|
|
|
590
|
|
public Type ParameterDbType
|
591
|
|
{
|
592
|
16199
|
get { return _parameterDbType; }
|
593
|
|
}
|
594
|
|
|
595
|
|
|
596
|
|
|
597
|
|
|
598
|
|
|
599
|
|
|
600
|
0
|
public override bool Equals(object obj)
|
601
|
|
{
|
602
|
|
if ((obj != null) && (obj is Provider))
|
603
|
|
{
|
604
|
|
Provider that = (Provider) obj;
|
605
|
|
return ((this._name == that._name) &&
|
606
|
|
(this._assemblyName == that._assemblyName) &&
|
607
|
|
(this._connectionClass == that._connectionClass));
|
608
|
|
}
|
609
|
|
return false;
|
610
|
|
}
|
611
|
|
|
612
|
|
|
613
|
|
|
614
|
|
|
615
|
|
|
616
|
0
|
public override int GetHashCode()
|
617
|
|
{
|
618
|
|
return (_name.GetHashCode() ^ _assemblyName.GetHashCode() ^ _connectionClass.GetHashCode());
|
619
|
|
}
|
620
|
|
|
621
|
|
|
622
|
|
|
623
|
|
|
624
|
|
|
625
|
0
|
public override string ToString()
|
626
|
|
{
|
627
|
|
return "Provider " + _name;
|
628
|
|
}
|
629
|
|
#endregion
|
630
|
|
|
631
|
|
}
|
632
|
|
}
|
633
|
|
|