Skip to content

Commit 419a90f

Browse files
author
shuxinqin
committed
fix code.
1 parent 4bce857 commit 419a90f

12 files changed

+178
-217
lines changed

src/Chloe.MySql/MySqlContext.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public virtual int Update<TEntity>(Expression<Func<TEntity, bool>> condition, Ex
224224
if (e.UpdateColumns.Count == 0)
225225
return 0;
226226

227-
return this.ExecuteSqlCommand(e);
227+
return this.ExecuteNonQuery(e);
228228
}
229229

230230
public virtual int Delete<TEntity>(Expression<Func<TEntity, bool>> condition, int limits)
@@ -246,17 +246,7 @@ public virtual int Delete<TEntity>(Expression<Func<TEntity, bool>> condition, st
246246
MySqlDbDeleteExpression e = new MySqlDbDeleteExpression(explicitDbTable ?? typeDescriptor.Table, conditionExp);
247247
e.Limits = limits;
248248

249-
return this.ExecuteSqlCommand(e);
250-
}
251-
252-
int ExecuteSqlCommand(DbExpression e)
253-
{
254-
IDbExpressionTranslator translator = this.DatabaseProvider.CreateDbExpressionTranslator();
255-
List<DbParam> parameters;
256-
string cmdText = translator.Translate(e, out parameters);
257-
258-
int r = this.Session.ExecuteNonQuery(cmdText, CommandType.Text, parameters.ToArray());
259-
return r;
249+
return this.ExecuteNonQuery(e);
260250
}
261251
}
262252
}

src/Chloe.Oracle/OracleContext.cs

+6-20
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public override TEntity Insert<TEntity>(TEntity entity, string table)
7171
e.Returns.AddRange(outputColumns.Select(a => a.Column));
7272

7373
List<DbParam> parameters;
74-
this.ExecuteSqlCommand(e, out parameters);
74+
this.ExecuteNonQuery(e, out parameters);
7575

7676
List<DbParam> outputParams = parameters.Where(a => a.Direction == ParamDirection.Output).ToList();
7777

@@ -154,7 +154,7 @@ public override object Insert<TEntity>(Expression<Func<TEntity>> content, string
154154
}
155155

156156
List<DbParam> parameters;
157-
this.ExecuteSqlCommand(insertExp, out parameters);
157+
this.ExecuteNonQuery(insertExp, out parameters);
158158

159159
if (keyPropertyDescriptor != null && keyPropertyDescriptor.HasSequence())
160160
{
@@ -307,7 +307,7 @@ public override int Update<TEntity>(TEntity entity, string table)
307307
TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity));
308308
PublicHelper.EnsureHasPrimaryKey(typeDescriptor);
309309

310-
Dictionary<PropertyDescriptor, object> keyValueMap = CreateKeyValueMap(typeDescriptor);
310+
Dictionary<PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor);
311311

312312
IEntityState entityState = this.TryGetTrackedEntityState(entity);
313313
Dictionary<PropertyDescriptor, DbExpression> updateColumns = new Dictionary<PropertyDescriptor, DbExpression>();
@@ -336,15 +336,15 @@ public override int Update<TEntity>(TEntity entity, string table)
336336
return 0;
337337

338338
DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema);
339-
DbExpression conditionExp = MakeCondition(keyValueMap, dbTable);
339+
DbExpression conditionExp = PrimaryKeyHelper.MakeCondition(keyValueMap, dbTable);
340340
DbUpdateExpression e = new DbUpdateExpression(dbTable, conditionExp);
341341

342342
foreach (var item in updateColumns)
343343
{
344344
e.UpdateColumns.Add(item.Key.Column, item.Value);
345345
}
346346

