delphi 根据DELTA自动生成SQL语句


本文整理自网络,侵删。

 delphi 根据DELTA自动生成SQL语句

上传客户端的CLIENTDATASET.delta到服务器的clientdataset.data,服务端解析clientdataset的数据生成相应的SQL语句。
 
相对于直接调用datasetprovider.applyupdates()方法提交数据而言,前者的可控性更强,对于某些要求灵活性很强的场合,前者可能是必须的提交方式。
 
procedure TBaseService.ApplyUpdates(const Delta: OleVariant; TableName, KeyField: WideString);
var
  Flag: Boolean;
begin
  if VarIsNull(Delta) then exit;
  with (FParent as TDataServer2) do
    begin
      cdsDelta.Close;
      cdsDelta.Data := Delta;
      Flag := cdsDelta.FindField('SYS_STATUS') <> nil;
    end; // with
  if Flag then
    InnerApplyUpdates2(TableName, KeyField)
  else
    InnerApplyUpdates(TableName, KeyField);
end;
 
procedure TBaseService.InnerApplyUpdates(TableName, KeyField: WideString);
var
  i: integer;
  s1, s2: string;
  CmdStr: string;
  FieldList: TStringList;
begin
  with (FParent as TDataServer2) do
    begin
      FieldList := TStringList.Create;
      Connection.GetFieldNames(TableName, FieldList);
      if not cdsDelta.Active then cdsDelta.Open;
      for i := 1 to FieldList.Count do
        if cdsDelta.FindField(FieldList[i - 1]) <> nil then
          cdsDelta.FindField(FieldList[i - 1]).Tag := 1;
      FieldList.Free;
      if cdsDelta.RecordCount > 0 then
        begin
          cdsDelta.First;
          s1 := '';
          s2 := '';
          while not cdsDelta.Eof do
            begin
              CmdStr := '';
              case cdsDelta.UpdateStatus of
                usUnmodified:
                  begin
                    s2 := VarToSql(cdsDelta[KeyField]);
                  end;
                usModified:
                  begin
                    s1 := '';
                    for i := 1 to cdsDelta.FieldCount do
                      if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDelta.Fields[i - 1].Tag = 1) then
                        begin
                          if s1 = '' then
                            s1 := Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value)
                          else
                            s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value);
                        end;
                    if s1 <> '' then
                      begin
                        CmdStr := 'Update ' + TableName + ' Set ' + s1 + ' Where ' + KeyField + ' = ' + s2;
                      end;
                  end;
                usInserted:
                  begin
                    s1 := '';
                    s2 := '';
                    for i := 1 to cdsDelta.FieldCount do
                      if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDelta.Fields[i - 1].Tag = 1) then
                        begin
                          if s1 = '' then
                            begin
                              s1 := Trim(cdsDelta.Fields[i - 1].FieldName);
                              s2 := VarToSql(cdsDelta.Fields[i - 1].Value);
                            end
                          else
                            begin
                              s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName);
                              s2 := s2 + ',' + VarToSql(cdsDelta.Fields[i - 1].Value);
                            end;
                        end;
                    if s1 <> '' then
                      begin
                        CmdStr := 'Insert into ' + TableName + '(' + s1 + ') Values (' + s2 + ')';
                      end;
                  end;
                usDeleted:
                  begin
                    s2 := VarToSql(cdsDelta[KeyField]);
                    CmdStr := 'Delete ' + TableName + ' Where ' + KeyField + ' = ' + s2;
                  end;
              end;
              if CmdStr <> '' then Cmd.Execute(CmdStr);
              cdsDelta.Next;
            end;
          cdsDelta.First;
          cdsDelta.EmptyDataSet;
          cdsDelta.Close;
        end;
    end;
end;

相关阅读 >>

Delphi xe中将正则表达式tregex的使用

Delphi cef4Delphi 浏览器事件

Delphi 日期加减

Delphi 获取dll文件的函数列表

Delphi 系统服务状态获取管理员用户名

Delphi webbrowser同时访问两个网址导致程序出错的解决办法

Delphi 十进制十六进制转换

Delphi如何获得当前操作系统语言环境

使用indy解决base64回车换行问题

Delphi2007在win7系统下的日期问题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...