//Anweisung in Form.Create von Form1
if DM_Update.DatenbankVersion(Vers) = true
then
begin
...
//Neues Unit DataManagement. Soll die komplette Verwaltung der Datenbank in Zukunft übernehmen
unit DataManagement;
interface
uses
SysUtils, Classes, ZConnection,
DB, ZAbstractRODataset, ZAbstractDataset,
ZDataset, Dialogs;
type
TDM_Update =
class(TDataModule)
Con_UpdateConfig: TZConnection;
Con_UpdateSecurdat: TZConnection;
Con_UpdateUser: TZConnection;
Qry_DataVersion: TZQuery;
Qry_UpdateVersion: TZQuery;
private
{ Private-Deklarationen }
public
function DatenbankVersion(
var Version : Integer) : Boolean;
function UpdateVersion(
var Version : Integer) : Boolean;
function UpdateDataDB(
var Version : Integer) : Boolean;
function UpdateUserDB(
var Version : Integer) : Boolean;
{ Public-Deklarationen }
end;
var
DM_Update: TDM_Update;
implementation
uses
Alarm;
{$R *.dfm}
function TDM_Update.DatenbankVersion(
var Version : Integer) : Boolean;
var
CurrentVersion : integer;
begin
DatenbankVersion := true;
try
Qry_DataVersion.SQL.Text :=
'
SELECT 1 FROM rdb$relations where rdb$relation_name = :TableName';
Qry_DataVersion.ParamByName('
TableName').AsString:= '
DBINFO';
Qry_DataVersion.Open;
if Qry_DataVersion.RecordCount = 0
then
begin
Qry_DataVersion.Close;
Qry_DataVersion.SQL.Text :=
'
CREATE GENERATOR GEN_DBINFO_ID; ';
Qry_DataVersion.ExecSQL;
DM_Update.Con_UpdateConfig.Commit;
Qry_DataVersion.SQL.Text :=
'
CREATE TABLE DBINFO (ID_INFO ID_DOM, VERSION NUMMER_DOM)';
Qry_DataVersion.ExecSQL;
DM_Update.Con_UpdateConfig.Commit;
Qry_DataVersion.SQL.Text :=
'
ALTER TABLE DBINFO ADD CONSTRAINT PK_DBINFO PRIMARY KEY (ID_INFO) USING INDEX ID_INFO';
Qry_DataVersion.ExecSQL;
DM_Update.Con_UpdateConfig.Commit;
Qry_DataVersion.SQL.Text :=
'
CREATE OR ALTER TRIGGER TRI_DBINFO FOR DBINFO ACTIVE BEFORE INSERT POSITION 0 '+
'
AS BEGIN '+
'
IF (NEW.ID_INFO IS NULL) THEN '+
'
NEW.ID_INFO = GEN_ID(GEN_DBINFO_ID,1);END';
Qry_DataVersion.ExecSQL;
DM_Update.Con_UpdateConfig.Commit;
Qry_DataVersion.Close;
Qry_DataVersion.SQL.Text :=
'
INSERT INTO DBINFO (VERSION)VALUES(:Bereich)';
Qry_DataVersion.ParamByName('
Bereich').Value := 1;
Qry_DataVersion.ExecSQL;
DM_Update.Con_UpdateConfig.Commit;
end
else
begin
CurrentVersion := 2;
//Wert kann je nach Softwareversion der DB angepasst werden
Qry_DataVersion.SQL.Text :=
'
SELECT * FROM DBINFO';
Qry_DataVersion.Open;
if Qry_DataVersion.FieldByName('
Version').AsInteger <> CurrentVersion
then
begin
if UpdateVersion(CurrentVersion) = true
then
begin
Form1.ServiceMemo.Lines.Add('
Datenbankupdate auf Version '+IntToStr(CurrentVersion)+
'
erfolgreich durchgeführt!');
Version := CurrentVersion;
end
end
else
Version := CurrentVersion;
end;
except
DatenbankVersion := false;
end;
end;
function TDM_Update.UpdateVersion(
var Version : Integer) : Boolean;
begin
UpdateVersion := true;
try
Qry_UpdateVersion.SQL.Text :=
'
UPDATE DBINFO SET VERSION = :Version WHERE ID_INFO =:Id';
Qry_UpdateVersion.ParamByName('
Version').Value := Version;
Qry_UpdateVersion.ParamByName('
Id').Value := 1;
Qry_UpdateVersion.ExecSQL;
Con_UpdateConfig.Commit;
case Version
of
1:
begin
UpdateDataDB(Version);
//in die Case Anweisungen je nach Notwendigkeit
UpdateUserDB(Version);
end;
2:
begin
UpdateDataDB(Version);
//in die Case Anweisungen je nach Notwendigkeit
end;
end;
except
UpdateVersion := false;
end;
end;
function TDM_Update.UpdateDataDB(
var Version : Integer) : Boolean;
begin
//Update der Datenbank Data
end;
function TDM_Update.UpdateUserDB(
var Version : Integer) : Boolean;
begin
//Update der Datenbank User
end;
end.