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

Commit 7bc1157

Browse files
committed
DRY SetParamValue logic and reuse in RowVersionSql()
1 parent b20e64d commit 7bc1157

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,7 @@ internal static IDbCommand SetParameters(this IDbCommand dbCmd, Type type, objec
174174
p.Direction = ParameterDirection.Input;
175175
dialectProvider.InitDbParam(p, item.GetType());
176176

177-
var pValue = dialectProvider.GetFieldValue(item.GetType(), item);
178-
var valueType = pValue?.GetType();
179-
if (valueType != null && valueType != propType)
180-
dialectProvider.InitDbParam(p, valueType);
181-
182-
p.Value = pValue == null ?
183-
DBNull.Value
184-
: p.DbType == DbType.String ?
185-
pValue.ToString() :
186-
pValue;
177+
dialectProvider.SetParamValue(p, item, propType);
187178

188179
dbCmd.Parameters.Add(p);
189180
}
@@ -200,26 +191,10 @@ internal static IDbCommand SetParameters(this IDbCommand dbCmd, Type type, objec
200191
p.Direction = ParameterDirection.Input;
201192
dialectProvider.InitDbParam(p, propType);
202193

203-
if (fieldMap != null && fieldMap.TryGetValue(columnName, out var fieldDef))
204-
{
205-
value = dialectProvider.GetFieldValue(fieldDef, value);
206-
var valueType = value?.GetType();
207-
if (valueType != null && valueType != propType)
208-
dialectProvider.InitDbParam(p, valueType);
209-
}
210-
else
211-
{
212-
value = dialectProvider.GetFieldValue(propType, value);
213-
var valueType = value?.GetType();
214-
if (valueType != null && valueType != propType)
215-
dialectProvider.InitDbParam(p, valueType);
216-
}
194+
FieldDefinition fieldDef = null;
195+
fieldMap?.TryGetValue(columnName, out fieldDef);
217196

218-
p.Value = value == null ?
219-
DBNull.Value
220-
: p.DbType == DbType.String ?
221-
value.ToString() :
222-
value;
197+
dialectProvider.SetParamValue(p, value, propType, fieldDef);
223198

224199
dbCmd.Parameters.Add(p);
225200
}
@@ -229,6 +204,30 @@ internal static IDbCommand SetParameters(this IDbCommand dbCmd, Type type, objec
229204
return dbCmd;
230205
}
231206

207+
internal static void SetParamValue(this IOrmLiteDialectProvider dialectProvider, IDbDataParameter p, object value, Type propType, FieldDefinition fieldDef=null)
208+
{
209+
if (fieldDef != null)
210+
{
211+
value = dialectProvider.GetFieldValue(fieldDef, value);
212+
var valueType = value?.GetType();
213+
if (valueType != null && valueType != propType)
214+
dialectProvider.InitDbParam(p, valueType);
215+
}
216+
else
217+
{
218+
value = dialectProvider.GetFieldValue(propType, value);
219+
var valueType = value?.GetType();
220+
if (valueType != null && valueType != propType)
221+
dialectProvider.InitDbParam(p, valueType);
222+
}
223+
224+
p.Value = value == null
225+
? DBNull.Value
226+
: p.DbType == DbType.String
227+
? value.ToString()
228+
: value;
229+
}
230+
232231
internal delegate void ParamIterDelegate(string propName, string columnName, object value);
233232

234233
internal static void ForEachParam(this Dictionary<string,object> values, ModelDefinition modelDef, bool excludeDefaults, ParamIterDelegate fn)

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,11 @@ internal static string RowVersionSql(this IDbCommand dbCmd, ModelDefinition mode
983983

984984
dbCmd.Parameters.Clear();
985985
var idParam = dbCmd.CreateParameter();
986+
idParam.Direction = ParameterDirection.Input;
986987
idParam.ParameterName = idParamString;
987-
idParam.Value = id;
988+
989+
dialectProvider.SetParamValue(idParam, id, modelDef.PrimaryKey.ColumnType, modelDef.PrimaryKey);
990+
988991
dbCmd.Parameters.Add(idParam);
989992
return sql;
990993
}

tests/ServiceStack.OrmLite.Tests/EnumTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,35 @@ namespace ServiceStack.OrmLite.Tests
99
{
1010
using System;
1111

12+
public class TypeWithEnumAsStringAsPk
13+
{
14+
[PrimaryKey]
15+
public SomeEnum Id { get; set; }
16+
17+
[Default(typeof(bool), "0")]
18+
public bool IsDeleted { get; set; }
19+
20+
21+
[RowVersion]
22+
public byte[] RowVersion { get; set; }
23+
24+
}
25+
1226
public class EnumTests : OrmLiteTestBase
1327
{
28+
[Test]
29+
public void Can_use_RowVersion_on_EnumAsString_PrimaryKey()
30+
{
31+
using (var db = OpenDbConnection())
32+
{
33+
db.DropAndCreateTable<TypeWithEnumAsStringAsPk>();
34+
35+
db.Insert(new TypeWithEnumAsStringAsPk { Id = SomeEnum.Value1 });
36+
37+
db.Save(new TypeWithEnumAsStringAsPk { Id = SomeEnum.Value2 });
38+
}
39+
}
40+
1441
[Test]
1542
public void CanCreateTable()
1643
{

tests/ServiceStack.OrmLite.Tests/OrmLiteTestBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace ServiceStack.OrmLite.Tests
1212
{
1313
public class Config
1414
{
15+
public static Dialect DefaultDialect = Dialect.Sqlite;
1516
public const bool EnableDebugLogging = false;
17+
1618
public static string SqliteMemoryDb = ":memory:";
1719
public static string SqliteFileDir = "~/App_Data/".MapAbsolutePath();
1820
public static string SqliteFileDb = "~/App_Data/db.sqlite".MapAbsolutePath();
@@ -25,7 +27,6 @@ public class Config
2527
public static string PostgreSqlDb = "Server=localhost;Port=5432;User Id=test;Password=test;Database=test;Pooling=true;MinPoolSize=0;MaxPoolSize=200";
2628
public static string FirebirdDb = @"User=SYSDBA;Password=masterkey;Database=C:\src\ServiceStack.OrmLite\tests\ServiceStack.OrmLite.Tests\App_Data\TEST.FDB;DataSource=localhost;Dialect=3;charset=ISO8859_1;";
2729

28-
public static Dialect DefaultDialect = Dialect.Sqlite;
2930

3031
public static IOrmLiteDialectProvider DefaultProvider = SqlServerDialect.Provider;
3132
public static string DefaultConnection = SqlServerBuildDb;

0 commit comments

Comments
 (0)