сохранение records почти доделал вкладку команды, осталось лишь групповые ответы добавить

This commit is contained in:
PC1\PTyTb
2025-08-07 18:14:03 +03:00
parent dacd2e6050
commit b62450b6ec
12 changed files with 1082 additions and 679 deletions
+73 -13
View File
@@ -484,6 +484,7 @@ begin
end;
end;
procedure TSettingsDatabase.SaveRecordArray<T>(const TableName: string; const Items: array of T);
var
Context: TRttiContext;
@@ -493,12 +494,13 @@ var
Rec: T;
Field: TRttiField;
FieldNames, Placeholders: string;
i: Integer;
i, j: Integer;
Value: TValue;
Param: TFDParam;
begin
if Length(Items) = 0 then Exit;
EnsureTableForRecord(TableName, TypeInfo(T));
FConnection.ExecSQL(Format('DELETE FROM %s', [TableName]));
Context := TRttiContext.Create;
try
@@ -509,28 +511,85 @@ begin
try
Query.Connection := FConnection;
// Ïîñòðîåíèå ñïèñêà ïîëåé è ïëåéñõîëäåðîâ
FieldNames := '';
Placeholders := '';
for i := 0 to High(Fields) do
for j := 0 to High(Fields) do
begin
if i > 0 then
if j > 0 then
begin
FieldNames := FieldNames + ', ';
Placeholders := Placeholders + ', ';
end;
FieldNames := FieldNames + Fields[i].Name;
Placeholders := Placeholders + ':p_' + Fields[i].Name;
FieldNames := FieldNames + Fields[j].Name;
Placeholders := Placeholders + ':' + Fields[j].Name;
end;
Query.SQL.Text := 'INSERT INTO '+TableName+' ('+FieldNames+') VALUES ('+Placeholders+')';
Query.SQL.Text := 'INSERT INTO ' + TableName + ' (' + FieldNames + ') VALUES (' + Placeholders + ')';
// Ïîäãîòàâëèâàåì çàïðîñ (ñîçäàåò ïàðàìåòðû)
//Query.Prepare;
for Rec in Items do
begin
Query.Params.Clear;
for Field in Fields do
Query.ParamByName('p_'+Field.Name).Value := Field.GetValue(@Rec).AsVariant;
Query.ExecSQL;
// Óñòàíàâëèâàåì ðàçìåð ìàññèâà ïàðàìåòðîâ
Query.Params.ArraySize := Length(Items);
// Íà÷àëî òðàíçàêöèè
FConnection.StartTransaction;
try
// Î÷èñòêà òàáëèöû ïåðåä âñòàâêîé íîâûõ äàííûõ
FConnection.ExecSQL('DELETE FROM ' + TableName);
i := 0;
for Rec in Items do
begin
for Field in Fields do
begin
Value := Field.GetValue(@Rec);
// Íàõîäèì ïàðàìåòð
Param := Query.FindParam(Field.Name);
if not Assigned(Param) then
raise Exception.CreateFmt('Parameter "%s" not found in query', [Field.Name]);
// Îáðàáîòêà NULL-çíà÷åíèé
if Value.IsEmpty then
begin
Param.Clear(i);
end
else
begin
// Îáðàáîòêà ïî òèïó
case Field.FieldType.TypeKind of
tkInteger, tkInt64:
Param.AsIntegers[i] := Value.AsInteger;
tkFloat:
if (Field.FieldType = TypeInfo(TDateTime)) or
(Field.FieldType = TypeInfo(TDate)) or
(Field.FieldType = TypeInfo(TTime)) then
Param.AsDateTimes[i] := Value.AsExtended
else
Param.AsFloats[i] := Value.AsExtended;
tkEnumeration:
if Field.FieldType = TypeInfo(Boolean) then
Param.AsBooleans[i] := Value.AsBoolean
else
Param.AsIntegers[i] := Value.AsOrdinal;
tkUString, tkString, tkWString, tkLString:
Param.AsStrings[i] := Value.AsString;
else
Param.AsStrings[i] := Value.ToString;
end;
end;
end;
Inc(i);
end;
// Ïàêåòíîå âûïîëíåíèå
Query.Execute(Length(Items), 0);
FConnection.Commit;
except
FConnection.Rollback;
raise;
end;
finally
Query.Free;
@@ -540,6 +599,7 @@ begin
end;
end;
procedure TSettingsDatabase.LoadRecordArray<T>(const TableName: string; var Items: TArray<T>);
var
Context: TRttiContext;