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

Commit 0d17690

Browse files
committed
2 parents ab49810 + 9505bd6 commit 0d17690

19 files changed

+2272
-1978
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
using ServiceStack.DataAnnotations;
7+
8+
namespace ServiceStack.OrmLite.Tests
9+
{
10+
[TestFixture]
11+
public class SchemaTests : OrmLiteTestBase
12+
{
13+
[Alias("Users")]
14+
[Schema("TestSchema")]
15+
public class User
16+
{
17+
[AutoIncrement]
18+
public int Id { get; set; }
19+
20+
[Index]
21+
public string Name { get; set; }
22+
23+
public DateTime CreatedDate { get; set; }
24+
}
25+
26+
private void CreateSchemaIfNotExists(System.Data.IDbConnection db)
27+
{
28+
const string createSchemaSQL = @"DO $$
29+
BEGIN
30+
31+
IF NOT EXISTS(
32+
SELECT schema_name
33+
FROM information_schema.schemata
34+
WHERE schema_name = 'TestSchema'
35+
)
36+
THEN
37+
EXECUTE 'CREATE SCHEMA ""TestSchema""';
38+
END IF;
39+
40+
END
41+
$$;";
42+
db.ExecuteSql(createSchemaSQL);
43+
}
44+
45+
[Test]
46+
public void Can_Create_Tables_With_Schema_in_PostgreSQL()
47+
{
48+
using (var db = ConnectionString.OpenDbConnection())
49+
using (var dbCmd = db.CreateCommand())
50+
{
51+
CreateSchemaIfNotExists(db);
52+
db.DropAndCreateTable<User>();
53+
54+
var tables = db.GetFirstColumn<string>
55+
(@"SELECT '[' || n.nspname || '].[' || c.relname ||']' FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = 'Users' AND n.nspname = 'TestSchema'");
56+
57+
//PostgreSQL dialect should create the table in the schema
58+
Assert.That(tables.Contains("[TestSchema].[Users]"));
59+
}
60+
}
61+
62+
[Test]
63+
public void Can_Perform_CRUD_Operations_On_Table_With_Schema()
64+
{
65+
using (var db = ConnectionString.OpenDbConnection())
66+
using (var dbCmd = db.CreateCommand())
67+
{
68+
CreateSchemaIfNotExists(db);
69+
db.CreateTable<User>(true);
70+
71+
db.Insert(new User { Id = 1, Name = "A", CreatedDate = DateTime.Now });
72+
db.Insert(new User { Id = 2, Name = "B", CreatedDate = DateTime.Now });
73+
db.Insert(new User { Id = 3, Name = "B", CreatedDate = DateTime.Now });
74+
75+
var lastInsertId = db.GetLastInsertId();
76+
Assert.That(lastInsertId, Is.GreaterThan(0));
77+
78+
var rowsB = db.Select<User>("\"Name\" = {0}", "B");
79+
Assert.That(rowsB, Has.Count.EqualTo(2));
80+
81+
var rowIds = rowsB.ConvertAll(x => x.Id);
82+
Assert.That(rowIds, Is.EquivalentTo(new List<long> { 2, 3 }));
83+
84+
rowsB.ForEach(x => db.Delete(x));
85+
86+
rowsB = db.Select<User>("\"Name\" = {0}", "B");
87+
Assert.That(rowsB, Has.Count.EqualTo(0));
88+
89+
var rowsLeft = db.Select<User>();
90+
Assert.That(rowsLeft, Has.Count.EqualTo(1));
91+
92+
Assert.That(rowsLeft[0].Name, Is.EqualTo("A"));
93+
}
94+
}
95+
}
96+
}

src/ServiceStack.OrmLite.PostgreSQL.Tests/ServiceStack.OrmLite.PostgreSQL.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="OrmLiteTestBase.cs" />
9191
<Compile Include="Properties\AssemblyInfo.cs" />
9292
<Compile Include="OrmLiteGetScalarTests.cs" />
93+
<Compile Include="SchemaTests.cs" />
9394
<Compile Include="TypeWithByteArrayFieldTests.cs" />
9495
</ItemGroup>
9596
<ItemGroup>

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,15 @@ public override string ToExecuteProcedureStatement(object objWithProperties)
184184

