Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 0b2523f

Browse files
committed
Add InitQueryParam/InitUpdateParam to Dialect provder
1 parent 12cdd5e commit 0b2523f

File tree

10 files changed

+50
-22
lines changed

10 files changed

+50
-22
lines changed

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public override void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithP
269269
sqlFilter
270270
.Append(GetQuotedColumnName(fieldDef.FieldName))
271271
.Append("=")
272-
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
272+
.Append(this.AddQueryParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
273273

274274
continue;
275275
}
@@ -283,7 +283,7 @@ public override void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithP
283283
sql
284284
.Append(GetQuotedColumnName(fieldDef.FieldName))
285285
.Append("=")
286-
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
286+
.Append(this.AddUpdateParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
287287
}
288288

289289
var strFilter = StringBuilderCacheAlt.ReturnAndFree(sqlFilter);

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public override void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithP
448448
sqlFilter
449449
.Append(GetQuotedColumnName(fieldDef.FieldName))
450450
.Append("=")
451-
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
451+
.Append(this.AddQueryParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
452452

453453
continue;
454454
}
@@ -462,7 +462,7 @@ public override void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithP
462462
sql
463463
.Append(GetQuotedColumnName(fieldDef.FieldName))
464464
.Append("=")
465-
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
465+
.Append(this.AddUpdateParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
466466
}
467467

468468
var strFilter = StringBuilderCacheAlt.ReturnAndFree(sqlFilter);

