{ Unit name: UZeoslibWrapper.pas Author: Ruslan Nuryadin (nuryadin@gmail.com) Version: 0.1 Notes: - Please read license agreement below - This software is on alpha stage, use at your own risk - If you make a bug fix, modifications or enhancements, please tell the author and spread it
License Agreement:
This libray is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit UZeoslibWrapper;
interface
uses SysUtils, Classes, Dialogs, INIFiles, ZConnection, ZDataset, ZSQLUpdate, ZStoredProcedure, ZSQLMetadata, ZSQLProcessor, ZSQLMonitor, ZSequence, ZAbstractDataset;
type PTableItem = ^TTableItem; TTableItem = record
Name: string; Table: TZTable; end;
TZeoslibWrapper = class private FTables: TList; //of TTableItem FConnection: TZConnection; FQuery: TZQuery; function GetQueryResult: TZQuery; function IsConnected: boolean; function FindTable(name: string): TZTable; protected
public constructor Create; destructor Destroy; override;
//connection function LoadConfig(conf: string): boolean; procedure Connect; procedure Disconnect; property Connected: boolean read IsConnected;
//query related
procedure DoQuerySQL(sql: string); overload; procedure DoQuerySQL(sql: string; var result: TZQuery); overload; procedure DoModifySQL(sql: string); property QueryResult: TZQuery read GetQueryResult;
//table related
function GetTable(name: string): TZTable; end;
implementation
{ TZeoslibWrapper }
constructor TZeoslibWrapper.Create;
begin FConnection := TZConnection.Create(nil);
FQuery := TZQuery.Create(nil); FQuery.Connection := FConnection;
FTables := TList.Create; end;
destructor TZeoslibWrapper.Destroy; var
pti: PTableItem; i: integer; begin //free registered table for i := 0 to FTables.Count-1 do begin
pti := FTables.Items[i]; pti^.Table.Free; Dispose(pti); end; FTables.Free;
FQuery.Free; FConnection.Free; inherited; end;
procedure TZeoslibWrapper.Connect; begin try FConnection.Connect; except
MessageDlg('database connection failed!', mtError, [mbOK], 0); end; end;
procedure TZeoslibWrapper.Disconnect; begin FConnection.Disconnect;
end;
procedure TZeoslibWrapper.DoModifySQL(sql: string); var zq: TZQuery; begin zq := TZQuery.Create(nil); try
zq.Connection := FConnection; zq.SQL.Add(sql); zq.ExecSQL; finally zq.Free; end; end;
procedure TZeoslibWrapper.DoQuerySQL(sql: string; var result: TZQuery);
begin try result.Connection := FConnection; result.SQL.Clear; result.SQL.Add(sql); result.ExecSQL; except MessageDlg('unable to execute query! (' + sql + ')', mtError, [mbOK], 0); end;
end;
function TZeoslibWrapper.FindTable(name: string): TZTable; var pti: PTableItem; i: integer; begin
result := nil; for i := 0 to FTables.Count-1 do begin
pti := FTables.Items[i]; if CompareText(pti^.Name, name) = 0 then begin result := pti^.Table; break; end; end;
end;
procedure TZeoslibWrapper.DoQuerySQL(sql: string); begin if not FConnection.Connected then begin
MessageDlg('database connection is not active!', mtWarning, [mbOK], 0); Exit; end;
FQuery.SQL.Clear; FQuery.SQL.Add(sql); try FQuery.ExecSQL; except MessageDlg('query failed! (' + sql + ')', mtError, [mbOK], 0); end;
end;
function TZeoslibWrapper.GetQueryResult: TZQuery; begin if not FQuery.Active then begin
FQuery.Open; end; result := FQuery; end;
function TZeoslibWrapper.GetTable(name: string): TZTable; var zt, res: TZTable; pti: PTableItem;
begin result := nil;
zt := FindTable(name); if zt <> nil then begin
if not zt.Active then zt.Open; result := zt; end else begin res := TZTable.Create(nil); try
res.Connection := FConnection; res.TableName := name; res.Open; new(pti); with pti^ do begin Name := name; Table := res; end; FTables.Add(pti); result := res; except
res.Free; end; end; end;
function TZeoslibWrapper.IsConnected: boolean; begin result := FConnection.Connected; end;
function TZeoslibWrapper.LoadConfig(conf: string): boolean; var ini: TINIFile; begin if not FileExists(conf) then begin
MessageDlg('config file not found! (' + conf + ')', mtError, [mbOK], 0); result := false; Exit; end;
ini := TIniFile.Create(conf); try with FConnection do begin
Protocol := ini.ReadString('zeoslib', 'protocol', ''); HostName := ini.ReadString('zeoslib', 'host', ''); Port := ini.ReadInteger('zeoslib', 'port', 0); Database := ini.ReadString('zeoslib', 'database', ''); User := ini.ReadString('zeoslib', 'username', ''); Password := ini.ReadString('zeoslib', 'password', ''); end; finally
ini.Free; end;
result := true; end;
end.
|