字符串通用类

本文深入探讨了操作数据库的关键技巧与实用工具,包括如何高效地进行数据插入、更新与删除,以及如何利用字符串列表进行数据处理。通过实例演示了如何使用自定义类来实现复杂的数据库操作,如数据过滤、数据排序、数据合并等,为数据库管理和数据处理提供了强大的支持。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值