347-
int ret = this.ExecuteSqlCommand(e);
347+
int ret = this.ExecuteNonQuery(e);
348348
if (entityState != null)
349349
entityState.Refresh();
350350
return ret;
@@ -388,21 +388,7 @@ public override int Update<TEntity>(Expression<Func<TEntity, bool>> condition, E
388388
if (e.UpdateColumns.Count == 0)
389389
return 0;
390390

391-
return this.ExecuteSqlCommand(e);
392-
}
393-
394-
int ExecuteSqlCommand(DbExpression e)
395-
{
396-
List<DbParam> parameters;
397-
return this.ExecuteSqlCommand(e, out parameters);
398-
}
399-
int ExecuteSqlCommand(DbExpression e, out List<DbParam> parameters)
400-
{
401-
IDbExpressionTranslator translator = this.DatabaseProvider.CreateDbExpressionTranslator();
402-
string cmdText = translator.Translate(e, out parameters);
403-
404-
int r = this.Session.ExecuteNonQuery(cmdText, parameters.ToArray());
405-
return r;
391+
return this.ExecuteNonQuery(e);
406392
}
407393

408394
string AppendInsertRangeSqlTemplate(DbTable table, List<PropertyDescriptor> mappingPropertyDescriptors, bool keepIdentity)

src/Chloe.Oracle/OracleContext_Helper.cs

-29
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,6 @@ namespace Chloe.Oracle
1818
{
1919
public partial class OracleContext : DbContext
2020
{
21-
static Dictionary<PropertyDescriptor, object> CreateKeyValueMap(TypeDescriptor typeDescriptor)
22-
{
23-
Dictionary<PropertyDescriptor, object> keyValueMap = new Dictionary<PropertyDescriptor, object>(typeDescriptor.PrimaryKeys.Count);
24-
foreach (PropertyDescriptor keyPropertyDescriptor in typeDescriptor.PrimaryKeys)
25-
{
26-
keyValueMap.Add(keyPropertyDescriptor, null);
27-
}
28-
29-
return keyValueMap;
30-
}
31-
static DbExpression MakeCondition(Dictionary<PropertyDescriptor, object> keyValueMap, DbTable dbTable)
32-
{
33-
DbExpression conditionExp = null;
34-
foreach (var kv in keyValueMap)
35-
{
36-
PropertyDescriptor keyPropertyDescriptor = kv.Key;
37-
object keyVal = kv.Value;
38-
39-
if (keyVal == null)
40-
throw new ArgumentException(string.Format("The primary key '{0}' could not be null.", keyPropertyDescriptor.Property.Name));
41-
42-
DbExpression left = new DbColumnAccessExpression(dbTable, keyPropertyDescriptor.Column);
43-
DbExpression right = DbExpression.Parameter(keyVal, keyPropertyDescriptor.PropertyType, keyPropertyDescriptor.Column.DbType);
44-
DbExpression equalExp = new DbEqualExpression(left, right);
45-
conditionExp = conditionExp == null ? equalExp : DbExpression.And(conditionExp, equalExp);
46-
}
47-
48-
return conditionExp;
49-
}
5021

5122
}
5223
}

src/Chloe.PostgreSQL/PostgreSQLContext.cs

+4-14
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override TEntity Insert<TEntity>(TEntity entity, string table)
4242

4343
TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity));
4444

45-
Dictionary<PropertyDescriptor, object> keyValueMap = CreateKeyValueMap(typeDescriptor);
45+
Dictionary<PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor);
4646

4747
Dictionary<PropertyDescriptor, DbExpression> insertColumns = new Dictionary<PropertyDescriptor, DbExpression>();
4848
foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.PropertyDescriptors)
@@ -92,7 +92,7 @@ public override TEntity Insert<TEntity>(TEntity entity, string table)
9292

9393
if (mappers.Count == 0)
9494
{
95-
this.ExecuteSqlCommand(insertExp);
95+
this.ExecuteNonQuery(insertExp);
9696
return entity;
9797
}
9898

@@ -183,12 +183,12 @@ public override object Insert<TEntity>(Expression<Func<TEntity>> content, string
183183

184184
if (keyPropertyDescriptor == null)
185185
{
186-
this.ExecuteSqlCommand(insertExp);
186+
this.ExecuteNonQuery(insertExp);
187187
return keyVal; /* It will return null if an entity does not define primary key. */
188188
}
189189
if (!keyPropertyDescriptor.IsAutoIncrement && !keyPropertyDescriptor.HasSequence())
190190
{
191-
this.ExecuteSqlCommand(insertExp);
191+
this.ExecuteNonQuery(insertExp);
192192
return keyVal;
193193
}
194194

@@ -335,15 +335,5 @@ protected override string GetSelectLastInsertIdClause()
335335
{
336336
throw new NotSupportedException();
337337
}
338-
339-
int ExecuteSqlCommand(DbExpression e)
340-
{
341-
IDbExpressionTranslator translator = this.DatabaseProvider.CreateDbExpressionTranslator();
342-
List<DbParam> parameters;
343-
string cmdText = translator.Translate(e, out parameters);
344-
345-
int r = this.Session.ExecuteNonQuery(cmdText, CommandType.Text, parameters.ToArray());
346-
return r;
347-
}
348338
}
349339
}

