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

Commit 6038558

Browse files
committed
Fixed failed SqlServer tests
1 parent ca4fadc commit 6038558

File tree

10 files changed

+99
-103
lines changed

10 files changed

+99
-103
lines changed

README.md

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,17 @@ Creating tables is a simple 1-liner:
926926
```csharp
927927
using (IDbConnection db = ":memory:".OpenDbConnection())
928928
{
929-
const bool overwrite = false;
930-
db.CreateTables(overwrite, typeof(Shipper), typeof(ShipperType));
929+
db.CreateTable<ShipperType>();
930+
db.CreateTable<Shipper>();
931931
}
932932

933933
/* In debug mode the line above prints:
934+
DEBUG: CREATE TABLE "ShipperTypes"
935+
(
936+
"ShipperTypeID" INTEGER PRIMARY KEY AUTOINCREMENT,
937+
"Name" VARCHAR(40) NOT NULL
938+
);
939+
DEBUG: CREATE UNIQUE INDEX uidx_shippertypes_name ON "ShipperTypes" ("Name" ASC);
934940
DEBUG: CREATE TABLE "Shippers"
935941
(
936942
"ShipperID" INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -941,37 +947,30 @@ Creating tables is a simple 1-liner:
941947
CONSTRAINT "FK_Shippers_ShipperTypes" FOREIGN KEY ("ShipperTypeId") REFERENCES "ShipperTypes" ("ShipperID")
942948
);
943949
DEBUG: CREATE UNIQUE INDEX uidx_shippers_companyname ON "Shippers" ("CompanyName" ASC);
944-
DEBUG: CREATE TABLE "ShipperTypes"
945-
(
946-
"ShipperTypeID" INTEGER PRIMARY KEY AUTOINCREMENT,
947-
"Name" VARCHAR(40) NOT NULL
948-
);
949-
DEBUG: CREATE UNIQUE INDEX uidx_shippertypes_name ON "ShipperTypes" ("Name" ASC);
950950
*/
951951
```
952952

953953
### Transaction Support
954954
As we have direct access to IDbCommand and friends - playing with transactions is easy:
955955

956956
```csharp
957-
int trainsTypeId, planesTypeId;
958-
using (IDbTransaction dbTrans = db.OpenTransaction())
959-
{
960-
db.Insert(new ShipperType { Name = "Trains" });
961-
trainsTypeId = (int) db.GetLastInsertId();
957+
var trainsType = new ShipperType { Name = "Trains" };
958+
var planesType = new ShipperType { Name = "Planes" };
962959

963-
db.Insert(new ShipperType { Name = "Planes" });
964-
planesTypeId = (int) db.GetLastInsertId();
960+
//Playing with transactions
961+
using (IDbTransaction dbTrans = db.OpenTransaction())
962+
{
963+
db.Save(trainsType);
964+
db.Save(planesType);
965965

966-
dbTrans.Commit();
967-
}
968-
using (IDbTransaction dbTrans = db.OpenTransaction(IsolationLevel.ReadCommitted))
969-
{
970-
db.Insert(new ShipperType { Name = "Automobiles" });
971-
Assert.That(db.Select<ShipperType>(), Has.Count(3));
966+
dbTrans.Commit();
967+
}
972968

973-
dbTrans.Rollback();
974-
}
969+
using (IDbTransaction dbTrans = db.OpenTransaction(IsolationLevel.ReadCommitted))
970+
{
971+
db.Insert(new ShipperType { Name = "Automobiles" });
972+
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(3));
973+
}
975974
Assert.That(db.Select<ShipperType>(), Has.Count(2));
976975
```
977976

@@ -980,53 +979,52 @@ No ORM is complete without the standard crud operations:
980979

