分享
 
 
 

SQL relay的C接口

王朝mssql·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

Programming with SQL Relay using the C API

Compiling an SQL Relay Client Program Establishing a Sessions Executing Queries Commits and Rollbacks Temporary Tables Catching Errors Substitution and Bind Variables Re-Binding and Re-Executing Accessing Fields in the Result Set Dealing With Large Result Sets Cursors Getting Column Information Stored Procedures Caching The Result Set Suspending and Resuming Sessions Compiling an SQL Relay Client Program

When writing an SQL Relay client program using the C API, you need to include the sqlrclientwrapper.h file.

#include <sqlrelay/sqlrclientwrapper.h>

You'll also need to link against the sqlrclientwrapper, sqlrclient and rudiments libraries. The include file is usually found in /usr/local/firstworks/include and the libraries are usually found in /usr/local/firstworks/lib.

The command to compile your .c file to object code will look something like this (assuming you're using the GNU C compiler):

gcc -I/usr/local/firstworks/include -c myprogram.c The command to compile your .o file to an executable will look something like this (assuming you're using the GNU C++ compiler):

g++ -o myprogram myprogram.o -L/usr/local/firstworks/lib -lsqlrclientwrapper -lsqlrclient -lrudiments Note that g++ was used to link, not gcc. You could alternatively link using gcc like this:

gcc -o myprogram myprogram.o -L/usr/local/firstworks/lib -lsqlrclientwrapper -lsqlrclient -lrudiments -lstdc++ When using the C API, it is important to compile the .c files to object code using the C compiler before linking them using the C++ compiler. Compiling/Linking in one step using the C++ compiler the will most likely fail as it will generate C++ style symbols for function calls which will not be resolved in the sqlrclientwrapper library since it contains C style function symbols. Compiling to object code using the C compiler as a seperate step ensures that C style symbols will be generated for function calls.

Establishing a Session

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

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

... execute some queries ...

sqlrcon_free(con);

}

After calling the constructor, a session is established when the first query, sqlrcur_ping() or sqlrcur_identify() 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 sqlrcur_sendQuery() or sqlrcur_sendFileQuery() to run a query.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_sendQuery(cur,"select * from my_table");

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

sqlrcur_sendFileQuery(cur,"/usr/local/myprogram/sql","myquery.sql");

sqlrcon_endSession(con);

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

sqlrcur_sendQuery(cur,"select * from my_other_table");

sqlrcon_endSession(con);

... process the result set ...

sqlrcur_free(cur);

sqlrcon_free(con);

}

Note the call to sqlrcon_endSession() after the call to sqlrcur_sendFileQuery(). Since the program does some stuff that takes a long time between that query and the next, ending the session there allows another client an opportunity to use that database connection while your client is busy. The next call to sqlrcur_sendQuery() 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.

Commits and Rollbacks

If you need to execute a commit or rollback, you should use the sqlrcon_commit() and sqlrcon_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 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 sqlrcon_commit() and sqlrcon_rollback() functions 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 with the sqlrcon_autoCommitOn() and sqlrcon_autoCommitOff() functions. 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, sqlrcon_autoCommitOn() and sqlrcon_autoCommitOff() have 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 sqlrcur_sendQuery() or sqlrcur_sendFileQuery() returns a 0, the query failed. You can find out why by calling sqlrcur_errorMessage().

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

if (!sqlrcur_sendQuery(cur,"select * from my_nonexistant_table")) {

printf("%s\n",sqlrcur_errorMessage(cur));

}

sqlrcur_free(cur);

sqlrcon_free(con);

}

Substitution and Bind Variables

Programs rarely execute fixed queries. More often than not, some part of the query is dynamically generated. It's convenient to store queries in files so they can be changed by a non-C programmer. The SQL Relay API provides functions for making substitutions and binds in those queries.

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

Rather than just calling sqlrcur_sendFileQuery() you call sqlrcur_prepareFileQuery(), one or more of sqlrcur_subString(), sqlrcur_subLong() or sqlrcur_subDouble, one or more of sqlrcur_inputBindString(), sqlrcur_inputBindLong() or sqlr_inputBindDouble() and sqlrcur_executeQuery().

/usr/local/myprogram/sql/myquery.sql:

select * from mytable $(whereclause)

Program code:

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_prepareFileQuery(cur,"/usr/local/myprogram/sql","myquery.sql");

sqlrcur_subString(cur,"whereclause","where col1=:value1");

sqlrcur_inputBindString(cur,"value1","true");