src/Chloe.PostgreSQL/PostgreSQLContext_Helper.cs

-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ static Action<TEntity, IDataReader> GetMapper<TEntity>(PropertyDescriptor proper
3333

3434
return mapper;
3535
}
36-
static Dictionary<PropertyDescriptor, object> CreateKeyValueMap(TypeDescriptor typeDescriptor)
37-
{
38-
Dictionary<PropertyDescriptor, object> keyValueMap = new Dictionary<PropertyDescriptor, object>(typeDescriptor.PrimaryKeys.Count);
39-
foreach (PropertyDescriptor keyPropertyDescriptor in typeDescriptor.PrimaryKeys)
40-
{
41-
keyValueMap.Add(keyPropertyDescriptor, null);
42-
}
43-
44-
return keyValueMap;
45-
}
4636

4737
string AppendInsertRangeSqlTemplate(DbTable table, List<PropertyDescriptor> mappingPropertyDescriptors)
4838
{

src/Chloe/Core/InternalAdoSession.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static string GetTypeName(Type type)
523523

524524
static ChloeException WrapException(Exception ex)
525525
{
526-
return new ChloeException("An exception occurred while executing DbCommand. For details please see the inner exception.", ex);
526+
return new ChloeException($"An exception occurred while executing DbCommand. For details please see the inner exception. {ex.Message}", ex);
527527
}
528528
}
529529
}

src/Chloe/DbContext.cs

+36-25
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public virtual TEntity QueryByKey<TEntity>(object key, string table, bool tracki
8686
}
8787
public virtual TEntity QueryByKey<TEntity>(object key, string table, LockType @lock, bool tracking = false)
8888
{
89-
Expression<Func<TEntity, bool>> condition = BuildCondition<TEntity>(key);
89+
Expression<Func<TEntity, bool>> condition = PrimaryKeyHelper.BuildCondition<TEntity>(key);
9090
var q = this.Query<TEntity>(table, @lock).Where(condition);
9191

9292
if (tracking)
@@ -173,7 +173,7 @@ public virtual TEntity Insert<TEntity>(TEntity entity, string table)
173173

174174
TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity));
175175

176-
Dictionary<PropertyDescriptor, object> keyValueMap = CreateKeyValueMap(typeDescriptor);
176+
Dictionary<PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor);
177177

178178
Dictionary<PropertyDescriptor, DbExpression> insertColumns = new Dictionary<PropertyDescriptor, DbExpression>();
179179
foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.PropertyDescriptors)
@@ -210,7 +210,7 @@ public virtual TEntity Insert<TEntity>(TEntity entity, string table)
210210
PropertyDescriptor autoIncrementPropertyDescriptor = typeDescriptor.AutoIncrement;
211211
if (autoIncrementPropertyDescriptor == null)
212212
{
213-
this.ExecuteSqlCommand(e);
213+
this.ExecuteNonQuery(e);
214214
return entity;
215215
}
216216

@@ -298,7 +298,7 @@ public virtual object Insert<TEntity>(Expression<Func<TEntity>> content, string
298298

299299
if (keyPropertyDescriptor == null || !keyPropertyDescriptor.IsAutoIncrement)
300300
{
301-
this.ExecuteSqlCommand(e);
301+
this.ExecuteNonQuery(e);
302302
return keyVal; /* It will return null if an entity does not define primary key. */
303303
}
304304

@@ -334,7 +334,7 @@ public virtual int Update<TEntity>(TEntity entity, string table)
334334
TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity));
335335
PublicHelper.EnsureHasPrimaryKey(typeDescriptor);
336336