981980
```csharp
982981
//Performing standard Insert's and Selects
983-
db.Insert(new Shipper { CompanyName = "Trains R Us", Phone = "555-TRAINS", ShipperTypeId = trainsTypeId });
984-
db.Insert(new Shipper { CompanyName = "Planes R Us", Phone = "555-PLANES", ShipperTypeId = planesTypeId });
985-
db.Insert(new Shipper { CompanyName = "We do everything!", Phone = "555-UNICORNS", ShipperTypeId = planesTypeId });
986-
987-
var trainsAreUs = db.First<Shipper>("ShipperTypeId = {0}", trainsTypeId);
988-
Assert.That(trainsAreUs.CompanyName, Is.EqualTo("Trains R Us"));
989-
Assert.That(db.Select<Shipper>("CompanyName = {0} OR Phone = {1}", "Trains R Us", "555-UNICORNS"), Has.Count(2));
990-
Assert.That(db.Select<Shipper>("ShipperTypeId = {0}", planesTypeId), Has.Count(2));
991-
992-
//Lets update a record
993-
trainsAreUs.Phone = "666-TRAINS";
994-
db.Update(trainsAreUs);
995-
Assert.That(db.SingleById<Shipper>(trainsAreUs.Id).Phone, Is.EqualTo("666-TRAINS"));
996-
997-
//Then make it disappear
998-
db.Delete(trainsAreUs);
999-
Assert.That(db.SingleById<Shipper>(trainsAreUs.Id), Is.Null);
1000-
1001-
//And bring it back again
1002-
db.Insert(trainsAreUs);
982+
db.Insert(new Shipper { CompanyName = "Trains R Us", Phone = "555-TRAINS", ShipperTypeId = trainsType.Id });
983+
db.Insert(new Shipper { CompanyName = "Planes R Us", Phone = "555-PLANES", ShipperTypeId = planesType.Id });
984+
db.Insert(new Shipper { CompanyName = "We do everything!", Phone = "555-UNICORNS", ShipperTypeId = planesType.Id });
985+
986+
var trainsAreUs = db.SingleFmt<Shipper>("ShipperTypeId = {0}", trainsType.Id);
987+
Assert.That(trainsAreUs.CompanyName, Is.EqualTo("Trains R Us"));
988+
Assert.That(db.SelectFmt<Shipper>("CompanyName = {0} OR Phone = {1}", "Trains R Us", "555-UNICORNS"), Has.Count.EqualTo(2));
989+
Assert.That(db.SelectFmt<Shipper>("ShipperTypeId = {0}", planesType.Id), Has.Count.EqualTo(2));
990+
991+
//Lets update a record
992+
trainsAreUs.Phone = "666-TRAINS";
993+
db.Update(trainsAreUs);
994+
Assert.That(db.SingleById<Shipper>(trainsAreUs.Id).Phone, Is.EqualTo("666-TRAINS"));
995+
996+
//Then make it dissappear
997+
db.Delete(trainsAreUs);
998+
Assert.That(db.SingleById<Shipper>(trainsAreUs.Id), Is.Null);
999+
1000+
//And bring it back again
1001+
db.Insert(trainsAreUs);
10031002
```
10041003

10051004
### Performing custom queries
10061005
And with access to raw sql when you need it - the database is your oyster :)
10071006

