Beispiel #1
0
        public void CreateMultiReferencingTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <MultiReferencedTable>();
            db.CreateTable <MultiReferencingTable>();

            TableMapping refingMap = db.GetMapping <MultiReferencingTable>();

            var sql     = refingMap.GetCreateSql();
            var correct =
                @"CREATE TABLE [MultiReferencingTable] (
[RefId] integer NOT NULL,
[Indexed] integer NOT NULL,
CONSTRAINT [FK_Foreign_Key]
FOREIGN KEY ([Indexed], [RefId])
REFERENCES [MultiReferencedTable] ([Id2], [Id])
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual(1, refingMap.ForeignKeys.Count);
            Assert.AreEqual("FK_Foreign_Key", refingMap.ForeignKeys.First().Name);
            TableMapping.ForeignKey[] fk = refingMap.ForeignKeys.Where(f => f.ChildTable == "MultiReferencedTable").ToArray();
            Assert.AreEqual(1, fk.Count());

            Assert.AreEqual("RefId", fk[0].Keys.Skip(1).First().Key);
            Assert.AreEqual("Id", fk[0].Keys.Skip(1).First().Value);
            Assert.AreEqual("Indexed", fk[0].Keys.First().Key);
            Assert.AreEqual("Id2", fk[0].Keys.First().Value);
        }
Beispiel #2
0
        public void CreateReferencingTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <ReferencedTable>();
            db.CreateTable <ReferencingTable>();

            TableMapping refingMap = db.GetMapping <ReferencingTable>();

            var sql     = refingMap.GetCreateSql();
            var correct =
                @"CREATE TABLE [ReferencingTable] (
[RefId] integer NOT NULL,
[RandomName] integer NOT NULL,
CONSTRAINT [FK_Foreign_Key]
FOREIGN KEY ([RefId])
REFERENCES [ReferencedTable] ([Id]),
FOREIGN KEY ([RandomName])
REFERENCES [ReferencedTable] ([Id2])
ON UPDATE CASCADE
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual(2, refingMap.ForeignKeys.Count);
            Assert.AreEqual(1, refingMap.ForeignKeys.Count(f => f.Name == "FK_Foreign_Key"));
            TableMapping.ForeignKey[] fk = refingMap.ForeignKeys.Where(f => f.ChildTable == "ReferencedTable").ToArray();
            Assert.AreEqual(2, fk.Count());
            Assert.AreEqual("Id", fk.Where(f => f.Keys.First().Key == "RefId").First().Keys.First().Value);
            Assert.AreEqual("Id2", fk.Where(f => f.Keys.First().Key == "RandomName").First().Keys.First().Value);
        }
Beispiel #3
0
        public void CreateVeryAdvancedTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <VeryAdvancedTable>();

            TableMapping mapping = db.GetMapping <VeryAdvancedTable>();

            var sql     = mapping.GetCreateSql();
            var correct =
                @"CREATE TABLE [VeryAdvancedTable] (
[Id] integer NOT NULL CHECK (Id <= 25),
[DiffName] varchar(255) COLLATE RTrim,
[IsWorking] integer NOT NULL DEFAULT(1),
[AnotherId] integer NOT NULL,
CONSTRAINT PK_MyPrimaryKey
PRIMARY KEY ([AnotherId], [Id] Desc),
CHECK (Id <= 10)
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual("Id <= 10", mapping.Checks.First());
            Assert.AreEqual(2, mapping.PrimaryKey.Columns.Length);
            Assert.AreEqual("AnotherId", mapping.PrimaryKey.Columns[0].Name);
            Assert.AreEqual("Id", mapping.PrimaryKey.Columns[1].Name);

            TableMapping.Column idCol = mapping.Columns.First(c => c.Name == "Id");
            Assert.AreEqual("Id <= 25", idCol.Checks.First());
            Assert.AreEqual("PK_MyPrimaryKey", idCol.PrimaryKey.Name);
            Assert.AreEqual(Direction.Desc, idCol.PrimaryKey.Direction);

            TableMapping.Column difCol = mapping.Columns.First(c => c.Name == "DiffName");
            Assert.AreEqual(255, difCol.MaxStringLength);
            Assert.AreEqual(Collation.RTrim, difCol.Collation);

            TableMapping.Column workinCol = mapping.Columns.First(c => c.Name == "IsWorking");
            Assert.IsFalse(workinCol.IsNullable);
            Assert.AreEqual("1", workinCol.DefaultValue);

            Assert.IsFalse(mapping.Columns.Any(c => c.Name == "IgnoredColumn"));
        }
Beispiel #4
0
        public void CreateCustomTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <CustomTable>();

            TableMapping mapping = db.GetMapping <CustomTable>();

            var sql     = mapping.GetCreateSql();
            var correct =
                @"CREATE TABLE [DifferentName] (
[Id] integer NOT NULL,
[NewName] text ,
[IsWorking] integer NOT NULL
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual("DifferentName", mapping.TableName);
            Assert.AreEqual("NewName", mapping.Columns[1].Name);
        }
Beispiel #5
0
        public void CreateAdvancedTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <AdvancedTable>();

            TableMapping mapping = db.GetMapping <AdvancedTable>();

            var sql     = mapping.GetCreateSql();
            var correct =
                @"CREATE TABLE [AdvancedTable] (
[Id] integer CONSTRAINT PK_MyPrimaryKey PRIMARY KEY Desc ON CONFLICT Fail NOT NULL,
[IsWorking] integer UNIQUE ON CONFLICT Rollback
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual(2, mapping.Columns.Count);
            Assert.IsNotNull(mapping.Columns[1].Unique);
            Assert.AreEqual(true, mapping.Columns.First(c => c.Name == "IsWorking").IsNullable);
            Assert.AreEqual(1, mapping.PrimaryKey.Columns.Length);
            Assert.AreEqual(ConflictResolution.Fail, mapping.OnPrimaryKeyConflict);
        }