[SQL Relay文档] SQL Relay用于PHP API的程序设计 (英文)

转载 2006年05月17日 23:18:00

 

Programming with SQL Relay using the PHP API

Establishing a Session

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

     "";

     sqlrcon_alloc"",,"","","",,;

      execute some queries 

     sqlrcon_free;

An alternative to running dl(sql_relay.so) is to put a line like:

extension=sql_relay.so

In your php.ini file. Doing this will improve performance as the library isn't loaded and unloaded each time a script runs, but only once when the web-server is started.

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.

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_sendQuery,"";

       some stuff that takes a short  

     sqlrcur_sendFileQuery,"","";
     sqlrcon_endSession;

       some stuff that takes a long  

     sqlrcur_sendQuery,"";
     sqlrcon_endSession;

      process the result set 

     sqlrcur_free;
     sqlrcon_free;

Note the call to sqlrcur_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 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 sqlrcon_commit() and sqlrcon_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. 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().

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

      sqlrcur_sendQuery,"" 
              sqlrcur_errorMessage;
              "";
     

     sqlrcur_free;
     sqlrcon_free;

Substitution and Bind Variables

Programs rarely execute fixed queries. More often than not, some part of the query is dynamically generated. The SQL Relay API provides methods for making substitutions and binds in those queries.

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

Rather than just calling sendQuery() you call prepareQuery(), substitution(), inputBind() and executeQuery(). If you using queries stored in a file, you can call prepareFileQuery() instead of prepareQuery().

When passing a floating point number in as a bind or substitution variable, you have to supply precision and scale for the number. See this page for a discussion of precision and scale.

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_prepareQuery,""
     sqlrcur_substitution,"","";
     sqlrcur_inputBind,"","";
     sqlrcur_inputBind,"",;
     sqlrcur_inputBind,"",,,;
     sqlrcur_executeQuery;

      process the result set 

     sqlrcur_free;
     sqlrcon_free;


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

If you're using a database with an embedded procedural language, you may want to retrieve data from function calls. To facilitate this, SQL Relay provides methods for defining and retrieving output bind variables.

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_prepareQuery,"";
     sqlrcur_inputBind,"",;
     sqlrcur_inputBind,"",;
     sqlrcur_inputBind,"",,,;
     sqlrcur_inputBind,"",,,;
     sqlrcur_inputBind,"",;
     sqlrcur_defineOutputBindInteger,"";
     sqlrcur_defineOutputBindDouble,"";
     sqlrcur_defineOutputBindString,"",;
     sqlrcur_executeQuery;
     sqlrcur_getOutputBindInteger,"";
     sqlrcur_getOutputBindDouble,"";
     sqlrcur_getOutputBindString,"";
     sqlrcon_endSession;

       something with the result 

     sqlrcur_free;
     sqlrcon_free;


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

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

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_executeQuery,"";

     "";
     ;

      read an image from a  into imagedata  the length of the
              into imagelength 

     "";
     ;

      read a description from a  into description  the length of
             the  into desclength 

     sqlrcur_prepareQuery,"";
     sqlrcur_inputBindBlob,"",,;
     sqlrcur_inputBindClob,"",,;
     sqlrcur_executeQuery;

     sqlrcur_free;
     sqlrcon_free;


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

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_prepareQuery,"";
     sqlrcur_defineOutputBindBlob,"";
     sqlrcur_defineOutputBindClob,"";
     sqlrcur_executeQuery;

     sqlrcur_getOutputBindBlob,"";
     sqlrcur_getOutputBindLength,"";

     sqlrcur_getOutputBindClob,"";
     sqlrcur_getOutputBindLength,"";

     sqlrcon_endSession;

       something with image  desc 

     sqlrcur_free;
     sqlrcon_free;


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, however 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.

       "";

       sqlrcon_alloc"",,"","","",,;
       sqlrcur_alloccon;

       sqlrcur_prepareQuery,"";
       sqlrcur_inputBind,"",;
       sqlrcur_executeQuery;

        process the result set 

       sqlrcur_clearBinds;
       sqlrcur_inputBind,"",;
       sqlrcur_executeQuery;

        process the result set 

       sqlrcur_clearBinds;
       sqlrcur_inputBind,"",;
       sqlrcur_executeQuery;

        process the result set 

       sqlrcur_free;
       sqlrcon_free;

