Programming with SQL Relay using the PHP PDO API

Introduction

PDO is a popular database abstraction layer for PHP.

A good PDO guide and reference can be found on the PHP site but a short discussion of it follows here as well.

Configuring PHP

To use the SQL Relay PDO driver, PHP must be configured to load the appropriate module - pdo_sqlrelay.so on Linux or Unix and php_pdo_sqlrelay.dll on Windows.

If you are using SQL Relay version 0.58 or higher on reasonably modern Linux or Solaris with PHP installed from a package, then the installation process should have configured PHP correctly and you can probably ignore the rest of this section.

But on other operating sytems, or if you built PHP from source, or if the installation process didn't go smoothly, then you may have to configure PHP manually.

The PHP configuration file is usually named php.ini. The name and location of the file may vary though. Here are some common locations, by platform.

To load the php module on Linux or Unix, you usually just need to add lines to the configuration file like:

extension=pdo.so
extension=pdo_sqlrelay.so

To load the php module on Windows, you usually need to add lines like:

extension_dir=C:\PHP\ext
extension=php_pdo.dll
extension=php_pdo_sqlrelay.dll

(the extension_dir value might need to be changed, depending on where PHP is installed on your system)

It is possible, even likely that the configuration file already contains a line to load the pdo.so (or php_pdo.dll) module, so look through the file and verify whether it is or not before loading it again.

Also, some Linux and Unix distributions have PDO compiled directly into the PHP module or binary and don't require pdo.so module to be loaded at all. If you built PHP from source with the default options then this is likely. You can verify this by running:

strings <path-to-php-binary>/php | grep "PDO drivers"

If PDO is built-in then it should return something like:

You MUST load PDO before loading any PDO drivers

And if that wasn't complex enough, some PHP distributions on Linux or Unix have configuration directories. PHP parses the php.ini file first, then goes alphabetically through the files in the configuration directory. The configuration directory is usually named conf.d and is found somewhere under the directory that the main configuration file is found in. If there is a pdo.ini file in there, or if one of the files in that directory loads the pdo.so module, then you'll have to either add the lines above to the end of that same file or create a new pdo_sqlrelay.ini file that contains the lines to load pdo_sqlrelay.so or php_pdo_sqlrelay.dll

Establishing a Session

To use SQL Relay, you have to identify the connection that you intend to use.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

The following connect string variables control attributes of the connection:

In addition, the following connect string variables can be used to establish Kerberos or Active Directory encryption and authentication with the server:

See the SQL Relay Configuration Guide for more information about Kerberos and Active Directory configurations. In particular, the user and password are usually left as empty strings or null when using Kerberos/AD.

The following connect string variables can be used to establish TLS/SSL encryption and authentication with the server:

See the SQL Relay Configuration Guide for more information about TLS/SSL configurations.

Note that the supported tlscert and tlsca file formats may vary between platforms. A variety of file formats are generally supported on Linux/Unix platfoms (.pem, .pfx, etc.) but only the .pfx format is currently supported on Windows.

These additional connect string variables control the default statement attributes:

Setting one of the statement attribute connect string variables has the same effect as if the corresponding statement handle attribute were set immediately after creating each statement. See Driver-Specific Attributes below.

The other two parameters following the connect string are.

After calling the constructor, a session is established when the first query or other operation is run, unless lazyconnect=0 is used, in which case a session is established immediately.

For the duration of the session, the client occupies one of the database connections, so care should be taken to minimize the length of a session.

Executing Queries

Call exec() to run a query and return the number of rows affected. This is best used with DML or DDL queries.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

if (!$dbh->exec("insert into testtable values (1,'hello')")) {
	die("exec failed");
}

Call query() to run a query and return a statement object which can be used to fetch the result set. This is best used with selects or other queries that return result sets.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->query("select * from testtable"));

Transactions

