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

Commit 8494199

Browse files
committed
Work around lack of signed types support in SqlServer + PostgreSql
1 parent e3760bd commit 8494199

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public override void OnAfterInitColumnTypeMap()
4343
DbTypeMap.Set<DateTimeOffset>(DbType.DateTimeOffset, DateTimeOffsetColumnDefinition);
4444
DbTypeMap.Set<DateTimeOffset?>(DbType.DateTimeOffset, DateTimeOffsetColumnDefinition);
4545

46+
//throws unknown type exceptions in parameterized queries, e.g: p.DbType = DbType.SByte
47+
DbTypeMap.Set<sbyte>(DbType.Byte, IntColumnDefinition);
48+
DbTypeMap.Set<ushort>(DbType.Int16, IntColumnDefinition);
49+
DbTypeMap.Set<uint>(DbType.Int32, IntColumnDefinition);
50+
DbTypeMap.Set<ulong>(DbType.Int64, LongColumnDefinition);
51+
4652
base.OnAfterInitColumnTypeMap();
4753
}
4854

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public override void OnAfterInitColumnTypeMap()
3939

4040
DbTypeMap.Set<TimeSpan>(DbType.DateTime, TimeColumnDefinition);
4141
DbTypeMap.Set<TimeSpan?>(DbType.DateTime, TimeColumnDefinition);
42+
43+
//throws unknown type exceptions in parameterized queries, e.g: p.DbType = DbType.SByte
44+
DbTypeMap.Set<sbyte>(DbType.Byte, IntColumnDefinition);
45+
DbTypeMap.Set<ushort>(DbType.Int16, IntColumnDefinition);
46+
DbTypeMap.Set<uint>(DbType.Int32, IntColumnDefinition);
47+
DbTypeMap.Set<ulong>(DbType.Int64, LongColumnDefinition);
4248
}
4349

4450
public override string GetQuotedValue(string paramValue)

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,11 @@ public virtual void SetParameter(FieldDefinition fieldDef, IDbDataParameter p)
657657
p.ParameterName = this.GetParam(SanitizeFieldNameForParamName(fieldDef.FieldName));
658658

659659
DbType dbType;
660-
p.DbType = DbTypeMap.ColumnDbTypeMap.TryGetValue(fieldDef.ColumnType, out dbType)
661-
? dbType
660+
var sqlDbType = DbTypeMap.ColumnDbTypeMap.TryGetValue(fieldDef.ColumnType, out dbType)
661+
? dbType
662662
: DbType.String;
663+
664+
p.DbType = sqlDbType;
663665
}
664666

665667
public virtual void SetParameterValues<T>(IDbCommand dbCmd, object obj)

tests/ServiceStack.OrmLite.Tests/OrmLiteCreateTableTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using NUnit.Framework;
33
using ServiceStack.Common.Tests.Models;
4+
using ServiceStack.OrmLite.Tests.Shared;
45
using ServiceStack.Text;
56

67
namespace ServiceStack.OrmLite.Tests
@@ -242,5 +243,25 @@ public void Can_create_table_containing_Alias_with_space()
242243
Assert.That(row.Field, Is.EqualTo("The Value"));
243244
}
244245
}
246+
247+
[Test]
248+
public void Can_create_table_with_all_number_types()
249+
{
250+
using (var db = OpenDbConnection())
251+
{
252+
db.DropAndCreateTable<ModelWithNumerics>();
253+
db.GetLastSql().Print();
254+
255+
var defaultValues = new ModelWithNumerics {
256+
Id = 1, Byte = 0, Short = 0, UShort = 0,
257+
Int = 0, UInt = 0, Long = 0, ULong = 0,
258+
Float = 0, Double = 0, Decimal = 0,
259+
};
260+
db.Insert(defaultValues);
261+
262+
var fromDb = db.SingleById<ModelWithNumerics>(defaultValues.Id);
263+
Assert.That(ModelWithNumerics.ModelWithNumericsComparer.Equals(fromDb, defaultValues));
264+
}
265+
}
245266
}
246267
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
<Compile Include="OrmLiteExecFilterTests.cs" />
132132
<Compile Include="OrmLiteFiltersTests.cs" />
133133
<Compile Include="PerfTests.cs" />
134+
<Compile Include="Shared\ModelWithNumerics.cs" />
134135
<Compile Include="StringSerializerTests.cs" />
135136
<Compile Include="TestHelpers.cs" />
136137
<Compile Include="TypeDescriptorMetadataTests.cs" />
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Collections.Generic;
2+
3+
namespace ServiceStack.OrmLite.Tests.Shared
4+
{
5+
public class ModelWithNumerics
6+
{
7+
public int Id { get; set; }
8+
9+
public byte Byte { get; set; }
10+
public sbyte SByte { get; set; }
11+
public short Short { get; set; }
12+
public ushort UShort { get; set; }
13+
public int Int { get; set; }
14+
public uint UInt { get; set; }
15+
public long Long { get; set; }
16+
public ulong ULong { get; set; }
17+
public float Float { get; set; }
18+
public double Double { get; set; }
19+
public decimal Decimal { get; set; }
20+
21+
private sealed class ModelWithNumericsEqualityComparer : IEqualityComparer<ModelWithNumerics>
22+
{
23+
public bool Equals(ModelWithNumerics x, ModelWithNumerics y)
24+
{
25+
if (ReferenceEquals(x, y)) return true;
26+
if (ReferenceEquals(x, null)) return false;
27+
if (ReferenceEquals(y, null)) return false;
28+
if (x.GetType() != y.GetType()) return false;
29+
return x.Byte == y.Byte
30+
&& x.SByte == y.SByte
31+
&& x.Short == y.Short
32+
&& x.UShort == y.UShort
33+
&& x.Int == y.Int
34+
&& x.UInt == y.UInt
35+
&& x.Long == y.Long
36+
&& x.ULong == y.ULong
37+
&& x.Float.Equals(y.Float)
38+
&& x.Double.Equals(y.Double)
39+
&& x.Decimal == y.Decimal;
40+
}
41+
42+
public int GetHashCode(ModelWithNumerics obj)
43+
{
44+
unchecked
45+
{
46+
var hashCode = obj.Byte.GetHashCode();
47+
hashCode = (hashCode*397) ^ obj.SByte.GetHashCode();
48+
hashCode = (hashCode*397) ^ obj.Short.GetHashCode();
49+
hashCode = (hashCode*397) ^ obj.UShort.GetHashCode();
50+
hashCode = (hashCode*397) ^ obj.Int;
51+
hashCode = (hashCode*397) ^ (int) obj.UInt;
52+
hashCode = (hashCode*397) ^ obj.Long.GetHashCode();
53+
hashCode = (hashCode*397) ^ obj.ULong.GetHashCode();
54+
hashCode = (hashCode*397) ^ obj.Float.GetHashCode();
55+
hashCode = (hashCode*397) ^ obj.Double.GetHashCode();
56+
hashCode = (hashCode*397) ^ obj.Decimal.GetHashCode();
57+
return hashCode;
58+
}
59+
}
60+
}
61+
62+
private static readonly IEqualityComparer<ModelWithNumerics> ModelWithNumericsComparerInstance = new ModelWithNumericsEqualityComparer();
63+
64+
public static IEqualityComparer<ModelWithNumerics> ModelWithNumericsComparer
65+
{
66+
get { return ModelWithNumericsComparerInstance; }
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)