Support renaming columns in SQLite, and generate separate ALTER TABLE queries for each change. Closes #1256

This commit is contained in:
Ansgar Becker
2022-04-21 09:20:33 +02:00
parent 8b4556f292
commit 89f034cc8e
2 changed files with 20 additions and 4 deletions

View File

@@ -3056,7 +3056,7 @@ begin
FSQLSpecifities[spCurrentUserHost] := 'SELECT CURRENT_USER()';
FSQLSpecifities[spLikeCompare] := '%s LIKE %s';
FSQLSpecifities[spAddColumn] := 'ADD COLUMN %s';
FSQLSpecifities[spChangeColumn] := 'CHANGE COLUMN %s %s';
FSQLSpecifities[spChangeColumn] := 'RENAME COLUMN %s TO %s'; // SQLite only supports renaming
FSQLSpecifities[spSessionVariables] := 'SELECT null, null'; // Todo: combine "PRAGMA pragma_list" + "PRAGMA a; PRAGMY b; ..."?
FSQLSpecifities[spGlobalVariables] := 'SHOW GLOBAL VARIABLES';
FSQLSpecifities[spISSchemaCol] := '%s_SCHEMA';

View File

@@ -656,7 +656,7 @@ begin
case Conn.Parameters.NetTypeGroup of
ngMySQL, ngSQLite: begin
ngMySQL: begin
ColSpec := Col.SQLCode(OverrideCollation);
// Server version requirement, see http://dev.mysql.com/doc/refman/4.1/en/alter-table.html
if Conn.ServerVersionInt >= 40001 then begin
@@ -730,6 +730,22 @@ begin
AddQuery('COMMENT ON COLUMN %s.'+Conn.QuoteIdent(Col.Name)+' IS '+Conn.EscapeString(Col.Comment));
end;
ngSQLite: begin
ColSpec := Col.SQLCode;
case Col.Status of
esModified: begin
// Rename
if Col.Name <> Col.OldName then begin
Specs.Add(Format(AlterColBase, [Conn.QuoteIdent(Col.OldName), Conn.QuoteIdent(Col.Name)]));
end;
end;
esAddedUntouched, esAddedModified: begin
Specs.Add(Format(AddColBase, [ColSpec]));
end;
end;
FinishSpecs;
end;
end;
end;
@@ -741,8 +757,8 @@ begin
for i:=0 to FColumns.Count-1 do begin
if FColumns[i].Status = esDeleted then begin
Specs.Add('DROP COLUMN '+Conn.QuoteIdent(FColumns[i].OldName));
// MSSQL wants one ALTER TABLE query per DROP COLUMN
if Conn.Parameters.IsAnyMSSQL then
// MSSQL + SQLite want one ALTER TABLE query per DROP COLUMN
if Conn.Parameters.NetTypeGroup in [ngMSSQL, ngSQLite] then
FinishSpecs;
end;
end;