Delphi - 如何泛化查询结果集

发布于 2024-12-11 15:35:18 字数 736 浏览 0 评论 0 原文

我正在同一应用程序中使用多个数据库。我正在使用来自两家不同公司的驱动程序。两家公司都有运行良好的 tTable 和 tQuery 后代。

我需要一种对数据进行通用访问的方法,无论我使用哪个驱动程序/tQuery 组件来返回一组数据。这些数据不会与组件相关,而只会与我的逻辑相关。

例如...(伪代码)让我们创建一个可以针对任一 tQuery 组件运行的函数

function ListAllTables(NameOfDatabase : String) :ReturnSet??
begin

If NameOfDataBase = 'X' then use tQuery(Vendor 1)
    else use tQuery(Vendor 2)
RunQuery;
Return Answer...
end;

当正常运行查询时,我会执行

Query.Open;
While not Query.EOF do
   begin
   Read my rows..
   next;
end;

如果我调用 ListAllTables,我的返回类型是什么,以便我可以迭代行?每个 tQuery 供应商都是不同的,所以我不能使用它(我可以吗?如果是的话,我会想要吗?)我可以构建一个内存表,并将其传回,但这似乎是 ListAllRows 构建一个内存表的额外工作内存表,然后将其传递回调用例程,以便它可以“取消构建”,即迭代行......

您的想法和建议是什么? 谢谢 GS

I am working with multiple databases within the same application. I am using drivers from two different companies. Both companies have tTable and tQuery Descendants that work well.

I need a way to have generic access to the data, regardless of which driver/tQuery component I am using to return a set of data. This data would NOT tie to components, just to my logic.

For example...(pseudocode) Let's create a function which can run against either tQuery component

function ListAllTables(NameOfDatabase : String) :ReturnSet??
begin

If NameOfDataBase = 'X' then use tQuery(Vendor 1)
    else use tQuery(Vendor 2)
RunQuery;
Return Answer...
end;

When NORMALLY running a query, I do

Query.Open;
While not Query.EOF do
   begin
   Read my rows..
   next;
end;

If I am CALLING ListAllTables, what is my return type so that I can iterate through the rows? Each tQuery Vendor is different, so I can't use that (can I, and if so, would I want to?) I could build a Memory Table, and pass that back, but that seems like extra work for ListAllRows to build a memory table, and then to pass it back to the calling routine so that it can "un-build", i.e. iterate through the rows...

What are your ideas and suggestions?
Thanks
GS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2024-12-18 15:35:18

几乎所有 Delphi 数据集都源自 TDataset,并且最有用的行为是在 TDataset 上定义的。

因此,如果将每个表或查询分配给 TDataset 类型的变量,则应该能够以供应商中立的方式对该数据集执行逻辑。

我还将数据集的生成隔离到一组工厂函数中,这些函数仅创建特定于供应商的数据集并将其作为 TDataset 返回。每个工厂功能都有自己的单元。然后,只有那些小单元需要了解供应商特定组件。

Almost all Delphi datasets descend from TDataset, and most useful behavior is defined on TDataset.

Therefore, if you assign each table or query to a variable of type TDataset, you should be able to perform your logic on that dataset in a vendor neutral fashion.

I would also isolate the production of the datasets into a set of factory functions that only create the vendor-specific dataset and return it as a TDataset. Each factory function goes in it's own unit. Then, only those small units need have any knowledge of the vendor specific components.

ら栖息 2024-12-18 15:35:18
  1. 您可以使用 IProviderSupport 来概括查询执行。我期望所使用的查询支持 IProviderSupport。该接口允许设置命令文本、参数、执行命令等。
  2. 所用查询的共同点是 TDataSet。因此,您需要传递 TDataSet 引用。

例如:

var
  oDS: TDataSet;
...
if NameOfDataBase = 'X' then
  oDS := T1Query.Create(nil)
else
  oDS := T2Query.Create(nil);
(oDS as IProviderSupport).PSSetCommandText('select * from mytab');
oDS.Open;
while not oDS.Eof do begin
  //
  oDS.Next;
end;
  1. You can use IProviderSupport to generalize the query execution. I am expecting, that used query's are supporting IProviderSupport. This interface allows to set command text, parameters, execute commands, etc.
  2. The common denominator for used query's is TDataSet. So, you will need pass TDataSet reference.

For example:

var
  oDS: TDataSet;
...
if NameOfDataBase = 'X' then
  oDS := T1Query.Create(nil)
else
  oDS := T2Query.Create(nil);
(oDS as IProviderSupport).PSSetCommandText('select * from mytab');
oDS.Open;
while not oDS.Eof do begin
  //
  oDS.Next;
end;
凉栀 2024-12-18 15:35:18

也许您可以考虑使用通用数据访问组件,例如 UniDAC 或 AnyDAC。这允许您仅使用一个组件集以一致的方式访问不同的数据库。

您可能还对 RemObjects 的 DataAbstract 感兴趣。一个强大的数据抽象、多层、远程处理解决方案,具有许多功能。不便宜,但物超所值。

相关链接:

Perhaps you could consider a universal data access component such as UniDAC or AnyDAC. This allows you to use only one component set to access different databases in a consistent way.

You might also be interested in DataAbstract from RemObjects. A powerful data abstraction, multi-tier, remoting solution with a lot of features. Not inexpensive, but excellent value for the money.

Relevant links:

抚你发端 2024-12-18 15:35:18

一个简单的方法是让 ListAllTables 返回(或填充)一个字符串列表。

A simple approach would be to have ListAllTables return (or populate) a stringlist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文