Run begin() to start a transaction. To complete a transaction, run commit() or rollBack(). You should use the commit() and rollBack() functions rather than sending a "commit" or "rollback" query. There are two reasons for this. First, it's much more efficient to call the functions. Second, if you're writing code that can run on transactional or non-transactional databases, some non-transactional databases will throw errors if they receive a "commit" or "rollback" query, but by calling the commit() and rollBack() functions you instruct the database connection daemon to call the commit and rollback API functions for that database rather than issuing them as queries. If the API's have no commit or rollback functions, the calls do nothing and the database throws no error.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$dbh->beginTransaction();

... run some queries that we want to keep the results of ...

$dbh->commit();

$dbh->beginTransaction();

... run some queries that we don't want to keep the results of ...

$dbh->rollBack();

You can also turn Autocommit on or off using the setAttribute() method.

The following command turns Autocommit on.

$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,TRUE);

The following command turns Autocommit off.

$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,FALSE);

When Autocommit is on, the database performs a commit after each successful DML or DDL query. When Autocommit is off, the database commits when the client instructs it to, or (by default) when a client disconnects. For databases that don't support Autocommit, setting Autocommit on or off has no effect.

Temporary Tables

Some databases support temporary tables. That is, tables which are automatically dropped or truncated when an application closes its connection to the database or when a transaction is committed or rolled back.

For databases which drop or truncate tables when a transaction is committed or rolled back, temporary tables work naturally.

However, for databases which drop or truncate tables when an application closes its connection to the database, there is an issue. Since SQL Relay maintains persistent database connections, when an application disconnects from SQL Relay, the connection between SQL Relay and the database remains, so the database does not know to drop or truncate the table. To remedy this situation, SQL Relay parses each query to see if it created a temporary table, keeps a list of temporary tables and drops (or truncates them) when the application disconnects from SQL Relay. Since each database has slightly different syntax for creating a temporary table, SQL Relay parses each query according to the rules for that database.

In effect, temporary tables should work when an application connects to SQL Relay in the same manner that they would work if the application connected directly to the database.

Catching Errors

Most PDO methods return a value indicating that they failed. There are 3 options for error handling too that can be set using setAttribute().

After determining that an error has occurred you can find out why by calling errorCode() or errorInfo() on the database handle or statement objects.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

if (!$dbh->exec("insert into testtable values (1,1.1,'hello')")) {
	die($dbh->errorCode().":".$dbh->errorInfo());
}

$stmt=$dbh->prepare("bad query");
if ($stmt->execute()) {
	die($stmt->errorCode().":".$stmt->errorInfo());
}

Bind Variables

Programs rarely execute fixed queries. More often than not, some part of the query is dynamically generated. The PDO API provides means for using bind variables (also known as parameters) in those queries.

For a detailed discussion of substitutions and binds, see this document.

PDO provides several means for using bind variables but in all cases, you must bind variable/value pairs to the statement. Here is an example of using the bindValue() method.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("insert into testtable values (:var1,:var2,:var3,:var4)");
$stmt->bindValue(":var",1,PDO::PARAM_INT);
$stmt->bindValue(":var","1.1",PDO::PARAM_STR);
$stmt->bindValue(":var","hello",PDO::PARAM_STR);
$stmt->bindValue(":var","hello",PDO::PARAM_LOB);
$stmt->execute();

(Note: these examples use colon-delimited variable names. These are not supported by all databases. See "A few words about the format of bind variables..." below for more information.)

PDO supports binding integer, string and LOB values. Oddly, it doesn't support binding floating point values. You have to pass them as strings. Also, CLOB's have to be passed as LOB's.

PDO also provides a bindParam() method and allows you to pass an associative array into the execute() method. See the official PDO documentation for info on those methods.

