gpt4 book ai didi

delphi - 在 const 数组中传递通用记录

转载 作者:行者123 更新时间:2023-12-03 14:48:25 24 4
gpt4 key购买 nike

是否可以以任何方式将常量参数数组中的通用记录传递给函数调用?

我想使用 Nullable Allen Bauer 在一种自制的 ORM 中使用“普通旧 Delphi 对象”来映射数据库行的记录:

type
Nullable<T> = record
...
end;

TMyTableObject = class(TDbOject)
private
FId: Integer;
FOptionalField: Nullable<string>;
protected
procedure InternalSave; override;
public
property Id: Integer read FId write SetId;
property OptionalField: Nullable<string> read FOptionalField write SetOptionalField;
end;

...

implementation

procedure TMyTableObject.InternalSave;
begin
{ FDbController is declared and managed in TDbObject, it contains fonction to run queries
on the database }

FDbController.Execute(
'update or insert into MY_TABLE(TABLE_ID, OPTIONAL_FIELD) ' +
'values (?, ?) ' +
'matching(TABLE_ID) returning TABLE_ID', [FId, FOptionalField],
procedure (Fields: TSQLResult)
begin
FId := Fields.AsInteger[0];
end;
end;
end;

上述代码会导致错误:“E2250:无法使用这些参数调用“执行”函数的重写版本”(翻译自法语:E2250 Aucune version surchargée de 'Execute' ne peut être appelée avec ces argument )

我可以将 FOptionalField 显式转换为字符串或其他任何内容,因为 Nullable 会覆盖临时运算符,但我确实必须知道 Nullable 是否有值,直到我将 const 数组映射到数据库查询对象的 Params 字段:

procedure TDbContext.MapParams(Q: TUIBQuery; Params: TConstArray);
const
BOOL_STR: array[Boolean] of string = ('F', 'V');
var
i: Integer;
begin
for i := 0 to High(Params) do
case Params[i].VType of
vtInteger : Q.Params.AsInteger[i] := Params[i].VInteger;
vtInt64 : Q.Params.AsInt64[i] := Params[i].VInt64^;
vtBoolean : Q.Params.AsString[i] := BOOL_STR[Params[i].VBoolean];
vtChar : Q.Params.AsAnsiString[i] := Params[i].VChar;
vtWideChar: Q.Params.AsString[i] := Params[i].VWideChar;
vtExtended: Q.Params.AsDouble[i] := Params[i].VExtended^;
vtCurrency: Q.Params.AsCurrency[i] := Params[i].VCurrency^;
vtString : Q.Params.AsString[i] := string(Params[i].VString^);
vtPChar : Q.Params.AsAnsiString[i] := Params[i].VPChar^;
vtAnsiString: Q.Params.AsAnsiString[i] := AnsiString(Params[i].VAnsiString);
vtWideString: Q.Params.AsUnicodeString[i] := PWideChar(Params[i].VWideString);
vtVariant : Q.Params.AsVariant[i] := Params[i].VVariant^;
vtUnicodeString: Q.Params.AsUnicodeString[i] := string(Params[i].VUnicodeString);
vtPointer :
begin
if Params[i].VPointer = nil then
Q.Params.IsNull[i] := True
else
Assert(False, 'not nil pointer is not supported');
end
else
Assert(False, 'not supported');
end;
end;

您知道如何使这种构造成为可能吗?我找到了一种方法,使用 RTTI 在 Nullable 中向 Variant 添加显式强制转换运算符覆盖,但这有点棘手,因为它需要在 const 调用数组中显式强制转换为 Variant :

class operator Nullable<T>.Explicit(Value: Nullable<T>): Variant;
begin
if Value.HasValue then
Result := TValue.From<T>(Value.Value).AsVariant
else
Result := Null;
end;

...

FDbController.Execute(
'update or insert into MY_TABLE(TABLE_ID, OPTIONAL_FIELD) ' +
'values (?, ?) ' +
'matching(TABLE_ID) returning TABLE_ID', [FId, Variant(FOptionalField)],
procedure (Fields: TSQLResult)
begin
FId := Fields.AsInteger[0];
end;
end;

最佳答案

正如您所说,没有 record允许在array of const参数,在编译器的当前状态。

事实上,TVarRec.VType value 没有任何 vtRecord。

还有Variant类型本身没有 varRecord Delphi 处理的类型。 Windows 世界中存在这样的变体类型(例如,DotNet 结构体在 COM 中映射为 vt_Record 类型),但 Delphi 不处理这种变体。

可以将指针传递给记录的类型信息,然后传递给记录:

case Params[i].VType of
vtPointer:
if (i<high(Params)) and (Params[i+1].VType=vtPointer) then
if Params[i].VPointer = TypeInfo(MyRecord) then begin
...
end;

也许这不是一个很好的答案,因为您使用的是泛型...但至少是一个开始...在所有情况下,这就是我如何将其与预泛型 Delphi 编译器一起使用的方式。 TypeInfo() 关键字已经相当强大,并且比"new"RTTI 实现更快。

另一种可能性(与泛型兼容)应该是在记录内容中添加一些记录类型 ID。然后将记录作为指针传递,读取 ID,并按预期操作:

case Params[i].VType of
vtPointer:
if Params[i].VPointer <> nil then
case PRecordType(Params[i].VPointer)^ of
aRecordType: ...
...
end;

这可以通过简单扩展初始 Nullable<T> 来实现。定义。

后记:

在 ORM 中使用记录也许不是最好的解决方案......

class模型比 record/object 更先进Delphi 中的模型,并且由于 ORM 是关于对象建模的,因此您应该使用可用的最佳 OOP 模型,恕我直言。通过依赖类而不是记录,您可以拥有继承、嵌入类型信息(通过 Class 方法或仅通过 PPointer(aObject)^ )、可空值和虚拟方法。 > (这对于 ORM 来说非常方便)。甚至是array of const参数问题将允许直接处理此类vtObject类型和专用的类虚拟方法。基于记录的 ORM 只是序列化表行的一种方式,而不是使用 OOP 最佳实践来设计应用程序。关于性能、内存分配class与复制记录的潜在问题相比(_CopyRecord RTL 函数 - 由编译器以隐藏方式调用,例如对于函数参数 - can be very slow ),实例不是问题。

例如,在我们的 ORM 中,我们 handle dynamic array properties, even dynamic arrays of records in properties (Delphi 不会为记录的已发布属性生成 RTTI,这是当前类实现的另一个不一致行为)。我们连载records and dynamic arrays使用“旧”RTTI 转换为 JSON 或二进制文件,效果非常好。两个对象世界中最好的。

关于delphi - 在 const 数组中传递通用记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com