Middleware analytical method generates SQL FDMEMTABLE.delta

Traversing Delta.DataView.Rows, Delta.DataView.Rows rowset is recorded by rows 

 objects TFDDatSRow, means one of the rows 

TFDDatSRow method: 

 the GetData (), the SetData () Get value of a row or a set of row Numerical 
 fdmemtable.delta submitted directly to the intermediate member may be parsed SQL the uPDATE OR fdmemtable.delta generate the INSERT 

 { 
 references System.Json; 

  function name: DBToJSON (TFDMemTable update data into SQL script) 

  parameters: 
  DB: TFDMemTable dataset 
  sTable: update table 
  sKey: key fields written: ID, name comma distinguish 
  sNoField: no modification or additional fields: written: ID, Sex comma distinguish 
} 
function DBToJSON (DB: TFDMemTable; STABLE: String ; sKey: String ; sNoField: String): String; 
Var 
  ItemKey, ItemNoField: TStringList; 
  JA: TJSONArray; 
  sField, sValue, sSQL, STMP, sName: String;
  i,j:Integer;
begin
  JA:=TJSONArray.Create;
  ItemKey:=TStringlist.Create;
  ItemNoField:=TStringlist.Create;

  ItemKey.Delimiter:=',';
  ItemKey.DelimitedText:=sKey;

  ItemNoField.Delimiter:=',';
  ItemNoField.DelimitedText:=sNoField;

  sField:='';
  sValue:='';
  sSQL:='';
   With DB.Delta.DataView.Rows do  the begin 
    for the I: = 0  to Count - . 1  do   the begin 

      // determine the operating state data: Insert 
      IF ItemsI [I] = TFDDatSRowState.rsInserted .RowState the then  the begin 

        // cycle corresponds to data field 
        for J: = 0  to DB.Fields.Count- . 1  do  the begin 
          // Get field name 
          sName: = DB.Fields [J] .FieldName; 

          // excluded without inserting field information 
          IF ItemNoField.IndexOf (sName)> - 1  the then 
            the Continue; 

          //Analyzing the data type field 
          Case DB.Fields [J] .datatype of 
            ftString, ftWideString, ftMemo: STMP: = . VarToStr (ItemsI [I] .getValues (sName)) QuotedString; 
            ftSmallint, ftInteger, ftFloat, ftCurrency, ftBCD: STMP : = VarToStr (ItemsI [I] .getValues (sName)); 
            ftDateTime: the begin 
              STMP: = VarToStr (ItemsI [I] .getValues (sName));
               // date how to fill in default is no Null 
              IF STMP = ''  the then 
                STMP : = ' Null ' 
              the else 
                STMP: = the FormatDateTime ( 'yyyy-mm-dd hh:ss:mm',StrToDateTime(sTmp)).QuotedString;
            end;
            ftBoolean: begin
              if ItemsI[i].GetValues(sName)=True then
                sTmp:='1'
              else
                sTmp:='0';
            end;
            else
             sTmp:=VarToStr(ItemsI[i].GetValues(sName)).QuotedString;
          end;

          //累积字段和插入值
          sField:=sField+sName+',';
          sValue:STMP sValue + + = ' , ' ;
         End ;
         // spliced into SQL and inserted into an array 
        JA.Add ( ' the Insert Into ' + + STABLE ' ( ' + the Copy (sField, . 1 , the Length (sField) - . 1 ) + ' ) ' + # 13 is # 10 
          + ' Values ( ' + the Copy (sValue, . 1 , the Length (sValue) - . 1 ) + ' ) ' + # 13 is # 10 ); 

      End
      // determine if the operation state data: modifying 
      the else  IF ItemsI [I] .RowState in [TFDDatSRowState.rsModified, 
        TFDDatSRowState.rsEditing] the then  the begin 

        for J: = 0  to DB.Fields.Count- . 1  do  the begin 
          sName: = DB.Fields [J] .FieldName; 

          // excluded without inserting field information 
          IF ItemNoField.IndexOf (sName)> - . 1  the then 
            the Continue; 

          // Analyzing the data type field 
          Case DB.Fields [J] .datatype of 
            ftString, ftWideString, ftMemo : STMP: = . VarToStr (ItemsI [I] .getValues (sName)) QuotedString;
            ftSmallint, ftInteger,ftFloat, ftCurrency, ftBCD: sTmp:=VarToStr(ItemsI[i].GetValues(sName));
            ftDateTime: begin
              sTmp:=VarToStr(ItemsI[i].GetValues(sName));
              if sTmp='' then
                sTmp:='Null'
              else
                sTmp:=FormatDateTime('yyyy-mm-dd hh:ss:mm',StrToDateTime(sTmp)).QuotedString;
            end;
            ftBoolean: begin
              ifItemsI [I] .getValues (sName) = True the then
                STMP: = ' . 1 ' 
              the else 
                STMP: = ' 0 ' ;
             End ;
             the else 
             STMP: = VarToStr (ItemsI [I] .getValues (sName)) QuotedString;.
           End ;
           // cumulative update field values 
          sField: = sField sName + + ' = ' + + STMP ' , ' ; 

          // modify the data key field, condition value 
          IF ItemKey.IndexOf (sName)> - . 1  the then 
            sSQL: = + sSQL ' And ' + + sName ' = ' + VarToStr (ItemsI [I] .GetData (sName, rvOriginal)); 

        End ;
         // spliced into SQL and inserted into an array 
        JA.Add ( ' the Update ' + + STABLE ' SET ' + the Copy (sField, . 1 , the Length (sField) - . 1 ) + ' the Where ' + the Copy (sSQL, . 5 , the Length (sSQL)));
       End 
      the else  IF ItemsI [I] .RowState in [TFDDatSRowState.rsDeleted] the then  begin 
        sSQL: = '' ;
        // delete the data key field, condition value 
        for J: = 0  to ItemKey.Count- . 1  do  the begin 
          sValue: = ItemsI [I] .getValues (ItemKey.Strings [J]);
           IF sValue.Trim <> ''  the then 
            sSQL: = + sSQL ' and ' + ItemKey.Strings [J] + ' = ' + QuotedStr (sValue.Trim);
         End ; 

        // spliced into SQL and inserted into the array 
        IF sSQL <> ''  the then 
          JA.Add ( ' the Delete ' + + STABLE ' Where '+Copy(sSQL,5,Length(sSQL)) );
      end;
    end;
  end;
  Result:=JA.ToString;
  FreeAndNil(JA);
  FreeAndNil(ItemKey);
  FreeAndNil(ItemNoField);

end;

 

Guess you like

Origin www.cnblogs.com/jijm123/p/11300957.html