10081007
```csharp
1009-
//Select only a subset from the table
1010-
var partialColumns = db.Select<SubsetOfShipper>(typeof (Shipper), "ShipperTypeId = {0}", planesTypeId);
1011-
Assert.That(partialColumns, Has.Count(2));
1008+
var partialColumns = db.SelectFmt<SubsetOfShipper>(typeof (Shipper), "ShipperTypeId = {0}", planesType.Id);
1009+
Assert.That(partialColumns, Has.Count.EqualTo(2));
10121010

1013-
//Select into another POCO class that matches the sql results
1014-
var rows = db.Select<ShipperTypeCount>(
1015-
"SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)");
1011+
//Select into another POCO class that matches sql
1012+
var rows = db.SelectFmt<ShipperTypeCount>(
1013+
"SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)");
10161014

1017-
Assert.That(rows, Has.Count(2));
1018-
Assert.That(rows[0].ShipperTypeId, Is.EqualTo(trainsTypeId));
1019-
Assert.That(rows[0].Total, Is.EqualTo(1));
1020-
Assert.That(rows[1].ShipperTypeId, Is.EqualTo(planesTypeId));
1021-
Assert.That(rows[1].Total, Is.EqualTo(2));
1015+
Assert.That(rows, Has.Count.EqualTo(2));
1016+
Assert.That(rows[0].ShipperTypeId, Is.EqualTo(trainsType.Id));
1017+
Assert.That(rows[0].Total, Is.EqualTo(1));
1018+
Assert.That(rows[1].ShipperTypeId, Is.EqualTo(planesType.Id));
1019+
Assert.That(rows[1].Total, Is.EqualTo(2));
10221020

10231021

1024-
//And finally lets quickly clean up the mess we've made:
1025-
db.DeleteAll<Shipper>();
1026-
db.DeleteAll<ShipperType>();
1022+
//And finally lets quickly clean up the mess we've made:
1023+
db.DeleteAll<Shipper>();
1024+
db.DeleteAll<ShipperType>();
10271025

1028-
Assert.That(db.Select<Shipper>(), Has.Count(0));
1029-
Assert.That(db.Select<ShipperType>(), Has.Count(0));
1026+
Assert.That(db.Select<Shipper>(), Has.Count.EqualTo(0));
1027+
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(0));
10301028
```
10311029

10321030
## Other notable Micro ORMs for .NET

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,6 @@ public override void SetParameter(FieldDefinition fieldDef, IDbDataParameter p)
137137

