/// <summary> /// Generate the SQL Server scripts. /// </summary> /// <param name="schemaTable">DataTable with all the PostgreSQL schema/table/column info.</param> /// <param name="seqTable"></param> /// <param name="conn">PG connection.</param> private static void GenerateTableScripts( DataTable schemaTable, DataTable seqTable, NpgsqlConnection conn) { var createPath = Path.Combine( _params[Parameters.OtherWorkPath].ToString(), Constants.CreateTables); var dropPath = Path.Combine( _params[Parameters.OtherWorkPath].ToString(), Constants.CreateDropTables); var truncPath = Path.Combine( _params[Parameters.OtherWorkPath].ToString(), Constants.CreateSTruncateTables); StreamWriter swCreate = null; StreamWriter swDrop = null; StreamWriter swTrunc = null; try { swCreate = new StreamWriter(createPath, false, Encoding.Default); swDrop = new StreamWriter(dropPath, false, Encoding.Default); swTrunc = new StreamWriter(truncPath, false, Encoding.Default); swCreate.WriteBeginTrans(); swDrop.WriteBeginTrans(); swTrunc.WriteBeginTrans(); swCreate.PrepCreateTable(); var savedSchema = ""; var savedTable = ""; var colInfo = new List<ColumnInfo>(); foreach (DataRow row in schemaTable.Rows) { var schema = row["schema_name"].ToString(); var table = row["table_name"].ToString(); // Schema or table changed: close table defenition. if (!savedSchema.Equals(schema) || !savedTable.Equals(table)) { if (!string.IsNullOrEmpty(savedTable)) { CloseCreateTable(swCreate, colInfo); UpdateSeqTable(colInfo, seqTable); colInfo.Clear(); } savedSchema = schema; savedTable = table; swCreate.OpenCreateTable(schema, table); swDrop.WriteDropCommand(schema, table); swTrunc.WriteTruncateCommand(schema, table); } else swCreate.WriteLine(","); // Generate column definition. ColumnInfo tmpColInfo; swCreate.GenerateColumn(row, out tmpColInfo); if (!string.IsNullOrEmpty(tmpColInfo.Schema)) { colInfo.Add(tmpColInfo); } } if (string.IsNullOrEmpty(savedSchema)) return; // Complete last writes. swCreate.CloseCreateTable(colInfo); swCreate.WriteTableDesc(conn); swCreate.WriteCommitTrans(); swDrop.WriteCommitTrans(); swTrunc.WriteCommitTrans(); } catch (Exception ex) { _log.WriteEx('E', Constants.LogTsType, ex); } finally { swCreate?.Dispose(); swDrop?.Dispose(); swTrunc?.Dispose(); } }