sqlrcur_executeQuery(cur);

... process the result set ...

sqlrcur_free(cur);

sqlrcon_free(con);

}

If you're using a database with an embedded procedural language, you may want to retrieve data from a call to one of it's functions. To facilitate this, SQL Relay provides the sqlrcur_defineOutputBind() and sqlrcur_getOutputBind() methods.

PL/SQL Procedure:

FUNCTION sp_mytable RETURN types.cursorType

l_cursor types.cursorType;

BEGIN

OPEN l_cursor FOR SELECT * FROM mytable;

RETURN l_cursor;

END;

Program code: #include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int result;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_prepareQuery(cur,"begin :result:=addTwoNumbers(:num1,:num2); end;");

sqlrcur_inputBindLong(cur,"num1",10);

sqlrcur_inputBindLong(cur,"num2",20);

sqlrcur_defineOutputBind(cur,"result",100);

sqlrcur_executeQuery(cur);

result=atoi(sqlrcur_getOutputBind(cur,"result"));

sqlrcon_endSession(con);

... do something with the result ...

sqlrcur_free(cur);

sqlrcon_free(con);

}

The sqlrcur_getOutputBind() function returns a NULL value as an empty string. If you would it to come back as a NULL instead, you can call the sqlrcur_getNullsAsNulls() method. To revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().

The sqlrcur_getOutputBind() function returns a string, if you would like to get the value as a long or double, you can use sqlrcur_getOutputBindAsLong() or sqlrcur_getOutputBindAsDouble().

If you are using Oracle 8i or higher, you can insert data into BLOB and CLOB columns using the inputBindBlob(), inputBindClob() methods.

If you are curious how many bind variables have been declared in a query, you can call sqlrcur_countBindVariables() after preparing the query.

#include <sqlrelay/sqlrclientwrapper.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_executeQuery(cur,"create table images (image blob, description clob)");

unsigned char imagedata[40000];

unsigned long imagelength;

... read an image from a file into imagedata and the length of the

file into imagelength ...

unsigned char description[40000];

unsigned long desclength;

... read a description from a file into description and the length of

the file into desclength ...

sqlrcur_prepareQuery(cur,"insert into images values (:image,:desc)");

sqlrcur_inputBindBlob(cur,"image",imagedata,imagelength);

sqlrcur_inputBindClob(cur,"desc",description,desclength);

sqlrcur_executeQuery(cur);

sqlrcur_free(cur);

sqlrcur_free(con);

}

Likewise, with Oracle 8i, you can retreive BLOB or CLOB data using defineOutputBindBlob(), defineOutputBindClob(), getOutputBind() and getOutputBindLength().

#include <sqlrelay/sqlrclientwrapper.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_prepareQuery(cur,"begin select image into :image from images; select description into :desc from images; end;");

sqlrcur_defineOutputBindBlob(cur,"image");

sqlrcur_defineOutputBindClob(cur,"desc");

sqlrcur_executeQuery(cur);

char *image=sqlrcur_getOutputBind(cur,"image");

long imagelength=sqlrcur_getOutputBindLength(cur,"image");

char *desc=sqlrcur_getOutputBind(cur,"desc");

char *desclength=sqlrcur_getOutputBindLength(cur,"desc");

sqlrcon_endSession(con);

... do something with image and desc ...

sqlrcur_free(cur);

sqlrcon_free(con);

}

Sometimes it's convenient to bind a bunch of variables that may or may not actually be in the query. For example, if you are building a web based application, it may be easy to just bind all the form variables/values from the previous page, even though some of them don't appear in the query. Databases usually generate errors in this case. Calling validateBinds() just prior to calling executeQuery() causes the API to check the query for each bind variable before actually binding it, preventing those kinds of errors. There is a performance cost associated with calling validateBinds().

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.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_prepareQuery(cur,"select * from mytable where mycolumn>:value");

sqlrcur_inputBindLong(cur,"value",1);

sqlrcur_executeQuery(cur);

... process the result set ...

sqlrcur_clearBinds(cur);

sqlrcur_inputBindLong(cur,"value",5);

sqlrcur_executeQuery(cur);

... process the result set ...

sqlrcur_clearBinds(cur);

sqlrcur_inputBindLong(cur,"value",10);

sqlrcur_executeQuery(cur);

... process the result set ...

sqlrcur_free(cur);

sqlrcon_free(con);

}

Accessing Fields in the Result Set

The sqlrcur_rowCount(), sqlrcur_colCount(), sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions are useful for processing result sets.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int row,col;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_sendQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