Accessing Fields in the Result Set

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

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_sendQuery,"";
     sqlrcon_endSession;

      ; sqlrcur_rowCount;  
              ; sqlrcur_colCount;  
                      sqlrcur_getField,,;
                      "";
             
              "";
     

     sqlrcur_free;
     sqlrcon_free;

The sqlrcur_getField() function returns a string. If you would like to get a field as a long or double, you can use sqlrcur_getFieldAsLong() and sqlrcur_getFieldAsDouble().

You can also use sqlrcur_getRow() or sqlrcur_getRowAssoc() to get the entire row.

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_sendQuery,"";
     sqlrcon_endSession;

      ; sqlrcur_rowCount;  
             sqlrcur_getRow,;
              ; sqlrcur_colCount;  
                      rowarray;
                      "";
             
              "";
     

     sqlrcur_free;
     sqlrcon_free;

The sqlrcur_getField(), sqlrcur_getRow() and sqlrcur_getRowAssoc() 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() function. 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() function 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_getRowAssoc() and sqlrcur_getField() 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, getField(0,0) will return NULL and getField(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() function returns the number of rows returned so far. The sqlrcur_firstRowIndex() function returns the index of the first row of the currently buffered chunk.

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_setResultSetBufferSize,;

     sqlrcur_sendQuery,"";

      done 
              ; sqlrcur_colCount;  
                      sqlrcur_getField,, 
                              ;
                              "";
                       
                             done;
                     
             
              "";
             ;
     

    sqlrcur_sendQuery,"";

     process this query's result set in chunks also 

    sqlrcur_setResultSetBufferSize,;

    sqlrcur_sendQuery,"";

     process this query's result set all at once 

    sqlrcon_endSession;

    sqlrcur_free;
    sqlrcon_free;

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:

        "";

        sqlrcon_alloc"",,"","","",,;
        sqlrcur_alloc;
        sqlrcur_alloc;

        sqlrcur_setResultSetBufferSize,;
        sqlrcur_sendQuery,"";

        ;
         sqlrcur_endOfResultSet 
                sqlrcur_prepareQuery,"";
                sqlrcur_inputBind,"",sqlrcur_getField,index,;
                sqlrcur_inputBind,"",sqlrcur_getField,index,;
                sqlrcur_inputBind,"",sqlrcur_getField,index,;
                sqlrcur_executeQuery;
        

        sqlrcur_free;
        sqlrcur_free;
        sqlrcon_free;

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

        "";

        sqlrcon_alloc"",,"","","",,;
        sqlrcur_alloc;

        sqlrcur_prepareQuery,"";
        sqlrcur_defineOutputBindCursor,"";
        sqlrcur_executeQuery;

        sqlrcur         bindcursqlrcur_getOutputBindCursor,"";
        sqlrcur_fetchFromBindCursor;

        
         ; sqlrcur_rowCount;  
                 ; sqlrcur_colCount;  
                         sqlrcur_getField,,;
                         "";
                
                 "";
        

        sqlrcur_free;

        sqlrcur_free;
        sqlrcon_free;

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 (see this page for a discussion of precision and 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.

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_sendQuery,"";
     sqlrcon_endSession;

      ; sqlrcur_colCount;  
              "";
              sqlrcur_getColumnName,;
              "";
              "";
              sqlrcur_getColumnType,;
              "";
              "";
              sqlrcur_getColumnLength,;
              "";
              "";
              sqlrcur_getColumnPrecision,;
              "";
              "";
              sqlrcur_getColumnScale,;
              "";
              "";
              sqlrcur_getLongest,;
              "";
              "";
              sqlrcur_getColumnIsNullable,;
              "";
              "";
              sqlrcur_getColumnIsPrimaryKey,;
              "";
              "";
              sqlrcur_getColumnIsUnique,;
              "";
              "";
              sqlrcur_getColumnIsPartOfKey,;
              "";
              "";
              sqlrcur_getColumnIsUnsigned,;
              "";
              "";
              sqlrcur_getColumnIsZeroFilled,;
              "";
              "";
              sqlrcur_getColumnIsBinary,;
              "";
              "";
              sqlrcur_getColumnIsAutoIncrement,;
              "";
     

     sqlrcur_free;
     sqlrcon_free;

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.

        "";

        sqlrcon_alloc"",,"","","",,;
        sqlrcur_alloc;

        
        sqlrcur_upperCaseColumnNames;
        sqlrcur_endQuery,"";
        sqlrcon_endSession;

         ; sqlrcur_colCount;  
                 "";
                 sqlrcur_getColumnName,;
                 "";
        

        
        sqlrcur_lowerCaseColumnNames;
        sqlrcur_endQuery,"";
        sqlrcon_endSession;

         ; sqlrcur_colCount;  
                 "";
                 sqlrcur_getColumnName,;
                 "";
        

        
        sqlrcur_mixedCaseColumnNames;
        sqlrcur_endQuery,"";
        sqlrcon_endSession;

         ; sqlrcur_colCount;  
                 "";
                 sqlrcur_getColumnName,;
                 "";
        

        sqlrcur_free;
        sqlrcon_free;

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.

  testproc(in1  , in2  , in3  ) 

          mytable  (in1,in2,in3);
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;

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

  testproc
Sybase and Microsoft SQL Server

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

  testproc @in1 int, @in2 , @in3 () 
          mytable  (@in1,@in2,@in3)

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;

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

  testproc
Interbase and Firebird

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

  testproc(in1 , in2 , in3 ()) 

          mytable  (in1,in2,in3);
        suspend;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;

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

  testproc
DB2

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

  testproc( in1 int,  in2 double,  in3 ()) language sql

          mytable  (in1,in2,in3);
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;

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

  testproc
Postgresql

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

  testproc(int,,()) returns void  '

          mytable  ($,$,$);
        ;
;' language plpgsql

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;

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

  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.

  testproc(in1  , in2  , in3  ) returns  

         in1;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;
sqlrcur_getField,,;

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

  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.

  testproc(in1  , in2  , in3  , out1  ) 

        out1:=in1;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";

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

  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.

  testproc @in1 int, @in2 , @in3 (), @out1 int output 
         @out1=convert((),@in1)

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";

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

  testproc
Interbase and Firebird

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

  testproc(in1 , in2 , in3 ()) returns (out1 ) 

        out1=in1;
        suspend;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;
sqlrcur_getField,,;

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,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";

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

  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.

  testproc( in1 int,  in2 double,  in3 (),  out1 int) language sql

         out1 = in1;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";

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

  testproc
Postgresql

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

  testfunc(int,,()) returns int  '
declare
        in1 int;
        in2 ;
        in3 ();

        in1:=$;
        ;
;
' language plpgsql

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;
sqlrcur_getField,,;

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

  testfunc(int,,())

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.

  testproc(in1  , in2  , in3  , out1  , out2  , out3  ) 

        out1:=in1;
        out2:=in2;
        out3:=in3;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";

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

  testproc
Sybase and Microsoft SQL Server

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

  testproc @in1 int, @in2 , @in3 (), @out1 int output, @out2 int output, @out3 int output 
         @out1=convert((),@in1),
                @out2=convert((),@in2),
                @out2=convert((),@in2)

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";

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

  testproc
Interbase and Firebird

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

  testproc(in1 , in2 , in3 ()) returns (out1 , out2 , out3 ()) 

        out1=in1;
        out2=in2;
        out3=in3;
        suspend;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;

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,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";

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

  testproc
DB2

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

  testproc( in1 int,  in2 double,  in3 (),  out1 int,  out2 double,  out3 ()) language sql

         out1 = in1;
         out2 = in2;
         out3 = in3;

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_defineOutputBind,"",;
sqlrcur_executeQuery;
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";
sqlrcur_getOutputBind,"";

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

  testproc
Postgresql

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

  testfunc(int,,()) returns record  '
declare
        output record;

         $,$,$  output;
         output;
;
' language plpgsql

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

sqlrcur_prepareQuery,"";
sqlrcur_inputBind,"",;
sqlrcur_inputBind,"",,,;
sqlrcur_inputBind,"","";
sqlrcur_executeQuery;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;

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

  testfunc(int,,())

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.

  replace package types 
         cursorType  ref ;
;

  testproc  types.cursortype 
        l_cursor    types.cursorType;

        open l_cursor   *  mytable;
         l_cursor;
;

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

sqlrcur_prepareQuery,"";
sqlrcur_defineOutputBindCursor,"";
sqlrcur_executeQuery;
sqlrcur_getOutputBindCursor,"";
sqlrcur_fetchFromBindCursor;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;

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

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

  testfunc() returns setof record  '
        declare output record;

         output   *  mytable 
                 next output;
         ;
        ;
;
' language plpgsql

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

sqlrcur_sendQuery,"";
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;
sqlrcur_getField,,;

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

  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:

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

      generate a unique file name 

     sqlrcur_cacheToFile;
     sqlrcur_setCacheTtl,;
     sqlrcur_sendQuery,"";
     sqlrcon_endSession;
     sqlrcur_cacheOff;

      pass the filename to the next page 

     sqlrcur_free;
     sqlrcon_free;

Second CGI:

     "";

      get the filename from the previous page 

      get the page to display from the previous page 

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_openCachedResultSet,filename;
     sqlrcon_endSession;

      ; ;  
              ; sqlrcur_colCount;  
                      sqlrcur_getField,,;
                      "";
             
              "";
     

     sqlrcur_free;
     sqlrcon_free;

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:

     "";

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcur_sendQuery,"";
     sqlrcur_suspendResultSet;
     sqlrcon_suspendSession;
     sqlrcur_getResultSetId;
     sqlrcon_getConnectionPort;
     sqlrcon_getConnectionSocket;

      pass the rs, port  socket to the  page 

     sqlrcur_free;
     sqlrcon_free;

Second CGI:

     "";

      get rs, port  socket from previous page 

     sqlrcon_alloc"",,"","","",,;
     sqlrcur_alloc;

     sqlrcon_resumeSession,,;
     sqlrcur_resumeResultSet,;
     sqlrcur_sendQuery,"";
     sqlrcon_endSession;

     sqlrcur_free;
     sqlrcon_free;

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.

 From: http://sqlrelay.sourceforge.net/sqlrelay/programming/php.html

[SQL Relay文档] SQL Relay中PHP API参考手册 (英文)

 SQL-relay PHP module PHP API USING FUNCTIONS AUTHOR USINGFUNCTIONSAUTHORYou can use the ...
  • heiyeshuwu
  • heiyeshuwu
  • 2006年05月17日 23:27
  • 3875

[SQL Relay文档] SQL Relay中PHP API参考手册 (英文)

 SQL-relay PHP module PHP API USING FUNCTIONS AUTHOR USINGFUNCTIONSAUTHORYou can use th...
  • hejishan
  • hejishan
  • 2008年04月04日 14:27
  • 306

[SQL Relay文档] SQL Relay用于PHP Pear DB API的程序设计 (英文)

 Programming with SQL Relay using the PHP Pear DB API Establishing a Sessions Executing Queri...
  • heiyeshuwu
  • heiyeshuwu
  • 2006年05月17日 23:25
  • 2598

[SQL Relay文档] SQL Relay用于PHP Pear DB API的程序设计 (英文)

 Programming with SQL Relay using the PHP Pear DB API Establishing a Sessions Executing Que...
  • hejishan
  • hejishan
  • 2008年04月04日 14:28
  • 428

[SQL Relay文档] SQL Relay用于PHP API的程序设计 (英文)

 Programming with SQL Relay using the PHP API Establishing a Sessions Executing Queries ...
  • hejishan
  • hejishan
  • 2008年04月04日 14:29
  • 325

开源的数据库连接池 SQL Relay 介绍

  开源的数据库连接池 SQL Relay 介绍author: heiyelurenblog: http://blog.csdn.net/heiyeshuwu概述:SQL Relay是个功能强大并且非...
  • heiyeshuwu
  • heiyeshuwu
  • 2006年05月17日 23:07
  • 5290

SQL relay的C接口

Programming with SQL Relay using the C APICompiling an SQL Relay Client Program Establishing a Sessi...
  • playmud
  • playmud
  • 2005年06月02日 10:32
  • 2139

SQL-Relay学习笔记(1)

SQL-Relay笔记基本配置文件位置Unix和Linux下其配置文件位于 Built from source - /usr/local/firstworks/etc/sqlrelay.conf R...
  • liuyueyi1995
  • liuyueyi1995
  • 2016年09月23日 18:20
  • 665

Error reading relay log event: slave SQL thread was killed

1.版本 1)操作系统  cat /etc/issue Red Hat Enterprise Linux Server release 5.5 (Tikanga) Kernel \r on an ...
  • shaochenshuo
  • shaochenshuo
  • 2015年12月15日 13:59
  • 2995

SQL Relay学习笔记(5)

SQL Relay运行启动使用sqlr-start开启SQL Relay服务器进程sqlr-start[-id ID][-config CONFIGFILE][-localstatedir LOCAL...
  • liuyueyi1995
  • liuyueyi1995
  • 2016年09月29日 15:17
  • 280
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[SQL Relay文档] SQL Relay用于PHP API的程序设计 (英文)
举报原因:
原因补充:

(最多只允许输入30个字)