src/ServiceStack.OrmLite.SqlServer/SqlServerExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ internal static void PrepareSqlServerUpdateStatement<T>(IDbCommand dbCmd, SqlExp
9595
if (setFields.Length > 0)
9696
setFields.Append(", ");
9797

98-
var param = dialectProvider.AddParam(dbCmd, value, fieldDef);
98+
var param = dialectProvider.AddUpdateParam(dbCmd, value, fieldDef);
9999
setFields
100100
.Append(dialectProvider.GetQuotedColumnName(fieldDef.FieldName))
101101
.Append("=")

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public override void PrepareInsertRowStatement<T>(IDbCommand dbCmd, Dictionary<s
438438
try
439439
{
440440
sbColumnNames.Append(GetQuotedColumnName(fieldDef.FieldName));
441-
sbColumnValues.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
441+
sbColumnValues.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
442442
}
443443
catch (Exception ex)
444444
{

src/ServiceStack.OrmLite.VistaDB/VistaDBExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override void PrepareUpdateStatement(IDbCommand dbCmd, T item, bool exclu
3131
if (setFields.Length > 0)
3232
setFields.Append(", ");
3333

34-
var param = DialectProvider.AddParam(dbCmd, value, fieldDef);
34+
var param = DialectProvider.AddUpdateParam(dbCmd, value, fieldDef);
3535
setFields
3636
.Append(DialectProvider.GetQuotedColumnName(fieldDef.FieldName))
3737
.Append("=")

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ public virtual IDbDataParameter AddParam(object value)
11321132
var paramValue = value;
11331133

11341134
var parameter = CreateParam(paramName, paramValue);
1135+
DialectProvider.InitQueryParam(parameter);
11351136
Params.Add(parameter);
11361137
return parameter;
11371138
}
@@ -1209,7 +1210,7 @@ public virtual void PrepareUpdateStatement(IDbCommand dbCmd, T item, bool exclud
12091210
setFields
12101211
.Append(DialectProvider.GetQuotedColumnName(fieldDef.FieldName))
12111212
.Append("=")
1212-
.Append(DialectProvider.AddParam(dbCmd, value, fieldDef).ParameterName);
1213+
.Append(DialectProvider.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
12131214
}
12141215

12151216
if (setFields.Length == 0)
@@ -1248,7 +1249,7 @@ public virtual void PrepareUpdateStatement(IDbCommand dbCmd, Dictionary<string,
12481249
setFields
12491250
.Append(DialectProvider.GetQuotedColumnName(fieldDef.FieldName))
12501251
.Append("=")
1251-
.Append(DialectProvider.AddParam(dbCmd, value, fieldDef).ParameterName);
1252+
.Append(DialectProvider.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
12521253
}
12531254

12541255
if (setFields.Length == 0)
@@ -2994,13 +2995,25 @@ public static IDbDataParameter CreateParam(this IOrmLiteDialectProvider dialectP
29942995
return to;
29952996
}
29962997

2998+
public static IDbDataParameter AddQueryParam(this IOrmLiteDialectProvider dialectProvider,
2999+
IDbCommand dbCmd,
3000+
object value,
3001+
FieldDefinition fieldDef) => dialectProvider.AddParam(dbCmd, value, fieldDef, paramFilter: dialectProvider.InitQueryParam);
3002+
3003+
public static IDbDataParameter AddUpdateParam(this IOrmLiteDialectProvider dialectProvider,
3004+
IDbCommand dbCmd,
3005+
object value,
3006+
FieldDefinition fieldDef) => dialectProvider.AddParam(dbCmd, value, fieldDef, paramFilter: dialectProvider.InitUpdateParam);
3007+
29973008
public static IDbDataParameter AddParam(this IOrmLiteDialectProvider dialectProvider,
29983009
IDbCommand dbCmd,
29993010
object value,
3000-
FieldDefinition fieldDef)
3011+
FieldDefinition fieldDef, Action<IDbDataParameter> paramFilter)
30013012
{
30023013
var paramName = dbCmd.Parameters.Count.ToString();
30033014
var parameter = dialectProvider.CreateParam(paramName, value, fieldDef?.ColumnType);
3015+
3016+
paramFilter?.Invoke(parameter);
30043017

30053018
if (fieldDef != null)
30063019
dialectProvider.SetParameter(fieldDef, parameter);

src/ServiceStack.OrmLite/Expressions/WriteExpressionCommandExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ internal static void PrepareUpdateAnonSql<T>(this IDbCommand dbCmd, IOrmLiteDial
206206
var value = setField.CreateGetter()(updateOnly);
207207
if (string.IsNullOrEmpty(whereSql) && (fieldDef.IsPrimaryKey || fieldDef.AutoIncrement))
208208
{
209-
whereSql = $"WHERE {dialectProvider.GetQuotedColumnName(fieldDef.FieldName)} = {dialectProvider.AddParam(dbCmd, value, fieldDef).ParameterName}";
209+
whereSql = $"WHERE {dialectProvider.GetQuotedColumnName(fieldDef.FieldName)} = {dialectProvider.AddQueryParam(dbCmd, value, fieldDef).ParameterName}";
210210
continue;
211211
}
212212

@@ -216,7 +216,7 @@ internal static void PrepareUpdateAnonSql<T>(this IDbCommand dbCmd, IOrmLiteDial
216216
sql
217217
.Append(dialectProvider.GetQuotedColumnName(fieldDef.FieldName))
218218
.Append("=")
219-
.Append(dialectProvider.AddParam(dbCmd, value, fieldDef).ParameterName);
219+
.Append(dialectProvider.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
220220
}
221221

222222
dbCmd.CommandText = $"UPDATE {dialectProvider.GetQuotedTableName(modelDef)} " +

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public interface IOrmLiteDialectProvider
7474

7575
object GetParamValue(object value, Type fieldType);
7676

77+
// Customize DB Parameters in SELECT or WHERE queries
78+
void InitQueryParam(IDbDataParameter param);
79+
80+
// Customize UPDATE or INSERT DB Parameters
81+
void InitUpdateParam(IDbDataParameter param);
82+
7783
object ToDbValue(object value, Type type);
7884

7985
object FromDbValue(object value, Type type);

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ public virtual void PrepareInsertRowStatement<T>(IDbCommand dbCmd, Dictionary<st
687687
try
688688
{
689689
sbColumnNames.Append(GetQuotedColumnName(fieldDef.FieldName));
690-
sbColumnValues.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
690+
sbColumnValues.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
691691
}
692692
catch (Exception ex)
693693
{
@@ -826,7 +826,7 @@ public virtual bool PrepareParameterizedDeleteStatement<T>(IDbCommand cmd, IDict
826826

827827
var sqlFilter = StringBuilderCache.Allocate();
828828
var modelDef = typeof(T).GetModelDefinition();
829-
var hadRowVesion = false;
829+
var hadRowVersion = false;
830830

831831
cmd.Parameters.Clear();
832832

@@ -839,7 +839,7 @@ public virtual bool PrepareParameterizedDeleteStatement<T>(IDbCommand cmd, IDict
839839
continue;
840840

841841
if (fieldDef.IsRowVersion)
842-
hadRowVesion = true;
842+
hadRowVersion = true;
843843

844844
try
845845
{
@@ -863,7 +863,7 @@ public virtual bool PrepareParameterizedDeleteStatement<T>(IDbCommand cmd, IDict
863863

864864
cmd.CommandText = $"DELETE FROM {GetQuotedTableName(modelDef)} WHERE {StringBuilderCache.ReturnAndFree(sqlFilter)}";
865865

866-
return hadRowVesion;
866+
return hadRowVersion;
867867
}
868868

869869
public virtual void PrepareStoredProcedureStatement<T>(IDbCommand cmd, T obj)
@@ -872,10 +872,14 @@ public virtual void PrepareStoredProcedureStatement<T>(IDbCommand cmd, T obj)
872872
cmd.CommandType = CommandType.StoredProcedure;
873873
}
874874

875+
/// <summary>
876+
/// Used for adding updated DB params in INSERT and UPDATE statements
877+
/// </summary>
875878
protected IDbDataParameter AddParameter(IDbCommand cmd, FieldDefinition fieldDef)
876879
{
877880
var p = cmd.CreateParameter();
878881
SetParameter(fieldDef, p);
882+
InitUpdateParam(p);
879883
cmd.Parameters.Add(p);
880884
return p;
881885
}
@@ -1043,7 +1047,7 @@ public virtual void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithPr
10431047
sqlFilter
10441048
.Append(GetQuotedColumnName(fieldDef.FieldName))
10451049
.Append("=")
1046-
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
1050+
.Append(this.AddQueryParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
10471051

10481052
continue;
10491053
}
@@ -1057,7 +1061,7 @@ public virtual void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithPr
10571061
sql
10581062
.Append(GetQuotedColumnName(fieldDef.FieldName))
10591063
.Append("=")
1060-
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
1064+
.Append(this.AddUpdateParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
10611065
}
10621066
catch (Exception ex)
10631067
{
@@ -1094,7 +1098,7 @@ public virtual void PrepareUpdateRowStatement<T>(IDbCommand dbCmd, Dictionary<st
10941098
sql
10951099
.Append(GetQuotedColumnName(fieldDef.FieldName))
10961100
.Append("=")
1097-
.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
1101+
.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
10981102
}
10991103
catch (Exception ex)
11001104
{
@@ -1137,14 +1141,14 @@ public virtual void PrepareUpdateRowAddStatement<T>(IDbCommand dbCmd, Dictionary
11371141
.Append("=")
11381142
.Append(quotedFieldName)
11391143
.Append("+")
1140-
.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
1144+
.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
11411145
}
11421146
else
11431147
{
11441148
sql
11451149
.Append(quotedFieldName)
11461150
.Append("=")
1147-
.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
1151+
.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
11481152
}
11491153
}
11501154
catch (Exception ex)
@@ -1573,6 +1577,9 @@ public virtual object GetParamValue(object value, Type fieldType)
15731577
return ToDbValue(value, fieldType);
15741578
}
15751579

1580+
public virtual void InitQueryParam(IDbDataParameter param) {}
1581+
public virtual void InitUpdateParam(IDbDataParameter param) {}
1582+
15761583
public virtual string EscapeWildcards(string value)
15771584
{
15781585
return value?.Replace("^", @"^^")

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,9 +995,11 @@ public static IDbDataParameter AddParam(this IDbCommand dbCmd,
995995
DbType? dbType = null,
996996
byte? precision = null,
997997
byte? scale = null,
998-
int? size=null)
998+
int? size=null,
999+
Action<IDbDataParameter> paramFilter = null)
9991000
{
10001001
var p = dbCmd.CreateParam(name, value, direction, dbType, precision, scale, size);
1002+
paramFilter?.Invoke(p);
10011003
dbCmd.Parameters.Add(p);
10021004
return p;
10031005
}

0 commit comments

Comments
 (0)