码迷,mamicode.com
首页 > 数据库 > 详细

将 FireDAC Detal 转换为 SQL

时间:2016-08-22 23:27:48      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:

以下代码实现如何将 FireDAC Detal 转换为 SQL

 1 function TFDDataSetHelper.DeltaToSQL(const AKeyFields: string): string;
 2 var
 3   Row, Col: Integer;
 4   DatSRow: TFDDatSRow;
 5   DataType: TFDDataType;
 6   OldValue, NewValue: string;
 7   FieldNames, Values, SqlWhere, TableName, KeyFields: string;
 8 begin
 9 
10   Result := ‘‘;
11 
12   KeyFields := AKeyFields;
13   if KeyFields.IsEmpty then
14     KeyFields := UpdateOptions.KeyFields;
15 
16   TableName := Delta.DataView.Table.Name;
17   for Row := 0 to Delta.DataView.Rows.Count - 1 do
18   begin
19     DatSRow := Delta.DataView.Rows[Row];
20 
21     if DatSRow.RowState in [TFDDatSRowState.rsModified, TFDDatSRowState.rsDeleted] then
22       SqlWhere := PrepareSqlWhere(DatSRow, KeyFields);
23 
24     Values := ‘‘;
25     FieldNames := ‘‘;
26     case DatSRow.RowState of
27       TFDDatSRowState.rsInserted:
28         begin
29           for Col := 0 to DatSRow.Table.Columns.Count - 1 do
30           begin
31             DataType := DatSRow.Table.Columns[Col].DataType;
32 
33             if DataType in [dtBlob, dtHBlob] then
34               Continue;
35 
36             if not FieldNames.IsEmpty then
37               FieldNames := FieldNames + ,;
38 
39             if not Values.IsEmpty then
40               Values := Values + ,;
41 
42             FieldNames := FieldNames + DatSRow.Table.Columns[Col].Name;
43             Values := Values + Var2Sql(DataType, DatSRow.GetData(Col, TFDDatSRowVersion.rvDefault));
44 
45           end;
46 
47           if not Result.IsEmpty then
48             Result := Result + ; + sLineBreak;
49 
50           Result := Result + INSERT INTO  + TableName + ( + FieldNames + ) VALUES( + Values + );
51         end;
52 
53       TFDDatSRowState.rsModified:
54         begin
55           for Col := 0 to DatSRow.Table.Columns.Count - 1 do
56           begin
57             DataType := DatSRow.Table.Columns[Col].DataType;
58             OldValue := Var2Sql(DataType, DatSRow.GetData(Col, TFDDatSRowVersion.rvOriginal));
59             NewValue := Var2Sql(DataType, DatSRow.GetData(Col, TFDDatSRowVersion.rvDefault));
60 
61             if OldValue <> NewValue then
62             begin
63               if not Values.IsEmpty then
64                 Values := Values + ,;
65 
66               Values := Values + Format(%s=%s, [DatSRow.Table.Columns[Col].Name, NewValue]);
67             end;
68 
69           end;
70           if not Values.IsEmpty then
71             if not Result.IsEmpty then
72               Result := Result + ; + sLineBreak;
73 
74           Result := Result + UPDATE  + TableName +  SET  + Values + SqlWhere;
75         end;
76       TFDDatSRowState.rsDeleted:
77         begin
78           if not Result.IsEmpty then
79             Result := Result + ; + sLineBreak;
80 
81           Result := Result + DELETE FROM  + TableName + SqlWhere;
82         end;
83     end;
84 
85   end;
86 
87 end;

 

将 FireDAC Detal 转换为 SQL

标签:

原文地址:http://www.cnblogs.com/oldfarmer/p/5797481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!