Permalink
Browse files

Add InitQueryParam/InitUpdateParam to Dialect provder

  • Loading branch information...
mythz committed Jan 23, 2019
1 parent 12cdd5e commit 0b2523fbda6b2428ff1f2337fb108c5779b022a1
@@ -269,7 +269,7 @@ public override void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithP
sqlFilter
.Append(GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
.Append(this.AddQueryParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);

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

var strFilter = StringBuilderCacheAlt.ReturnAndFree(sqlFilter);
@@ -448,7 +448,7 @@ public override void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithP
sqlFilter
.Append(GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
.Append(this.AddQueryParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);

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

var strFilter = StringBuilderCacheAlt.ReturnAndFree(sqlFilter);
@@ -95,7 +95,7 @@ internal static void PrepareSqlServerUpdateStatement<T>(IDbCommand dbCmd, SqlExp
if (setFields.Length > 0)
setFields.Append(", ");

var param = dialectProvider.AddParam(dbCmd, value, fieldDef);
var param = dialectProvider.AddUpdateParam(dbCmd, value, fieldDef);
setFields
.Append(dialectProvider.GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
@@ -438,7 +438,7 @@ public override void PrepareInsertRowStatement<T>(IDbCommand dbCmd, Dictionary<s
try
{
sbColumnNames.Append(GetQuotedColumnName(fieldDef.FieldName));
sbColumnValues.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
sbColumnValues.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
}
catch (Exception ex)
{
@@ -31,7 +31,7 @@ public override void PrepareUpdateStatement(IDbCommand dbCmd, T item, bool exclu
if (setFields.Length > 0)
setFields.Append(", ");

var param = DialectProvider.AddParam(dbCmd, value, fieldDef);
var param = DialectProvider.AddUpdateParam(dbCmd, value, fieldDef);
setFields
.Append(DialectProvider.GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
@@ -1132,6 +1132,7 @@ public virtual IDbDataParameter AddParam(object value)
var paramValue = value;

var parameter = CreateParam(paramName, paramValue);
DialectProvider.InitQueryParam(parameter);
Params.Add(parameter);
return parameter;
}
@@ -1209,7 +1210,7 @@ public virtual void PrepareUpdateStatement(IDbCommand dbCmd, T item, bool exclud
setFields
.Append(DialectProvider.GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
.Append(DialectProvider.AddParam(dbCmd, value, fieldDef).ParameterName);
.Append(DialectProvider.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
}

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

if (setFields.Length == 0)
@@ -2994,13 +2995,25 @@ public static class DbDataParameterExtensions
return to;
}

public static IDbDataParameter AddQueryParam(this IOrmLiteDialectProvider dialectProvider,
IDbCommand dbCmd,
object value,
FieldDefinition fieldDef) => dialectProvider.AddParam(dbCmd, value, fieldDef, paramFilter: dialectProvider.InitQueryParam);

public static IDbDataParameter AddUpdateParam(this IOrmLiteDialectProvider dialectProvider,
IDbCommand dbCmd,
object value,
FieldDefinition fieldDef) => dialectProvider.AddParam(dbCmd, value, fieldDef, paramFilter: dialectProvider.InitUpdateParam);

public static IDbDataParameter AddParam(this IOrmLiteDialectProvider dialectProvider,
IDbCommand dbCmd,
object value,
FieldDefinition fieldDef)
FieldDefinition fieldDef, Action<IDbDataParameter> paramFilter)
{
var paramName = dbCmd.Parameters.Count.ToString();
var parameter = dialectProvider.CreateParam(paramName, value, fieldDef?.ColumnType);

paramFilter?.Invoke(parameter);

if (fieldDef != null)
dialectProvider.SetParameter(fieldDef, parameter);
@@ -206,7 +206,7 @@ internal static void PrepareUpdateAnonSql<T>(this IDbCommand dbCmd, IOrmLiteDial
var value = setField.CreateGetter()(updateOnly);
if (string.IsNullOrEmpty(whereSql) && (fieldDef.IsPrimaryKey || fieldDef.AutoIncrement))
{
whereSql = $"WHERE {dialectProvider.GetQuotedColumnName(fieldDef.FieldName)} = {dialectProvider.AddParam(dbCmd, value, fieldDef).ParameterName}";
whereSql = $"WHERE {dialectProvider.GetQuotedColumnName(fieldDef.FieldName)} = {dialectProvider.AddQueryParam(dbCmd, value, fieldDef).ParameterName}";
continue;
}

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

dbCmd.CommandText = $"UPDATE {dialectProvider.GetQuotedTableName(modelDef)} " +
@@ -74,6 +74,12 @@ public interface IOrmLiteDialectProvider

object GetParamValue(object value, Type fieldType);

// Customize DB Parameters in SELECT or WHERE queries
void InitQueryParam(IDbDataParameter param);

// Customize UPDATE or INSERT DB Parameters
void InitUpdateParam(IDbDataParameter param);

object ToDbValue(object value, Type type);

object FromDbValue(object value, Type type);
@@ -687,7 +687,7 @@ public virtual void PrepareInsertRowStatement<T>(IDbCommand dbCmd, Dictionary<st
try
{
sbColumnNames.Append(GetQuotedColumnName(fieldDef.FieldName));
sbColumnValues.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
sbColumnValues.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
}
catch (Exception ex)
{
@@ -826,7 +826,7 @@ public virtual bool PrepareParameterizedDeleteStatement<T>(IDbCommand cmd, IDict

var sqlFilter = StringBuilderCache.Allocate();
var modelDef = typeof(T).GetModelDefinition();
var hadRowVesion = false;
var hadRowVersion = false;

cmd.Parameters.Clear();

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

if (fieldDef.IsRowVersion)
hadRowVesion = true;
hadRowVersion = true;

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

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

return hadRowVesion;
return hadRowVersion;
}

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

/// <summary>
/// Used for adding updated DB params in INSERT and UPDATE statements
/// </summary>
protected IDbDataParameter AddParameter(IDbCommand cmd, FieldDefinition fieldDef)
{
var p = cmd.CreateParameter();
SetParameter(fieldDef, p);
InitUpdateParam(p);
cmd.Parameters.Add(p);
return p;
}
@@ -1043,7 +1047,7 @@ public virtual void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithPr
sqlFilter
.Append(GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
.Append(this.AddQueryParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);

continue;
}
@@ -1057,7 +1061,7 @@ public virtual void PrepareUpdateRowStatement(IDbCommand dbCmd, object objWithPr
sql
.Append(GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
.Append(this.AddParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
.Append(this.AddUpdateParam(dbCmd, fieldDef.GetValue(objWithProperties), fieldDef).ParameterName);
}
catch (Exception ex)
{
@@ -1094,7 +1098,7 @@ public virtual void PrepareUpdateRowStatement<T>(IDbCommand dbCmd, Dictionary<st
sql
.Append(GetQuotedColumnName(fieldDef.FieldName))
.Append("=")
.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
}
catch (Exception ex)
{
@@ -1137,14 +1141,14 @@ public virtual void PrepareUpdateRowAddStatement<T>(IDbCommand dbCmd, Dictionary
.Append("=")
.Append(quotedFieldName)
.Append("+")
.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
}
else
{
sql
.Append(quotedFieldName)
.Append("=")
.Append(this.AddParam(dbCmd, value, fieldDef).ParameterName);
.Append(this.AddUpdateParam(dbCmd, value, fieldDef).ParameterName);
}
}
catch (Exception ex)
@@ -1573,6 +1577,9 @@ public virtual object GetParamValue(object value, Type fieldType)
return ToDbValue(value, fieldType);
}

public virtual void InitQueryParam(IDbDataParameter param) {}
public virtual void InitUpdateParam(IDbDataParameter param) {}

public virtual string EscapeWildcards(string value)
{
return value?.Replace("^", @"^^")
@@ -995,9 +995,11 @@ public static FieldDefinition GetSelfRefFieldDefIfExists(this ModelDefinition mo
DbType? dbType = null,
byte? precision = null,
byte? scale = null,
int? size=null)
int? size=null,
Action<IDbDataParameter> paramFilter = null)
{
var p = dbCmd.CreateParam(name, value, direction, dbType, precision, scale, size);
paramFilter?.Invoke(p);
dbCmd.Parameters.Add(p);
return p;
}

0 comments on commit 0b2523f

Please sign in to comment.