for (row=0; row<sqlrcur_rowCount(cur); row++) {

for (col=0; col<sqlrcur_colCount(cur); col++) {

printf("%s,",sqlrcur_getFieldByIndex(cur,row,col));

}

printf("\n");

}

sqlrcur_free(cur);

sqlrcon_free(con);

}

The sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions return strings. If you would like to get a field as a long or double, you can use sqlrcur_getFieldAsLongByName()/sqlrcur_getFieldAsLongByIndex() and sqlrcur_getFieldAsDoubleByName()/sqlrcur_getFieldAsDoubleByIndex().

You can also use sqlrcur_getRow() which returns a NULL-terminated array of the fields in the row.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int row,col;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_sendQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

for (row=0; row<sqlrcur_rowCount(cur); row++) {

char **rowarray=sqlrcur_getRow(cur,row);

for (col=0; col<sqlrcur_colCount(cur); col++) {

printf("%s,",rowarray[col]);

}

printf("\n");

}

sqlrcur_free(cur);

sqlrcon_free(con);

}

The sqlrcur_getFieldByIndex(), sqlrcur_getFieldByName() and sqlrcur_getRow() functions return NULL fields as empty strings. If you would like them to come back as NULL's instead, you can call the sqlrcur_getNullsAsNulls() method. To revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().

If you want to access the result set, but don't care about the column information (column names, types or sizes) and don't mind getting fields by their numeric index instead of by name, you can call the sqlrcur_dontGetColumnInfo() method prior to executing your query. This can result in a performance improvement, especially when many queries with small result sets are executed in rapid succession. You can call sqlrcur_getColumnInfo() again later to turn off this feature.

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 sqlrcur_setResultSetBufferSize() to set the number of rows to buffer at a time. Calls to sqlrcur_getRow(), sqlrcur_getFieldByIndex() and sqlrcur_getFieldByName() 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 setResultSetBufferSize(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, getFieldByIndex(0,0) will return NULL and getFieldByIndex(11,0) will cause rows 10-14 to be fetched and return the requested value.

When buffering the result set in chunks, don't end the session until after you're done with the result set.

If you call sqlrcur_setResultSetBufferSize() and forget what you set it to, you can always call sqlrcur_getResultSetBufferSize().

When buffering a result set in chunks, the sqlrcur_rowCount() method returns the number of rows returned so far. The sqlrcur_firstRowIndex() method returns the index of the first row of the currently buffered chunk.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int done=0;

int row=0;

int col;

char *field;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_setResultSetBufferSize(cur,5);

sqlrcur_sendQuery(cur,"select * from my_table");

while (!done) {

for (col=0; col<sqlrcur_colCount(cur); col++) {

if (field=sqlrcur_getFieldByIndex(cur,row,col)) {

printf("%s,",field);

} else {

done=1;

}

}

printf("\n");

row++;

}

sqlrcur_sendQuery(cur,"select * from my_other_table");

... process this query's result set in chunks also ...

sqlrcur_setResultSetBufferSize(cur,0);

sqlrcur_sendQuery(cur,"select * from my_third_table");

... process this query's result set all at once ...

sqlrcon_endSession(con);

sqlrcur_free(cur);

sqlrcon_free(con);

}

Cursors

Cursors make it 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:

#include <sqlrelay/sqlrclientwrapper.h>

main() {

sqlrcon con;

sqlrcur cursor1;

sqlrcur cursor2;

int index;

con=new sqlrcon_alloc("host",9000,"","user","password",0,1);

cursor1=new sqlrcur_alloc(con);

cursor2=new sqlrcur_alloc(con);

sqlrcur_setResultSetBufferSize(cursor1,10);

sqlrcur_sendQuery(cursor1,"select * from my_huge_table");

index=0;

while (!sqlrcur_endOfResultSet(cursor1)) {

sqlrcur_prepareQuery(cursor2,"insert into my_other_table values (:1,:2,:3)");

sqlrcur_inputBindString(cursor2,"1",sqlrcur_getFieldByIndex(cursor1,index,1));

sqlrcur_inputBindString(cursor2,"2",sqlrcur_getFieldByIndex(cursor1,index,2));

sqlrcur_inputBindString(cursor2,"3",sqlrcur_getFieldByIndex(cursor1,index,3));

sqlrcur_executeQuery(cursor2);

}

sqlrcur_free(cursor2);

sqlrcur_free(cursor1);

sqlrcon_free(con);

}

