dbExpress设计问题

发布于 2024-11-30 04:40:12 字数 1354 浏览 3 评论 0原文

有谁知道(或愿意做出假设)为什么 TSqlDataset 有一个 commandtext 属性(字符串),而 TSqlQuery 有一个 sql 属性(tstrings)?

考虑 sql 语句

select id, name from
table
order by name

如果我使用 TSqlQuery,那么我可以通过访问 sql[1] 动态更改查询中的表名称,但如果我使用 TSqlDataset(如果我需要 bidrelational 数据集,则数据集是连接到提供者,然后连接到 tclientdataset),我必须按字面设置命令文本字符串。虽然上面的示例很简单,但当 sql 语句涉及更多时,它可能会出现问题。

更新: 从到目前为止的评论和答案来看,我似乎被误解了。我不太关心提高组件的运行时性能(当查询需要一秒钟时,一毫秒有什么关系),但我确实关心程序员(即我)和维护程序的能力。在现实生活中,我有以下查询,它存储在 TSqlQuery 中:

select dockets.id, dockets.opendate, customers.name, statuses.statname,
dockets.totalcost, dockets.whopays, dockets.expected, dockets.urgent,
(dockets.totalcost - dockets.billed) as openbill,
(dockets.totalcost - dockets.paid) as opencost,
location.name as locname, dockets.attention,
statuses.colour, statuses.disporder, statuses.future, dockets.urgcomment
from location, statuses, dockets left join customers
on dockets.customer = customers.id
where dockets.location = location.id
and dockets.status = statuses.id

我没有计算字符串中的字符数,但我确信有超过 255 个,因此无法将查询存储在简单的字符串中。在某些情况下,我想通过添加行“and statuses.id = 3”或“andcustomers.id = 249”来过滤显示的数据量。如果查询存储为 TStrings,那么我可以将虚拟行“and 1 = 1”添加到基本查询中,然后根据需要更新此行。但查询是一个长字符串,我无法轻松访问它的末尾。

我当前正在做的(代替更好的解决方案)是创建另一个 TSqlDataSet,并将其命令文本设置为默认 TSqlDataSet 的命令文本,同时附加额外条件。

Does anybody know (or care to make a suppostion as to) why TSqlDataset has a commandtext property (string) whereas TSqlQuery has a sql property (tstrings)?

Consider the sql statement

select id, name from
table
order by name

If I use a TSqlQuery, then I can change the table name in the query dynamically by accessing sql[1], but if I am using a TSqlDataset (as I have to if I need a bidrectional dataset, the dataset is connected to a provider and thence to a tclientdataset), I have to set the commandtext string literally. Whilst the above example is trivial, it can be a problem when the sql statement is much more involved.

Update:
Judging by the comments and answers so far, it seems that I was misunderstood. I don't care very much for improving the runtime performance of the components (what does one millisecond matter when the query takes one second) but I do care about the programmer (ie me) and the ability to maintain the program. In real life, I have the following query which is stored in a TSqlQuery:

select dockets.id, dockets.opendate, customers.name, statuses.statname,
dockets.totalcost, dockets.whopays, dockets.expected, dockets.urgent,
(dockets.totalcost - dockets.billed) as openbill,
(dockets.totalcost - dockets.paid) as opencost,
location.name as locname, dockets.attention,
statuses.colour, statuses.disporder, statuses.future, dockets.urgcomment
from location, statuses, dockets left join customers
on dockets.customer = customers.id
where dockets.location = location.id
and dockets.status = statuses.id

I haven't counted the number of characters in the string, but I'm sure that there are more than 255, thus precluding storing the query in a simple string. In certain circumstances, I want to filter the amount of data being displayed by adding the line 'and statuses.id = 3' or 'and customers.id = 249'. If the query were stored as TStrings, then I could add to the basic query the dummy line 'and 1 = 1', and then update this line as needed. But the query is one long string and I can't easily access the end of it.

What I am currently doing (in lieu of a better solution) is creating another TSqlDataSet, and setting its commandtext to the default TSqlDataSet's commandtext whilst appending the extra condition.

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

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

发布评论

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

评论(2

暗地喜欢 2024-12-07 04:40:12

1) TSQLQuery 是为了与 BDE TQuery 兼容。 BDE TQuery 具有SQL: TStrings 属性。 TSQLDataSet 应该用于新应用程序。

