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 Imports
|
28
|
|
using System;
|
29
|
|
using System.Data;
|
30
|
|
using System.IO;
|
31
|
|
using System.Text;
|
32
|
|
using System.Collections;
|
33
|
|
using System.Collections.Specialized;
|
34
|
|
using System.Threading;
|
35
|
|
using System.Xml;
|
36
|
|
|
37
|
|
using IBatisNet.Common;
|
38
|
|
using IBatisNet.Common.Exceptions;
|
39
|
|
using IBatisNet.Common.Utilities;
|
40
|
|
using IBatisNet.DataMapper.SessionContainer;
|
41
|
|
|
42
|
|
using IBatisNet.DataMapper.Configuration;
|
43
|
|
using IBatisNet.DataMapper.Configuration.Alias;
|
44
|
|
using IBatisNet.DataMapper.Configuration.ParameterMapping;
|
45
|
|
using IBatisNet.DataMapper.Configuration.ResultMapping;
|
46
|
|
using IBatisNet.DataMapper.Configuration.Cache;
|
47
|
|
using IBatisNet.DataMapper.Exceptions;
|
48
|
|
using IBatisNet.DataMapper.MappedStatements;
|
49
|
|
using IBatisNet.DataMapper.TypeHandlers;
|
50
|
|
|
51
|
|
#endregion
|
52
|
|
|
53
|
|
namespace IBatisNet.DataMapper
|
54
|
|
{
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
public class SqlMapper
|
59
|
|
{
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
public delegate void RowDelegate(object obj, IList list);
|
65
|
|
|
66
|
|
#region Contants
|
67
|
|
private const string DEFAULT_FILE_CONFIG_NAME = "sqlmap.config";
|
68
|
|
#endregion
|
69
|
|
|
70
|
|
#region Fields
|
71
|
|
|
72
|
|
private HybridDictionary _mappedStatements = new HybridDictionary();
|
73
|
|
|
74
|
|
private HybridDictionary _resultMaps = new HybridDictionary();
|
75
|
|
|
76
|
|
private HybridDictionary _parameterMaps = new HybridDictionary();
|
77
|
|
|
78
|
|
private DataSource _dataSource = null;
|
79
|
|
|
80
|
|
private HybridDictionary _typeAliasMaps = new HybridDictionary();
|
81
|
|
|
82
|
|
private HybridDictionary _cacheMaps = new HybridDictionary();
|
83
|
|
private TypeHandlerFactory _typeHandlerFactory = null;
|
84
|
|
|
85
|
|
private bool _cacheModelsEnabled = false;
|
86
|
|
private bool _useEmbedStatementParams = false;
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
private ISessionContainer _sessionContainer = null;
|
92
|
|
|
93
|
|
#endregion
|
94
|
|
|
95
|
|
#region Properties
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
public IDalSession LocalSession
|
102
|
|
{
|
103
|
14
|
get { return _sessionContainer.LocalSession; }
|
104
|
|
}
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
|
|
111
|
|
public bool IsSessionStarted
|
112
|
|
{
|
113
|
20
|
get { return (_sessionContainer.LocalSession != null); }
|
114
|
|
}
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
0
|
public bool IsCacheModelsEnabled
|
121
|
|
{
|
122
|
|
get { return _cacheModelsEnabled; }
|
123
|
|
}
|
124
|
|
|
125
|
|
|
126
|
|
|
127
|
|
|
128
|
|
|
129
|
|
public bool UseEmbedStatementParams
|
130
|
|
{
|
131
|
26520
|
get { return _useEmbedStatementParams; }
|
132
|
|
}
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
internal TypeHandlerFactory TypeHandlerFactory
|
138
|
|
{
|
139
|
134620
|
get { return _typeHandlerFactory; }
|
140
|
|
}
|
141
|
|
#endregion
|
142
|
|
|
143
|
|
#region Constructor (s) / Destructor
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
170
|
internal SqlMapper(TypeHandlerFactory typeHandlerFactory)
|
148
|
|
{
|
149
|
170
|
_typeHandlerFactory = typeHandlerFactory;
|
150
|
170
|
_sessionContainer = SessionContainerFactory.GetSessionContainer(IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(this).ToString());
|
151
|
|
}
|
152
|
|
#endregion
|
153
|
|
|
154
|
|
#region Methods
|
155
|
|
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
170
|
internal void SetCacheModelsEnabled(bool value)
|
160
|
|
{
|
161
|
170
|
_cacheModelsEnabled = value;
|
162
|
|
}
|
163
|
|
|
164
|
|
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
170
|
internal void SetUseEmbedStatementParams(bool value)
|
169
|
|
{
|
170
|
170
|
_useEmbedStatementParams = value;
|
171
|
|
}
|
172
|
|
|
173
|
|
#region Configure
|
174
|
|
|
175
|
|
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
0
|
static public SqlMapper Configure( XmlDocument document )
|
181
|
|
{
|
182
|
|
return new DomSqlMapBuilder().Build( document, false );
|
183
|
|
}
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
|
|
188
|
|
|
189
|
|
|
190
|
0
|
static public SqlMapper Configure()
|
191
|
|
{
|
192
|
|
return Configure( Resources.GetConfigAsXmlDocument(DEFAULT_FILE_CONFIG_NAME) );
|
193
|
|
}
|
194
|
|
|
195
|
|
|
196
|
|
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
|
|
201
|
0
|
public static SqlMapper Configure(string fileName)
|
202
|
|
{
|
203
|
|
XmlDocument document = Resources.GetConfigAsXmlDocument(fileName);
|
204
|
|
return new DomSqlMapBuilder().Build( document, false);
|
205
|
|
}
|
206
|
|
|
207
|
|
|
208
|
|
|
209
|
|
|
210
|
|
|
211
|
|
|
212
|
|
|
213
|
0
|
public static SqlMapper ConfigureAndWatch(ConfigureHandler configureDelegate)
|
214
|
|
{
|
215
|
|
return ConfigureAndWatch( DEFAULT_FILE_CONFIG_NAME, configureDelegate ) ;
|
216
|
|
}
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
|
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
|
|
228
|
170
|
public static SqlMapper ConfigureAndWatch( string fileName, ConfigureHandler configureDelegate )
|
229
|
|
{
|
230
|
170
|
XmlDocument document = Resources.GetConfigAsXmlDocument( fileName );
|
231
|
|
|
232
|
170
|
ConfigWatcherHandler.ClearFilesMonitored();
|
233
|
170
|
ConfigWatcherHandler.AddFileToWatch( Resources.GetFileInfo( fileName ) );
|
234
|
|
|
235
|
170
|
TimerCallback callBakDelegate = new TimerCallback( SqlMapper.OnConfigFileChange );
|
236
|
|
|
237
|
170
|
StateConfig state = new StateConfig();
|
238
|
170
|
state.fileName = fileName;
|
239
|
170
|
state.configureHandler = configureDelegate;
|
240
|
|
|
241
|
170
|
new ConfigWatcherHandler( callBakDelegate, state );
|
242
|
|
|
243
|
170
|
return new DomSqlMapBuilder().Build( document, true );
|
244
|
|
}
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
|
|
249
|
|
|
250
|
0
|
public static void OnConfigFileChange(object obj)
|
251
|
|
{
|
252
|
|
StateConfig state = (StateConfig)obj;
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
state.configureHandler( null );
|
257
|
|
}
|
258
|
|
|
259
|
|
#endregion
|
260
|
|
|
261
|
|
#region Manage Connection, Transaction
|
262
|
|
|
263
|
|
|
264
|
|
|
265
|
|
|
266
|
|
|
267
|
1
|
public IDalSession OpenConnection()
|
268
|
|
{
|
269
|
1
|
if (_sessionContainer.LocalSession != null)
|
270
|
|
{
|
271
|
0
|
throw new DataMapperException("SqlMap could not invoke OpenConnection(). A connection is already started. Call CloseConnection first.");
|
272
|
|
}
|
273
|
1
|
SqlMapSession session = new SqlMapSession(this);
|
274
|
1
|
_sessionContainer.Store(session);
|
275
|
1
|
session.OpenConnection();
|
276
|
1
|
return session;
|
277
|
|
}
|
278
|
|
|
279
|
|
|
280
|
|
|
281
|
|
|
282
|
1
|
public void CloseConnection()
|
283
|
|
{
|
284
|
1
|
if (_sessionContainer.LocalSession == null)
|
285
|
|
{
|
286
|
0
|
throw new DataMapperException("SqlMap could not invoke CloseConnection(). No connection was started. Call OpenConnection() first.");
|
287
|
|
}
|
288
|
1
|
try
|
289
|
|
{
|
290
|
1
|
IDalSession session = _sessionContainer.LocalSession;
|
291
|
1
|
session.CloseConnection();
|
292
|
|
}
|
293
|
|
catch(Exception ex)
|
294
|
|
{
|
295
|
0
|
throw new DataMapperException("SqlMapper could not CloseConnection(). Cause :"+ex.Message, ex);
|
296
|
|
}
|
297
|
|
finally
|
298
|
|
{
|
299
|
1
|
_sessionContainer.Dispose();
|
300
|
|
}
|
301
|
|
}
|
302
|
|
|
303
|
|
|
304
|
|
|
305
|
|
|
306
|
|
|
307
|
5
|
public IDalSession BeginTransaction()
|
308
|
|
{
|
309
|
5
|
if (_sessionContainer.LocalSession != null)
|
310
|
|
{
|
311
|
1
|
throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A Transaction is already started. Call CommitTransaction() or RollbackTransaction first.");
|
312
|
|
}
|
313
|
4
|
SqlMapSession session = new SqlMapSession(this);
|
314
|
4
|
_sessionContainer.Store(session);
|
315
|
4
|
session.BeginTransaction();
|
316
|
4
|
return session ;
|
317
|
|
}
|
318
|
|
|
319
|
|
|
320
|
|
|
321
|
|
|
322
|
|
|
323
|
0
|
public IDalSession BeginTransaction(bool openConnection)
|
324
|
|
{
|
325
|
|
IDalSession session = null;
|
326
|
|
|
327
|
|
if (openConnection)
|
328
|
|
{
|
329
|
|
session = this.BeginTransaction();
|
330
|
|
}
|
331
|
|
else
|
332
|
|
{
|
333
|
|
session = _sessionContainer.LocalSession;
|
334
|
|
if (session == null)
|
335
|
|
{
|
336
|
|
throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A session must be Open. Call OpenConnection() first.");
|
337
|
|
}
|
338
|
|
session.BeginTransaction(openConnection);
|
339
|
|
}
|
340
|
|
|
341
|
|
return session;
|
342
|
|
}
|
343
|
|
|
344
|
|
|
345
|
|
|
346
|
|
|
347
|
|
|
348
|
|
|
349
|
|
|
350
|
0
|
public IDalSession BeginTransaction(IsolationLevel isolationLevel)
|
351
|
|
{
|
352
|
|
if (_sessionContainer.LocalSession != null)
|
353
|
|
{
|
354
|
|
throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A Transaction is already started. Call CommitTransaction() or RollbackTransaction first.");
|
355
|
|
}
|
356
|
|
SqlMapSession session = new SqlMapSession(this);
|
357
|
|
_sessionContainer.Store(session);
|
358
|
|
session.BeginTransaction(isolationLevel);
|
359
|
|
return session;
|
360
|
|
}
|
361
|
|
|
362
|
|
|
363
|
|
|
364
|
|
|
365
|
|
|
366
|
|
|
367
|
|
|
368
|
|
|
369
|
|
|
370
|
0
|
public IDalSession BeginTransaction(bool openConnection, IsolationLevel isolationLevel)
|
371
|
|
{
|
372
|
|
IDalSession session = null;
|
373
|
|
|
374
|
|
if (openConnection)
|
375
|
|
{
|
376
|
|
session = this.BeginTransaction(isolationLevel);
|
377
|
|
}
|
378
|
|
else
|
379
|
|
{
|
380
|
|
session = _sessionContainer.LocalSession;
|
381
|
|
if (session == null)
|
382
|
|
{
|
383
|
|
throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A session must be Open. Call OpenConnection() first.");
|
384
|
|
}
|
385
|
|
session.BeginTransaction(openConnection, isolationLevel);
|
386
|
|
}
|
387
|
|
return session;
|
388
|
|
}
|
389
|
|
|
390
|
|
|
391
|
|
|
392
|
|
|
393
|
|
|
394
|
|
|
395
|
|
|
396
|
4
|
public void CommitTransaction()
|
397
|
|
{
|
398
|
4
|
if (_sessionContainer.LocalSession == null)
|
399
|
|
{
|
400
|
1
|
throw new DataMapperException("SqlMap could not invoke CommitTransaction(). No Transaction was started. Call BeginTransaction() first.");
|
401
|
|
}
|
402
|
3
|
try
|
403
|
|
{
|
404
|
3
|
IDalSession session = _sessionContainer.LocalSession;
|
405
|
3
|
session.CommitTransaction();
|
406
|
|
}
|
407
|
|
finally
|
408
|
|
{
|
409
|
3
|
_sessionContainer.Dispose();
|
410
|
|
}
|
411
|
|
}
|
412
|
|
|
413
|
|
|
414
|
|
|
415
|
|
|
416
|
|
|
417
|
0
|
public void CommitTransaction(bool closeConnection)
|
418
|
|
{
|
419
|
|
if (_sessionContainer.LocalSession == null)
|
420
|
|
{
|
421
|
|
throw new DataMapperException("SqlMap could not invoke CommitTransaction(). No Transaction was started. Call BeginTransaction() first.");
|
422
|
|
}
|
423
|
|
try
|
424
|
|
{
|
425
|
|
IDalSession session = _sessionContainer.LocalSession;
|
426
|
|
session.CommitTransaction(closeConnection);
|
427
|
|
}
|
428
|
|
finally
|
429
|
|
{
|
430
|
|
if (closeConnection)
|
431
|
|
{
|
432
|
|
_sessionContainer.Dispose();
|
433
|
|
}
|
434
|
|
}
|
435
|
|
}
|
436
|
|
|
437
|
|
|
438
|
|
|
439
|
|
|
440
|
|
|
441
|
|
|
442
|
|
|
443
|
1
|
public void RollBackTransaction()
|
444
|
|
{
|
445
|
1
|
if (_sessionContainer.LocalSession == null)
|
446
|
|
{
|
447
|
0
|
throw new DataMapperException("SqlMap could not invoke RollBackTransaction(). No Transaction was started. Call BeginTransaction() first.");
|
448
|
|
}
|
449
|
1
|
try
|
450
|
|
{
|
451
|
1
|
IDalSession session = _sessionContainer.LocalSession;
|
452
|
1
|
session.RollBackTransaction();
|
453
|
|
}
|
454
|
|
finally
|
455
|
|
{
|
456
|
1
|
_sessionContainer.Dispose();
|
457
|
|
}
|
458
|
|
}
|
459
|
|
|
460
|
|
|
461
|
|
|
462
|
|
|
463
|
|
|
464
|
0
|
public void RollBackTransaction(bool closeConnection)
|
465
|
|
{
|
466
|
|
if (_sessionContainer.LocalSession == null)
|
467
|
|
{
|
468
|
|
throw new DataMapperException("SqlMap could not invoke RollBackTransaction(). No Transaction was started. Call BeginTransaction() first.");
|
469
|
|
}
|
470
|
|
try
|
471
|
|
{
|
472
|
|
IDalSession session = _sessionContainer.LocalSession;
|
473
|
|
session.RollBackTransaction(closeConnection);
|
474
|
|
}
|
475
|
|
finally
|
476
|
|
{
|
477
|
|
if (closeConnection)
|
478
|
|
{
|
479
|
|
_sessionContainer.Dispose();
|
480
|
|
}
|
481
|
|
}
|
482
|
|
}
|
483
|
|
|
484
|
|
#endregion
|
485
|
|
|
486
|
|
#region QueryForObject
|
487
|
|
|
488
|
|
|
489
|
|
|
490
|
|
|
491
|
|
|
492
|
|
|
493
|
|
|
494
|
|
|
495
|
|
|
496
|
|
|
497
|
|
|
498
|
125
|
public object QueryForObject(string statementName, object parameterObject)
|
499
|
|
{
|
500
|
125
|
bool isSessionLocal = false;
|
501
|
125
|
IDalSession session = _sessionContainer.LocalSession;
|
502
|
125
|
object result;
|
503
|
|
|
504
|
125
|
if (session == null)
|
505
|
|
{
|
506
|
122
|
session = new SqlMapSession(this.DataSource);
|
507
|
122
|
session.OpenConnection();
|
508
|
122
|
isSessionLocal = true;
|
509
|
|
}
|
510
|
|
|
511
|
125
|
IMappedStatement statement = GetMappedStatement(statementName);
|
512
|
|
|
513
|
125
|
try
|
514
|
|
{
|
515
|
125
|
result = statement.ExecuteQueryForObject(session, parameterObject);
|
516
|
|
}
|
517
|
|
catch
|
518
|
|
{
|
519
|
0
|
throw;
|
520
|
|
}
|
521
|
|
finally
|
522
|
|
{
|
523
|
125
|
if ( isSessionLocal )
|
524
|
|
{
|
525
|
122
|
session.CloseConnection();
|
526
|
|
}
|
527
|
|
}
|
528
|
|
|
529
|
125
|
return result;
|
530
|
|
}
|
531
|
|
|
532
|
|
|
533
|
|
|
534
|
|
|
535
|
|
|
536
|
|
|
537
|
|
|
538
|
|
|
539
|
|
|
540
|
|
|
541
|
1
|
public object QueryForObject(string statementName, object parameterObject, object resultObject)
|
542
|
|
{
|
543
|
1
|
bool isSessionLocal = false;
|
544
|
1
|
IDalSession session = _sessionContainer.LocalSession;
|
545
|
1
|
object result = null;
|
546
|
|
|
547
|
1
|
if (session == null)
|
548
|
|
{
|
549
|
1
|
session = new SqlMapSession(this.DataSource);
|
550
|
1
|
session.OpenConnection();
|
551
|
1
|
isSessionLocal = true;
|
552
|
|
}
|
553
|
|
|
554
|
1
|
IMappedStatement statement = GetMappedStatement(statementName);
|
555
|
|
|
556
|
1
|
try
|
557
|
|
{
|
558
|
1
|
result = statement.ExecuteQueryForObject(session, parameterObject, resultObject);
|
559
|
|
}
|
560
|
|
catch
|
561
|
|
{
|
562
|
0
|
throw;
|
563
|
|
}
|
564
|
|
finally
|
565
|
|
{
|
566
|
1
|
if ( isSessionLocal )
|
567
|
|
{
|
568
|
1
|
session.CloseConnection();
|
569
|
|
}
|
570
|
|
}
|
571
|
|
|
572
|
1
|
return result;
|
573
|
|
}
|
574
|
|
|
575
|
|
|
576
|
|
#endregion
|
577
|
|
|
578
|
|
#region QueryForMap, QueryForDictionary
|
579
|
|
|
580
|
|
|
581
|
|
|
582
|
|
|
583
|
|
|
584
|
|
|
585
|
|
|
586
|
|
|
587
|
|
|
588
|
0
|
public IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty)
|
589
|
|
{
|
590
|
|
return QueryForMap( statementName, parameterObject, keyProperty);
|
591
|
|
}
|
592
|
|
|
593
|
|
|
594
|
|
|
595
|
|
|
596
|
|
|
597
|
|
|
598
|
|
|
599
|
|
|
600
|
|
|
601
|
|
|
602
|
|
|
603
|
0
|
public IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty, string valueProperty)
|
604
|
|
{
|
605
|
|
return QueryForMap( statementName, parameterObject, keyProperty, valueProperty);
|
606
|
|
}
|
607
|
|
|
608
|
|
|
609
|
|
|
610
|
|
|
611
|
|
|
612
|
|
|
613
|
|
|
614
|
|
|
615
|
|
|
616
|
2
|
public IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty)
|
617
|
|
{
|
618
|
2
|
return QueryForMap(statementName, parameterObject, keyProperty, null);
|
619
|
|
}
|
620
|
|
|
621
|
|
|
622
|
|
|
623
|
|
|
624
|
|
|
625
|
|
|
626
|
|
|
627
|
|
|
628
|
|
|
629
|
|
|
630
|
|
|
631
|
|
|
632
|
|
|
633
|
3
|
public IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty, string valueProperty)
|
634
|
|
{
|
635
|
3
|
bool isSessionLocal = false;
|
636
|
3
|
IDalSession session = _sessionContainer.LocalSession;
|
637
|
3
|
IDictionary map = null;
|
638
|
|
|
639
|
3
|
if (session == null)
|
640
|
|
{
|
641
|
3
|
session = new SqlMapSession(this.DataSource);
|
642
|
3
|
session.OpenConnection();
|
643
|
3
|
isSessionLocal = true;
|
644
|
|
}
|
645
|
|
|
646
|
3
|
IMappedStatement statement = GetMappedStatement(statementName);
|
647
|
|
|
648
|
3
|
try
|
649
|
|
{
|
650
|
3
|
map = statement.ExecuteQueryForMap(session, parameterObject, keyProperty, valueProperty);
|
651
|
|
}
|
652
|
|
catch
|
653
|
|
{
|
654
|
0
|
throw;
|
655
|
|
}
|
656
|
|
finally
|
657
|
|
{
|
658
|
3
|
if ( isSessionLocal )
|
659
|
|
{
|
660
|
3
|
session.CloseConnection();
|
661
|
|
}
|
662
|
|
}
|
663
|
|
|
664
|
3
|
return map;
|
665
|
|
}
|
666
|
|
|
667
|
|
|
668
|
|
#endregion
|
669
|
|
|
670
|
|
#region QueryForList
|
671
|
|
|
672
|
|
|
673
|
|
|
674
|
|
|
675
|
|
|
676
|
|
|
677
|
|
|
678
|
|
|
679
|
|
|
680
|
|
|
681
|
|
|
682
|
63
|
public IList QueryForList(string statementName, object parameterObject)
|
683
|
|
{
|
684
|
63
|
bool isSessionLocal = false;
|
685
|
63
|
IDalSession session = _sessionContainer.LocalSession;
|
686
|
63
|
IList list;
|
687
|
|
|
688
|
63
|
if (session == null)
|
689
|
|
{
|
690
|
63
|
session = new SqlMapSession(this.DataSource);
|
691
|
63
|
session.OpenConnection();
|
692
|
63
|
isSessionLocal = true;
|
693
|
|
}
|
694
|
|
|
695
|
63
|
IMappedStatement statement = GetMappedStatement(statementName);
|
696
|
|
|
697
|
63
|
try
|
698
|
|
{
|
699
|
63
|
list = statement.ExecuteQueryForList(session, parameterObject);
|
700
|
|
}
|
701
|
|
catch
|
702
|
|
{
|
703
|
0
|
throw;
|
704
|
|
}
|
705
|
|
finally
|
706
|
|
{
|
707
|
63
|
if ( isSessionLocal )
|
708
|
|
{
|
709
|
63
|
session.CloseConnection();
|
710
|
|
}
|
711
|
|
}
|
712
|
|
|
713
|
63
|
return list;
|
714
|
|
}
|
715
|
|
|
716
|
|
|
717
|
|
|
718
|
|
|
719
|
|
|
720
|
|
|
721
|
|
|
722
|
|
|
723
|
|
|
724
|
|
|
725
|
|
|
726
|
|
|
727
|
|
|
728
|
1
|
public IList QueryForList(string statementName, object parameterObject, int skipResults, int maxResults)
|
729
|
|
{
|
730
|
1
|
bool isSessionLocal = false;
|
731
|
1
|
IDalSession session = _sessionContainer.LocalSession;
|
732
|
1
|
IList list;
|
733
|
|
|
734
|
1
|
if (session == null)
|
735
|
|
{
|
736
|
1
|
session = new SqlMapSession(this.DataSource);
|
737
|
1
|
session.OpenConnection();
|
738
|
1
|
isSessionLocal = true;
|
739
|
|
}
|
740
|
|
|
741
|
1
|
IMappedStatement statement = GetMappedStatement(statementName);
|
742
|
|
|
743
|
1
|
try
|
744
|
|
{
|
745
|
1
|
list = statement.ExecuteQueryForList(session, parameterObject, skipResults, maxResults);
|
746
|
|
}
|
747
|
|
catch
|
748
|
|
{
|
749
|
0
|
throw;
|
750
|
|
}
|
751
|
|
finally
|
752
|
|
{
|
753
|
1
|
if ( isSessionLocal )
|
754
|
|
{
|
755
|
1
|
session.CloseConnection();
|
756
|
|
}
|
757
|
|
}
|
758
|
|
|
759
|
1
|
return list;
|
760
|
|
}
|
761
|
|
|
762
|
|
|
763
|
|
|
764
|
|
|
765
|
|
|
766
|
|
|
767
|
|
|
768
|
|
|
769
|
|
|
770
|
|
|
771
|
|
|
772
|
|
|
773
|
|
|
774
|
1
|
public void QueryForList(string statementName, object parameterObject, IList resultObject)
|
775
|
|
{
|
776
|
1
|
bool isSessionLocal = false;
|
777
|
1
|
IDalSession session = _sessionContainer.LocalSession;
|
778
|
|
|
779
|
1
|
if (resultObject == null)
|
780
|
|
{
|
781
|
0
|
throw new DataMapperException("resultObject parameter must be instantiated before being passed to SqlMapper.QueryForList");
|
782
|
|
}
|
783
|
|
|
784
|
1
|
if (session == null)
|
785
|
|
{
|
786
|
1
|
session = new SqlMapSession(this.DataSource);
|
787
|
1
|
session.OpenConnection();
|
788
|
1
|
isSessionLocal = true;
|
789
|
|
}
|
790
|
|
|
791
|
1
|
IMappedStatement statement = GetMappedStatement(statementName);
|
792
|
|
|
793
|
1
|
try
|
794
|
|
{
|
795
|
1
|
statement.ExecuteQueryForList(session, parameterObject, resultObject);
|
796
|
|
}
|
797
|
|
catch
|
798
|
|
{
|
799
|
0
|
throw;
|
800
|
|
}
|
801
|
|
finally
|
802
|
|
{
|
803
|
1
|
if ( isSessionLocal )
|
804
|
|
{
|
805
|
1
|
session.CloseConnection();
|
806
|
|
}
|
807
|
|
}
|
808
|
|
}
|
809
|
|
|
810
|
|
|
811
|
|
#endregion
|
812
|
|
|
813
|
|
#region QueryForPaginatedList
|
814
|
|
|
815
|
|
|
816
|
|
|
817
|
|
|
818
|
|
|
819
|
|
|
820
|
|
|
821
|
|
|
822
|
4
|
public PaginatedList QueryForPaginatedList(String statementName, object parameterObject, int pageSize)
|
823
|
|
{
|
824
|
4
|
IMappedStatement statement = GetMappedStatement(statementName);
|
825
|
4
|
return new PaginatedList(statement, parameterObject, pageSize);
|
826
|
|
}
|
827
|
|
#endregion
|
828
|
|
|
829
|
|
#region QueryWithRowDelegate
|
830
|
|
|
831
|
|
|
832
|
|
|
833
|
|
|
834
|
|
|
835
|
|
|
836
|
|
|
837
|
|
|
838
|
|
|
839
|
|
|
840
|
|
|
841
|
|
|
842
|
1
|
public IList QueryWithRowDelegate(string statementName, object parameterObject, RowDelegate rowDelegate)
|
843
|
|
{
|
844
|
1
|
bool isSessionLocal = false;
|
845
|
1
|
IDalSession session = _sessionContainer.LocalSession;
|
846
|
1
|
IList list = null;
|
847
|
|
|
848
|
1
|
if (session == null)
|
849
|
|
{
|
850
|
1
|
session = new SqlMapSession(this.DataSource);
|
851
|
1
|
session.OpenConnection();
|
852
|
1
|
isSessionLocal = true;
|
853
|
|
}
|
854
|
|
|
855
|
1
|
IMappedStatement statement = GetMappedStatement(statementName);
|
856
|
|
|
857
|
1
|
try
|
858
|
|
{
|
859
|
1
|
list = statement.ExecuteQueryForRowDelegate(session, parameterObject, rowDelegate);
|
860
|
|
}
|
861
|
|
catch
|
862
|
|
{
|
863
|
0
|
throw;
|
864
|
|
}
|
865
|
|
finally
|
866
|
|
{
|
867
|
1
|
if ( isSessionLocal )
|
868
|
|
{
|
869
|
1
|
session.CloseConnection();
|
870
|
|
}
|
871
|
|
}
|
872
|
|
|
873
|
1
|
return list;
|
874
|
|
}
|
875
|
|
|
876
|
|
|
877
|
|
#endregion
|
878
|
|
|
879
|
|
#region Query Insert, Update, Delete
|
880
|
|
|
881
|
|
|
882
|
|
|
883
|
|
|
884
|
|
|
885
|
|
|
886
|
|
|
887
|
|
|
888
|
|
|
889
|
|
|
890
|
|
|
891
|
|
|
892
|
|
|
893
|
|
|
894
|
|
|
895
|
|
|
896
|
|
|
897
|
57
|
public object Insert(string statementName, object parameterObject)
|
898
|
|
{
|
899
|
57
|
bool isSessionLocal = false;
|
900
|
57
|
IDalSession session = _sessionContainer.LocalSession;
|
901
|
57
|
object generatedKey = null;
|
902
|
|
|
903
|
57
|
if (session == null)
|
904
|
|
{
|
905
|
54
|
session = new SqlMapSession(this.DataSource);
|
906
|
54
|
session.OpenConnection();
|
907
|
54
|
isSessionLocal = true;
|
908
|
|
}
|
909
|
|
|
910
|
57
|
IMappedStatement statement = GetMappedStatement(statementName);
|
911
|
|
|
912
|
57
|
try
|
913
|
|
{
|
914
|
57
|
generatedKey = statement.ExecuteInsert(session, parameterObject);
|
915
|
|
}
|
916
|
|
catch
|
917
|
|
{
|
918
|
0
|
throw;
|
919
|
|
}
|
920
|
|
finally
|
921
|
|
{
|
922
|
57
|
if ( isSessionLocal )
|
923
|
|
{
|
924
|
54
|
session.CloseConnection();
|
925
|
|
}
|
926
|
|
}
|
927
|
|
|
928
|
57
|
return generatedKey;
|
929
|
|
}
|
930
|
|
|
931
|
|
|
932
|
|
|
933
|
|
|
934
|
|
|
935
|
|
|
936
|
|
|
937
|
|
|
938
|
|
|
939
|
|
|
940
|
|
|
941
|
|
|
942
|
|
|
943
|
|
|
944
|
|
|
945
|
|
|
946
|
|
|
947
|
10
|
public int Update(string statementName, object parameterObject)
|
948
|
|
{
|
949
|
10
|
bool isSessionLocal = false;
|
950
|
10
|
IDalSession session = _sessionContainer.LocalSession;
|
951
|
10
|
int rows = 0;
|
952
|
|
|
953
|
10
|
if (session == null)
|
954
|
|
{
|
955
|
9
|
session = new SqlMapSession(this.DataSource);
|
956
|
9
|
session.OpenConnection();
|
957
|
9
|
isSessionLocal = true;
|
958
|
|
}
|
959
|
|
|
960
|
10
|
IMappedStatement statement = GetMappedStatement(statementName);
|
961
|
|
|
962
|
10
|
try
|
963
|
|
{
|
964
|
10
|
rows = statement.ExecuteUpdate(session, parameterObject);
|
965
|
|
}
|
966
|
|
catch
|
967
|
|
{
|
968
|
1
|
throw;
|
969
|
|
}
|
970
|
|
finally
|
971
|
|
{
|
972
|
10
|
if ( isSessionLocal )
|
973
|
|
{
|
974
|
9
|
session.CloseConnection();
|
975
|
|
}
|
976
|
|
}
|
977
|
|
|
978
|
|
|
979
|
|
|
980
|
|
|
981
|
|
|
982
|
|
|
983
|
|
|
984
|
|
|
985
|
9
|
return rows;
|
986
|
|
}
|
987
|
|
|
988
|
|
|
989
|
|
|
990
|
|
|
991
|
|
|
992
|
|
|
993
|
|
|
994
|
|
|
995
|
|
|
996
|
3
|
public int Delete(string statementName, object parameterObject)
|
997
|
|
{
|
998
|
3
|
bool isSessionLocal = false;
|
999
|
3
|
IDalSession session = _sessionContainer.LocalSession;
|
1000
|
3
|
int rows = 0;
|
1001
|
|
|
1002
|
3
|
if (session == null)
|
1003
|
|
{
|
1004
|
3
|
session = new SqlMapSession(this.DataSource);
|
1005
|
3
|
session.OpenConnection();
|
1006
|
3
|
isSessionLocal = true;
|
1007
|
|
}
|
1008
|
|
|
1009
|
3
|
IMappedStatement statement = GetMappedStatement(statementName);
|
1010
|
|
|
1011
|
3
|
try
|
1012
|
|
{
|
1013
|
3
|
rows = statement.ExecuteUpdate(session, parameterObject);
|
1014
|
|
}
|
1015
|
|
catch
|
1016
|
|
{
|
1017
|
0
|
throw;
|
1018
|
|
}
|
1019
|
|
finally
|
1020
|
|
{
|
1021
|
3
|
if ( isSessionLocal )
|
1022
|
|
{
|
1023
|
3
|
session.CloseConnection();
|
1024
|
|
}
|
1025
|
|
}
|
1026
|
|
|
1027
|
3
|
return rows;
|
1028
|
|
}
|
1029
|
|
|
1030
|
|
|
1031
|
|
#endregion
|
1032
|
|
|
1033
|
|
#region Get/Add ParemeterMap, ResultMap, MappedStatement, TypeAlias, DataSource, CacheModel
|
1034
|
|
|
1035
|
|
|
1036
|
|
|
1037
|
|
|
1038
|
|
|
1039
|
|
|
1040
|
643
|
public MappedStatement GetMappedStatement(string name)
|
1041
|
|
{
|
1042
|
643
|
if (_mappedStatements.Contains(name) == false)
|
1043
|
|
{
|
1044
|
0
|
throw new DataMapperException("This SQL map does not contain a MappedStatement named " + name);
|
1045
|
|
}
|
1046
|
643
|
return (MappedStatement) _mappedStatements[name];
|
1047
|
|
}
|
1048
|
|
|
1049
|
|
|
1050
|
|
|
1051
|
|
|
1052
|
|
|
1053
|
|
|
1054
|
26520
|
internal void AddMappedStatement(string key, MappedStatement mappedStatement)
|
1055
|
|
{
|
1056
|
26520
|
if (_mappedStatements.Contains(key) == true)
|
1057
|
|
{
|
1058
|
0
|
throw new DataMapperException("This SQL map already contains a MappedStatement named " + mappedStatement.Name);
|
1059
|
|
}
|
1060
|
26520
|
_mappedStatements.Add(key, mappedStatement);
|
1061
|
|
}
|
1062
|
|
|
1063
|
|
|
1064
|
|
|
1065
|
|
|
1066
|
|
internal HybridDictionary MappedStatements
|
1067
|
|
{
|
1068
|
170
|
get { return _mappedStatements; }
|
1069
|
|
}
|
1070
|
|
|
1071
|
|
|
1072
|
|
|
1073
|
|
|
1074
|
|
|
1075
|
|
|
1076
|
|
|
1077
|
4080
|
internal ParameterMap GetParameterMap(string name)
|
1078
|
|
{
|
1079
|
4080
|
if (!_parameterMaps.Contains(name))
|
1080
|
|
{
|
1081
|
0
|
throw new DataMapperException("This SQL map does not contain an ParameterMap named " + name + ". ");
|
1082
|
|
}
|
1083
|
4080
|
return (ParameterMap) _parameterMaps[name];
|
1084
|
|
}
|
1085
|
|
|
1086
|
|
|
1087
|
|
|
1088
|
|
|
1089
|
|
|
1090
|
|
|
1091
|
3400
|
internal void AddParameterMap(ParameterMap parameterMap)
|
1092
|
|
{
|
1093
|
3400
|
if (_parameterMaps.Contains(parameterMap.Id) == true)
|
1094
|
|
{
|
1095
|
0
|
throw new DataMapperException("This SQL map already contains an ParameterMap named " + parameterMap.Id);
|
1096
|
|
}
|
1097
|
3400
|
_parameterMaps.Add(parameterMap.Id, parameterMap);
|
1098
|
|
}
|
1099
|
|
|
1100
|
|
|
1101
|
|
|
1102
|
|
|
1103
|
|
|
1104
|
|
|
1105
|
|
|
1106
|
10710
|
internal ResultMap GetResultMap(string name)
|
1107
|
|
{
|
1108
|
10710
|
if (_resultMaps.Contains(name) == false)
|
1109
|
|
{
|
1110
|
0
|
throw new DataMapperException("This SQL map does not contain an ResultMap named " + name);
|
1111
|
|
}
|
1112
|
10710
|
return (ResultMap) _resultMaps[name];
|
1113
|
|
}
|
1114
|
|
|
1115
|
|
|
1116
|
|
|
1117
|
|
|
1118
|
|
|
1119
|
4930
|
internal void AddResultMap(ResultMap resultMap)
|
1120
|
|
{
|
1121
|
4930
|
if (_resultMaps.Contains(resultMap.Id) == true)
|
1122
|
|
{
|
1123
|
0
|
throw new DataMapperException("This SQL map already contains an ResultMap named " + resultMap.Id);
|
1124
|
|
}
|
1125
|
4930
|
_resultMaps.Add(resultMap.Id, resultMap);
|
1126
|
|
}
|
1127
|
|
|
1128
|
|
|
1129
|
|
|
1130
|
|
|
1131
|
|
internal HybridDictionary ParameterMaps
|
1132
|
|
{
|
1133
|
3910
|
get { return _parameterMaps; }
|
1134
|
|
}
|
1135
|
|
|
1136
|
|
|
1137
|
|
|
1138
|
|
|
1139
|
|
internal HybridDictionary ResultMaps
|
1140
|
|
{
|
1141
|
6290
|
get { return _resultMaps; }
|
1142
|
|
}
|
1143
|
|
|
1144
|
|
|
1145
|
|
|
1146
|
|
|
1147
|
|
|
1148
|
|
public DataSource DataSource
|
1149
|
|
{
|
1150
|
21740
|
get { return _dataSource; }
|
1151
|
170
|
set { _dataSource = value; }
|
1152
|
|
}
|
1153
|
|
|
1154
|
|
|
1155
|
|
|
1156
|
|
|
1157
|
|
|
1158
|
|
|
1159
|
36040
|
internal TypeAlias GetTypeAlias(string name)
|
1160
|
|
{
|
1161
|
36040
|
if (_typeAliasMaps.Contains(name) == true)
|
1162
|
|
{
|
1163
|
16830
|
return (TypeAlias) _typeAliasMaps[name];
|
1164
|
|
}
|
1165
|
|
else
|
1166
|
|
{
|
1167
|
19210
|
return null;
|
1168
|
|
}
|
1169
|
|
}
|
1170
|
|
|
1171
|
|
|
1172
|
|
|
1173
|
|
|
1174
|
|
|
1175
|
|
|
1176
|
2720
|
internal void AddTypeAlias(string key, TypeAlias typeAlias)
|
1177
|
|
{
|
1178
|
2720
|
if (_typeAliasMaps.Contains(key) == true)
|
1179
|
|
{
|
1180
|
0
|
throw new DataMapperException(" Alias name conflict occurred. The type alias '" + key + "' is already mapped to the value '"+typeAlias.ClassName+"'.");
|
1181
|
|
}
|
1182
|
2720
|
_typeAliasMaps.Add(key, typeAlias);
|
1183
|
|
}
|
1184
|
|
|
1185
|
|
|
1186
|
|
|
1187
|
|
|
1188
|
|
|
1189
|
|
|
1190
|
|
|
1191
|
|
|
1192
|
36040
|
internal Type GetType(string className)
|
1193
|
|
{
|
1194
|
36040
|
Type type = null;
|
1195
|
36040
|
TypeAlias typeAlias = this.GetTypeAlias(className) as TypeAlias;
|
1196
|
|
|
1197
|
36040
|
if (typeAlias != null)
|
1198
|
|
{
|
1199
|
16830
|
type = typeAlias.Class;
|
1200
|
|
}
|
1201
|
|
else
|
1202
|
|
{
|
1203
|
19210
|
type = Resources.TypeForName(className);
|
1204
|
|
}
|
1205
|
|
|
1206
|
36040
|
return type;
|
1207
|
|
}
|
1208
|
|
|
1209
|
|
|
1210
|
|
|
1211
|
|
|
1212
|
1
|
public void FlushCaches()
|
1213
|
|
{
|
1214
|
1
|
IDictionaryEnumerator enumerator = _cacheMaps.GetEnumerator();
|
1215
|
2
|
while (enumerator.MoveNext())
|
1216
|
|
{
|
1217
|
1
|
((CacheModel)enumerator.Value).Flush();
|
1218
|
|
}
|
1219
|
|
}
|
1220
|
|
|
1221
|
|
|
1222
|
|
|
1223
|
|
|
1224
|
|
|
1225
|
170
|
internal void AddCache(CacheModel cache)
|
1226
|
|
{
|
1227
|
170
|
if (_cacheMaps.Contains(cache.Id))
|
1228
|
|
{
|
1229
|
0
|
throw new DataMapperException("This SQL map already contains an Cache named " + cache.Id);
|
1230
|
|
}
|
1231
|
170
|
_cacheMaps.Add(cache.Id, cache);
|
1232
|
|
}
|
1233
|
|
|
1234
|
|
|
1235
|
|
|
1236
|
|
|
1237
|
|
|
1238
|
|
|
1239
|
510
|
internal CacheModel GetCache(string name)
|
1240
|
|
{
|
1241
|
510
|
if (!_cacheMaps.Contains(name))
|
1242
|
|
{
|
1243
|
0
|
throw new DataMapperException("This SQL map does not contain an Cache named " + name);
|
1244
|
|
}
|
1245
|
510
|
return (CacheModel) _cacheMaps[name];
|
1246
|
|
}
|
1247
|
|
|
1248
|
|
|
1249
|
|
|
1250
|
|
|
1251
|
|
|
1252
|
0
|
public string GetDataCacheStats()
|
1253
|
|
{
|
1254
|
|
StringBuilder buffer = new StringBuilder("\nCache Data Statistiques");
|
1255
|
|
buffer.Append("\n=======================\n");
|
1256
|
|
IDictionaryEnumerator enumerator = _mappedStatements.GetEnumerator();
|
1257
|
|
while (enumerator.MoveNext())
|
1258
|
|
{
|
1259
|
|
MappedStatement statement = (MappedStatement) enumerator.Value;
|
1260
|
|
buffer.Append(statement.Name);
|
1261
|
|
buffer.Append(": ");
|
1262
|
|
double hitRatio = statement.GetDataCacheHitRatio();
|
1263
|
|
if (hitRatio != -1)
|
1264
|
|
{
|
1265
|
|
buffer.Append(Math.Round(hitRatio * 100));
|
1266
|
|
buffer.Append("%");
|
1267
|
|
}
|
1268
|
|
else
|
1269
|
|
{
|
1270
|
|
buffer.Append("No Cache.");
|
1271
|
|
}
|
1272
|
|
buffer.Append("\n");
|
1273
|
|
}
|
1274
|
|
return buffer.ToString();
|
1275
|
|
}
|
1276
|
|
|
1277
|
|
#endregion
|
1278
|
|
|
1279
|
|
#endregion
|
1280
|
|
}
|
1281
|
|
}
|
1282
|
|
|