"Pre-parsing" of SQL statements?

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
peolsson
Posts: 11
Joined: Thu 30 Apr 2009 07:30

"Pre-parsing" of SQL statements?

Post by peolsson » Sat 15 Aug 2009 09:58

We have a situation where we want to insert SQL-queries into a table, for execution later on.

We have a table called "future_queries", which has two columns "sqlquery" (text) and "executetime" (datetime).

We insert data into this table using this code;

SQL.Text := 'INSERT INTO future_queries (sqlquery, executetime) VALUES (:sqlquery, :executetime)';
ParamByName('sqlquery').AsString := SQLQuery;
ParamByName('executetime').AsDateTime := Now + 1;
Execute;

The problem is that we need to have the data in SQLQuery string to be prepared somehow, since the query sometime can hold binary data etc, the data must be escaped the way that the provider expects it to be.

We would need something like this;

q.SQL.Text := 'INSERT INTO whatevertable (filename, data) VALUES (:filename, :data)';
q.ParamByName('filename').AsString := 'test.xml';
q.ParamByName('data').ReadFromStream(filestream);
SQLQuery := q.PreparsedQuery;

That is, to make the UniDAC to do all parsing of the parameters, but not execute the query, just return it as a string.

Is this possible somehow? This would also be a nice feature to have when logging the "real" query, instead of logging the query before it was parsed.

Regards,

Peter Olsson

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Mon 17 Aug 2009 07:25

Most UniDAC providers send the parameter values separately from a SQL statement.

Parameter values are inserted into SQL only in the MySQL provider or in the PostgreSQL provider (if you set the UnpreparedExecute PostgreSQL-specific option to True).

Please specify whether you are using this code with MySQL. Anyway it is responsibility of the provider to escape the parameter values. You should not do this manually.

peolsson
Posts: 11
Joined: Thu 30 Apr 2009 07:30

Post by peolsson » Fri 21 Aug 2009 13:42

Yes, I understand this. What I'm trying to do is to make prepared statements for later execution, so I'd just need to call the escape_string-procedures manually.

But if this is handled by the driver itself, I understand it will probably cause some problems..

/Peter

Post Reply