2) 虽然SQL: TStrings对于某些任务很有用,但它也容易出错。程序员经常忘记在再次填充之前清除 SQL 属性。另外,如果你的查询很大,SQL的填充可能会导致性能下降。因为在每次 SQL.Add(...) 调用中,当 ParamCheck 为 True 时,dbExpress 代码都会解析查询。这可以通过使用 BeginUpdate / EndUpdate 或将 ParamCheck 设置为 False 来解决。但请注意,将 ParamCheck 设置为 False 会停止自动创建参数。

SQLQuery1.SQL.BeginUpdate;
try
  SQLQuery1.SQL.Clear;
  SQLQuery1.SQL.Add('SELECT * FROM');
  SQLQuery1.SQL.Add('Orders');
finally
  SQLQuery1.SQL.EndUpdate;
end;

CommandText 不存在此类问题。

3) 您可以使用 Format 函数构建动态 SQL 字符串:

var
  sTableName: String;
...
sTableName := 'Orders';
SQLDataSet1.CommandText := Format('select * from %s', [sTableName]);

4) 其他数据访问库,例如 AnyDAC,具有宏变量,简化动态查询文本构建。例如:

ADQuery1.SQL.Text := 'SELECT * FROM &TabName';
ADQuery1.Macros[0].AsRaw := 'Orders';
ADQuery1.Open;

1) TSQLQuery is rather for compatibility with BDE TQuery. And BDE TQuery has SQL: TStrings property. TSQLDataSet is what supposed to be used for new applications.

2) Although SQL: TStrings is usefull for some tasks, it is also error prone. Often programmers forget to clear SQL property before filling again. Also if your query is a big one, the filling of SQL may lead to performance degradation. Because on each SQL.Add(...) call dbExpress code parses query when ParamCheck is True. That may be solved by using BeginUpdate / EndUpdate or setting ParamCheck to False. But note, setting ParamCheck to False stops automatic parameters creation.

SQLQuery1.SQL.BeginUpdate;
try
  SQLQuery1.SQL.Clear;
  SQLQuery1.SQL.Add('SELECT * FROM');
  SQLQuery1.SQL.Add('Orders');
finally
  SQLQuery1.SQL.EndUpdate;
end;

CommandText does not have such issues.

3) You can use Format function for building a dynamic SQL string:

var
  sTableName: String;
...
sTableName := 'Orders';
SQLDataSet1.CommandText := Format('select * from %s', [sTableName]);

4) Other data access libraries, like AnyDAC, have macro variables, simplifying dynamic query text building. For example:

ADQuery1.SQL.Text := 'SELECT * FROM &TabName';
ADQuery1.Macros[0].AsRaw := 'Orders';
ADQuery1.Open;
尬尬 2024-12-07 04:40:12

我不得不说 TSqlQuery 使用 TStrings(Delphi 2010 中的 TWideStrings),因为它更加灵活。

假设您的查询是:
<代码>

Select
Item1,
Item2,
Item3,
Item4
FROM MyTable

  • 它更容易阅读
  • 您可以复制并粘贴到外部查询工具中,并且它保持格式化
  • 很容易注释掉各个部分

Select
Item1,
/*
Item2,
Item3,
*/
Item4
FROM MyTable

  • 您可以轻松添加项目

Select
Item1,
Item2,
Item2a,
Item2b,
Item3,
Item3a,
Item3b,
Item4
FROM MyTable

尝试对一组连续的字符执行此操作,这些字符永远在一个长行中,在编辑窗口内没有换行符,该窗口总是太小,无法查看,不允许换行文本等。

只需 0.02 美元。

I would have to say that the TSqlQuery uses TStrings (TWideStrings in Delphi 2010) because it is much more flexible.

Suppose your query was:

Select
Item1,
Item2,
Item3,
Item4
FROM MyTable

  • It's a lot easier to read
  • You can copy and paste into an external query tool and it stays formatted
  • It's easy to comment out sections

Select
Item1,
/*
Item2,
Item3,
*/
Item4
FROM MyTable

  • You can easily add items

Select
Item1,
Item2,
Item2a,
Item2b,
Item3,
Item3a,
Item3b,
Item4
FROM MyTable

Try doing that to a contiguous set of characters that goes on forever in one long line with no line breaks inside an edit window that is always to small for viewing that doesn't allow for wrapped text etc. etc. etc.

Just $0.02.

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