Prior to SQL Relay version 0.25, you would have had to buffer the first result set or use 2 database connections instead of just 1.

If you are using stored procedures with Oracle 8i or higher, a stored procedure can execute a query and return a cursor. A cursor bind variable can then retrieve that cursor. Your program can retrieve the result set from the cursor. All of this can be accomplished using defineOutputBindCursor(), getOutputBindCursor() and fetchFromOutputBindCursor().

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int i,j;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_prepareQuery(cur,"begin :curs:=sp_mytable; end;");

sqlrcur_defineOutputBindCursor(cur,"curs");

sqlrcur_executeQuery(cur);

sqlrcur bindcur=sqlrcur_getOutputBindCursor(cur,"curs");

sqlrcur_fetchFromBindCursor(bindcur);

// print fields from table

for (i=0; i<sqlrcur_rowCount(bindcur); i++) {

for (j=0; j<sqlrcur_colCount(bindcur); j++) {

printf("%s,",sqlrcur_getFieldByIndex(bindcur,i,j));

}

printf("\n");

}

sqlrcur_free(bindcur);

sqlrcur_free(cur);

sqlrcon_free(con);

}

The number of cursors simultaneously available per-connection is set at compile time and defaults to 5.

Getting Column Information

For each column, the API supports getting the name, type and length of each field. All databases support these attributes. The API also supports getting the precision, scale, length of the longest field, and whether the column is nullable, the primary key, unique, part of a key, unsigned, zero-filled, binary, or an auto-incrementing field. However, not all databases support these attributes. If a database doesn't support an attribute, it is always returned as false.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int i;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_sendQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

for (i=0; i<sqlrcur_colCount(cur); i++) {

printf("Name: %s\n",sqlrcur_getColumnName(cur,i));

printf("Type: %s\n",sqlrcur_getColumnType(cur,i));

printf("Length: %d\n",sqlrcur_getColumnLength(cur,i));

printf("Precision: %d\n",sqlrcur_getColumnPrecision(cur,i));

printf("Scale: %d\n",sqlrcur_getColumnScale(cur,i));

printf("Longest Field: %d\n",sqlrcur_getLongest(cur,i));

printf("Nullable: %d\n",sqlrcur_getColumnIsNullable(cur,i));

printf("Primary Key: %d\n",sqlrcur_getColumnIsPrimaryKey(cur,i));

printf("Unique: %d\n",sqlrcur_getColumnIsUnique(cur,i));

printf("Part of Key: %d\n",sqlrcur_getColumnIsPartOfKey(cur,i));

printf("Unsigned: %d\n",sqlrcur_getColumnIsUnsigned(cur,i));

printf("Zero Filled: %d\n",sqlrcur_getColumnIsZeroFilled(cur,i));

printf("Binary: %d\n",sqlrcur_getColumnIsBinary(cur,i));

printf("Auth Increment:%d\n",sqlrcur_getColumnIsAutoIncrement(cur,i));

printf("\n");

}

sqlrcur_free(cur);

sqlrcon_free(con);

}

Some databases force column names to upper case, others force column names to lower case, and others still support mixed-case column names. Sometimes, when migrating between databases, you can run into trouble. You can use upperCaseColumnNames() and lowerCaseColumnNames() to cause column names to be converted to upper or lower case, or you can use mixedCaseColumnNames() to cause column names to be returned in the same case as they are defined in the database.

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int i;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

// column names will be forced to upper case

sqlrcur_upperCaseColumnNames(cur);

sqlrcur_endQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

for (i=0; i<sqlrcur_colCount(cur); i++) {

printf("Name: %s\n",sqlrcur_getColumnName(cur,i));

}

// column names will be forced to lower case

sqlrcur_lowerCaseColumnNames(cur);

sqlrcur_endQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

for (i=0; i<sqlrcur_colCount(cur); i++) {

printf("Name: %s\n",sqlrcur_getColumnName(cur,i));

}

// column names will be the same as they are in the database

sqlrcur_mixedCaseColumnNames(cur);

sqlrcur_endQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

for (i=0; i<sqlrcur_colCount(cur); i++) {

printf("Name: %s\n",sqlrcur_getColumnName(cur,i));

}

sqlrcur_free(cur);

sqlrcon_free(con);

}

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.

sqlrcur_prepareQuery(cur,"begin testproc(:in1,:in2,:in3); end;");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_executeQuery(cur);

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.

sqlrcur_prepareQuery(cur,"exec testproc");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_executeQuery(cur);

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.

