public string Convert(CTable table) { var converter = new CTableToCreateTableStatementConverter(); var createTable = converter.Convert(table); string tableScript; var scriptGen = new Sql120ScriptGenerator(); scriptGen.GenerateScript(createTable, out tableScript); /* * //generate DDL * var script = new TSqlScript(); * var batch = new TSqlBatch(); * script.Batches.Add(batch); * batch.Statements.Add(createTable); * var dacpacModel = new TSqlModel(SqlServerVersion.Sql120, new TSqlModelOptions()); * dacpacModel.AddObjects(script); * * string[] parts = { table.Schema.SchemaName, table.TableName }; * * var existing = dacpacModel.GetObject(Table.TypeClass, new ObjectIdentifier(parts), DacQueryScopes.All); * var tableScript = existing.GetScript(); */ var codeWriter = new CodeWriter(); codeWriter.WriteLine(tableScript); foreach (var column in table.Column) { if (!string.IsNullOrEmpty(column.ColumnDescription)) { codeWriter.WriteLine(); codeWriter.WriteLine("GO"); codeWriter.WriteLine(); codeWriter.WriteLine(GetExtendedPropertyScript(table.Schema.SchemaName, table.TableName, column.ColumnName, column.ColumnDescription)); } } return(codeWriter.ToString()); ; }
public string Convert(CTable table) { var converter = new CTableToCreateTableStatementConverter(); var createTable = converter.Convert(table); //generate DDL var codeWriter = new CodeWriter(); codeWriter.WriteLine($@"CREATE TABLE IF NOT EXISTS {table.Schema.SchemaName.WrapReservedAndSnakeCase( DataStoreTypes.MySql, table.ConvertToSnakeCase)}.{table.TableName.WrapReservedAndSnakeCase(DataStoreTypes.MySql, table.ConvertToSnakeCase)} ("); codeWriter.Indent(); var first = true; foreach (var col in table.Column) { if (!first) { codeWriter.WriteLine(","); } first = false; codeWriter.Write($"{col.ColumnName.WrapReservedAndSnakeCase( DataStoreTypes.MySql, table.ConvertToSnakeCase)}"); codeWriter.Write("\t"); codeWriter.Write($" {SqlMapper.MySqlDbTypeToMySql(SqlMapper.DbTypeToMySqlDbType(col.ColumnType))}"); if (col.DoesNeedLength()) { codeWriter.Write($"({col.ColumnLength})"); } codeWriter.Write("\t"); if (col.IsIdentity) { codeWriter.Write($" AUTO_INCREMENT"); } if (col.IsPrimaryKey) { codeWriter.Write($" PRIMARY KEY"); } } codeWriter.Unindent(); /* * foreach (var column in table.Column) * if (!string.IsNullOrEmpty(column.ColumnDescription)) * { * codeWriter.WriteLine(); * codeWriter.WriteLine("GO"); * codeWriter.WriteLine(); * codeWriter.WriteLine(GetExtendedPropertyScript(table.Schema.SchemaName, table.TableName, * column.ColumnName, column.ColumnDescription)); * }*/ codeWriter.WriteLine(); codeWriter.WriteLine(") ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;"); return(codeWriter.ToString()); ; }