A
amsea
Unregistered / Unconfirmed
GUEST, unregistred user!
我写的一段代码,算是核心部分吧,出错了,谁知道如何办?
功能:保存、删除、生成完整的 SQL
{功能:保存、删除、生成完整的 SQL}
unit UTSQLOperateList;
interface
uses SysUtils, Classes, Consts, StdCtrls, upublic;
type
{一条操作包含的内容}
TSQLOperate = class
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
constructor Create;
private
public
end;
{操作列表}
TSQLOperateList = class
constructor Create; virtual;
destructor Distroy; virtual;
function GetSize(): integer;
function IsExist(FieldName: string): boolean;
private
{ Private declarations }
SO: array of TSQLOperate;
Fcount: integer;
StrHeadSQL: string;
StrTablesName: string;
function GetCount: integer;
function BuildSQL: string;
procedure SetHeadSQL(HeadSQL: string);
function GetHeadSQL: string;
procedure SetTablesName(TablesName: string);
function GetTablesName: string;
public
{ Public declarations }
function Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string): integer;
function GetCnName(index: integer): string; overload;
function GetCnName(FieldName: string): string; overload;
function GetTableName(index: integer): string; overload;
function GetTableName(FieldName: string): string; overload;
function GetFieldNames: tstringlist;
function GetRows(Index: integer): tstringlist;
function clear(): boolean;
function Delete(FieldName: string): boolean; overload;
function Delete(index: integer): boolean; overload;
property Count: Integer read GetCount;
property baseSQL: string read BuildSQL;
property TablesName: string read GetTablesName write SetTablesName;
property HeadSQL: string read GetHeadSQL write SetHeadSQL;
property SQL: string read BuildSQL;
end;
var
SQLO:TSQLOperate;
implementation
constructor TSQLOperate.Create;
begin
{初始化为空}
inherited;
end;
constructor TSQLOperateList.Create;
begin
{初始化为空}
SQLO.Create;
//^^^^^^^^^^^^^^^^^^
//错误!
//原来用的是记录,现在改成类,就有问题了。
SQLO.i:=1;
setlength(SO, 255);
Fcount := 0;
TablesName := '';
HeadSQL := 'Select * from ';
end;
destructor TSQLOperateList.Distroy;
begin
inherited Destroy;
end;
function TSQLOperateList.GetSize(): integer;
begin
{返回最大值}
result := FCount;
end;
function TSQLOperateList.Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string):
integer;
begin
{添加新的纪录}
result := FCount;
try
if not IsExist(FieldName) then
begin
so[fcount].I := fcount;
so[fcount].StrFieldName := FieldName;
so[fcount].StrValue := StrValue;
so[fcount].StrFieldCnName := FieldCnName;
so[fcount].StrTableName := TableName;
so[fcount].carrySymbol := carrySymbol;
so[fcount].Logic := Logic;
inc(FCount);
result := FCount;
end
else
begin
result := -1;
end;
except
end;
end;
function TSQLOperateList.GetCnName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := SO[index].StrFieldCnName;
end;
function TSQLOperateList.GetCnName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if SO.StrFieldName = FieldName then
result := SO.StrFieldCnName;
end;
end;
function TSQLOperateList.GetTableName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := so[index].StrTableName;
end;
function TSQLOperateList.GetTableName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
result := so.StrTableName;
end;
end;
function TSQLOperateList.Delete(FieldName: string): boolean;
var
i, h, e: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
h := i;
for e := h to count - 1 do
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
setlength(so, count - 1);
result := true;
end;
end;
end;
end;
function TSQLOperateList.Delete(index: integer): boolean;
var
e: integer;
begin
result := false;
for e := index to Count - 1 do
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
setlength(so, count - 1);
result := true;
end;
end;
function TSQLOperateList.GetFieldNames: tstringlist;
var
i: integer;
begin
result := Tstringlist.Create;
for i := 0 to Count - 1 do
begin
result.Add(so.StrTableName + '.' + so.StrFieldName + ',');
end;
end;
function TSQLOperateList.GetRows(Index: integer): tstringlist;
begin
result := Tstringlist.Create;
if (index < count) and (index >= 0) then
begin
result.Add(so[index].Logic);
result.Add(so[index].StrFieldCnName);
result.Add(so[index].carrySymbol);
result.Add(so[index].StrValue);
end;
end;
function TSQLOperateList.BuildSQL: string;
var
i: integer;
StrSQL: string;
begin
StrSQL := '';
for i := 0 to count - 1 do
begin
if StrSQL <> '' then
StrSQL := StrSQL + so.Logic + ' ';
StrSQL := StrSQL + so.StrTableName + '.' + so.StrFieldName + ' ' + so.carrySymbol + ' ';
StrSQL := StrSQL + '''' + so.StrValue + ''' ';
end;
if StrSQL <> '' then
begin
Result := HeadSQL + TablesName + ' where ' + StrSQL;
end
else
begin
Result := HeadSQL + TablesName;
end;
end;
procedure TSQLOperateList.SetHeadSQL(HeadSQL: string);
begin
StrHeadSQL := HeadSQL;
end;
function TSQLOperateList.GetHeadSQL: string;
begin
result := StrHeadSQL;
end;
procedure TSQLOperateList.SetTablesName(TablesName: string);
begin
strTablesName := TablesName;
end;
function TSQLOperateList.GetTablesName: string;
begin
Result := StrTablesName;
end;
function TSQLOperateList.IsExist(FieldName: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
result := true;
exit;
end;
end;
end;
function TSQLOperateList.clear(): boolean;
begin
setlength(so, 0);
setlength(so, 255);
fcount := 0;
result := true;
end;
function TSQLOperateList.GetCount: integer;
begin
result := fcount;
end;
end.
功能:保存、删除、生成完整的 SQL
{功能:保存、删除、生成完整的 SQL}
unit UTSQLOperateList;
interface
uses SysUtils, Classes, Consts, StdCtrls, upublic;
type
{一条操作包含的内容}
TSQLOperate = class
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
constructor Create;
private
public
end;
{操作列表}
TSQLOperateList = class
constructor Create; virtual;
destructor Distroy; virtual;
function GetSize(): integer;
function IsExist(FieldName: string): boolean;
private
{ Private declarations }
SO: array of TSQLOperate;
Fcount: integer;
StrHeadSQL: string;
StrTablesName: string;
function GetCount: integer;
function BuildSQL: string;
procedure SetHeadSQL(HeadSQL: string);
function GetHeadSQL: string;
procedure SetTablesName(TablesName: string);
function GetTablesName: string;
public
{ Public declarations }
function Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string): integer;
function GetCnName(index: integer): string; overload;
function GetCnName(FieldName: string): string; overload;
function GetTableName(index: integer): string; overload;
function GetTableName(FieldName: string): string; overload;
function GetFieldNames: tstringlist;
function GetRows(Index: integer): tstringlist;
function clear(): boolean;
function Delete(FieldName: string): boolean; overload;
function Delete(index: integer): boolean; overload;
property Count: Integer read GetCount;
property baseSQL: string read BuildSQL;
property TablesName: string read GetTablesName write SetTablesName;
property HeadSQL: string read GetHeadSQL write SetHeadSQL;
property SQL: string read BuildSQL;
end;
var
SQLO:TSQLOperate;
implementation
constructor TSQLOperate.Create;
begin
{初始化为空}
inherited;
end;
constructor TSQLOperateList.Create;
begin
{初始化为空}
SQLO.Create;
//^^^^^^^^^^^^^^^^^^
//错误!
//原来用的是记录,现在改成类,就有问题了。
SQLO.i:=1;
setlength(SO, 255);
Fcount := 0;
TablesName := '';
HeadSQL := 'Select * from ';
end;
destructor TSQLOperateList.Distroy;
begin
inherited Destroy;
end;
function TSQLOperateList.GetSize(): integer;
begin
{返回最大值}
result := FCount;
end;
function TSQLOperateList.Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string):
integer;
begin
{添加新的纪录}
result := FCount;
try
if not IsExist(FieldName) then
begin
so[fcount].I := fcount;
so[fcount].StrFieldName := FieldName;
so[fcount].StrValue := StrValue;
so[fcount].StrFieldCnName := FieldCnName;
so[fcount].StrTableName := TableName;
so[fcount].carrySymbol := carrySymbol;
so[fcount].Logic := Logic;
inc(FCount);
result := FCount;
end
else
begin
result := -1;
end;
except
end;
end;
function TSQLOperateList.GetCnName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := SO[index].StrFieldCnName;
end;
function TSQLOperateList.GetCnName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if SO.StrFieldName = FieldName then
result := SO.StrFieldCnName;
end;
end;
function TSQLOperateList.GetTableName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := so[index].StrTableName;
end;
function TSQLOperateList.GetTableName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
result := so.StrTableName;
end;
end;
function TSQLOperateList.Delete(FieldName: string): boolean;
var
i, h, e: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
h := i;
for e := h to count - 1 do
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
setlength(so, count - 1);
result := true;
end;
end;
end;
end;
function TSQLOperateList.Delete(index: integer): boolean;
var
e: integer;
begin
result := false;
for e := index to Count - 1 do
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
setlength(so, count - 1);
result := true;
end;
end;
function TSQLOperateList.GetFieldNames: tstringlist;
var
i: integer;
begin
result := Tstringlist.Create;
for i := 0 to Count - 1 do
begin
result.Add(so.StrTableName + '.' + so.StrFieldName + ',');
end;
end;
function TSQLOperateList.GetRows(Index: integer): tstringlist;
begin
result := Tstringlist.Create;
if (index < count) and (index >= 0) then
begin
result.Add(so[index].Logic);
result.Add(so[index].StrFieldCnName);
result.Add(so[index].carrySymbol);
result.Add(so[index].StrValue);
end;
end;
function TSQLOperateList.BuildSQL: string;
var
i: integer;
StrSQL: string;
begin
StrSQL := '';
for i := 0 to count - 1 do
begin
if StrSQL <> '' then
StrSQL := StrSQL + so.Logic + ' ';
StrSQL := StrSQL + so.StrTableName + '.' + so.StrFieldName + ' ' + so.carrySymbol + ' ';
StrSQL := StrSQL + '''' + so.StrValue + ''' ';
end;
if StrSQL <> '' then
begin
Result := HeadSQL + TablesName + ' where ' + StrSQL;
end
else
begin
Result := HeadSQL + TablesName;
end;
end;
procedure TSQLOperateList.SetHeadSQL(HeadSQL: string);
begin
StrHeadSQL := HeadSQL;
end;
function TSQLOperateList.GetHeadSQL: string;
begin
result := StrHeadSQL;
end;
procedure TSQLOperateList.SetTablesName(TablesName: string);
begin
strTablesName := TablesName;
end;
function TSQLOperateList.GetTablesName: string;
begin
Result := StrTablesName;
end;
function TSQLOperateList.IsExist(FieldName: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
result := true;
exit;
end;
end;
end;
function TSQLOperateList.clear(): boolean;
begin
setlength(so, 0);
setlength(so, 255);
fcount := 0;
result := true;
end;
function TSQLOperateList.GetCount: integer;
begin
result := fcount;
end;
end.