You've already forked lazarus-ccr
tvplanit: Initial commit of sqlite3 datastore (based on sqldb)
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4761 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
47
components/tvplanit/packages/laz_visualplanit_sqlite3.lpk
Normal file
47
components/tvplanit/packages/laz_visualplanit_sqlite3.lpk
Normal file
@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<Package Version="4">
|
||||
<PathDelim Value="\"/>
|
||||
<Name Value="laz_visualplanit_sqlite3"/>
|
||||
<Type Value="RunAndDesignTime"/>
|
||||
<Author Value="W. Pamler"/>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<SearchPaths>
|
||||
<OtherUnitFiles Value="..\source"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Description Value="Visual PlanIt datastore for SQLITE3 connection using SqlDB components"/>
|
||||
<Version Major="1" Release="4"/>
|
||||
<Files Count="2">
|
||||
<Item1>
|
||||
<Filename Value="..\source\vpsqlite3ds.pas"/>
|
||||
<UnitName Value="VpSQLite3DS"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Filename Value="..\source\vpregsqlite3.pas"/>
|
||||
<HasRegisterProc Value="True"/>
|
||||
<UnitName Value="vpregsqlite3"/>
|
||||
</Item2>
|
||||
</Files>
|
||||
<RequiredPkgs Count="3">
|
||||
<Item1>
|
||||
<PackageName Value="SQLDBLaz"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<PackageName Value="laz_visualplanit"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<PackageName Value="FCL"/>
|
||||
</Item3>
|
||||
</RequiredPkgs>
|
||||
<UsageOptions>
|
||||
<UnitPath Value="$(PkgOutDir)"/>
|
||||
</UsageOptions>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
</Package>
|
||||
</CONFIG>
|
28
components/tvplanit/source/vpregsqlite3.pas
Normal file
28
components/tvplanit/source/vpregsqlite3.pas
Normal file
@ -0,0 +1,28 @@
|
||||
unit vpregsqlite3;
|
||||
{ Registration unit for the SQLite3 connection using SqlDb }
|
||||
|
||||
{$I Vp.INC} // Compiler version defines
|
||||
{$R vpregsqlite3.res} // Palette glyphs
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
{$IFDEF DELPHI}
|
||||
// Windows,
|
||||
{$IFDEF VERSION6} DesignIntf, DesignEditors, {$ELSE} DsgnIntf, {$ENDIF}
|
||||
{$ENDIF}
|
||||
Classes;
|
||||
|
||||
procedure Register;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
VpSqlite3DS;
|
||||
|
||||
procedure Register;
|
||||
begin
|
||||
RegisterComponents('Visual PlanIt', [TVpSqlite3Datastore]);
|
||||
end;
|
||||
|
||||
end.
|
BIN
components/tvplanit/source/vpregsqlite3.res
Normal file
BIN
components/tvplanit/source/vpregsqlite3.res
Normal file
Binary file not shown.
427
components/tvplanit/source/vpsqlite3ds.pas
Normal file
427
components/tvplanit/source/vpsqlite3ds.pas
Normal file
@ -0,0 +1,427 @@
|
||||
unit VpSQLite3DS;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, DB,
|
||||
VpBaseDS, VpDBDS,
|
||||
sqlite3conn, sqldb;
|
||||
|
||||
type
|
||||
TVpSqlite3Datastore = class(TVpCustomDBDatastore)
|
||||
private
|
||||
FConnection: TSqlite3Connection;
|
||||
FContactsTable: TSQLQuery;
|
||||
FEventsTable: TSQLQuery;
|
||||
FResourceTable: TSQLQuery;
|
||||
FTasksTable: TSQLQuery;
|
||||
procedure SetConnection(const AValue: TSqlite3Connection);
|
||||
|
||||
protected
|
||||
procedure CreateTable(const ATableName: String);
|
||||
function GetContactsTable: TDataset; override;
|
||||
function GetEventsTable: TDataset; override;
|
||||
function GetResourceTable: TDataset; override;
|
||||
function GetTasksTable: TDataset; override;
|
||||
procedure Loaded; override;
|
||||
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
|
||||
procedure OpenTables;
|
||||
procedure SetConnected(const AValue: Boolean); override;
|
||||
|
||||
public
|
||||
constructor Create(AOwner: TComponent); override;
|
||||
procedure CreateTables;
|
||||
function GetNextID(TableName: string): int64; override;
|
||||
procedure PostEvents; override;
|
||||
procedure PostContacts; override;
|
||||
procedure PostTasks; override;
|
||||
procedure PostResources; override;
|
||||
|
||||
property ResourceTable;
|
||||
property EventsTable;
|
||||
property ContactsTable;
|
||||
property TasksTable;
|
||||
|
||||
published
|
||||
property Connection: TSqlite3Connection read FConnection write SetConnection;
|
||||
|
||||
// inherited
|
||||
property AutoConnect;
|
||||
property AutoCreate default true;
|
||||
property Connected;
|
||||
end;
|
||||
|
||||
var
|
||||
// More information on the use of these values is below.
|
||||
// They need not be set as constants in your application. They can be any valid value
|
||||
APPLICATION_ID : LongWord = 1189021115; // must be a 32-bit Unsigned Integer (Longword 0..4294967295)
|
||||
USER_VERSION : LongInt = 23400001; // must be a 32-bit Signed Integer (LongInt -2147483648..2147483647)
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
LazFileUtils,
|
||||
VpConst;
|
||||
|
||||
{ TVpZeosDatastore }
|
||||
|
||||
constructor TVpSqlite3Datastore.Create(AOwner: TComponent);
|
||||
begin
|
||||
inherited;
|
||||
|
||||
FContactsTable := TSQLQuery.Create(self);
|
||||
FContactsTable.SQL.Add('SELECT * FROM Contacts');
|
||||
|
||||
FEventsTable := TSQLQuery.Create(Self);
|
||||
FEventsTable.SQL.Add('SELECT * FROM Events');
|
||||
|
||||
FResourceTable := TSQLQuery.Create(self);
|
||||
FResourceTable.SQL.Add('SELECT * FROM Resources');
|
||||
|
||||
FTasksTable := TSQLQuery.Create(self);
|
||||
FTasksTable.SQL.Add('SELECT * FROM Tasks');
|
||||
end;
|
||||
|
||||
// Connection and tables are active afterwards!
|
||||
procedure TVpSqlite3Datastore.CreateTables;
|
||||
begin
|
||||
if FileExists(FConnection.DatabaseName) then
|
||||
exit;
|
||||
|
||||
FConnection.Close;
|
||||
|
||||
if FContactsTable.Transaction = nil then
|
||||
FContactsTable.Transaction := FConnection.Transaction;
|
||||
if FEventsTable.Transaction = nil then
|
||||
FEventsTable.Transaction := FConnection.Transaction;
|
||||
if FResourceTable.Transaction = nil then
|
||||
FResourceTable.Transaction := FConnection.Transaction;
|
||||
if FTasksTable.Transaction = nil then
|
||||
FTasksTable.Transaction := FConnection.Transaction;
|
||||
|
||||
FConnection.Connected := true;
|
||||
FConnection.Transaction.Active := true;
|
||||
|
||||
// Per the SQLite Documentation (edited for clarity):
|
||||
// The pragma user_version is used to set or get the value of the user-version.
|
||||
// The user-version is a big-endian 32-bit signed integer stored in the database header at offset 60.
|
||||
// The user-version is not used internally by SQLite. It may be used by applications for any purpose.
|
||||
// http://www.sqlite.org/pragma.html#pragma_schema_version
|
||||
// FConnection.ExecuteDirect('PRAGMA user_version = ' + IntToStr(USER_VERSION) + ';');
|
||||
|
||||
// Per the SQLite Documentation:
|
||||
// The application_id PRAGMA is used to query or set the 32-bit unsigned big-endian
|
||||
// "Application ID" integer located at offset 68 into the database header.
|
||||
// Applications that use SQLite as their application file-format should set the
|
||||
// Application ID integer to a unique integer so that utilities such as file(1) can
|
||||
// determine the specific file type rather than just reporting "SQLite3 Database".
|
||||
// A list of assigned application IDs can be seen by consulting the magic.txt file
|
||||
// in the SQLite source repository.
|
||||
// http://www.sqlite.org/pragma.html#pragma_application_id
|
||||
// FConnection.ExecuteDirect('PRAGMA application_id = ' + IntToStr(APPLICATION_ID) + ';');
|
||||
|
||||
CreateTable(ContactsTableName);
|
||||
CreateTable(EventsTableName);
|
||||
CreateTable(ResourceTableName);
|
||||
CreateTable(TasksTableName);
|
||||
|
||||
FConnection.Transaction.Commit;
|
||||
FConnection.Connected := false;
|
||||
|
||||
Connected := AutoConnect;
|
||||
|
||||
//OpenTables;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.CreateTable(const ATableName: String);
|
||||
begin
|
||||
if ATableName = ContactsTableName then begin
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE TABLE Contacts ('+
|
||||
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
|
||||
'ResourceID INTEGER,' +
|
||||
'FirstName VARCHAR(50) ,'+
|
||||
'LastName VARCHAR(50) , '+
|
||||
'Birthdate DATE, '+
|
||||
'Anniversary DATE, '+
|
||||
'Title VARCHAR(50) ,'+
|
||||
'Company VARCHAR(50) ,'+
|
||||
'Job_Position VARCHAR(30), '+
|
||||
'Address VARCHAR(100), '+
|
||||
'City VARCHAR(50), '+
|
||||
'State VARCHAR(25), '+
|
||||
'Zip VARCHAR(10), '+
|
||||
'Country VARCHAR(25), '+
|
||||
'Notes VARCHAR(1024), '+
|
||||
'Phone1 VARCHAR(25), '+
|
||||
'Phone2 VARCHAR(25), '+
|
||||
'Phone3 VARCHAR(25), '+
|
||||
'Phone4 VARCHAR(25), '+
|
||||
'Phone5 VARCHAR(25), '+
|
||||
'PhoneType1 INTEGER, '+
|
||||
'PhoneType2 INTEGER, '+
|
||||
'PhoneType3 INTEGER, '+
|
||||
'PhoneType4 INTEGER, '+
|
||||
'PhoneType5 INTEGER, '+
|
||||
'Category INTEGER, '+
|
||||
'EMail VARCHAR(100), '+
|
||||
'Custom1 VARCHAR(100), '+
|
||||
'Custom2 VARCHAR(100),'+
|
||||
'Custom3 VARCHAR(100), '+
|
||||
'Custom4 VARCHAR(100), '+
|
||||
'UserField0 VARCHAR(100), '+
|
||||
'UserField1 VARCHAR(100), '+
|
||||
'UserField2 VARCHAR(100), '+
|
||||
'UserField3 VARCHAR(100), '+
|
||||
'UserField4 VARCHAR(100), '+
|
||||
'UserField5 VARCHAR(100), '+
|
||||
'UserField6 VARCHAR(100), '+
|
||||
'UserField7 VARCHAR(100), '+
|
||||
'UserField8 VARCHAR(100), '+
|
||||
'UserField9 VARCHAR(100) )'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX ContactsResourceID_idx ON Contacts(ResourceID)'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX ContactsName_idx ON Contacts(LastName, FirstName)'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX ContactsCompany_idx ON Contacts(Company)'
|
||||
);
|
||||
end else
|
||||
if ATableName = EventsTableName then begin
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE TABLE Events ('+
|
||||
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
|
||||
'StartTime TIMESTAMP, '+
|
||||
'EndTime TIMESTAMP, '+
|
||||
'ResourceID INTEGER, '+
|
||||
'Description VARCHAR(255), '+
|
||||
'Location VARCHAR(255), '+
|
||||
'Notes VARCHAR(1024), ' +
|
||||
'Category INTEGER, '+
|
||||
'AllDayEvent BOOL, '+
|
||||
'DingPath VARCHAR(255), '+
|
||||
'AlarmSet BOOL, '+
|
||||
'AlarmAdvance INTEGER, '+
|
||||
'AlarmAdvanceType INTEGER, '+
|
||||
'SnoozeTime TIMESTAMP, '+
|
||||
'RepeatCode INTEGER, '+
|
||||
'RepeatRangeEnd TIMESTAMP, '+
|
||||
'CustomInterval INTEGER, '+
|
||||
'UserField0 VARCHAR(100), '+
|
||||
'UserField1 VARCHAR(100), '+
|
||||
'UserField2 VARCHAR(100), '+
|
||||
'UserField3 VARCHAR(100), '+
|
||||
'UserField4 VARCHAR(100), '+
|
||||
'UserField5 VARCHAR(100), '+
|
||||
'UserField6 VARCHAR(100), '+
|
||||
'UserField7 VARCHAR(100), '+
|
||||
'UserField8 VARCHAR(100), '+
|
||||
'UserField9 VARCHAR(100) )'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX EventsResourceID_idx ON Events(ResourceID)'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX EventsStartTime_idx ON Events(StartTime)'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX EventsEndTime_idx ON Events(EndTime)'
|
||||
);
|
||||
end else
|
||||
if ATableName = ResourceTableName then begin
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE TABLE Resources ( '+
|
||||
'ResourceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
|
||||
'Description VARCHAR(255), '+
|
||||
'Notes VARCHAR(1024), '+
|
||||
'ImageIndex INTEGER, '+
|
||||
'ResourceActive BOOL, '+
|
||||
'UserField0 VARCHAR(100), '+
|
||||
'UserField1 VARCHAR(100), '+
|
||||
'UserField2 VARCHAR(100), '+
|
||||
'UserField3 VARCHAR(100), '+
|
||||
'UserField4 VARCHAR(100), '+
|
||||
'UserField5 VARCHAR(100), '+
|
||||
'UserField6 VARCHAR(100), '+
|
||||
'UserField7 VARCHAR(100), '+
|
||||
'UserField8 VARCHAR(100), '+
|
||||
'UserField9 VARCHAR(100) )'
|
||||
);
|
||||
end else
|
||||
if ATableName = TasksTableName then begin
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE TABLE Tasks ('+
|
||||
'RecordID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '+
|
||||
'ResourceID INTEGER, '+
|
||||
'Complete BOOL, '+
|
||||
'Description VARCHAR(255), '+
|
||||
'Details VARCHAR(1024), '+
|
||||
'CreatedOn TIMESTAMP, '+
|
||||
'Priority INTEGER, '+
|
||||
'Category INTEGER, '+
|
||||
'CompletedOn TIMESTAMP, '+
|
||||
'DueDate TIMESTAMP, '+
|
||||
'UserField0 VARCHAR(100), '+
|
||||
'UserField1 VARCHAR(100), '+
|
||||
'UserField2 VARCHAR(100), '+
|
||||
'UserField3 VARCHAR(100), '+
|
||||
'UserField4 VARCHAR(100), '+
|
||||
'UserField5 VARCHAR(100), '+
|
||||
'UserField6 VARCHAR(100), '+
|
||||
'UserField7 VARCHAR(100), '+
|
||||
'UserField8 VARCHAR(100), '+
|
||||
'UserField9 VARCHAR(100) )'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX TasksResourceID_idx ON Tasks(ResourceID)'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX TasksDueDate_idx ON Tasks(DueDate)'
|
||||
);
|
||||
FConnection.ExecuteDirect(
|
||||
'CREATE INDEX TasksCompletedOn_idx ON Tasks(CompletedOn)'
|
||||
);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TVpSqlite3Datastore.GetContactsTable: TDataset;
|
||||
begin
|
||||
Result := FContactsTable;
|
||||
end;
|
||||
|
||||
function TVpSqlite3Datastore.GetEventsTable: TDataset;
|
||||
begin
|
||||
Result := FEventsTable;
|
||||
end;
|
||||
|
||||
function TVpSqlite3DataStore.GetNextID(TableName: string): int64;
|
||||
begin
|
||||
{ This is not needed in the SQLITE3 datastore as these tables use
|
||||
autoincrement fields. }
|
||||
Result := -1;
|
||||
end;
|
||||
|
||||
function TVpSqlite3Datastore.GetResourceTable : TDataset;
|
||||
begin
|
||||
Result := FResourceTable;
|
||||
end;
|
||||
|
||||
function TVpSqlite3Datastore.GetTasksTable : TDataset;
|
||||
begin
|
||||
Result := FTasksTable;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.Loaded;
|
||||
begin
|
||||
inherited;
|
||||
if not (csDesigning in ComponentState) then
|
||||
Connected := AutoConnect;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.Notification(AComponent: TComponent;
|
||||
Operation: TOperation);
|
||||
begin
|
||||
inherited Notification(AComponent, Operation);
|
||||
if (Operation = opRemove) and (AComponent = FConnection) then
|
||||
FConnection := nil;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.OpenTables;
|
||||
begin
|
||||
if FContactsTable.Transaction = nil then
|
||||
FContactsTable.Transaction := FConnection.Transaction;
|
||||
FContactsTable.Open;
|
||||
|
||||
if FEventsTable.Transaction = nil then
|
||||
FEventsTable.Transaction := FConnection.Transaction;
|
||||
FEventsTable.Open;
|
||||
|
||||
if FResourceTable.Transaction = nil then
|
||||
FResourceTable.Transaction := FConnection.Transaction;
|
||||
FResourceTable.Open;
|
||||
|
||||
if FTasksTable.Transaction = nil then
|
||||
FTasksTable.Transaction := FConnection.Transaction;
|
||||
FTasksTable.Open;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.PostContacts;
|
||||
begin
|
||||
inherited;
|
||||
FContactsTable.ApplyUpdates;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.PostEvents;
|
||||
begin
|
||||
inherited;
|
||||
FEventsTable.ApplyUpdates;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.PostResources;
|
||||
begin
|
||||
inherited;
|
||||
FResourceTable.ApplyUpdates;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.PostTasks;
|
||||
begin
|
||||
inherited;
|
||||
FTasksTable.ApplyUpdates;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.SetConnected(const AValue: Boolean);
|
||||
begin
|
||||
if (FConnection = nil) or (FConnection.Transaction = nil) then
|
||||
exit;
|
||||
|
||||
if AValue = Connected then
|
||||
exit;
|
||||
|
||||
if AValue and AutoCreate then
|
||||
CreateTables;
|
||||
|
||||
FConnection.Connected := AValue;
|
||||
if AValue then
|
||||
begin
|
||||
FConnection.Transaction.Active := true;
|
||||
OpenTables;
|
||||
end;
|
||||
|
||||
inherited SetConnected(AValue);
|
||||
|
||||
if FConnection.Connected then
|
||||
Load;
|
||||
end;
|
||||
|
||||
procedure TVpSqlite3Datastore.SetConnection(const AValue: TSqlite3Connection);
|
||||
var
|
||||
wasConnected: Boolean;
|
||||
begin
|
||||
if AValue = FConnection then
|
||||
exit;
|
||||
|
||||
// To do: clear planit lists...
|
||||
if FConnection <> nil then begin
|
||||
wasConnected := FConnection.Connected;
|
||||
Connected := false;
|
||||
end;
|
||||
FConnection := AValue;
|
||||
|
||||
FContactsTable.Database := FConnection;
|
||||
FContactsTable.Transaction := FConnection.Transaction;
|
||||
|
||||
FEventsTable.Database := FConnection;
|
||||
FEventsTable.Transaction := FConnection.Transaction;
|
||||
|
||||
FResourceTable.Database := FConnection;
|
||||
FResourceTable.Transaction := FConnection.Transaction;
|
||||
|
||||
FTasksTable.Database := FConnection;
|
||||
FTasksTable.Transaction := FConnection.Transaction;
|
||||
if wasConnected then Connected := true;
|
||||
end;
|
||||
|
||||
end.
|
Reference in New Issue
Block a user