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;
type
{ TVpZeosDatastore }
TVpZeosDatastore = class(TVpCustomDBDatastore)
private
FConnection: TZConnection;
@ -18,7 +21,7 @@ type
FResourceTable: TZTable;
FTasksTable: TZTable;
procedure SetConnection(const AValue: TZConnection);
protected
procedure CreateTable(const ATableName: String; CreateIndex: Boolean = true);
procedure CreateAllTables;
@ -32,6 +35,11 @@ type
procedure SetTableConnections(AConnection: TZConnection);
function TablesExist: boolean;
protected
FIdFieldTypeNameInSQL: String;
FBoolFieldTypeNameInSQL: String;
procedure AdjustSQLFieldTypeNames; virtual;
protected
// Fix old tables
procedure AddField(ATableName, AFieldName: String; AFieldType: TFieldType;
@ -105,6 +113,33 @@ begin
FConnection.ExecuteDirect(sql);
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;
var
wasConnected: Boolean;
@ -131,8 +166,8 @@ begin
if ATableName = ContactsTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Contacts ('+
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
'ResourceID INTEGER, ' +
'RecordID ' + FIDFieldTypeNameInSQL + ', '+
'ResourceID INTEGER, '+
'FirstName VARCHAR(50), '+
'LastName VARCHAR(50), '+
'Title VARCHAR(20) ,'+
@ -205,7 +240,7 @@ begin
if ATableName = EventsTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Events ('+
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
'RecordID ' + FIdFieldTypeNameInSQL + ', '+
'StartTime TIMESTAMP, '+
'EndTime TIMESTAMP, '+
'ResourceID INTEGER, '+
@ -213,9 +248,9 @@ begin
'Location VARCHAR(255), '+
'Notes VARCHAR(1024), ' +
'Category INTEGER, '+
'AllDayEvent BOOL, '+
'AllDayEvent ' + FBoolFieldTypeNameInSQL + ', '+
'DingPath VARCHAR(255), '+
'AlarmSet BOOL, '+
'AlarmSet ' + FBoolFieldTypeNameInSQL + ', '+
'AlarmAdvance INTEGER, '+
'AlarmAdvanceType INTEGER, '+
'SnoozeTime TIMESTAMP, '+
@ -248,11 +283,11 @@ begin
if ATableName = ResourceTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Resources ( '+
'ResourceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
'ResourceID ' + FIdFieldTypeNameInSQL + ', '+
'Description VARCHAR(255), '+
'Notes VARCHAR(1024), '+
'ImageIndex INTEGER, '+
'ResourceActive BOOL, '+
'ResourceActive ' + FBoolFieldTypeNameInSQL + ', '+
'UserField0 VARCHAR(100), '+
'UserField1 VARCHAR(100), '+
'UserField2 VARCHAR(100), '+
@ -268,9 +303,9 @@ begin
if ATableName = TasksTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Tasks ('+
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
'RecordID ' + FIdFieldTypeNameInSQL + ', '+
'ResourceID INTEGER, '+
'Complete BOOL, '+
'Complete ' + FBoolFieldTypeNameInSQL + ', '+
'Description VARCHAR(255), '+
'Details VARCHAR(1024), '+
'CreatedOn TIMESTAMP, '+
@ -411,10 +446,10 @@ begin
Result := FEventsTable;
end;
function TVpZeosDataStore.GetNextID(TableName: string): integer;
{ This is not needed in the ZEOS datastore as these tables use
autoincrement fields. }
function TVpZeosDatastore.GetNextID(TableName: string): integer;
begin
{ This is not needed in the ZEOS datastore as these tables use
autoincrement fields. }
result := -1;
end;
@ -574,6 +609,8 @@ begin
(FConnection <> nil) and FConnection.Connected;
FConnection := AValue;
AdjustSQLFieldTypeNames;
if not Connected then
SetTableConnections(FConnection);