sqlrcur_prepareQuery(cur,"execute procedure testproc ?, ?, ?");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_executeQuery(cur);

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.

sqlrcur_prepareQuery(cur,"call testproc(?,?,?)");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_executeQuery(cur);

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.

sqlrcur_prepareQuery(cur,"select testproc(:in1,:in2,:in3)");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_executeQuery(cur);

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.

sqlrcur_prepareQuery(cur,"select testproc(:in1,:in2,:in3) from dual");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getFieldByIndex(cur,0,0);

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

drop function 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.

sqlrcur_prepareQuery(cur,"begin testproc(:in1,:in2,:in3,:out1); end;");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_defineOutputBind(cur,"out1",20);

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getOutputBind(cur,"out1");

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.

sqlrcur_prepareQuery(cur,"exec testproc");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_defineOutputBind(cur,"out1",20);

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getOutputBind(cur,"out1");

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)) returns (out1 integer) as

begin

out1=in1;

suspend;

end;

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

sqlrcur_prepareQuery(cur,"select * from testproc(?,?,?)");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getFieldByIndex(cur,0,0);

Alternatively, you can run a query like the following and receive the result using an output bind variable. Note that in Interbase/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.

sqlrcur_prepareQuery(cur,"execute procedure testproc ?, ?, ?");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_defineOutputBind(cur,"1",20);

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getOutputBind(cur,"1");

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.

sqlrcur_prepareQuery(cur,"call testproc(?,?,?,?)");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_defineOutputBind(cur,"4",25);

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getOutputBind(cur,"4");

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.

sqlrcur_prepareQuery(cur,"select * from testfunc(:in1,:in2,:in3)");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,4,2);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_executeQuery(cur);