Output bind variables can be used too. Just or the PDO
PARAM_INPUT_OUTPUT flag with the variable type.
$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("begin  :numvar:=1; :stringvar:='hello'; end;");
$param1=0;
$param2="";
$stmt->bindParam(":numvar",$param1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$stmt->bindParam(":stringvar",$param2,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$stmt->execute();
echo($param1.",".$param2);

BLOB's output binds can be used as well. PDO returns BLOB values as streams, so you have to use stream functions to access them.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("select blob_col into :blobvar from testtable");
$blobvar="";
$stmt->bindParam(":blobvar",$blobvar,PDO::PARAM_LOB|PDO::PARAM_INPUT_OUTPUT);
$stmt->execute();
echo(stream_get_contents($blobvar));

PDO allows you to bind by name and by position. So, you can pass either a variable name or a number as the first parameter of bindParam(). When binding by position, the first bind parameter is number 1, not 0.

A few words about the format of bind variables...

The official PDO documentation says to use ?'s in your queries when binding by position and colon-delimited names when binding by name.

However, databases differ widely in their bind variable formats and only SQLite supports both ?'s when binding by position and colon-delimited names when binding by name.

The most efficient thing to do is to ignore the PDO docs and just use whatever bind format is supported by the database you're using.

With Oracle:

insert into testtable values (:var1,:var2,:var3,:var4)

or

insert into testtable values (:1,:2,:3,:4)

With Sybase and MS SQL Server:

insert into testtable values (@var1,@var2,@var3,@var4)

With DB2, MySQL and Firebird:

insert into testtable values (?,?,?,?)

With PostgreSQL:

insert into testtable values ($1,$2,$3,$4)

With SQLite:

insert into testtable values (:var1,:var2,:var3,:var4)

or

insert into testtable values (?,?,?,?)

However...

Another solution is to configure SQL Relay to translate bind variables by setting translatebindvariables="yes" in the sqlrelay.conf file for the instance you're using. Simply put, this parameter makes every database support every bind variable format. See the SQL Relay Configuration Reference for more information on this parameter.

A third solution is to configure PDO to emulate prepares by setting the PDO
ATTR_EMULATE_PREPARES attribute to true. When the SQL Relay driver sees that this is set true, it rewrites the queries and translates the bind variables to substitution variables. This solution also makes every database support every bind variable format, but the translation is done on the client-side. Unfortunately it doesn't work with output bind variables.

Re-Binding and Re-Execution

Another feature of the prepare/bind/execute paradigm is the ability to prepare, bind and execute a query once, then re-bind and re-execute the query over and over without re-preparing it. If your backend database natively supports this paradigm, you can reap a substantial performance improvement.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("insert into testtable values (:var1,:var2,:var3)");
$stmt->bindValue(":var",1,PDO::PARAM_INT);
$stmt->bindValue(":var","1.1",PDO::PARAM_STR);
$stmt->bindValue(":var","hello",PDO::PARAM_STR);
$stmt->execute();

$stmt->bindValue(":var",2,PDO::PARAM_INT);
$stmt->bindValue(":var","2.2",PDO::PARAM_STR);
$stmt->bindValue(":var","bye",PDO::PARAM_STR);
$stmt->execute();

... re-bind and re-execute again and again ...

Accessing Fields in the Result Set

To fetch a row from a statement that has been executed, call the fetch() method. When called with no parameters, fetch() returns an array of fields with both numeric and associative entries. When called with the PDO
FETCH_NUM flag, it returns an array with only numeric entries. When called with the PDO::FETCH_ASSOC flag, it returns an array with only associative entries.

LOB values are returned as streams, just like output bind parameters.

Here is an example using fetch() to return both ordered and associative arrays (the default).

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->query("select int_col, float_col, string_col, blob_col from testtable");
$result=$stmt->fetch();
echo($result[0].",".$result[1].",".$result[2].",".stream_get_contents($result[3])."\n");
echo($result["int_col"].",".$result["float_col"].",".$result["string_col"].",".stream_get_contents("blob_col")."\n");
The bindColumn() method and PDO
FETCH_BOUND flag can be used to fetch values into preallocated variables.
$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("select int_col, float_col, string_col, blob_col from testtable");

$col1=0;
$col2="";
$col3="";
$col4="";
$stmt->bindColumn(1,$col1);
$stmt->bindColumn(2,$col2);
$stmt->bindColumn(3,$col3);
$stmt->bindColumn(4,$col4);

$stmt->execute();

$result=$stmt->fetch(PDO::FETCH_BOUND);
echo($col1.",".$col2.",".$col3.",".stream_get_contents($col4)."\n");

$result=$stmt->fetch(PDO::FETCH_BOUND);
echo($col1.",".$col2.",".$col3.",".stream_get_contents($col4)."\n");

The PDO API also provides the fetchAll() convenience function for fetching an entire result set in one step.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("select int_col, float_col, string_col, blob_col from testtable");
$stmt->execute();

$result=$stmt->fetchAll();

echo($result[0][0].",".$result[0][1].",".$result[0][2].",".stream_get_contents($result[0][3])."\n");
echo($result[1][0].",".$result[1][1].",".$result[1][2].",".stream_get_contents($result[1][3])."\n");
echo($result[2][0].",".$result[2][1].",".$result[2][2].",".stream_get_contents($result[2][3])."\n");
... and so on ...

The fetchColumn() method allows you to fetch a single column of the result set too.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->prepare("select int_col, float_col, string_col, blob_col from testtable");
$stmt->execute();

$result=$stmt->fetchColumn(0);

echo($result[0]."\n");
echo($result[1]."\n");
echo($result[2]."\n");
... and so on ...

Dealing With Large Result Sets

SQL Relay normally buffers the entire result set. This can speed things up at the cost of memory. With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.

Use the connect string variable "resultsetbuffersize" or the statement attribute PDO
SQLRELAY_ATTR_RESULT_SET_BUFFER_SIZE to set the number of rows to buffer at a time. Calls to fetch() cause the chunk containing the requested field to be fetched. Rows in that chunk are accessible but rows before it are not.

For example, if you set the result set buffer size to 5 and execute a query that returns 20 rows, rows 0-4 are available at once, then rows 5-9, then 10-14, then 15-19. When rows 5-9 are available, fetching row 0 will return false and fetching row 11 will cause rows 10-14 to be fetched and return the requested row.

The following code sets the result set buffer size to 5.

$stmt->setAttribute(PDO::SQLRELAY_ATTR_RESULT_SET_BUFFER_SIZE,5);

The following code retrieves the result set buffer size.

$rsbs=$stmt->getAttribute(PDO::SQLRELAY_ATTR_RESULT_SET_BUFFER_SIZE);

Statements

It's possible to execute new queries while processing the result set of another query. You can select rows from a table in one query, then iterate through its result set, inserting rows into another table, using only 1 database connection for both operations.

For example:

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt1=$dbh->prepare("select * from table1");
$stmt2=$dbh->prepare("insert into table2 values (:col1, :col2, :col3)");

$stmt1->execute();

while ($result=$stmt1->fetch()) {

	$stmt2->bindValue(":col1",$result[0],PDO::PARAM_INT);
	$stmt2->bindValue(":col2",$result[1],PDO::PARAM_STR);
	$stmt2->bindValue(":col3",$result[2],PDO::PARAM_STR);

	$stmt2->execute();
}

Getting Column Information

After executing a query, the column count may be retrieved using columnCount(). An associative array of column data, including column name, type, length and various flags are available from getColumnMeta().

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
if (!$dbh) {
	die("connection failed");
}

$stmt=$dbh->query("select * from testtable"));

for ($i=0; $i<$stmt->columnCount(); $i++) {

	$meta=$stmt->getColumnMeta($i);

	... do something with $meta ...
}

Stored Procedures

Many databases support stored procedures. Stored procedures are sets of queries and procedural code that are executed inside of the database itself. For example, a stored procedure may select rows from one table, iterate through the result set and, based on the values in each row, insert, update or delete rows in other tables. A client program could do this as well, but a stored procedure is generally more efficient because queries and result sets don't have to be sent back and forth between the client and database. Also, stored procedures are generally stored in the database in a compiled state, while queries may have to be re-parsed and re-compiled each time they are sent.

While many databases support stored procedures. The syntax for creating and executing stored procedures varies greatly between databases.

Stored procedures typically take input paramters from client programs through input bind variables and return values back to client programs either through bind variables or result sets. Stored procedures can be broken down into several categories, based on the values that they return. Some stored procedures don't return any values, some return a single value, some return multiple values and some return entire result sets.

No Values

Some stored procedures don't return any values. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.

Oracle

To create the stored procedure, run a query like the following.

create procedure testproc(in1 in number, in2 in number, in3 in varchar2) is
begin
        insert into mytable values (in1,in2,in3);
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("begin testproc(:in1,:in2,:in3); end;");
$stmt->bindValue(":in1",1);
$stmt->bindValue(":in2","1.1");
$stmt->bindValue(":in3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Sybase and Microsoft SQL Server

To create the stored procedure, run a query like the following.

create procedure testproc @in1 int, @in2 float, @in3 varchar(20) as
        insert into mytable values (@in1,@in2,@in3)

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("exec testproc");
$stmt->bindValue("@in1",1);
$stmt->bindValue("@in2","1.1");
$stmt->bindValue("@in3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Firebird

To create the stored procedure, run a query like the following.

create procedure testproc(in1 integer, in2 float, in3 varchar(20)) as
begin
        insert into mytable values (in1,in2,in3);
        suspend;
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("execute procedure testproc ?, ?, ?");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

DB2

To create the stored procedure, run a query like the following.

create procedure testproc(in in1 int, in in2 double, in in3 varchar(20)) language sql
begin
        insert into mytable values (in1,in2,in3);
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testproc(?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Postgresql

To create the stored procedure, run a query like the following.

create function testfunc(int,float,varchar(20)) returns void as '
begin
        insert into mytable values ($1,$2,$3);
        return;
end;' language plpgsql

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testfunc($1,$2,$3)");
$stmt->bindValue("$1",1);
$stmt->bindValue("$2","1.1");
$stmt->bindValue("$3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop function testfunc(int,float,varchar(20))

MySQL

To create the stored procedure, run a query like the following.

create procedure testproc(in in1 int, in in2 float, in in3 varchar(20))
begin
        insert into mytable values (in1,in2,in3);
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testproc(?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

Note: Versions of MySQL prior to 5.0 had trouble calling stored procedures using bind variables. If you are using a version of MySQL prior to 5.0 then SQL relay must fake the bind variables and you must use colon-delimited variables (:1, :2, :3, etc.) in your queries rather than the native-mysql queston marks.

To drop the stored procedure, run a query like the following.

drop procedure testproc

Single Values

Some stored procedures return single values. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.

Oracle

In Oracle, stored procedures can return values through output parameters or as return values of the procedure itself.

Here is an example where the procedure itself returns a value. Note that Oracle calls these functions.

To create the stored procedure, run a query like the following.

create function testproc(in1 in number, in2 in number, in3 in varchar2) returns number is
begin
        return in1;
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select testproc(:in1,:in2,:in3) from dual");
$stmt->bindValue(":in1",1);
$stmt->bindValue(":in2","1.1");
$stmt->bindValue(":in3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Here is an example where the value is returned through an output parameter.

To create the stored procedure, run a query like the following.

create procedure testproc(in1 in number, in2 in number, in3 in varchar2, out1 out number) as
begin
        out1:=in1;
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select testproc(:in1,:in2,:in3,:out1) from dual");
$stmt->bindValue(":in1",1);
$stmt->bindValue(":in2","1.1");
$stmt->bindValue(":in3","hello");
$out1=0;
$stmt->bindParam(":out1",$out1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Sybase and Microsoft SQL Server

In Sybase and Microsoft SQL Server, stored procedures return values through output parameters rather than as return values of the procedure itself.

To create the stored procedure, run a query like the following.

create procedure testproc @in1 int, @in2 float, @in3 varchar(20), @out1 int output as
        select @out1=convert(varchar(20),@in1)

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("exec testproc");
$stmt->bindValue("@in1",1);
$stmt->bindValue("@in2","1.1");
$stmt->bindValue("@in3","hello");
$out1=0;
$stmt->bindParam("@out1",$out1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Firebird

To create the stored procedure, run a query like the following.

drop procedure testproc

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testproc(?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

Alternatively, you can run a query like the following and receive the result using an output bind variable. Note that in Firebird, input and output bind variable indices are distict from one another. The index of the output bind variable is 1 rather than 4, even though there were 3 input bind variables.

$stmt=$dbh->prepare("execute procedure testproc ?, ?, ?");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$out1=0;
$stmt->bindParam("1",$out1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

DB2

In DB2, stored procedures return values through output parameters rather than as return values of the procedure itself.

To create the stored procedure, run a query like the following.

create procedure testproc(in in1 int, in in2 double, in in3 varchar(20), out out1 int) language sql
begin
        set out1 = in1;
end

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testproc(?,?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$out1=0;
$stmt->bindParam("4",$out1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Postgresql

To create the stored procedure, run a query like the following.

create function testfunc(int,float,char(20)) returns int as '
declare
        in1 int;
        in2 float;
        in3 char(20);
begin
        in1:=$1;
        return;
end;
' language plpgsql

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testfunc($1,$2,$3)");
$stmt->bindValue("$1",1);
$stmt->bindValue("$2","1.1");
$stmt->bindValue("$3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop function testfunc(int,float,char(20))

MySQL

A single value can be returned from a MySQL function.

To create the function, run a query like the following.

create function testfunc(in in1 int, in in2 float, in in3 varchar(20)) returns int return in1;

To execute the function from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testfunc(?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

Note: Versions of MySQL prior to 5.0 had trouble calling stored procedures using bind variables. If you are using a version of MySQL prior to 5.0 then SQL relay must fake the bind variables and you must use colon-delimited variables (:1, :2, :3, etc.) in your queries rather than the native-mysql queston marks.

To drop the function, run a query like the following.

drop procedure testproc

A single value can be returned in the result set of a MySQL procedure.

To create the procedure, run a query like the following.

create procedure testproc() begin select 1; end;

To execeute the procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testproc()");
$result=$stmt->execute();

To drop the procedure, run a query like the following.

drop procedure testproc

A single value can be returned using the output variable of a MySQL procedure.

To create the procedure, run a query like the following.

create procedure testproc(out out1 int) begin select 1 into out1; end;

To execeute the procedure from an SQL Relay program, use code like the following.

$dbh->query("set @out1=0");

$dbh->query("call testproc(@out1)");

$stmt=$dbh->prepare("select @out1");
$result=$stmt->execute();

To drop the procedure, run a query like the following.

drop procedure testproc

Multiple Values

Some stored procedures return multiple values. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.

Oracle

In Oracle, stored procedures can return values through output parameters or as return values of the procedure itself. If a procedure needs to return multiple values, it can return one of them as the return value of the procedure itself, but the rest must be returned through output parameters.

To create the stored procedure, run a query like the following.

create procedure testproc(in1 in number, in2 in number, in3 in varchar2, out1 out number, out2 out number, out3 out varchar2) is
begin
        out1:=in1;
        out2:=in2;
        out3:=in3;
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select testproc(:in1,:in2,:in3,:out1,:out2,:out3) from dual");
$stmt->bindValue(":in1",1);
$stmt->bindValue(":in2","1.1");
$stmt->bindValue(":in3","hello");
$out1=0;
$stmt->bindParam(":out1",$out1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$out2="";
$stmt->bindParam(":out2",$out2,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$out3="";
$stmt->bindParam(":out3",$out3,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Sybase and Microsoft SQL Server

To create the stored procedure, run a query like the following.

create procedure testproc @in1 int, @in2 float, @in3 varchar(20), @out1 int output, @out2 int output, @out3 int output as
        select @out1=convert(varchar(20),@in1),
                @out2=convert(varchar(20),@in2),
                @out2=convert(varchar(20),@in2)

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("exec testproc");
$stmt->bindValue("@in1",1);
$stmt->bindValue("@in2","1.1");
$stmt->bindValue("@in3","hello");
$out1=0;
$stmt->bindParam("@out1",$out1,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$out2="";
$stmt->bindParam("@out2",$out2,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$out3="";
$stmt->bindParam("@out3",$out3,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Firebird

To create the stored procedure, run a query like the following.

create procedure testproc(in1 integer, in2 float, in3 varchar(20)) returns (out1 integer, out2 float, out3 varchar(20)) as
begin
        out1=in1;
        out2=in2;
        out3=in3;
        suspend;
end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testproc(?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

DB2

To create the stored procedure, run a query like the following.

create procedure testproc(in in1 int, in in2 double, in in3 varchar(20), out out1 int, out out2 double, out out3 varchar(20)) language sql
begin
        set out1 = in1;
        set out2 = in2;
        set out3 = in3;
end

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testproc(?,?,?,?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$out4=0;
$stmt->bindParam("4",$out4,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$out5="";
$stmt->bindParam("5",$out5,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$out6="";
$stmt->bindParam("6",$out6,PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT);
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Postgresql

To create the stored procedure, run a query like the following.

create function testfunc(int,float,char(20)) returns record as '
declare
        output record;
begin
        select $1,$2,$3 into output;
        return output;
end;
' language plpgsql

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testfunc($1,$2,$3) as (col1 int, col2 float, col3 char(20))");
$stmt->bindValue("$1",1);
$stmt->bindValue("$2","1.1");
$stmt->bindValue("$3","hello");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop function testfunc(int,float,char(20))

MySQL

Here's how you can get multiple values from the result set of a MySQL procedure.

To create the stored procedure, run a query like the following.

create procedure testproc(in in1 int, in in2 float, in in3 varchar(20)) begin select in1, in2, in3; end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testproc(?,?,?)");
$stmt->bindValue("1",1);
$stmt->bindValue("2","1.1");
$stmt->bindValue("3","hello");
$result=$stmt->execute();

Note: Versions of MySQL prior to 5.0 had trouble calling stored procedures using bind variables. If you are using a version of MySQL prior to 5.0 then SQL relay must fake the bind variables and you must use colon-delimited variables (:1, :2, :3, etc.) in your queries rather than the native-mysql queston marks.

To drop the stored procedure, run a query like the following.

drop procedure testproc

Here's how you can get multiple values from the output variables of a MySQL procedure.

To create the stored procedure, run a query like the following.

create procedure testproc(out out1 int, out out2 float, out out3 varchar(20)) begin select 1,1.1,'hello' into out1, out2, out3; end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$dbh->query("set @out1=0, @out2=0.0, @out3=''");

$dbh->query("call testproc(@out1,@out2,@out3)");

$stmt=$dbh->prepare("select @out1,@out2,@out3");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Result Sets

Some stored procedures return entire result sets. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.

Oracle

In Oracle, for a result set to be fetched from a stored procedure, the result set must be bound to an output parameter of type cursor. PDO had a PDO
PARAM_STMT type that could theoretically be used to get one of these but it is currently unimplemented by any driver, including the SQL Relay driver. Quirks in the implementation of PDO itself make it impossible (or at least very difficult) to implement. Specifically, there's no obvious way of creating an instance of the PDO statement type to set the bound parameter to. Hopefully this will be remedied in a future version of PDO.

Sybase and Microsoft SQL Server

To create the stored procedure, run a query like the following.

create procedure testproc as select * from testtable

To exceute the stored procedure from an SQL Relay program, ue code like the following.

$stmt=$dbh->prepare("exec testproc");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Firebird

Stored procedures in Firebird can return a result set if a select query in the procedure selects values into the output parameters and then issues a suspend command, however SQL Relay doesn't currently support stored procedures that return result sets.

DB2

Stored procedures in DB2 can return a result set if the procedure is declared to return one, however SQL Relay doesn't currently support stored procedures that return result sets.

Postgresql

To create the stored procedure, run a query like the following.

create function testfunc() returns setof record as '
        declare output record;
begin
        for output in select * from mytable loop
                return next output;
        end loop;
        return;
end;
' language plpgsql

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("select * from testfunc() as (testint int, testfloat float, testchar char(40)");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop function testfunc

MySQL

The result sets of all select statements called within MySQL stored procedures (that aren't selected into variables) are returned from the procedure call. Though MySQL stored procedures can return multiple result sets, currently SQL Relay can only fetch the first result set.

To create the stored procedure which returns a result set, run a query like the following.

create procedure testproc() begin select * from mytable; end;

To execute the stored procedure from an SQL Relay program, use code like the following.

$stmt=$dbh->prepare("call testproc()");
$result=$stmt->execute();

To drop the stored procedure, run a query like the following.

drop procedure testproc

Getting the Last Insert ID

Databases with autoincrement or identity columns often provide functions which return the "last insert id"; the value of the autoincrement column that was generated during the insert into the database.

PDO provides the PDO
lastInsertId() method to get this value.

When using the SQLite database, you can also get the last insert id by running the query:

select last insert rowid

Driver-Specific Attributes

PHP PDO allows drivers to define their own attributes for database handles and statements. These attributes can be used with the getAttribute and setAttribute methods to control various parameters unique to the driver. SQL Relay implements several of these.

Database handle attributes defined by the SQL Relay driver:

Statement handle attributes defined by the SQL Relay driver:

Default values for the statement handle attributes can be set in the connect string. Setting the corresponding connect string variable has the same effect as if the statement handle attribute were set immediately after creating the statement. See Establishing a Session above.

Driver-Specific Methods

PHP PDO allows drivers to define their own custom methods to provide functionality unique to the driver. SQL Relay implements several of these related to ending, suspending and resuming sessions.

PDO provides a closeCursor method to close a cursor, but doesn't provide a similar method to close a database connection, aside from reusing the database handle or setting it to null.

Since SQL Relay can automatically re-establish closed connections when the next query is sent, it would be nice if there was a way to release the connection and give other clients a chance to access the database without invalidating the database handle. The endSession method makes this possible.

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
$stmt=$dbh->query("select * from my_table");

... do some stuff that takes a short time ...

$stmt=$dbh->query("select * from another_table");
$dbh->endSession();

... do some stuff that takes a long time ...

$stmt=$dbh->query("select * from yet_another_table");
$dbh->endSession();

... process the result set ...

Since the program does some stuff that takes a long time between the second and third queries, ending the session there allows another client an opportunity to use that database connection while this client is busy. The next call to query() establishes another session. Since the program does some stuff that takes a short time between the first two queries, it's OK to leave the session open between them.

Sometimes web-based applications need a single database transaction to span multiple pages. Since SQL Relay sessions can be suspended and resumed, this is possible.

First page:

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
$stmt=$dbh->query("select * from testtable");
$stmt->suspendResultSet();
$dbh->suspendSession();
$rs=$stmt->getResultSetId();
$port=$dbh->getConnectionPort();

... pass the rs, port and socket to the next page ...

Second page:

... get the rs, port and socket from previous page ...

$dbh=new PDO("sqlrelay:host=sqlrserver;port=9000;socket=/tmp/test.socket;tries=0;retrytime=1;debug=0","testuser","testpassword");
$stmt=$dbh->prepare(null);
$dbh->resumeSession($port,$socket);
$stmt->resumeResultSet($rs);

... run more queries in the same transaction ...

Note the strange call of $stmt->prepare(null) above. Resuming a result set requires an active statment to resume the result set into. Ideally resumeResultSet() would be a method of the database handle and return a statement, but the PDO developer API doesn't currently provide a method for creating a statement programatically. Until it does, this strangeness will be necessary.

You can also distribute the processing of a result set across a series of pages using suspended sessions. If you're buffering a result set in chunks instead of all at once and suspend a session, when you resume the session you can continue to retrieve rows from the result set.