dbExpress设计问题
有谁知道(或愿意做出假设)为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 会停止自动创建参数。CommandText
不存在此类问题。3) 您可以使用
Format
函数构建动态 SQL 字符串:4) 其他数据访问库,例如 AnyDAC,具有宏变量,简化动态查询文本构建。例如:
1)
TSQLQuery
is rather for compatibility with BDE TQuery. And BDE TQuery hasSQL: 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 eachSQL.Add(...)
call dbExpress code parses query when ParamCheck is True. That may be solved by usingBeginUpdate
/EndUpdate
or setting ParamCheck to False. But note, setting ParamCheck to False stops automatic parameters creation.CommandText
does not have such issues.3) You can use
Format
function for building a dynamic SQL string:4) Other data access libraries, like AnyDAC, have macro variables, simplifying dynamic query text building. For example:
我不得不说 TSqlQuery 使用 TStrings(Delphi 2010 中的 TWideStrings),因为它更加灵活。
假设您的查询是:
<代码>
尝试对一组连续的字符执行此操作,这些字符永远在一个长行中,在编辑窗口内没有换行符,该窗口总是太小,无法查看,不允许换行文本等。
只需 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:
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.