TvPlanIt: Extended ZEOS datastore sample to show usage of a Firebird database.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8254 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-04-18 21:41:51 +00:00
parent 3eab02dd33
commit 0a97392e0b
3 changed files with 79 additions and 10 deletions

View File

@ -0,0 +1,15 @@
This demo shows how the ZeosLib components can be used for VisualPlanIt. It
takes advantage of the prebuilt TVpZeosDatastore.
The test application was tested for sqlite3 and FireBird (v3) databases. In the
header of unit1.pas there are conditional defines to select the database type;
activate ONE of them.
Firebird is tested only for v3.
Login parameters for the created Firebird database:
username: SYSDBA
password: masterkey
NOTE:
The project creates a new database on the fly using these login parameters.

View File

@ -2,13 +2,18 @@ unit Unit1;
{$mode objfpc}{$H+}
// Activate ONE of the following defines for the database system to be used:
{$DEFINE sqlite3}
{.$DEFINE firebird3}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, ComCtrls, VpBaseDS, VpZeosDs, VpDayView, VpWeekView, VpTaskList,
VpContactGrid, VpMonthView, VpResEditDlg, VpContactButtons, ZConnection,
ZDataset;
ZDataset, ZDbcIntfs;
type
@ -57,7 +62,12 @@ uses
LazFileUtils;
const
{$IFDEF sqlite3}
DBFILENAME = 'data.db';
{$ENDIF}
{$IFDEF firebird3}
DBFILENAME = 'data.fdb';
{$ENDIF}
{ TForm1 }
@ -77,15 +87,36 @@ end;
// Setting up the database connection and the datastore. Preselect a resource
// in the resource combo.
procedure TForm1.FormCreate(Sender: TObject);
var
errMsg: String = '';
begin
try
ZConnection1.Database := AppendPathDelim(Application.Location) + DBFILENAME;
ZConnection1.Protocol := 'sqlite'; //-3';
ZConnection1.Database := Application.Location + DBFILENAME;
{$IFDEF sqlite3}
ZConnection1.Protocol := 'sqlite';
{$ENDIF}
{$IFDEF firebird3}
ZConnection1.Protocol := 'firebird';
ZConnection1.User := 'SYSDBA';
ZConnection1.Password := 'masterkey';
ZConnection1.HostName := '';
ZConnection1.TransactIsolationLevel := tiReadCommitted;
ZConnection1.Properties.Clear;
if not FileExists(ZConnection1.Database) then
ZConnection1.Properties.Add(
'CreateNewDatabase=CREATE DATABASE ' + QuotedStr(ZConnection1.Database) +
' USER ' + QuotedStr('SYSDBA') +
' PASSWORD ' + QuotedStr('masterkey') +
' PAGE_SIZE 4096 DEFAULT CHARACTER SET UTF8');
{$ENDIF}
// ZConnection1.Connect; // activate this to test issue #33717
VpZeosDatastore1.Connection := ZConnection1;
VpZeosDatastore1.AutoCreate := true;
{$IFDEF firebird3}
ZConnection1.Properties.Clear;
{$ENDIF}
VpZeosDatastore1.Connected := true;
if VpZeosDatastore1.Resources.Count > 0 then
@ -94,8 +125,13 @@ begin
except
on E:Exception do
begin
MessageDlg(E.Message + LineEnding + 'Or copy sqlite3.dll to the exe folder.',
mtError, [mbOK], 0);
{$IFDEF sqlite3}
errMsg := LineEnding + 'Or copy sqlite3.dll to the exe folder.';
{$ENDIF}
{$IFDEF firebird3}
errMsg := LineEnding + 'Or copy fbclient.dll to the exe folder.';
{$ENDIF}
MessageDlg(E.Message + errMsg, mtError, [mbOK], 0);
Close;
end;
end;

View File

@ -124,6 +124,12 @@ begin
if Assigned(FConnection) then
begin
protocol := Lowercase(FConnection.Protocol);
if protocol = 'firebird' then
begin
FIdFieldTypeNameInSQL := 'INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'; // This is for Firebird v3
// FIdFieldTypeNameInSQL := 'INTEGER NOT NULL PRIMARY KEY'; // This might work for firebird v2.x
FBoolFieldTypenameInSQL := 'BOOLEAN';
end else
if protocol = 'postgresql' then
FIdFieldTypeNameInSQL := 'SERIAL NOT NULL PRIMARY KEY'
else
@ -162,10 +168,17 @@ end;
procedure TVpZeosDatastore.CreateTable(const ATableName: String;
CreateIndex: Boolean = true);
var
CREATE_TABLE: String;
begin
if (Lowercase(FConnection.Protocol) = 'firebird') then
CREATE_TABLE := 'RECREATE TABLE ' // Not clear if this is correct for firebird v2.x, it is for fb v3
else
CREATE_TABLE := 'CREATE TABLE ';
if ATableName = ContactsTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Contacts ('+
CREATE_TABLE + 'Contacts ('+
'RecordID ' + FIDFieldTypeNameInSQL + ', '+
'ResourceID INTEGER, '+
'FirstName VARCHAR(50), '+
@ -239,7 +252,7 @@ begin
end else
if ATableName = EventsTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Events ('+
CREATE_TABLE + 'Events ('+
'RecordID ' + FIdFieldTypeNameInSQL + ', '+
'StartTime TIMESTAMP, '+
'EndTime TIMESTAMP, '+
@ -282,7 +295,7 @@ begin
end else
if ATableName = ResourceTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Resources ( '+
CREATE_TABLE + 'Resources ( '+
'ResourceID ' + FIdFieldTypeNameInSQL + ', '+
'Description VARCHAR(255), '+
'Notes VARCHAR(1024), '+
@ -302,7 +315,7 @@ begin
end else
if ATableName = TasksTableName then begin
FConnection.ExecuteDirect(
'CREATE TABLE Tasks ('+
CREATE_TABLE + 'Tasks ('+
'RecordID ' + FIdFieldTypeNameInSQL + ', '+
'ResourceID INTEGER, '+
'Complete ' + FBoolFieldTypeNameInSQL + ', '+
@ -450,7 +463,12 @@ end;
autoincrement fields. }
function TVpZeosDatastore.GetNextID(TableName: string): integer;
begin
result := -1;
{
if (FConnection.Protocol = 'firebird') then // Optionally use this for firebird 2.x
Result := random(maxInt)
else
}
Result := -1;
end;
function TVpZeosDatastore.GetResourceTable : TDataset;