Programming with SQL Relay using the Ruby DBI API

Establishing a Session

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

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

... execute some queries ...

After calling connect() and prepare(), a session is established when the first execute() is run.

For the duration of the session, the client stays connected to a database connection daemon. While one client is connected, no other client can connect. Care should be taken to minimize the length of a session.

If you're using a transactional database, ending a session has a catch. Database connection daemons can be configured to send either a commit or rollback at the end of a session if DML queries were executed during the session with no commit or rollback. Program accordingly.

Executing Queries

Call prepare() and execute() to run a query.

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt=db.prepare("select * from mytable")

stmt.execute()

... process the result set ...
Commits and Rollbacks

If you need to execute a commit or rollback, you should use the commit() and rollback() methods rather than sending a "commit" or "rollback" query. There are two reasons for this. First, it's much more efficient to call the methods. 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() methods you instruct the database connection daemon to call the commit and rollback API methods for that database rather than issuing them as queries. If the API's have no commit or rollback methods, the calls do nothing and the database throws no error. This is especially important when using SQL Relay with ODBC.

You can also turn Autocommit on or off by setting the AutoCommit attribute of the database handle.

The following command turns Autocommit on.

db["AutoCommit"]=true

The following command turns Autocommit off.

db["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 the AutoCommit attribute has no effect.

Temporary Tables

Some databases support temporary tables. That is, tables which are automatically dropped or truncated when an application closes it's 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 it's 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

If your call to execute() raises an exception, the query failed. You can find out why by catching the exception.

require 'dbi'

begin
        db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

        stmt=db.prepare("select * from mytable")

        stmt.execute()

rescue DBI::ProgrammingError => error
        print error
        print "\n"
end
Bind Variables

Programs rarely execute fixed queries. More often than not, some part of the query is dynamically generated. The Ruby DBI API provides the bind_param method for using bind variables in those queries.

For a detailed discussion of binds, see this document.

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt=db.prepare("select * from mytable where column1>:val1 and column2=:val2 and column3<val3")

stmt.bind_param("val1",1,false)
stmt.bind_param("val2","hello",false)
stmt.bind_param("val3",50.546,false)

stmt.execute()

... process the result set ...

Re-Binding and Re-Execution

A 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.

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt=db.prepare("select * from mytable where column1&gt;:val1 and column2=:val2 and column3&lt;val3")

stmt.bind_param("val1",1,false)
stmt.bind_param("val2","hello",false)
stmt.bind_param("val3",1.1,false)

stmt.execute()

... process the result set ...

stmt.bind_param("val1",2,false)
stmt.bind_param("val2","hi",false)
stmt.bind_param("val3",2.22,false)

stmt.execute()

... process the result set ...

stmt.bind_param("val1",3,false)
stmt.bind_param("val2","bye",false)
stmt.bind_param("val3",3.333,false)

stmt.execute()

... process the result set ...

Accessing Fields in the Result Set

The fetch(), fetch_many() and fetch_all() methods are useful for processing result sets. fetch() returns a list of values. fetch_many() and fetch_all() each return an Array of rows where each row is an Array of values.

The rows() method gives the number of rows in the result set of a select query.

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt=db.prepare("select * from mytable")

stmt.execute()

print "rowcount: "+stmt.rows().to_s+"\n"

print "the first row:\n"
for i in stmt.fetch()
        print i+","
end
print "\n\n"

print "the next three rows:\n"
for i in stmt.fetch_many()
        for j in i
                print j+","
        end
        print "\n"
end
print "\n"

print "the rest of the rows:\n"
for i in stmt.fetch_all()
        for j in i
                print j+","
        end
        print "\n"
end

The fetch_scroll() method provides arbitrary access to the result set. You can use it to skip forward or backward.

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt=db.prepare("select * from mytable")

stmt.execute()

print "the first row:\n"
for i in stmt.fetch_scroll(DBD:SQL_FETCH_FIRST)
        print i+","
end
print "\n\n"

print "the last row:\n"
for i in stmt.fetch_scroll(DBD:SQL_FETCH_LAST)
        print i+","
end
print "\n\n"

print "the second to last row:\n"
for i in stmt.fetch_scroll(DBD:SQL_FETCH_PRIOR)
        print i+","
end
print "\n\n"

print "the last row again:\n"
for i in stmt.fetch_scroll(DBD:SQL_FETCH_NEXT)
        print i+","
end
print "\n\n"

print "the first row again:\n"
for i in stmt.fetch_scroll(DBD:SQL_FETCH_ABSOLUTE,0)
        print i+","
end
print "\n\n"

print "the 4th row:\n"
for i in stmt.fetch_scroll(DBD:SQL_FETCH_RELATIVE,3)
        print i+","
end
print "\n\n"

Concurrent Statements

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

For example:

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt1=db.prepare("select * from mytable")
stmt2=db.prepare("insert into my_other_table values (:var1,:var2:,var3)")

stmt1.execute()

for i in stmt1.fetch_all():
        stmt2.bind_param("var1",i[0],false)
        stmt2.bind_param("var2",i[1],false)
        stmt2.bind_param("var3",i[2],false)
        stmt2.execute()
end
Getting Column Information

After executing a query, column information can be retrieved using the column_info() method. column_info() returns an Array of hashes. Each hash contains 'name', 'type_name' and 'precision' keys.

require 'dbi'

db=DBI.connect("DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket","myuser","mypassword")

stmt=db.prepare("select * from mytable")

stmt.execute()

for i in stmt.column_info()
        print "Name:       "+i['name']+"\n"
        print "Type:       "+i['type_name']+"\n"
        print "Length:     "+i['precision']+"\n"
end
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.

SQL Relay supports stored procedures for most databases, but there are some caveats. Stored procedures are not currently supported when using FreeTDS against Sybase or Microsoft SQL Server. Blob/Clob bind variables are only supported in Oracle 8i or higher. Sybase stored procedures must use varchar output parameters.

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=db.prepare("begin testproc(:in1,:in2,:in3); end;")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
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=db.prepare("exec testproc")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
stmt.execute()

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

drop procedure testproc
Interbase and 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=db.prepare("exec procedure testproc ?,?,?")
stmt.bind_param("1",1,false)
stmt.bind_param("2",1.1,false)
stmt.bind_param("3","hello",false)
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=db.prepare("call testproc(?,?,?)")
stmt.bind_param("1",1,false)
stmt.bind_param("2",1.1,false)
stmt.bind_param("3","hello",false)
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 testproc(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=db.prepare("select testproc(:in1,:in2,:in3)")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
stmt.execute()

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

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=db.prepare("select testproc(:in1,:in2,:in3) from dual")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
stmt.execute()
result=stmt.fetch()[0]

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

drop function 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. However, the SQL Relay Python DBdriver does not currently support output parameters.

Interbase and 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) as
begin
        out1=in1;
        suspend;
end;

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

stmt=db.prepare("select * from testproc(:in1,:in2,:in3)")
stmt.bind_param("1",1,false)
stmt.bind_param("2",1.1,false)
stmt.bind_param("3","hello",false)
stmt.execute()
result=stmt.fetch()[0]

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. However, the SQL Relay Python DB driver does not currently support output parameters.

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=db.prepare("select * from testfunc(:in1,:in2,:in3)")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
stmt.execute()
result=stmt.fetch()[0]

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

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

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. However, the SQL Relay Python DB driver does not currently support output parameters.

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. However, the SQL Relay Python DB driver does not currently support output parameters.

Interbase and 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=db.prepare("select * from testfunc(?,?,?)")
stmt.bind_param("1",1,false)
stmt.bind_param("2",1.1,false)
stmt.bind_param("3","hello",false)
stmt.execute()
out1=stmt.fetch()[0]
out2=stmt.fetch()[1]
out3=stmt.fetch()[2]

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. However, the SQL Relay Python DB driver does not currently support output parameters.

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=db.prepare("select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
stmt.execute()
out1=stmt.fetch()[0]
out2=stmt.fetch()[1]
out3=stmt.fetch()[2]

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

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

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

Stored procedures in Oracle can return open cursors as return values or output parameters. A client side cursor can be bound to this open cursor and rows can be fetched from it. However, the SQL Relay Python DB driver does not currently support output parameters.

Sybase and Microsoft SQL Server

Stored procedures in Sybase and Microsoft SQL Server can return a result set if the last command in the procedure is a select query, however SQL Relay doesn't currently support stored procedures that return result sets.

Interbase and Firebird

Stored procedures in Interbase and 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=db.prepare("select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))")
stmt.bind_param("in1",1,false)
stmt.bind_param("in2",1.1,false)
stmt.bind_param("in3","hello",false)
stmt.execute()
row1=stmt.fetch()
row2=stmt.fetch()
row3=stmt.fetch()
...

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

drop function testfunc