From 52c57c84e34c5fa5ef2cfcd8da1c92ed337aa834 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Sat, 30 Apr 2011 05:07:50 +0000 Subject: [PATCH] Fix follow up problems introduced in r3795: DROP DEFAULT on renamed column took the old column name, and the old table name is used also when it was renamed. Revert r3795, move DROP DEFAULT clauses before the main ALTER query, and introduce a .OldDataType property on TTableColumn to check if that can have a default value. --- source/dbconnection.pas | 3 ++- source/table_editor.pas | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 51da75b0..6d9c8b60 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -230,7 +230,7 @@ type procedure SetStatus(Value: TEditingStatus); public Name, OldName: String; - DataType: TDBDatatype; + DataType, OldDataType: TDBDatatype; LengthSet: String; Unsigned, AllowNull, ZeroFill, LengthCustomized: Boolean; DefaultType: TColumnDefaultType; @@ -2698,6 +2698,7 @@ begin // Datatype Col.DataType := GetDatatypeByName(UpperCase(rx.Match[2])); + Col.OldDataType := GetDatatypeByName(UpperCase(rx.Match[2])); // Length / Set // Various datatypes, e.g. BLOBs, don't have any length property diff --git a/source/table_editor.pas b/source/table_editor.pas index e0d09922..823a60cb 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -484,6 +484,19 @@ begin end; AddQuery; + // Special case for removed default values on columns, which can neither be done in + // ALTER TABLE ... CHANGE COLUMN query, as there is no "no default" clause, nor by + // appending an ALTER COLUMN ... DROP DEFAULT, without getting an "unknown column" error. + // Also, do this after the data type was altered, if from TEXT > VARCHAR e.g. + for i:=0 to FColumns.Count-1 do begin + if (FColumns[i].FStatus = esModified) + and (FColumns[i].DefaultType = cdtNothing) + and (FColumns[i].OldDataType.HasDefault) + then + Specs.Add('ALTER '+DBObject.Connection.QuoteIdent(FColumns[i].OldName)+' DROP DEFAULT'); + end; + AddQuery; + if editName.Text <> DBObject.Name then Specs.Add('RENAME TO ' + DBObject.Connection.QuoteIdent(editName.Text)); if memoComment.Tag = ModifiedFlag then @@ -607,19 +620,6 @@ begin AddQuery; - // Special case for removed default values on columns, which can neither be done in - // ALTER TABLE ... CHANGE COLUMN query, as there is no "no default" clause, nor by - // appending an ALTER COLUMN ... DROP DEFAULT, without getting an "unknown column" error. - // Also, do this after the data type was altered, if from TEXT > VARCHAR e.g. - for i:=0 to FColumns.Count-1 do begin - if (FColumns[i].FStatus = esModified) - and (FColumns[i].DefaultType = cdtNothing) - and (FColumns[i].DataType.HasDefault) - then - Specs.Add('ALTER '+DBObject.Connection.QuoteIdent(FColumns[i].OldName)+' DROP DEFAULT'); - end; - AddQuery; - FreeAndNil(Specs); Mainform.ShowStatusMsg; Mainform.ProgressBarStatus.Hide;