337-
Dictionary<PropertyDescriptor, object> keyValueMap = CreateKeyValueMap(typeDescriptor);
337+
Dictionary<PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor);
338338

339339
IEntityState entityState = this.TryGetTrackedEntityState(entity);
340340
Dictionary<PropertyDescriptor, DbExpression> updateColumns = new Dictionary<PropertyDescriptor, DbExpression>();
@@ -362,15 +362,15 @@ public virtual int Update<TEntity>(TEntity entity, string table)
362362
return 0;
363363

364364
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);
366366
DbUpdateExpression e = new DbUpdateExpression(dbTable, conditionExp);
367367

368368
foreach (var item in updateColumns)
369369
{
370370
e.UpdateColumns.Add(item.Key.Column, item.Value);
371371
}
372372

373-
int ret = this.ExecuteSqlCommand(e);
373+
int ret = this.ExecuteNonQuery(e);
374374
if (entityState != null)
375375
entityState.Refresh();
376376
return ret;
@@ -417,7 +417,7 @@ public virtual int Update<TEntity>(Expression<Func<TEntity, bool>> condition, Ex
417417
if (e.UpdateColumns.Count == 0)
418418
return 0;
419419

420-
return this.ExecuteSqlCommand(e);
420+
return this.ExecuteNonQuery(e);
421421
}
422422

423423
public virtual int Delete<TEntity>(TEntity entity)
@@ -426,12 +426,12 @@ public virtual int Delete<TEntity>(TEntity entity)
426426
}
427427
public virtual int Delete<TEntity>(TEntity entity, string table)
428428
{
429-
Utils.CheckNull(entity);
429+
PublicHelper.CheckNull(entity);
430430

431431
TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity));
432432
PublicHelper.EnsureHasPrimaryKey(typeDescriptor);
433433

434-
Dictionary<PropertyDescriptor, object> keyValueMap = new Dictionary<PropertyDescriptor, object>();
434+
Dictionary<PropertyDescriptor, object> keyValueMap = new Dictionary<PropertyDescriptor, object>(typeDescriptor.PrimaryKeys.Count);
435435

436436
foreach (PropertyDescriptor keyPropertyDescriptor in typeDescriptor.PrimaryKeys)
437437
{
@@ -440,9 +440,9 @@ public virtual int Delete<TEntity>(TEntity entity, string table)
440440
}
441441

442442
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);
444444
DbDeleteExpression e = new DbDeleteExpression(dbTable, conditionExp);
445-
return this.ExecuteSqlCommand(e);
445+
return this.ExecuteNonQuery(e);
446446
}
447447
public virtual int Delete<TEntity>(Expression<Func<TEntity, bool>> condition)
448448
{
@@ -462,15 +462,15 @@ public virtual int Delete<TEntity>(Expression<Func<TEntity, bool>> condition, st
462462

463463
DbDeleteExpression e = new DbDeleteExpression(explicitDbTable ?? typeDescriptor.Table, conditionExp);
464464

465-
return this.ExecuteSqlCommand(e);
465+
return this.ExecuteNonQuery(e);
466466
}
467467
public virtual int DeleteByKey<TEntity>(object key)
468468
{
469469
return this.DeleteByKey<TEntity>(key, null);
470470
}
471471
public virtual int DeleteByKey<TEntity>(object key, string table)
472472
{
473-
Expression<Func<TEntity, bool>> condition = BuildCondition<TEntity>(key);
473+
Expression<Func<TEntity, bool>> condition = PrimaryKeyHelper.BuildCondition<TEntity>(key);
474474
return this.Delete<TEntity>(condition, table);
475475
}
476476

@@ -558,6 +558,28 @@ protected virtual IEntityState TryGetTrackedEntityState(object entity)
558558
return ret;
559559
}
560560

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+
561583
public void Dispose()
562584
{
563585
if (this._disposed)
@@ -580,17 +602,6 @@ void CheckDisposed()
580602
}
581603
}
582604

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-
594605
class TrackEntityCollection
595606
{
596607
public TrackEntityCollection(TypeDescriptor typeDescriptor)

0 commit comments

Comments
 (0)