TvPlanIt: Implement basic infrastructure to use the correct sql field type names according to the ZConnection protocol. Based on patch by paweld (https://forum.lazarus.freepascal.org/index.php/topic,59085.0.html).

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8253 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-04-18 16:48:40 +00:00
parent 509f33e4d7
commit 3eab02dd33

View File

@ -10,6 +10,9 @@ uses
ZCompatibility, ZConnection, ZDataset; ZCompatibility, ZConnection, ZDataset;
type type
{ TVpZeosDatastore }
TVpZeosDatastore = class(TVpCustomDBDatastore) TVpZeosDatastore = class(TVpCustomDBDatastore)
private private
FConnection: TZConnection; FConnection: TZConnection;
@ -32,6 +35,11 @@ type
procedure SetTableConnections(AConnection: TZConnection); procedure SetTableConnections(AConnection: TZConnection);
function TablesExist: boolean; function TablesExist: boolean;
protected
FIdFieldTypeNameInSQL: String;
FBoolFieldTypeNameInSQL: String;
procedure AdjustSQLFieldTypeNames; virtual;
protected protected
// Fix old tables // Fix old tables
procedure AddField(ATableName, AFieldName: String; AFieldType: TFieldType; procedure AddField(ATableName, AFieldName: String; AFieldType: TFieldType;
@ -105,6 +113,33 @@ begin
FConnection.ExecuteDirect(sql); FConnection.ExecuteDirect(sql);
end; end;
{ Select the correct SQL field type names which vary between SQL dialects. }
procedure TVpZeosDatastore.AdjustSQLFieldTypeNames;
var
protocol: String;
begin
FIdFieldTypeNameInSQL := 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT';
FBoolFieldTypeNameInSQL := 'BOOL';
if Assigned(FConnection) then
begin
protocol := Lowercase(FConnection.Protocol);
if protocol = 'postgresql' then
FIdFieldTypeNameInSQL := 'SERIAL NOT NULL PRIMARY KEY'
else
if protocol = 'sqlite' then
FIdFieldTypeNameInSQL := 'INTEGER PRIMARY KEY'
else
if protocol = 'mssql' then
begin
FIdFieldTypeNameInSQL := 'INTEGER NOT NULL IDENTITY PRIMARY KEY';
FBoolFieldTypeNameInSQL := 'BIT';
end else
if protocol = 'mysql' then
FBoolFieldTypeNameInSQL := 'BIT';
end;
end;
procedure TVpZeosDatastore.CreateAllTables; procedure TVpZeosDatastore.CreateAllTables;
var var
wasConnected: Boolean; wasConnected: Boolean;
@ -131,7 +166,7 @@ begin
if ATableName = ContactsTableName then begin if ATableName = ContactsTableName then begin
FConnection.ExecuteDirect( FConnection.ExecuteDirect(
'CREATE TABLE Contacts ('+ 'CREATE TABLE Contacts ('+
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+ 'RecordID ' + FIDFieldTypeNameInSQL + ', '+
'ResourceID INTEGER, '+ 'ResourceID INTEGER, '+
'FirstName VARCHAR(50), '+ 'FirstName VARCHAR(50), '+
'LastName VARCHAR(50), '+ 'LastName VARCHAR(50), '+
@ -205,7 +240,7 @@ begin
if ATableName = EventsTableName then begin if ATableName = EventsTableName then begin
FConnection.ExecuteDirect( FConnection.ExecuteDirect(
'CREATE TABLE Events ('+ 'CREATE TABLE Events ('+
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+ 'RecordID ' + FIdFieldTypeNameInSQL + ', '+
'StartTime TIMESTAMP, '+ 'StartTime TIMESTAMP, '+
'EndTime TIMESTAMP, '+ 'EndTime TIMESTAMP, '+
'ResourceID INTEGER, '+ 'ResourceID INTEGER, '+
@ -213,9 +248,9 @@ begin
'Location VARCHAR(255), '+ 'Location VARCHAR(255), '+
'Notes VARCHAR(1024), ' + 'Notes VARCHAR(1024), ' +
'Category INTEGER, '+ 'Category INTEGER, '+
'AllDayEvent BOOL, '+ 'AllDayEvent ' + FBoolFieldTypeNameInSQL + ', '+
'DingPath VARCHAR(255), '+ 'DingPath VARCHAR(255), '+
'AlarmSet BOOL, '+ 'AlarmSet ' + FBoolFieldTypeNameInSQL + ', '+
'AlarmAdvance INTEGER, '+ 'AlarmAdvance INTEGER, '+
'AlarmAdvanceType INTEGER, '+ 'AlarmAdvanceType INTEGER, '+
'SnoozeTime TIMESTAMP, '+ 'SnoozeTime TIMESTAMP, '+
@ -248,11 +283,11 @@ begin
if ATableName = ResourceTableName then begin if ATableName = ResourceTableName then begin
FConnection.ExecuteDirect( FConnection.ExecuteDirect(
'CREATE TABLE Resources ( '+ 'CREATE TABLE Resources ( '+
'ResourceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+ 'ResourceID ' + FIdFieldTypeNameInSQL + ', '+
'Description VARCHAR(255), '+ 'Description VARCHAR(255), '+
'Notes VARCHAR(1024), '+ 'Notes VARCHAR(1024), '+
'ImageIndex INTEGER, '+ 'ImageIndex INTEGER, '+
'ResourceActive BOOL, '+ 'ResourceActive ' + FBoolFieldTypeNameInSQL + ', '+
'UserField0 VARCHAR(100), '+ 'UserField0 VARCHAR(100), '+
'UserField1 VARCHAR(100), '+ 'UserField1 VARCHAR(100), '+
'UserField2 VARCHAR(100), '+ 'UserField2 VARCHAR(100), '+
@ -268,9 +303,9 @@ begin
if ATableName = TasksTableName then begin if ATableName = TasksTableName then begin
FConnection.ExecuteDirect( FConnection.ExecuteDirect(
'CREATE TABLE Tasks ('+ 'CREATE TABLE Tasks ('+
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+ 'RecordID ' + FIdFieldTypeNameInSQL + ', '+
'ResourceID INTEGER, '+ 'ResourceID INTEGER, '+
'Complete BOOL, '+ 'Complete ' + FBoolFieldTypeNameInSQL + ', '+
'Description VARCHAR(255), '+ 'Description VARCHAR(255), '+
'Details VARCHAR(1024), '+ 'Details VARCHAR(1024), '+
'CreatedOn TIMESTAMP, '+ 'CreatedOn TIMESTAMP, '+
@ -411,10 +446,10 @@ begin
Result := FEventsTable; Result := FEventsTable;
end; end;
function TVpZeosDataStore.GetNextID(TableName: string): integer;
begin
{ This is not needed in the ZEOS datastore as these tables use { This is not needed in the ZEOS datastore as these tables use
autoincrement fields. } autoincrement fields. }
function TVpZeosDatastore.GetNextID(TableName: string): integer;
begin
result := -1; result := -1;
end; end;
@ -575,6 +610,8 @@ begin
FConnection := AValue; FConnection := AValue;
AdjustSQLFieldTypeNames;
if not Connected then if not Connected then
SetTableConnections(FConnection); SetTableConnections(FConnection);