public void BuildCreate_WithSchemaAndMultipleKeys() { var tableMD = GetTestMetadata(); tableMD.Properties.Add(new PropertyMetadata() { IsPK = true, ColumnName = "a2", PropertyName = "aa22" }); tableMD.Schema = "testschema"; var sql = TSQLGenerator.BuildCreate(tableMD, true); Assert.AreEqual(@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[testschema].[testtable]') AND type in (N'U')) BEGIN CREATE TABLE [testschema].[testtable]( [a] NOT NULL, [a2] NOT NULL, [b] NOT NULL, [c] NOT NULL, CONSTRAINT [PK_testtable] PRIMARY KEY CLUSTERED ( [a] ASC,[a2] ASC ) ); END ", sql); }
public static void CreateTable <T>(this IDbConnection connection, bool onlyIfNotExists, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var type = typeof(T); var values = new Dictionary <string, object>(); var metadata = MetadataFor(type); var sql = TSQLGenerator.BuildCreate(metadata, onlyIfNotExists: onlyIfNotExists); connection.Execute(sql, transaction: transaction, commandTimeout: commandTimeout); }
public void BuildCreate_NoSchema() { var tableMD = GetTestMetadata(); Assert.IsNull(tableMD.Schema); var sql = TSQLGenerator.BuildCreate(tableMD); Assert.AreEqual(@"CREATE TABLE [dbo].[testtable]( [a] NOT NULL, [b] NOT NULL, [c] NOT NULL, CONSTRAINT [PK_testtable] PRIMARY KEY CLUSTERED ( [a] ASC ) ); ", sql); }
public void BuildCreate_OneIntKey() { var md = new TableMetadata(); MetadataHelper.MetadataForClass(typeof(TableWithOneIntKey), ref md); var sql = TSQLGenerator.BuildCreate(md, true); Assert.AreEqual(@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableWithOneIntKey]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[TableWithOneIntKey]( [Key1] [int] IDENTITY(1,1) NOT NULL, [Other] [NVARCHAR](50) NULL, CONSTRAINT [PK_TableWithOneIntKey] PRIMARY KEY CLUSTERED ( [Key1] ASC ) ); END ", sql); }
public void BuildCreate_WithSchema() { var tableMD = GetTestMetadata(); tableMD.Schema = "testschema"; var sql = TSQLGenerator.BuildCreate(tableMD, true); Assert.AreEqual(@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[testschema].[testtable]') AND type in (N'U')) BEGIN CREATE TABLE [testschema].[testtable]( [a] NOT NULL, [b] NOT NULL, [c] NOT NULL, CONSTRAINT [PK_testtable] PRIMARY KEY CLUSTERED ( [a] ASC ) ); END ", sql); }
public void BuildCreate_WithSchemaAndNoKeys() { var tableMD = GetTestMetadata(); tableMD.Properties.Where(x => x.IsPK).ToList().ForEach(x => x.IsPK = false); tableMD.Schema = "testschema"; var sql = TSQLGenerator.BuildCreate(tableMD, true); Assert.AreEqual(@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[testschema].[testtable]') AND type in (N'U')) BEGIN CREATE TABLE [testschema].[testtable]( [a] NOT NULL, [b] NOT NULL, [c] NOT NULL, ); END ", sql); }