在运行时向查询添加计算字段
我正在使用 Delphi 中的查询获取数据,并且希望在运行之前向查询添加计算字段。计算字段使用代码和查询中的值,因此我不能只在 SQL 中计算它。
我知道我可以附加一个 OnCalcFields 事件来实际进行计算,但问题是添加计算字段后查询中没有其他字段...
我做了一些挖掘,发现所有创建了字段定义,但仅创建了实际字段
if DefaultFields then
CreateFields
指定了默认字段
procedure TDataSet.DoInternalOpen;
begin
FDefaultFields := FieldCount = 0;
...
end;
这表明如果您添加字段,您只会获得您添加的字段。
我想要查询中的所有字段以及我添加的字段。
这是可能的还是我必须添加我正在使用的所有字段?
I'm getting data using a query in Delphi, and would like to add a calculated field to the query before it runs. The calculated field is using values in code as well as the query so I can't just calculate it in SQL.
I know I can attach an OnCalcFields
Event to actually make the calculation, but the problem is after adding the calculated field there are no other fields in the query...
I did some digging and found that all of the field defs are created but the actual fields are only created
if DefaultFields then
CreateFields
Default Fields is specified
procedure TDataSet.DoInternalOpen;
begin
FDefaultFields := FieldCount = 0;
...
end;
Which would indicate that if you add fields you only get the fields you added.
I would like all the fields in the query AS WELL AS the ones I Add.
Is this possible or do I have to add all the fields I'm using as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有什么可以阻止您首先在代码中创建所有字段,
然后添加您的计算字段。
您可以使用“黑客类型”来使用受保护的 CreateFields:
或借用 CreateFields: 的一些代码,
然后创建您的计算字段:
Nothing prevents you from creating all the fields first in your code,
then add your calculated fields.
You can either use a "hacked type" to use the protected CreateFields:
or borrowing some code from CreateFields:
then create your calculated fields:
Delphi 现在可以选择组合自动生成字段和计算字段: Data.DB.TFieldOptions.AutoCreateMode 类型的枚举 TFieldsAutoCreationMode 。这样您就可以在运行时添加计算字段。弗朗索瓦在他的回答中写道如何在运行时添加字段。
TFieldsAutoCreationMode 的不同模式:
acExclusive
当根本没有持久字段时,就会创建自动字段。这是默认模式。
acCombineCompulated
当数据集没有持久字段或仅存在计算持久字段时,将创建自动字段。这是在设计时创建持久计算字段并让数据集创建自动数据字段的便捷方法。
acCombineAlways
当没有持久字段时,将创建数据库字段的自动字段。
Delphi now has the option to combine automatic generated fields and calculated fields : Data.DB.TFieldOptions.AutoCreateMode an enumeration of type TFieldsAutoCreationMode. This way you can add your calculated fields at runtime. Francois wrote in his answer how to add a field at runtime.
Different modes of TFieldsAutoCreationMode :
acExclusive
When there are no persistent fields at all, then automatic fields are created. This is the default mode.
acCombineComputed
The automatic fields are created when the dataset has no persistent fields or there are only calculated persistent fields. This is a convenient way to create the persistent calculated fields at design time and let the dataset create automatic data fields.
acCombineAlways
Automatic fields for the database fields will be created when there are no persistent fields.
除了计算字段之外,您还需要添加所有字段。
添加字段后,您必须添加数据集中所需的所有字段。
Delphi 将其称为持久字段与动态字段。所有字段都是持久的或动态的。不幸的是,你不能混合两者。
另一件需要注意的事情是,从文档中可以看出
因此,请小心,如果您稍后向表中添加其他字段,则需要将新字段添加到组件中。删除字段也是如此。
如果您确实不想要持久字段,还有另一种解决方案。在任何应显示计算字段的网格或控件上,您可以自定义绘制它。例如,许多网格控件都有 OnCustomDraw 事件。您可以在那里进行计算。
You need to add all fields in addition to your calculated field.
Once you add a field, you have to add all of the fields that you want in the data set.
Delphi calls this persistent fields versus dynamic fields. All fields are either persistent or dynamic. Unfortunately, you can't have a mixture of both.
Another thing to note, from the documentation is
So, be careful, if you later add additional fields to a table, you will need to add the new fields to the component. Same thing with deleting fields.
If you really don't want persistent fields, there is another solution. On any grid or control that should show the calculated field, you can custom draw it. For example, many grid controls have a OnCustomDraw event. You can do your calculation there.
如果您知道要在运行时计算的字段名称,则可以使用类似的东西。
If you have know your to be calculated fields names at runtime, you can use something like that.
笔记:
这个方法有问题
计算字段放置在最后一个字段的位置
要解决这个问题,在查询中添加一个附加字段就足够了,例如将值零视为最终字段
像这样:
现在你可以为 myField 编写 onClacField 方法
note:
This method have a problem
The calculated field is placed in place of the last field
To solve this problem, it is enough to add an additional field in query, for example, consider the value zero as the final field
like this :
now you can write onClacField method for myField