Pascal Data Objects

function TPDO.prepare_as_is (sql: AnsiString):IPDOStatement;

Prepares an SQL statement to be executed by the IPDOStatement.execute or IPDOStatement.fetch_bound methods. The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed. You can mix both named and question mark parameter markers within the same SQL statement. (Note: You can not mix these in the PHP version)

You must include a unique parameter marker for each value you wish to pass in to the statement when you call IPDOStatement.execute or IPDOStatement.fetch_bound. You cannot use a named parameter marker of the same name twice in a prepared statement. You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement.

Calling TPDO.prepare_as_is/TPDO.prepare_select and IPDOStatement.execute/IPDOStatement.fetch_bound for statements that will be issued multiple times with different parameter values optimizes the performance of your application by allowing the driver to negotiate client and/or server side caching of the query plan and meta information, and helps to prevent SQL injection attacks by eliminating the need to manually quote the parameters.

PDO will not emulate prepared statements/bound parameters for drivers that do not natively support them. An exception will be thrown if this is attempted.

Parameters

sql: The SQL statement to prepare and execute.

Return Values

TPDO.query returns a IPDOStatement object.

Examples

Example 1. Prepare an SQL statement with named parameters

{Execute a prepared statement by concatenating the values with the pipe symbol}
{second execution is standard bind}

procedure example1;
var
   sql: AnsiString;
   red, yellow: TPDORowSetNative;
begin
   red    := TPDORowSetNative.Create;
   yellow := TPDORowSetNative.Create;

   sql = 'SELECT name, colour, calories ' +
         'FROM fruit ' +
         'WHERE calories < :calories AND colour = :colour';
   stmt := db.prepare_as_is (sql);
   stmt.execute ('150|red');
   stmt.fetch_all (red);
   stmt.bindValue ('calories', 175);
   stmt.bindValue ('colour', 'yellow');
   stmt.execute();
   stmt.fetch_all (yellow);

   {do something with  red/yellow here}
   red.free;
   yellow.free;
end;

Example 2. Prepare an SQL statement with question mark parameters

{Execute a prepared statement by passing an array of values}
{second execution is standard bind}

procedure example2;
var
   sql: AnsiString;
   red, yellow: TPDORowSetNative;
begin
   red    := TPDORowSetNative.Create;
   yellow := TPDORowSetNative.Create;

   sql = 'SELECT name, colour, calories ' +
         'FROM fruit ' +
         'WHERE calories < ? AND colour = ?';
   stmt := db.prepare_as_is (sql);
   stmt.execute ('150|red');
   stmt.fetch_all (red);
   stmt.bindValue (0, 175);
   stmt.bindValue (1, 'yellow');
   stmt.execute();
   stmt.fetch_all (yellow);

   {do something with  red/yellow here}
   red.free;
   yellow.free;
end;