185185
return sql;
186186
}
187+
188+
public override string GetQuotedTableName(ModelDefinition modelDef)
189+
{
190+
if (!modelDef.IsInSchema)
191+
{
192+
return base.GetQuotedTableName(modelDef);
193+
}
194+
string escapedSchema = modelDef.Schema.Replace(".", "\".\"");
195+
return string.Format("\"{0}\".\"{1}\"", escapedSchema, base.NamingStrategy.GetTableName(modelDef.ModelName));
196+
}
187197
}
188198
}

src/ServiceStack.OrmLite.SqlServerTests/ForeignKeyAttributeTests.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ public void Setup()
1111
{
1212
using (var dbConn = ConnectionString.OpenDbConnection())
1313
{
14+
dbConn.DropTable<TypeWithOnDeleteAndUpdateCascade>();
15+
dbConn.DropTable<TypeWithOnDeleteSetNull>();
16+
dbConn.DropTable<TypeWithOnDeleteSetDefault>();
17+
dbConn.DropTable<TypeWithOnDeleteRestrict>();
18+
dbConn.DropTable<TypeWithOnDeleteNoAction>();
19+
dbConn.DropTable<TypeWithOnDeleteCascade>();
20+
dbConn.DropTable<TypeWithSimpleForeignKey>();
21+
dbConn.DropTable<ReferencedType>();
22+
1423
dbConn.CreateTable<ReferencedType>(true);
1524
}
1625
}
@@ -98,22 +107,6 @@ public void CanCreateForeignWithOnDeleteSetNull()
98107
dbConn.CreateTable<TypeWithOnDeleteSetNull>(true);
99108
}
100109
}
101-
102-
[TestFixtureTearDown]
103-
public void TearDwon()
104-
{
105-
using (var dbConn = ConnectionString.OpenDbConnection())
106-
{
107-
dbConn.DropTable<TypeWithOnDeleteAndUpdateCascade>();
108-
dbConn.DropTable<TypeWithOnDeleteSetNull>();
109-
dbConn.DropTable<TypeWithOnDeleteSetDefault>();
110-
dbConn.DropTable<TypeWithOnDeleteRestrict>();
111-
dbConn.DropTable<TypeWithOnDeleteNoAction>();
112-
dbConn.DropTable<TypeWithOnDeleteCascade>();
113-
dbConn.DropTable<TypeWithSimpleForeignKey>();
114-
dbConn.DropTable<ReferencedType>();
115-
}
116-
}
117110
}
118111

119112
public class ReferencedType

src/ServiceStack.OrmLite.SqlServerTests/SqlServerExpressionVisitorQueryTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ public void test_if_ev_still_works_without_limit_and_orderby()
9898
Assert.IsTrue(result.Count > 0);
9999
}
100100
}
101+
102+
[Test]
103+
public void test_if_and_works_with_nullable_parameter()
104+
{
105+
using(var db = ConnectionString.OpenDbConnection())
106+
{
107+
db.CreateTable<TestEntity>(true);
108+
db.Insert(new TestEntity
109+
{
110+
Foo = this.RandomString(16),
111+
Bar = this.RandomString(16),
112+
Baz = this.RandomDecimal()
113+
});
114+
115+
var id = (int)db.GetLastInsertId();
116+
117+
var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<TestEntity>();
118+
ev.Where(e => e.Id == id);
119+
int? i = null;
120+
ev.And(e => e.NullInt == i);
121+
122+
var result = db.Select(ev);
123+
Assert.NotNull(result);
124+
Assert.IsTrue(result.Count > 0);
125+
}
126+
}
101127

102128
protected void FillTestEntityTableWithTestData(IDbConnection db)
103129
{

src/ServiceStack.OrmLite.SqlServerTests/UseCase/TestEntity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class TestEntity
1515

1616
public String Foo { get; set; }
1717
public String Bar { get; set; }
18+
public int? NullInt { get; set; }
19+
1820
[Index]
1921
public Decimal Baz { get; set; }
2022

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public override object ConvertDbValue(object value, Type type)
9696
{
9797
if (value == null || value is DBNull) return null;
9898

99-
if (type == typeof(bool))
99+
if (type == typeof(bool) && !(value is bool))
100100
{
101101
var intVal = int.Parse(value.ToString());
102102
return intVal != 0;

0 commit comments

Comments
 (0)