TMSQuery -> TQuery cast

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
shreq
Posts: 8
Joined: Fri 20 Mar 2009 11:26

TMSQuery -> TQuery cast

Post by shreq » Mon 06 Apr 2009 12:39

Sample code:
var
msq: TMSQuery;
q: TQuery;
begin
msq := TMSQuery.Create(Self);
..........
msq.SQL.Add('SELECT * FROM blahblah');
q := TQuery(msq);
q.SQL.Add('WHERE smthg = 5'); <-------- This line gives an exception, in debugger q.sql IS NULL - WHY??

Sample is a little stupid, but I REALLY NEED casting.

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 07 Apr 2009 07:42

There is no way to cast TMSQuery to TQuery, because its classes have different hierarchy and implementation.
To solve this problem please describe why you need to do it in more details.

shreq
Posts: 8
Joined: Fri 20 Mar 2009 11:26

Post by shreq » Tue 07 Apr 2009 08:38

Dimon wrote:There is no way to cast TMSQuery to TQuery, because its classes have different hierarchy and implementation.
To solve this problem please describe why you need to do it in more details.
I need to use SDAC in plugin system. Other database plugins (for example MySQL, PostgreSQL) give me datasets, which I use as TQuery.

Ludek
Posts: 296
Joined: Thu 12 Oct 2006 09:34

Post by Ludek » Wed 08 Apr 2009 10:55

I personally never use classes in plugin environment, I prefer self defined interfaces. under an interface you can give anything - TQuery, TMSQuery, even .Net or java classes, simply anything.
just write a wrapper for each class with query functionality and use only the interface in plugin communication.

if you don't want to use interface wrappers, you can use type testing (very ugly, but possible):

if q is TQuery then
TQuery(q).sql.Add(...
else if q is TMSQuery then
TMSQuery(q).sql.Add(...
else
raise .... WTF ...

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Thu 09 Apr 2009 06:42

Ludek wrote:I personally never use classes in plugin environment, I prefer self defined interfaces. under an interface you can give anything - TQuery, TMSQuery, even .Net or java classes, simply anything.
just write a wrapper for each class with query functionality and use only the interface in plugin communication.

if you don't want to use interface wrappers, you can use type testing (very ugly, but possible):

if q is TQuery then
TQuery(q).sql.Add(...
else if q is TMSQuery then
TMSQuery(q).sql.Add(...
else
raise .... WTF ...
Also if you need to execute a SQL query using TMSQuery and to use result dataset, you can use methods of base class TDataSet. But you can not cast TMSQuery to TQuery directly in any case.

shreq
Posts: 8
Joined: Fri 20 Mar 2009 11:26

Post by shreq » Wed 15 Apr 2009 07:19

Ludek wrote:I personally never use classes in plugin environment, I prefer self defined interfaces. under an interface you can give anything - TQuery, TMSQuery, even .Net or java classes, simply anything.
just write a wrapper for each class with query functionality and use only the interface in plugin communication.

if you don't want to use interface wrappers, you can use type testing (very ugly, but possible):

if q is TQuery then
TQuery(q).sql.Add(...
else if q is TMSQuery then
TMSQuery(q).sql.Add(...
else
raise .... WTF ...
Thx for response, it may be interresting - but how I can wrap MSConnection to use in code

MSQuery.Connection := MSConnection;

shreq
Posts: 8
Joined: Fri 20 Mar 2009 11:26

Post by shreq » Wed 15 Apr 2009 07:23

Dimon wrote: Also if you need to execute a SQL query using TMSQuery and to use result dataset, you can use methods of base class TDataSet. But you can not cast TMSQuery to TQuery directly in any case.
Thx for response - but what should I do if I need access to SQL property? Not all queries are static, in this case code may be too complicated :)

Ludek
Posts: 296
Joined: Thu 12 Oct 2006 09:34

Post by Ludek » Thu 16 Apr 2009 07:36

shreq wrote:
Thx for response, it may be interresting - but how I can wrap MSConnection to use in code

MSQuery.Connection := MSConnection;
you wrote, you had "database plugins". so the plugin is responsible for creating the queries and setting the connection property, i think. you don't have to wrap such command, the plugin has to deliver a tmsquery INCLUSIVE db connection.

But it is possible, that I simply don't know your plugin environment :)

Post Reply