unit uDM;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sqlite3conn, sqldb, sqldblib, IBConnection, mysql40conn,
mysql56conn, mysql57conn, mysql55conn, mssqlconn, db, LazFileUtils;
const
connList : array [0..4] of string =
('SQLite3', 'MySQL 4.0', 'MySQL 5.5', 'MySQL 5.6', 'MS SQL');
type
{ TDM }
TDM = class(TDataModule)
dsDataSource: TDataSource;
qSQLQuery: TSQLQuery;
SQLDBLibraryLoader: TSQLDBLibraryLoader;
SQLTransaction: TSQLTransaction;
SQLTranUpdate: TSQLTransaction;
connSQL : TSQLConnection;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
FconnType: integer;
paramList : TStringList;
procedure SetconnType(AValue: integer);
public
function GetCLParameter(const paramName : string; const defValue : string = '') : string;
function FindConfigName(const fName : string) : string;
function ConnectToDatabase(var SQLConnection : TSQLConnection; const test : boolean = false) : boolean;
property connType : integer read FconnType write SetconnType;
end;
var
DM: TDM;
implementation
{$R *.lfm}
uses uGlobal;
// this part is skipped
.....
// ConnectToDatabase is to create a new connection depending on what connection type is in the configuration file
// parameters: SQLconnection returns working connection
// test is true on program start to find out if we can connect at all; the returned connection serves system tables
function TDM.ConnectToDatabase(var SQLConnection : TSQLConnection; const test : boolean = false) : boolean;
var
connName : string;
i : integer;
begin
Result := false;
if test then
begin
connName := gConf.ReadString(secConn, 'connectionType', connList[0]);
if gConf.ReadError then exit; // connection type does not exists
FconnType := -1;
for i := 0 to High(connList) do
if connName = connList[i] then
FconnType := i;
if FconnType = -1 then exit; // no match
SQLconnection := connSQL;
SQLTranUpdate.DataBase := SQLConnection;
end;
SQLDBLibraryLoader.ConnectionType := connList[FconnType];
case FconnType of
0 : begin // SQLite3
SQLiteLibraryName := gConf.ReadString(secConn, 'libraryName', SQLiteLibraryName);
SQLConnection := TSQLite3Connection.Create(self);
end;
1 : begin //MySQL4.0
SQLConnection := TMySQL40Connection.Create(self);
TMySQL40Connection(SQLConnection).Port := gConf.ReadInteger(secConn, 'port');
end;
2 : begin //MySQL5.5
SQLConnection := TMySQL55Connection.Create(self);
TMySQL55Connection(SQLConnection).Port := gConf.ReadInteger(secConn, 'port');
end;
3 : begin //MySQL5.7
SQLConnection := TMySQL57Connection.Create(self);
TMySQL57Connection(SQLConnection).Port := gConf.ReadInteger(secConn, 'port');
TMySQL57Connection(SQLConnection).SkipLibraryVersionCheck:= true;
{$IFDEF UNIX}
SQLDBLibraryLoader.LibraryName := gConf.ReadString(secConn, 'libraryName', 'libmysqlclient.so');
{$ENDIF}
end;
4 : begin //MSSQL
SQLConnection := TMSSQLConnection.Create(self);
end;
end;
SQLConnection.DatabaseName := gConf.ReadString(secConn, 'databaseName');
SQLConnection.HostName := gConf.ReadString(secConn, 'hostName');
SQLConnection.UserName := gConf.ReadString(secConn, 'userName');
SQLConnection.Password := gConf.ReadString(secConn, 'password');
SQLConnection.LoginPrompt := gConf.ReadBoolean(secConn, 'loginPrompt');
if test then
begin
SQLDBLibraryLoader.Enabled := true;
SQLDBLibraryLoader.LoadLibrary;
end;
SQLConnection.Open;
Result := SQLConnection.Connected;
if test then SQLConnection.Close;
end;