数据库解析delta的得到sql语句的函数_Oracle数据库_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

数据库解析delta的得到sql语句的函数

作者:黑客防线网安Oracle维护基地 来源:黑客防线网安Oracle维护基地 浏览次数:0

黑客防线网安网讯:   {解析出sql语句}  function TForm1.gensqls(AdoCon:TADOConnection; pdelta: OleVariant; const ptablename, pkeyfields: WideString): WideString;  var  i, j: integer;  s1, ...
   {解析出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;
  ado.GetFieldNames(FieldList);
  ado.Free;
  for i:=1 to FieldList.Count do
  if Cdsupdate.FindField(FieldList[i-1])<>nil then
  Cdsupdate.FindField(FieldList[i-1]).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[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]])
  else
  s2:=s2+' and '+Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]);
  end;
  end;
  usModified:
  begin
  s1:='';
  for i:=1 to Cdsupdate.FieldCount do
  begin
  if (not Cdsupdate.Fields[i-1].isNull)and(Cdsupdate.Fields[i-1].tag=1) then
  begin
  if s1='' then
  s1:=Trim(Cdsupdate.Fields[i-1].FieldName)+' = '+vartosql(Cdsupdate.Fields[i-1].value)
  else
  s1:=s1+','+Trim(Cdsupdate.Fields[i-1].FieldName)+' = '+vartosql(Cdsupdate.Fields[i-1].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[i-1].isNull)and(Cdsupdate.Fields[i-1].tag=1) then
  begin
  if s1='' then
  begin
  s1:=Trim(Cdsupdate.Fields[i-1].FieldName);
  s2:=vartosql(Cdsupdate.Fields[i-1].value);
  end
  else
  begin
  s1:=s1+','+Trim(Cdsupdate.Fields[i-1].FieldName);
  s2:=s2+','+vartosql(Cdsupdate.Fields[i-1].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[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]])
  else
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-13216-1.html
网站维护教程更新时间:2012-03-23 00:52:14  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer