TMyString = class(TObject)
private
FStrBeginTran: string;
FStrEndTran: string;
protected
function GetData(myData: TMyField): string; virtual;
function GetFields(myData: TData): string; virtual;
public
property StrBeginTran: string read FStrBeginTran;
property StrEndTran: string read FStrEndTran;
class procedure Split(const A, OldPattern: string; var Str: TStringList);
class function SetInsert(tbName, strData, strFields: string): string;
class function SetUpdate(tbName, strData, strFields: string): string;
class function SetDelete(tbName, strData, strFields: string): string;
class function GetStrUnion(str: TStringList): string;
class procedure StrListCompare(s1, s2: TStringList; var s3, s4: TStringList);
procedure SetInsertList(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList); virtual;
procedure SetUpdateList(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList); virtual;
procedure SetDeleteList(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList); virtual;
procedure SetInsertListByDept(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList; Dept: string); virtual;
procedure SetInsertListByDept1(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList; DeptNo, DeptName: string); virtual;
procedure SetUpdateListByDept(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList; Dept: string); virtual;
procedure SetDeleteListByDept(tbName: string; strDataList: TStringList; strFields: string; var
strList: TStringList; Dept: string); virtual;
constructor Create; virtual;
end;
implementation
{ TMyString }
constructor TMyString.Create;
begin
FStrBeginTran := 'Begin Tran';
FStrEndTran := 'Commit Tran';
end;
/// <summary>
/// 获得数据
/// </summary>
/// <param name="myData"> </param>
/// <returns> 数据</returns>
function TMyString.GetData(myData: TMyField): string;
begin
Result := '';
end;
/// <summary>
/// 获得字段
/// </summary>
/// <param name="myData"> </param>
/// <returns> 字段值</returns>
function TMyString.GetFields(myData: TData): string;
begin
Result := '';
end;
class function TMyString.GetStrUnion(str: TStringList): string;
var
i: Integer;
stlTemp, stlTemp1: TStringList;
s, temp: string;
begin
s := '';
stlTemp := TStringList.Create;
stlTemp1 := TStringList.Create;
for i := 0 to str.Count - 1 do
begin
if Trim(str[i]) = '' then
Continue;
s := s + str[i] + ',';
end;
s := copy(s, 1, Length(s) - 1);
stlTemp.CommaText := s;
stlTemp.Sorted := True;
for i := 0 to stlTemp.Count - 1 do
begin
temp := stlTemp[i];
if stlTemp1.IndexOf(temp) = -1 then
stlTemp1.Add(temp);
end;
Result := stlTemp1.CommaText;
stlTemp.Free;
stlTemp1.Free;
end;
class function TMyString.SetDelete(tbName, strData, strFields: string): string;
var
i: Integer;
DataList, FieldsList: TStringList;
str: string;
begin
DataList := TStringList.Create;
FieldsList := TStringList.Create;
Split(strData, ',', DataList);
Split(strFields, ',', FieldsList);
str := 'Delete from ' + tbName + ' where 1=1 ';
for i := 0 to DataList.Count - 1 do
begin
str := str + ' and ' + FieldsList[i] + '=' + DataList[i];
end;
//str :=Copy(str,1,Length(str)-1);
DataList.Free;
FieldsList.Free;
Result := str;
end;
procedure TMyString.SetDeleteList(tbName: string; strDataList: TStringList;
strFields: string; var strList: TStringList);
var
i: Integer;
begin
for i := 0 to strDataList.Count - 1 do
begin
strList.Add(SetDelete(tbName, strDataList[i], strFields));
end;
end;
procedure TMyString.SetDeleteListByDept(tbName: string;
strDataList: TStringList; strFields: string; var strList: TStringList;
Dept: string);
var
i: Integer;
begin
for i := 0 to strDataList.Count - 1 do
begin
strList.Add(SetDelete(tbName, strDataList[i], strFields));
end;
end;
class function TMyString.SetInsert(tbName, strData, strFields: string): string;
var
str: string;
begin
str := 'insert into ' + tbName + '(' + strFields + ') values(' + strData + ')';
Result := str;
end;
procedure TMyString.SetInsertList(tbName: string; strDataList: TStringList;
strFields: string; var strList: TStringList);
var
i: Integer;
begin
for i := 0 to strDataList.Count - 1 do
begin
strList.Add(SetInsert(tbName, strDataList[i], strFields));
end;
end;
procedure TMyString.SetInsertListByDept(tbName: string;
strDataList: TStringList; strFields: string; var strList: TStringList;
Dept: string);
var
i: Integer;
sList: TStringList;
begin
for i := 0 to strDataList.Count - 1 do
begin
strDataList[0] := Dept;
strList.Add(SetInsert(tbName, strDataList[i], strFields));
end;
end;
procedure TMyString.SetInsertListByDept1(tbName: string;
strDataList: TStringList; strFields: string; var strList: TStringList;
DeptNo, DeptName: string);
var
i: Integer;
sList: TStringList;
begin
for i := 0 to strDataList.Count - 1 do
begin
strDataList[0] := DeptNo;
strDataList[1] := DeptName;
strList.Add(SetInsert(tbName, strDataList[i], strFields));
end;
end;
class function TMyString.SetUpdate(tbName, strData, strFields: string): string;
var
i: Integer;
DataList, FieldsList: TStringList;
str: string;
begin
DataList := TStringList.Create;
FieldsList := TStringList.Create;
Split(strData, ',', DataList);
Split(strFields, ',', FieldsList);
str := 'update ' + tbName + ' set ';
for i := 0 to DataList.Count - 1 do
begin
str := str + FieldsList[i] + '=' + DataList[i] + ',';
end;
str := Copy(str, 1, Length(str) - 1);
DataList.Free;
FieldsList.Free;
Result := str;
end;
procedure TMyString.SetUpdateList(tbName: string; strDataList: TStringList;
strFields: string; var strList: TStringList);
var
i: Integer;
begin
//strList.Add(StrBeginTran);
for i := 0 to strDataList.Count - 1 do
begin
strList.Add(SetUpdate(tbName, strDataList[i], strFields));
end;
//strList.Add(StrEndTran);
end;
procedure TMyString.SetUpdateListByDept(tbName: string;
strDataList: TStringList; strFields: string; var strList: TStringList;
Dept: string);
begin
end;
class procedure TMyString.Split(const A, OldPattern: string; var Str: TStringList);
begin
Str.Text := Stringreplace(A, OldPattern, sLineBreak, [rfReplaceAll]);
end;
class procedure TMyString.StrListCompare(s1, s2: TStringList; var s3,
s4: TStringList);
var
i: Integer;
begin
s1.Sort;
s2.Sort;
if s3.Count > 0 then
s3.Clear;
if s4.Count > 0 then
s4.Clear;
for i := 0 to s1.Count - 1 do
begin
if s2.IndexOf(s1[i]) = -1 then
s3.Add(s1[i]);
end;
for i := 0 to s2.Count - 1 do
begin
if s1.IndexOf(s2[i]) = -1 then
s4.Add(s2[i]);
end;
end;
end.