会计考友 发表于 2012-8-4 14:06:19

Oracle技术:解析delta得到sql语句的函数

{解析出sql语句}   function TForm1.gensqls(AdoCon:TADOConnection; pdelta: OleVariant; const ptablename, pkeyfields: WideString): WideString;
  var
  i, j: integer;
  s1, s2: string;
  Cmdstr: string;
  FieldList, Keylist: TstringList;
  Cdsupdate: TClientDataSet;
  sqlstr: WideString;
  ado: TADOQuery;
  begin
  if varisnull(pdelta) then
  Exit;
  Cdsupdate:=TClientDataSet.Create(nil);
  Cdsupdate.data:=pdelta;
  if not Cdsupdate.Active then
  Cdsupdate.Open;
  try
  FieldList:=TstringList.Create;
  Keylist:=TstringList.Create;
  Keylist.Delimiter:=',';
  Keylist.DelimitedText:=pkeyfields;
  ado:=TADOQuery.Create(nil);
  ado.Connection:=AdoCon;
  ado.sql.Text:='select * from '+ptablename+' where 1=0';

  ado.Open;

会计考友 发表于 2012-8-4 14:06:20

Oracle技术:解析delta得到sql语句的函数

</p>  ado.GetFieldNames(FieldList);
  ado.Free;
  for i:=1 to FieldList.Count do
  if Cdsupdate.FindField(FieldList)nil then
  Cdsupdate.FindField(FieldList).tag:=1;
  FieldList.Free;
  if Cdsupdate.RecordCount>0 then
  begin
  Cdsupdate.First;
  s1:='';
  s2:='';
  while not Cdsupdate.Eof do
  begin
  Cmdstr:='';
  case Cdsupdate.UpdateStatus of
  usUnmodified: //年夜原数据行取得改削前提
  begin
  s2:='';
  for j:=1 to Keylist.Count do
  begin
  if s2='' then
  s2:=Keylist+'='+vartosql(Cdsupdate])
  else
  s2:=s2+' and '+Keylist+'='+vartosql(Cdsupdate]);
  end;
  end;
  usModified:
  begin
  s1:='';
  for i:=1 to Cdsupdate.FieldCount do
  begin
  if (not Cdsupdate.Fields.isNull)and(Cdsupdate.Fields.tag=1) then
  begin
  if s1='' then
  s1:=Trim(Cdsupdate.Fields.FieldName)+' = '+vartosql(Cdsupdate.Fields.value)
  else
  s1:=s1+','+Trim(Cdsupdate.Fields.FieldName)+' = '+vartosql(Cdsupdate.Fields.value);
  end;
  end;
  if s1'' then
  begin
  Cmdstr:=' update '+ptablename+' set '+s1+' where '+s2;
  end;
  end;
  usInserted:
  begin
  s1:='';
  s2:='';
  for i:=1 to Cdsupdate.FieldCount do
  if (not Cdsupdate.Fields.isNull)and(Cdsupdate.Fields.tag=1) then

  begin

会计考友 发表于 2012-8-4 14:06:21

Oracle技术:解析delta得到sql语句的函数

</p>  if s1='' then
  begin
  s1:=Trim(Cdsupdate.Fields.FieldName);
  s2:=vartosql(Cdsupdate.Fields.value);
  end
  else
  begin
  s1:=s1+','+Trim(Cdsupdate.Fields.FieldName);
  s2:=s2+','+vartosql(Cdsupdate.Fields.value);
  end;
  end;
  if s1'' then
  begin
  Cmdstr:=' Insert into '+ptablename+'('+s1+') values('+s2+')';
  end;
  end;
  usDeleted:
  begin
  s2:='';
  for j:=1 to Keylist.Count do
  begin
  if s2='' then
  s2:=Keylist+'='+vartosql(Cdsupdate])
  else
  s2:=s2+' and '+Keylist+'='+vartosql(Cdsupdate]);
  end;
  Cmdstr:='Delete '+ptablename+' where '+s2;
  end;
  end;
  if Cmdstr'' then
  sqlstr:=sqlstr+Cmdstr+';'+chr(13)+chr(10);
  Cdsupdate.Next;
  end;
  end;
  finally
  Cdsupdate.close;
  Cdsupdate.Free();
  end;
  Result:=sqlstr;
  end;
页: [1]
查看完整版本: Oracle技术:解析delta得到sql语句的函数