unit uDataModuleDB;
interface
uses
SysUtils, Classes, Forms, Dialogs, Variants,
DB,
ZConnection, ZAbstractTable, ZDataset, ZAbstractRODataset,
ZAbstractDataset, ZSqlProcessor, ZSqlMonitor;
type
TDMDb =
class(TDataModule)
public
function GenerateZUpdateSQLInsert(ATable: TZTable):
String;
overload;
function GenerateZUpdateSQLInsert(ATable: TZTable; ANullAsString: Boolean):
String;
overload;
function GenerateZUpdateSQLModify(ATable: TZTable):
String;
overload;
function GenerateZUpdateSQLModify(ATable: TZTable; ANullAsString: Boolean):
String;
overload;
function GenerateZUpdateSQLDelete(ATable: TZTable):
String;
overload;
function GenerateZUpdateSQLDelete(ATable: TZTable; ANullAsString: Boolean):
String;
overload;
function GenerateZTableSQLSelectOldValues(ATable: TZTable):
String;
function GenerateZTableSQLSelectCountOldValues(ATable: TZTable):
String;
function GenerateZTableSQLWhereOldValues(ATable: TZTable):
String;
end;
var
DMDb: TDMDb;
implementation
function TDMDb.GenerateZTableSQLWhereOldValues(ATable: TZTable):
string;
var
liZ1: integer;
lSQLWhere:
String;
begin
lSQLWhere := '
';
for liZ1 := 0
to ATable.FieldCount - 1
do
begin
if (liZ1 > 0)
then
begin
lSQLWhere := lSQLWhere + '
AND ';
end;
lSQLWhere := lSQLWhere + '
(';
if(VarIsNull(ATable.Fields[liZ1].OldValue))
then
begin
lSQLWhere := lSQLWhere + '
(';
end;
lSQLWhere := lSQLWhere + ATable.Fields[liZ1].FieldName + '
= ' + '
:OLD_' + ATable.Fields[liZ1].FieldName;
if(VarIsNull(ATable.Fields[liZ1].OldValue))
then
begin
lSQLWhere := lSQLWhere + '
)';
lSQLWhere := lSQLWhere + '
OR (' + ATable.Fields[liZ1].FieldName + '
IS NULL )';
lSQLWhere := lSQLWhere + '
OR (' + ATable.Fields[liZ1].FieldName + '
= 0 )';
end;
lSQLWhere := lSQLWhere + '
)';
end;
if (lSQLWhere <> '
')
then
begin
lSQLWhere := '
WHERE ' + lSQLWhere;
end;
Result := lSQLWhere;
end;
function TDMDb.GenerateZUpdateSQLInsert(ATable: TZTable):
string;
begin
Result := GenerateZUpdateSQLInsert(ATable,true);
end;
function TDMDb.GenerateZUpdateSQLInsert(ATable: TZTable; ANullAsString: boolean):
string;
var
liZ1: integer;
lSQL:
String;
lSQLFields:
String;
lSQLValues:
String;
begin
lSQLFields := '
';
lSQLValues := '
';
lSQL := '
INSERT INTO ' + ATable.TableName;
for liZ1 := 0
to ATable.FieldCount - 1
do
begin
if (liZ1 > 0)
then
begin
lSQLFields := lSQLFields + '
, ';
lSQLValues := lSQLValues + '
, ';
end;
lSQLFields := lSQLFields + ATable.Fields[liZ1].FieldName;
if ( (ANullAsString)
and (ATable.Fields[liZ1].DataType
in [ftString,ftMemo,ftFmtMemo,ftFixedChar,ftWideString,ftFixedWideChar,ftWideMemo])
and (VarToStr(ATable.Fields[liZ1].Value) = '
')
)
then
begin
lSQLValues := lSQLValues + '
''
' + '
''
';
end
else
begin
lSQLValues := lSQLValues + '
:' + ATable.Fields[liZ1].FieldName;
end;
end;
lSQL := lSQL + '
(' + lSQLFields + '
) VALUES (' + lSQLValues + '
)';
Result := lSQL + '
;';
end;
function TDMDb.GenerateZUpdateSQLModify(ATable: TZTable):
string;
begin
Result := GenerateZUpdateSQLModify(ATable,true);
end;
function TDMDb.GenerateZUpdateSQLModify(ATable: TZTable; ANullAsString: boolean):
string;
var
liZ1: integer;
lSQL:
String;
lSQLWhere:
String;
begin
lSQL := '
UPDATE ' + ATable.TableName + '
SET ';
for liZ1 := 0
to ATable.FieldCount - 1
do
begin
if (liZ1 > 0)
then
begin
lSQL := lSQL + '
, ';
end;
lSQL := lSQL + ATable.Fields[liZ1].FieldName + '
= ';
if ( (ANullAsString)
and (ATable.Fields[liZ1].DataType
in [ftString,ftMemo,ftFmtMemo,ftFixedChar,ftWideString,ftFixedWideChar,ftWideMemo])
and (VarToStr(ATable.Fields[liZ1].Value) = '
')
)
then
begin
lSQL := lSQL + '
''
' + '
''
';
end
else
begin
lSQL := lSQL + '
:' + ATable.Fields[liZ1].FieldName;
end;
end;
lSQLWhere := GenerateZTableSQLWhereOldValues(ATable);
Result := lSQL + lSQLWhere + '
;';
end;
function TDMDb.GenerateZUpdateSQLDelete(ATable: TZTable):
string;
begin
Result := GenerateZUpdateSQLDelete(ATable,true);
end;
function TDMDb.GenerateZUpdateSQLDelete(ATable: TZTable; ANullAsString: boolean):
string;
var
lSQL:
String;
lSQLWhere:
String;
begin
lSQL := '
DELETE FROM ' + ATable.TableName;
lSQLWhere := GenerateZTableSQLWhereOldValues(ATable);
Result := lSQL + lSQLWhere + '
;';
end;
end;