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

Commit 4a2f109

Browse files
committed
Don't escape serialized complex types in parameterized queries
1 parent 5191621 commit 4a2f109

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

src/ServiceStack.OrmLite/FieldDefinition.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
using System;
1313
using System.Reflection;
14-
using ServiceStack.Text;
1514

1615
namespace ServiceStack.OrmLite
1716
{
@@ -85,6 +84,8 @@ public string GetQuotedValue(object fromInstance)
8584
public bool IsReference { get; set; }
8685

8786
public string CustomFieldDefinition { get; set; }
87+
88+
public bool IsRefType { get; set; }
8889
}
8990

9091
public class ForeignKeyConstraint

src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
173173
Scale = decimalAttribute != null ? decimalAttribute.Scale : (int?)null,
174174
BelongToModelName = belongToAttribute != null ? belongToAttribute.BelongToTableType.GetModelDefinition().ModelName : null,
175175
CustomFieldDefinition = customFieldAttr != null ? customFieldAttr.Sql : null,
176+
IsRefType = propertyType.IsRefType(),
176177
};
177-
178+
178179
var isIgnored = propertyInfo.HasAttributeNamed(typeof(IgnoreAttribute).Name)
179180
|| fieldDefinition.IsReference;
180181
if (isIgnored)

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,7 @@ public virtual void SetParameterValues<T>(IDbCommand dbCmd, object obj)
682682

683683
public virtual void SetParameterValue<T>(FieldDefinition fieldDef, IDataParameter p, object obj)
684684
{
685-
var knownType = DbTypeMap.ColumnDbTypeMap.ContainsKey(fieldDef.ColumnType);
686-
var value = knownType
687-
? GetValueOrDbNull<T>(fieldDef, obj)
688-
: GetQuotedValueOrDbNull<T>(fieldDef, obj);
689-
685+
var value = GetValueOrDbNull<T>(fieldDef, obj);
690686
p.Value = value;
691687
}
692688

@@ -698,9 +694,14 @@ protected virtual object GetValue<T>(FieldDefinition fieldDef, object obj)
698694

699695
if (value != null)
700696
{
701-
if (fieldDef.ColumnType == typeof(object))
697+
if (fieldDef.IsRefType)
702698
{
703-
return value.ToJsv();
699+
//Let ADO.NET providers handle byte[]
700+
if (fieldDef.FieldType == typeof(byte[]))
701+
{
702+
return value;
703+
}
704+
return OrmLiteConfig.DialectProvider.StringSerializer.SerializeToString(value);
704705
}
705706
if (fieldDef.FieldType == typeof(TimeSpan))
706707
{
@@ -1250,7 +1251,7 @@ public virtual string GetQuotedValue(object value, Type fieldType)
12501251
if (value == null) return "NULL";
12511252

12521253
var dialectProvider = OrmLiteConfig.DialectProvider;
1253-
if ((!fieldType.UnderlyingSystemType.IsValueType || JsConfig.TreatValueAsRefTypes.Contains(fieldType.IsGeneric() ? fieldType.GenericTypeDefinition() : fieldType)) && fieldType != typeof(string))
1254+
if (fieldType.IsRefType())
12541255
{
12551256
return dialectProvider.GetQuotedValue(dialectProvider.StringSerializer.SerializeToString(value));
12561257
}

src/ServiceStack.OrmLite/OrmLiteUtilExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Collections.Generic;
1515
using System.Data;
1616
using System.Text;
17+
using ServiceStack.Text;
1718

1819
namespace ServiceStack.OrmLite
1920
{
@@ -279,5 +280,13 @@ public static Dictionary<string, int> GetIndexFieldsCache(this IDataReader reade
279280
return cache;
280281
}
281282

283+
public static bool IsRefType(this Type fieldType)
284+
{
285+
return (!fieldType.UnderlyingSystemType.IsValueType
286+
|| JsConfig.TreatValueAsRefTypes.Contains(fieldType.IsGeneric()
287+
? fieldType.GenericTypeDefinition()
288+
: fieldType))
289+
&& fieldType != typeof(string);
290+
}
282291
}
283292
}

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,17 @@ public void Can_Select_using_new()
8787
using (var con = OpenDbConnection())
8888
{
8989
con.Insert(new TestType
90-
{
91-
Id = 5,
92-
BoolCol = false,
93-
DateCol = new DateTime(2012, 5, 1),
94-
TextCol = "uiop",
95-
EnumCol = TestEnum.Val3,
96-
ComplexObjCol = new TestType {TextCol = "poiu"}
97-
});
98-
99-
var target =
100-
OpenDbConnection().Select<TestType>(
101-
q => q.ComplexObjCol == new TestType() {TextCol = "poiu"});
90+
{
91+
Id = 5,
92+
BoolCol = false,
93+
DateCol = new DateTime(2012, 5, 1),
94+
TextCol = "uiop",
95+
EnumCol = TestEnum.Val3,
96+
ComplexObjCol = new TestType { TextCol = "poiu" }
97+
});
98+
99+
var target = OpenDbConnection().Select<TestType>(
100+
q => q.ComplexObjCol == new TestType { TextCol = "poiu"});
102101
Assert.AreEqual(1, target.Count);
103102
}
104103
}

tests/ServiceStack.OrmLite.Tests/OrmLiteComplexTypesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public void Can_insert_Contact_with_Complex_NameDetail()
102102

103103
var contact = new Contact
104104
{
105-
FullName = new NameDetail("Test", "Contact"),
106-
Email = "test@email.com",
105+
FullName = new NameDetail("Sinéad", "O'Connor"),
106+
Email = "Sinéad@O'Connor.com",
107107
Age = 10
108108
};
109109
db.Save(contact);

0 commit comments

Comments
 (0)