Delphi 判断ClientDataSet控件的UpdateStatus属性类型为 (usUnmodified, usModified, usInserted, usDeleted))
根据ClientDataSet控件的UpdateStatus属性类型为(usUnmodified, usModified, usInserted, usDeleted),解析并拼成相应的增、删和改操作语句:
实例如下:
// ATableName : 将要修改的表名
// APrimaryKey : 表的主键值
// AIsMulti : 数据中包含的表的个数
// ADelta : 更新的数据集
procedure TForm1.Save(const ATableName, APrimaryKey, AIsMulti: WideString;
ADelta: OleVariant; var ErrorMsg: WideString);
var
ActualFieldList: TStringList;
PrimaryFieldList: TStringList;
i: Integer;
FieldNameList: string; //字段名称列表
FieldValueList: string; //字段值列表
FieldNameValueList: string; //字段值=名称列表
SetValueList: string; //Set字段值=名称列表, update的Sql用
SqlStr: string;
//返回欲修改表的字段名称列表
procedure GetFieldNameList(const ATableName: string);
var
QueryStr: string;
begin
QueryStr := 'select * from ' + ATableName + ' where 1 = 0 ';
try
FClientDataSet.Data := FDBoperation.cdsGetDataForRead(QueryStr, ErrorMsg);
except
Exit;
end;
FClientDataSet.GetFieldNames(ActualFieldList);
end;
//返回欲修改表的 主键字段名称=值串
function GetPKValueList: string;
var
j: Integer;
ValueStr: string;
ResultStr: string;
begin
ResultStr := ' ';
for j := 0 to PrimaryFieldList.Count - 1 do
begin
with FClientDataSet do
begin
case FieldByName(PrimaryFieldList[j]).DataType of
ftString, ftDate, ftDateTime:
ValueStr := QuotedStr(FieldByName(PrimaryFieldList[j]).AsString);
ftInteger:
ValueStr := FieldByName(PrimaryFieldList[j]).AsString;
end;
end;
if j > 0 then
ResultStr := ResultStr + ' and ';
ResultStr := ResultStr + PrimaryFieldList[j] + ' = ' + ValueStr;
end;
Result := ResultStr;
end;
begin
if AIsMulti = '1 ' then //Delta中仅包含一个表的数据
begin
FDBoperation.cdsUpdateDate(ATableName, ADelta, ErrorMsg);
end
else
begin //Delta中包含多个表的数据
ActualFieldList := TStringList.Create;
PrimaryFieldList := TStringList.Create;
try
{=== 得到欲修改表的字段名称列表 ===}
GetFieldNameList(ATableName);
FClientDataSet.Data := ADelta;
FClientDataSet.First;
with FClientDataSet do
begin
while not Eof do
begin
case UpdateStatus of
usModified:
begin
SetValueList := ' Set ';
for i := 0 to FieldCount - 1 do
begin
//是欲修改表的字段之一
if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then
begin
case Fields[i].DataType of
ftString, ftDate, ftDateTime:
begin
if not Fields[i].IsNull then
SetValueList := SetValueList + Fields[i].FieldName
+ '= ' + QuotedStr(Fields[i].AsString) + ', ';
end;
ftInteger:
begin
if not Fields[i].IsNull then
SetValueList := SetValueList + Fields[i].FieldName
+ '= ' + Fields[i].AsString + ', ';
end;
end;
end;
end;
System.Delete(SetValueList, Length(SetValueList), 1);
SqlStr := 'update ' + ATableName + SetValueList + ' where ' + FieldNameValueList;
end;
usInserted:
begin
FieldNameList := ' ';
FieldValueList := ' ';
for i := 0 to FieldCount - 1 do
begin
//是欲修改表的字段之一
if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then
begin
FieldNameList := FieldNameList + Fields[i].FieldName + ', ';
if Fields[i].IsNull then
FieldValueList := FieldValueList + ' null, '
else begin
case Fields[i].DataType of
ftString, ftDate:
begin
FieldValueList := FieldValueList + QuotedStr(Fields[i].AsString) + ', ';
end;
ftInteger:
begin
FieldValueList := FieldValueList + Fields[i].AsString + ', ';
end;
end;
end;
end;
end;
//删除末尾的逗号
System.Delete(FieldNameList, Length(FieldNameList), 1);
System.Delete(FieldValueList, Length(FieldValueList), 1);
SqlStr := 'insert into ' + ATableName + '( ' + FieldNameList + ') '
+ ' values ( ' + FieldValueList + ') ';
end;
usUnmodified, usDeleted:
begin
{=== 得到欲修改表的主键字段名称列表 ===}
PrimaryFieldList.CommaText := APrimaryKey;
{=== 得到欲修改表的主键字段名称=值串 ===}
FieldNameValueList := GetPKValueList;
if UpdateStatus = usDeleted then
SqlStr := 'delete from ' + ATableName + ' where ' + FieldNameValueList;
end;
end;
if UpdateStatus <> usUnmodified then
begin
try
FDBoperation.ExecSql(SqlStr, ErrorMsg);
except
raise;
end;
end;
Next;
end;
end;
finally
ActualFieldList.Free;
PrimaryFieldList.Free;
end;
end;
end;