138138
public override void SetDbValue(FieldDefinition fieldDef, IDataReader dataReader, int colIndex, object instance)
139139
{
140-
if (fieldDef == null || fieldDef.SetValueFn == null || colIndex == NotFound) return;
141-
if (dataReader.IsDBNull(colIndex))
142-
{
143-
if (fieldDef.IsNullable)
144-
{
145-
fieldDef.SetValueFn(instance, null);
146-
}
147-
else
148-
{
149-
fieldDef.SetValueFn(instance, fieldDef.FieldType.GetDefaultValue());
150-
}
151-
return;
152-
}
153-
154140
var fieldType = Nullable.GetUnderlyingType(fieldDef.FieldType) ?? fieldDef.FieldType;
155141
if (fieldType == typeof(Guid))
156142
{

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,14 @@ public virtual void SetDbValue(FieldDefinition fieldDef, IDataReader dataReader,
282282
if (fieldDef == null || fieldDef.SetValueFn == null || colIndex == NotFound) return;
283283
if (dataReader.IsDBNull(colIndex))
284284
{
285-
fieldDef.SetValueFn(instance, null);
285+
if (fieldDef.IsNullable)
286+
{
287+
fieldDef.SetValueFn(instance, null);
288+
}
289+
else
290+
{
291+
fieldDef.SetValueFn(instance, fieldDef.FieldType.GetDefaultValue());
292+
}
286293
return;
287294
}
288295

src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static GetValueDelegate GetValueFn<T>(IDataRecord reader)
9999
case TypeCode.Int32:
100100
return Convert.ToInt32(value);
101101
case TypeCode.Int64:
102-
return Convert.ToInt16(value);
102+
return Convert.ToInt64(value);
103103
case TypeCode.Single:
104104
return Convert.ToSingle(value);
105105
case TypeCode.Double:

tests/ServiceStack.OrmLite.Tests/CustomSqlTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class PocoTable
99
{
1010
public int Id { get; set; }
1111

12-
[CustomField("CHAR(20) null")]
12+
[CustomField("CHAR(20)")]
1313
public string CharColumn { get; set; }
1414

15-
[CustomField("DECIMAL(18,4) null")]
15+
[CustomField("DECIMAL(18,4)")]
1616
public decimal? DecimalColumn { get; set; }
1717
}
1818

@@ -72,7 +72,7 @@ public void Can_create_field_with_custom_sql()
7272
{
7373
db.DropAndCreateTable<PocoTable>();
7474

75-
var createTableSql = db.GetLastSql();
75+
var createTableSql = db.GetLastSql().Replace("NULL","null");
7676
createTableSql.Print();
7777

7878
Assert.That(createTableSql, Is.StringContaining("\"CharColumn\" CHAR(20) null")

tests/ServiceStack.OrmLite.Tests/Issues/MismatchSchemaTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Does_allow_reading_from_table_with_mismatched_nullable_int_type()
3232
{
3333
using (var db = OpenDbConnection())
3434
{
35-
db.CreateTable<Nullable.ModelIntValue>();
35+
db.DropAndCreateTable<Nullable.ModelIntValue>();
3636

3737
db.Insert(new Nullable.ModelIntValue { Id = 1, Value = null, Text = "Foo" });
3838

tests/ServiceStack.OrmLite.Tests/OrmLiteComplexTypesTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public void Lists_Of_Guids_Are_Formatted_Correctly()
7878

7979
db.Insert(item);
8080

81-
var savedGuidOne = db.Select<string>("SELECT GuidOne FROM WithAListOfGuids").First();
82-
Assert.That(savedGuidOne, Is.EqualTo("32cb0acb-db43-4061-a6aa-7f4902a7002a"));
81+
var savedGuidOne = db.Select<Guid>("SELECT GuidOne FROM WithAListOfGuids").First();
82+
Assert.That(savedGuidOne, Is.EqualTo(new Guid("32cb0acb-db43-4061-a6aa-7f4902a7002a")));
8383

84-
var savedGuidTwo = db.Select<string>("SELECT GuidTwo FROM WithAListOfGuids").First();
85-
Assert.That(savedGuidTwo, Is.EqualTo("13083231-b005-4ff4-ab62-41bdc7f50a4d"));
84+
var savedGuidTwo = db.Select<Guid>("SELECT GuidTwo FROM WithAListOfGuids").First();
85+
Assert.That(savedGuidTwo, Is.EqualTo(new Guid("13083231-b005-4ff4-ab62-41bdc7f50a4d")));
8686

8787
var savedGuidList = db.Select<string>("SELECT TheGuids FROM WithAListOfGuids").First();
8888
Assert.That(savedGuidList, Is.EqualTo("[18176030-7a1c-4288-82df-a52f71832381,017f986b-f7be-4b6f-b978-ff05fba3b0aa]"));

tests/ServiceStack.OrmLite.Tests/OrmLiteFiltersTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public interface IAudit
1313

1414
public class AuditTableA : IAudit
1515
{
16+
public AuditTableA()
17+
{
18+
this.CreatedDate = this.ModifiedDate = DateTime.UtcNow;
19+
}
20+
1621
[AutoIncrement]
1722
public int Id { get; set; }
1823
public DateTime CreatedDate { get; set; }

tests/ServiceStack.OrmLite.Tests/ShippersExample.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,42 @@ public void Shippers_UseCase()
6161
{
6262
using (IDbConnection db = OpenDbConnection())
6363
{
64-
db.DropTables(typeof(Shipper), typeof(ShipperType));
65-
const bool overwrite = false;
66-
db.CreateTables(overwrite, typeof(ShipperType), typeof(Shipper));
64+
db.DropTable<Shipper>();
65+
db.DropTable<ShipperType>();
6766

68-
int trainsTypeId, planesTypeId;
67+
db.CreateTable<ShipperType>();
68+
db.CreateTable<Shipper>();
69+
70+
var trainsType = new ShipperType { Name = "Trains" };
71+
var planesType = new ShipperType { Name = "Planes" };
6972

7073
//Playing with transactions
7174
using (IDbTransaction dbTrans = db.OpenTransaction())
7275
{
73-
db.Insert(new ShipperType { Name = "Trains" });
74-
trainsTypeId = (int)db.LastInsertId();
75-
76-
db.Insert(new ShipperType { Name = "Planes" });
77-
planesTypeId = (int)db.LastInsertId();
76+
db.Save(trainsType);
77+
db.Save(planesType);
7878

7979
dbTrans.Commit();
8080
}
81+
8182
using (IDbTransaction dbTrans = db.OpenTransaction(IsolationLevel.ReadCommitted))
8283
{
8384
db.Insert(new ShipperType { Name = "Automobiles" });
8485
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(3));
8586

86-
dbTrans.Rollback();
8787
}
8888
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(2));
8989

9090

9191
//Performing standard Insert's and Selects
92-
db.Insert(new Shipper { CompanyName = "Trains R Us", Phone = "555-TRAINS", ShipperTypeId = trainsTypeId });
93-
db.Insert(new Shipper { CompanyName = "Planes R Us", Phone = "555-PLANES", ShipperTypeId = planesTypeId });
94-
db.Insert(new Shipper { CompanyName = "We do everything!", Phone = "555-UNICORNS", ShipperTypeId = planesTypeId });
92+
db.Insert(new Shipper { CompanyName = "Trains R Us", Phone = "555-TRAINS", ShipperTypeId = trainsType.Id });
93+
db.Insert(new Shipper { CompanyName = "Planes R Us", Phone = "555-PLANES", ShipperTypeId = planesType.Id });
94+
db.Insert(new Shipper { CompanyName = "We do everything!", Phone = "555-UNICORNS", ShipperTypeId = planesType.Id });
9595

96-
var trainsAreUs = db.SingleFmt<Shipper>("ShipperTypeId = {0}", trainsTypeId);
96+
var trainsAreUs = db.SingleFmt<Shipper>("ShipperTypeId = {0}", trainsType.Id);
9797
Assert.That(trainsAreUs.CompanyName, Is.EqualTo("Trains R Us"));
9898
Assert.That(db.SelectFmt<Shipper>("CompanyName = {0} OR Phone = {1}", "Trains R Us", "555-UNICORNS"), Has.Count.EqualTo(2));
99-
Assert.That(db.SelectFmt<Shipper>("ShipperTypeId = {0}", planesTypeId), Has.Count.EqualTo(2));
99+
Assert.That(db.SelectFmt<Shipper>("ShipperTypeId = {0}", planesType.Id), Has.Count.EqualTo(2));
100100

101101
//Lets update a record
102102
trainsAreUs.Phone = "666-TRAINS";
@@ -113,17 +113,17 @@ public void Shippers_UseCase()
113113

114114
//Performing custom queries
115115
//Select only a subset from the table
116-
var partialColumns = db.SelectFmt<SubsetOfShipper>(typeof (Shipper), "ShipperTypeId = {0}", planesTypeId);
116+
var partialColumns = db.SelectFmt<SubsetOfShipper>(typeof (Shipper), "ShipperTypeId = {0}", planesType.Id);
117117
Assert.That(partialColumns, Has.Count.EqualTo(2));
118118

119119
//Select into another POCO class that matches sql
120120
var rows = db.SelectFmt<ShipperTypeCount>(
121121
"SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)");
122122

123123
Assert.That(rows, Has.Count.EqualTo(2));
124-
Assert.That(rows[0].ShipperTypeId, Is.EqualTo(trainsTypeId));
124+
Assert.That(rows[0].ShipperTypeId, Is.EqualTo(trainsType.Id));
125125
Assert.That(rows[0].Total, Is.EqualTo(1));
126-
Assert.That(rows[1].ShipperTypeId, Is.EqualTo(planesTypeId));
126+
Assert.That(rows[1].ShipperTypeId, Is.EqualTo(planesType.Id));
127127
Assert.That(rows[1].Total, Is.EqualTo(2));
128128

129129

tests/ServiceStack.OrmLite.Tests/TypeDescriptorMetadataTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public void Can_change_column_definition()
6565

6666
typeof(DynamicCacheEntry)
6767
.GetProperty("Data")
68-
.AddAttributes(new StringLengthAttribute(64000));
68+
.AddAttributes(new StringLengthAttribute(7000));
6969

7070
db.DropAndCreateTable<DynamicCacheEntry>();
7171

72-
Assert.That(db.GetLastSql(), Is.StringContaining("\"Data\" VARCHAR(64000)"));
72+
Assert.That(db.GetLastSql(), Is.StringContaining("\"Data\" VARCHAR(7000)"));
7373
db.GetLastSql().Print();
7474
}
7575

0 commit comments

Comments
 (0)