/// <summary> /// Alters the column. /// </summary> /// <param name="databaseTable">The database table.</param><param name="databaseColumn">The database column.</param><param name="originalColumn">The original column.</param> /// <returns/> public override string AlterColumn(DatabaseTable databaseTable, DatabaseColumn databaseColumn, DatabaseColumn originalColumn) { var tableGenerator = CreateTableGenerator(databaseTable); if (!AlterColumnIncludeDefaultValue) { tableGenerator.IncludeDefaultValues = false; } var columnDefinition = tableGenerator.WriteColumn(databaseColumn).Trim(); var originalDefinition = "?"; if (originalColumn != null) { originalDefinition = tableGenerator.WriteColumn(originalColumn).Trim(); } //add a nice comment var comment = string.Format(CultureInfo.InvariantCulture, "-- {0} from {1} to {2}", databaseTable.Name, originalDefinition, columnDefinition); if (!SupportsAlterColumn) { //SQLite does not have modify column return(comment + Environment.NewLine + "-- TODO: change manually (no ALTER COLUMN)"); } if (databaseColumn.IsPrimaryKey || databaseColumn.IsForeignKey) { //you can't change primary keys //you can't change foreign key columns return(comment + Environment.NewLine + "-- TODO: change manually (PK or FK)"); } var dtw = new DataTypeWriter(); var dataType = dtw.WriteDataType(databaseColumn); var columnDef = Escape(databaseColumn.Name) + " TYPE " + dataType.TrimEnd(); return(comment + Environment.NewLine + string.Format(CultureInfo.InvariantCulture, AlterColumnFormat, TableName(databaseTable), columnDef.Replace("NOT NULL", String.Empty)) + string.Format("\r\nALTER TABLE {0} ALTER COLUMN {1} {2} NOT NULL;", TableName(databaseTable), Escape(databaseColumn.Name), (databaseColumn.Nullable ? "DROP" : "SET"))); }
/// <summary> /// Alters the column. /// </summary> /// <param name="databaseTable">The database table.</param><param name="databaseColumn">The database column.</param><param name="originalColumn">The original column.</param> /// <returns/> public override string AlterColumn(DatabaseTable databaseTable, DatabaseColumn databaseColumn, DatabaseColumn originalColumn) { var tableGenerator = CreateTableGenerator(databaseTable); if (!AlterColumnIncludeDefaultValue) { tableGenerator.IncludeDefaultValues = false; } var columnDefinition = tableGenerator.WriteColumn(databaseColumn).Trim(); var originalDefinition = "?"; if (originalColumn != null) { originalDefinition = tableGenerator.WriteColumn(originalColumn).Trim(); } //add a nice comment var comment = string.Format(CultureInfo.InvariantCulture, "-- {0} from {1} to {2}", databaseTable.Name, originalDefinition, columnDefinition); if (!SupportsAlterColumn) { //SQLite does not have modify column return comment + Environment.NewLine + "-- TODO: change manually (no ALTER COLUMN)"; } if (databaseColumn.IsPrimaryKey || databaseColumn.IsForeignKey) { //you can't change primary keys //you can't change foreign key columns return comment + Environment.NewLine + "-- TODO: change manually (PK or FK)"; } var dtw = new DataTypeWriter(); var dataType = dtw.WriteDataType(databaseColumn); var columnDef = Escape(databaseColumn.Name) + " TYPE " + dataType.TrimEnd(); return comment + Environment.NewLine + string.Format(CultureInfo.InvariantCulture, AlterColumnFormat, TableName(databaseTable), columnDef.Replace("NOT NULL", String.Empty)) + string.Format("\r\nALTER TABLE {0} ALTER COLUMN {1} {2} NOT NULL;", TableName(databaseTable), Escape(databaseColumn.Name), (databaseColumn.Nullable ? "DROP" : "SET")); }
public override void PostProcessing(DatabaseTable databaseTable) { if (databaseTable == null) return; //the devart providers GetSchema are a little weird so we fix them up here var typeWriter = new DataTypeWriter(); foreach (var databaseColumn in databaseTable.Columns) { var santizedType = typeWriter.WriteDataType(databaseColumn); //all the different native types are reworked if ((santizedType.StartsWith("VARCHAR", StringComparison.OrdinalIgnoreCase) || santizedType.StartsWith("CHAR", StringComparison.OrdinalIgnoreCase))) { if (databaseColumn.Length == -1 && databaseColumn.Precision > 0) { databaseColumn.Length = databaseColumn.Precision; databaseColumn.Precision = -1; } } if ((santizedType.StartsWith("NUMERIC", StringComparison.OrdinalIgnoreCase) || santizedType.StartsWith("DECIMAL", StringComparison.OrdinalIgnoreCase) || santizedType.StartsWith("INTEGER", StringComparison.OrdinalIgnoreCase))) { if (databaseColumn.Length > 0 && databaseColumn.Precision == -1) { databaseColumn.Precision = databaseColumn.Length; databaseColumn.Length = -1; } } //if it's a varchar or char, and the length is -1 but the precision is positive, swap them //and vice versa for numerics. var defaultValue = databaseColumn.DefaultValue; if (!string.IsNullOrEmpty(defaultValue) && defaultValue.StartsWith("nextval('", StringComparison.OrdinalIgnoreCase)) { databaseColumn.IsAutoNumber = true; databaseColumn.IsPrimaryKey = true; } //if defaultValue looks like the nextval from a sequence, it's a pk //change the type to serial (or bigserial), ensure it's the primary key } }
public TableGenerator(DatabaseTable table) : base(table) { DataTypeWriter = new DataTypeWriter(); }