char *result=sqlrcur_getFieldByIndex(cur,0,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.

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.

sqlrcur_prepareQuery(cur,"begin testproc(:in1,:in2,:in3,:out1,:out2,:out3); end;");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_defineOutputBind(cur,"out1",20);

sqlrcur_defineOutputBind(cur,"out2",20);

sqlrcur_defineOutputBind(cur,"out3",20);

sqlrcur_executeQuery(cur);

char *out1=sqlrcur_getOutputBind(cur,"out1");

char *out2=sqlrcur_getOutputBind(cur,"out2");

char *out3=sqlrcur_getOutputBind(cur,"out3");

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.

sqlrcur_prepareQuery(cur,"exec testproc");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_defineOutputBind(cur,"out1",20);

sqlrcur_defineOutputBind(cur,"out2",20);

sqlrcur_defineOutputBind(cur,"out3",20);

sqlrcur_executeQuery(cur);

char *out1=sqlrcur_getOutputBind(cur,"out1");

char *out2=sqlrcur_getOutputBind(cur,"out2");

char *out3=sqlrcur_getOutputBind(cur,"out3");

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

sqlrcur_prepareQuery(cur,"select * from testproc(?,?,?)");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_executeQuery(cur);

char *out1=sqlrcur_getFieldByIndex(cur,0,0);

char *out2=sqlrcur_getFieldByIndex(cur,0,1);

char *out3=sqlrcur_getFieldByIndex(cur,0,2);

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

sqlrcur_prepareQuery(cur,"execute procedure testproc ?, ?, ?");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_defineOutputBind(cur,"1",20);

sqlrcur_defineOutputBind(cur,"2",20);

sqlrcur_defineOutputBind(cur,"3",20);

sqlrcur_executeQuery(cur);

char *out1=sqlrcur_getOutputBind(cur,"1");

char *out2=sqlrcur_getOutputBind(cur,"2");

char *out3=sqlrcur_getOutputBind(cur,"3");

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.

sqlrcur_prepareQuery(cur,"call testproc(?,?,?,?,?,?)");

sqlrcur_inputBindLong(cur,"1",1);

sqlrcur_inputBindDouble(cur,"2",1.1,2,1);

sqlrcur_inputBindString(cur,"3","hello");

sqlrcur_defineOutputBind(cur,"4",25);

sqlrcur_defineOutputBind(cur,"5",25);

sqlrcur_defineOutputBind(cur,"6",25);

sqlrcur_executeQuery(cur);

char *out1=sqlrcur_getOutputBind(cur,"4");

char *out2=sqlrcur_getOutputBind(cur,"5");

char *out3=sqlrcur_getOutputBind(cur,"6");

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.

sqlrcur_prepareQuery(cur,"select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))");

sqlrcur_inputBindLong(cur,"in1",1);

sqlrcur_inputBindDouble(cur,"in2",1.1,4,2);

sqlrcur_inputBindString(cur,"in3","hello");

sqlrcur_executeQuery(cur);

char *out1=sqlrcur_getFieldByIndex(cur,0,0);

char *out2=sqlrcur_getFieldByIndex(cur,0,1);

char *out3=sqlrcur_getFieldByIndex(cur,0,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

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

create or replace package types as

type cursorType is ref cursor;

end;

create function testproc return types.cursortype is

l_cursor types.cursorType;

begin

open l_cursor for select * from mytable;

return l_cursor;

end;

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

sqlrcur_prepareQuery(cur,"begin :curs:=testproc; end;");

sqlrcur_defineOutputBindCursor(cur,"curs");

sqlrcur_executeQuery(cur);

sqlrcur bindcur=sqlrcur_getOutputBindCursor(cur,"curs");

sqlrcur_fetchFromBindCursor(bindcur);

char *field00=sqlrcur_getFieldByIndex(bindcur,0,0);

char *field01=sqlrcur_getFieldByIndex(bindcur,0,1);

char *field02=sqlrcur_getFieldByIndex(bindcur,0,2);

char *field10=sqlrcur_getFieldByIndex(bindcur,1,0);

char *field11=sqlrcur_getFieldByIndex(bindcur,1,1);

char *field12=sqlrcur_getFieldByIndex(bindcur,1,2);

...

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

drop function testproc

drop package types

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.

sqlrcur_sendQuery(cur,"select * from testfunc() as (testint int, testfloat float, testchar char(40))");

char *field00=sqlrcur_getFieldByIndex(cur,0,0);

char *field01=sqlrcur_getFieldByIndex(cur,0,1);

char *field02=sqlrcur_getFieldByIndex(cur,0,2);

char *field10=sqlrcur_getFieldByIndex(cur,1,0);

char *field11=sqlrcur_getFieldByIndex(cur,1,1);

char *field12=sqlrcur_getFieldByIndex(cur,1,2);

...

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

drop function testfunc

Caching The Result Set

Say you're writing a web-based report where a query with a huge result set is executed and 20 rows are displayed per page. Rather than rerunning the query for every page every time and dumping all but the 20 rows you want to display, you can run the query once, cache the result set to a local file and just open the file for each page of the report.

First CGI:

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

char *filename;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

... generate a unique file name ...

sqlrcur_cacheToFile(cur,filename);

sqlrcur_setCacheTtl(cur,600);

sqlrcur_sendQuery(cur,"select * from my_table");

sqlrcon_endSession(con);

sqlrcur_cacheOff(cur);

... pass the filename to the next page ...

sqlrcur_free(cur);

sqlrcon_free(con);

}

Second CGI:

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int row,col;

... get the filename from the previous page ...

... get the page to display from the previous page ...

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_openCachedResultSet(cur,filename);

sqlrcon_endSession(con);

for (row=pagetodisplay*20; row<(pagetodisplay+1)*20; row++) {

for (col=0; col<sqlrcur_colCount(cur); col++) {

printf("%s,",sqlrcur_getFieldByIndex(cur,row,col));

}

printf("\n");

}

sqlrcur_free(cur);

sqlrcon_free(con);

}

Suspending and Resuming Sessions

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 CGI:

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

int port;

char *socket;

int rs;

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcur_sendQuery(cur,"insert into my_table values (1,2,3)");

port=getConnectionPort(cur);

socket=getConnectionSocket(cur);

rs=sqlrcur_getResultSetId(cur);

sqlrcur_suspendResultSet(cur);

sqlrcon_suspendSession(con);

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

sqlrcur_free(cur);

sqlrcon_free(con);

}

Second CGI:

#include <sqlrelay/sqlrclientwrapper.h>

#include <stdio.h>

main() {

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

sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);

sqlrcur cur=sqlrcur_alloc(con);

sqlrcon_resumeSession(con,port,socket);

sqlrcur_resumeResultSet(cur,rs);

sqlrcur_sendQuery(cur,"commit");

sqlrcon_endSession(con);

sqlrcur_free(cur);

sqlrcon_free(con);

}

You can also distribute the processing of a result set across a series of CGI's 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.

Similarly, if you're buffering a result set in chunks, caching that result set and suspend your session. When you resume the session, you can continue caching the result set. You must use sqlrcur_resumeCachedResultSet() instead of sqlrcur_resumeResultSet() however.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有