@@ -86,7 +86,7 @@ public virtual TEntity QueryByKey<TEntity>(object key, string table, bool tracki
86
86
}
87
87
public virtual TEntity QueryByKey < TEntity > ( object key , string table , LockType @lock , bool tracking = false )
88
88
{
89
- Expression < Func < TEntity , bool > > condition = BuildCondition < TEntity > ( key ) ;
89
+ Expression < Func < TEntity , bool > > condition = PrimaryKeyHelper . BuildCondition < TEntity > ( key ) ;
90
90
var q = this . Query < TEntity > ( table , @lock ) . Where ( condition ) ;
91
91
92
92
if ( tracking )
@@ -173,7 +173,7 @@ public virtual TEntity Insert<TEntity>(TEntity entity, string table)
173
173
174
174
TypeDescriptor typeDescriptor = EntityTypeContainer . GetDescriptor ( typeof ( TEntity ) ) ;
175
175
176
- Dictionary < PropertyDescriptor , object > keyValueMap = CreateKeyValueMap ( typeDescriptor ) ;
176
+ Dictionary < PropertyDescriptor , object > keyValueMap = PrimaryKeyHelper . CreateKeyValueMap ( typeDescriptor ) ;
177
177
178
178
Dictionary < PropertyDescriptor , DbExpression > insertColumns = new Dictionary < PropertyDescriptor , DbExpression > ( ) ;
179
179
foreach ( PropertyDescriptor propertyDescriptor in typeDescriptor . PropertyDescriptors )
@@ -210,7 +210,7 @@ public virtual TEntity Insert<TEntity>(TEntity entity, string table)
210
210
PropertyDescriptor autoIncrementPropertyDescriptor = typeDescriptor . AutoIncrement ;
211
211
if ( autoIncrementPropertyDescriptor == null )
212
212
{
213
- this . ExecuteSqlCommand ( e ) ;
213
+ this . ExecuteNonQuery ( e ) ;
214
214
return entity ;
215
215
}
216
216
@@ -298,7 +298,7 @@ public virtual object Insert<TEntity>(Expression<Func<TEntity>> content, string
298
298
299
299
if ( keyPropertyDescriptor == null || ! keyPropertyDescriptor . IsAutoIncrement )
300
300
{
301
- this . ExecuteSqlCommand ( e ) ;
301
+ this . ExecuteNonQuery ( e ) ;
302
302
return keyVal ; /* It will return null if an entity does not define primary key. */
303
303
}
304
304
@@ -334,7 +334,7 @@ public virtual int Update<TEntity>(TEntity entity, string table)
334
334
TypeDescriptor typeDescriptor = EntityTypeContainer . GetDescriptor ( typeof ( TEntity ) ) ;
335
335
PublicHelper . EnsureHasPrimaryKey ( typeDescriptor ) ;
336
336
337
- Dictionary < PropertyDescriptor , object > keyValueMap = CreateKeyValueMap ( typeDescriptor ) ;
337
+ Dictionary < PropertyDescriptor , object > keyValueMap = PrimaryKeyHelper . CreateKeyValueMap ( typeDescriptor ) ;
338
338
339
339
IEntityState entityState = this . TryGetTrackedEntityState ( entity ) ;
340
340
Dictionary < PropertyDescriptor , DbExpression > updateColumns = new Dictionary < PropertyDescriptor , DbExpression > ( ) ;
@@ -362,15 +362,15 @@ public virtual int Update<TEntity>(TEntity entity, string table)
362
362
return 0 ;
363
363
364
364
DbTable dbTable = table == null ? typeDescriptor . Table : new DbTable ( table , typeDescriptor . Table . Schema ) ;
365
- DbExpression conditionExp = MakeCondition ( keyValueMap , dbTable ) ;
365
+ DbExpression conditionExp = PrimaryKeyHelper . MakeCondition ( keyValueMap , dbTable ) ;
366
366
DbUpdateExpression e = new DbUpdateExpression ( dbTable , conditionExp ) ;
367
367
368
368
foreach ( var item in updateColumns )
369
369
{
370
370
e . UpdateColumns . Add ( item . Key . Column , item . Value ) ;
371
371
}
372
372
373
- int ret = this . ExecuteSqlCommand ( e ) ;
373
+ int ret = this . ExecuteNonQuery ( e ) ;
374
374
if ( entityState != null )
375
375
entityState . Refresh ( ) ;
376
376
return ret ;
@@ -417,7 +417,7 @@ public virtual int Update<TEntity>(Expression<Func<TEntity, bool>> condition, Ex
417
417
if ( e . UpdateColumns . Count == 0 )
418
418
return 0 ;
419
419
420
- return this . ExecuteSqlCommand ( e ) ;
420
+ return this . ExecuteNonQuery ( e ) ;
421
421
}
422
422
423
423
public virtual int Delete < TEntity > ( TEntity entity )
@@ -426,12 +426,12 @@ public virtual int Delete<TEntity>(TEntity entity)
426
426
}
427
427
public virtual int Delete < TEntity > ( TEntity entity , string table )
428
428
{
429
- Utils . CheckNull ( entity ) ;
429
+ PublicHelper . CheckNull ( entity ) ;
430
430
431
431
TypeDescriptor typeDescriptor = EntityTypeContainer . GetDescriptor ( typeof ( TEntity ) ) ;
432
432
PublicHelper . EnsureHasPrimaryKey ( typeDescriptor ) ;
433
433
434
- Dictionary < PropertyDescriptor , object > keyValueMap = new Dictionary < PropertyDescriptor , object > ( ) ;
434
+ Dictionary < PropertyDescriptor , object > keyValueMap = new Dictionary < PropertyDescriptor , object > ( typeDescriptor . PrimaryKeys . Count ) ;
435
435
436
436
foreach ( PropertyDescriptor keyPropertyDescriptor in typeDescriptor . PrimaryKeys )
437
437
{
@@ -440,9 +440,9 @@ public virtual int Delete<TEntity>(TEntity entity, string table)
440
440
}
441
441
442
442
DbTable dbTable = table == null ? typeDescriptor . Table : new DbTable ( table , typeDescriptor . Table . Schema ) ;
443
- DbExpression conditionExp = MakeCondition ( keyValueMap , dbTable ) ;
443
+ DbExpression conditionExp = PrimaryKeyHelper . MakeCondition ( keyValueMap , dbTable ) ;
444
444
DbDeleteExpression e = new DbDeleteExpression ( dbTable , conditionExp ) ;
445
- return this . ExecuteSqlCommand ( e ) ;
445
+ return this . ExecuteNonQuery ( e ) ;
446
446
}
447
447
public virtual int Delete < TEntity > ( Expression < Func < TEntity , bool > > condition )
448
448
{
@@ -462,15 +462,15 @@ public virtual int Delete<TEntity>(Expression<Func<TEntity, bool>> condition, st
462
462
463
463
DbDeleteExpression e = new DbDeleteExpression ( explicitDbTable ?? typeDescriptor . Table , conditionExp ) ;
464
464
465
- return this . ExecuteSqlCommand ( e ) ;
465
+ return this . ExecuteNonQuery ( e ) ;
466
466
}
467
467
public virtual int DeleteByKey < TEntity > ( object key )
468
468
{
469
469
return this . DeleteByKey < TEntity > ( key , null ) ;
470
470
}
471
471
public virtual int DeleteByKey < TEntity > ( object key , string table )
472
472
{
473
- Expression < Func < TEntity , bool > > condition = BuildCondition < TEntity > ( key ) ;
473
+ Expression < Func < TEntity , bool > > condition = PrimaryKeyHelper . BuildCondition < TEntity > ( key ) ;
474
474
return this . Delete < TEntity > ( condition , table ) ;
475
475
}
476
476
@@ -558,6 +558,28 @@ protected virtual IEntityState TryGetTrackedEntityState(object entity)
558
558
return ret ;
559
559
}
560
560
561
+ protected int ExecuteNonQuery ( DbExpression e )
562
+ {
563
+ List < DbParam > parameters ;
564
+ return this . ExecuteNonQuery ( e , out parameters ) ;
565
+ }
566
+ protected int ExecuteNonQuery ( DbExpression e , out List < DbParam > parameters )
567
+ {
568
+ IDbExpressionTranslator translator = this . DatabaseProvider . CreateDbExpressionTranslator ( ) ;
569
+ string cmdText = translator . Translate ( e , out parameters ) ;
570
+
571
+ int rowsAffected = this . Session . ExecuteNonQuery ( cmdText , parameters . ToArray ( ) ) ;
572
+ return rowsAffected ;
573
+ }
574
+ protected IDataReader ExecuteReader ( DbExpression e )
575
+ {
576
+ IDbExpressionTranslator translator = this . DatabaseProvider . CreateDbExpressionTranslator ( ) ;
577
+ List < DbParam > parameters ;
578
+ string cmdText = translator . Translate ( e , out parameters ) ;
579
+ IDataReader dataReader = this . Session . ExecuteReader ( cmdText , parameters . ToArray ( ) ) ;
580
+ return dataReader ;
581
+ }
582
+
561
583
public void Dispose ( )
562
584
{
563
585
if ( this . _disposed )
@@ -580,17 +602,6 @@ void CheckDisposed()
580
602
}
581
603
}
582
604
583
-
584
- int ExecuteSqlCommand ( DbExpression e )
585
- {
586
- IDbExpressionTranslator translator = this . DatabaseProvider . CreateDbExpressionTranslator ( ) ;
587
- List < DbParam > parameters ;
588
- string cmdText = translator . Translate ( e , out parameters ) ;
589
-
590
- int r = this . AdoSession . ExecuteNonQuery ( cmdText , parameters . ToArray ( ) , CommandType . Text ) ;
591
- return r ;
592
- }
593
-
594
605
class TrackEntityCollection
595
606
{
596
607
public TrackEntityCollection ( TypeDescriptor typeDescriptor )
0 commit comments