TMSQuery -> TQuery cast
TMSQuery -> TQuery cast
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.
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.
I need to use SDAC in plugin system. Other database plugins (for example MySQL, PostgreSQL) give me datasets, which I use as TQuery.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 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 ...
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.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 codeLudek 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 ...
MSQuery.Connection := MSConnection;
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 complicatedDimon 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.
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.shreq wrote:
Thx for response, it may be interresting - but how I can wrap MSConnection to use in code
MSQuery.Connection := MSConnection;
But it is possible, that I simply don't know your plugin environment