From 89f034cc8e96a3baa9b2ba9af7edb23ebf08c35b Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Thu, 21 Apr 2022 09:20:33 +0200 Subject: [PATCH] Support renaming columns in SQLite, and generate separate ALTER TABLE queries for each change. Closes #1256 --- source/dbconnection.pas | 2 +- source/table_editor.pas | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index affbd5a6..76bc6df9 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -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'; diff --git a/source/table_editor.pas b/source/table_editor.pas index 4c5f15d0..1deb6811 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -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;