|
| 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 | +} |
0 commit comments