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

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

 

Programming with SQL Relay using the PHP Pear DB API

Establishing a Session

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


 '';

  DBconnect"";
 DBisError 
         getMessage;


 execute some queries 

disconnect;


In this connect string, the following components have the following meanings:

testuser - the username to use to connect to the SQL Relay server, corresponds to the username in an entry in the "users" tag of the sqlrelay.conf file
testpassword - the password to use to connect to the SQL Relay server, corresponds to the password in an entry in the "users" tag of the sqlrelay.conf file
testhost - the hostname that the SQL Relay server is running on
9000 - the port that the SQL Relay server is listening on
testdb - this parameter is ignored; the pear DB spec requires that a database name be included in the connect string, but the database that SQL Relay is connected to is defined in the sqlrelay.conf file

After calling the constructor, 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 query() to run a query. query() will return DB_OK for successful DML or DDL queries or a result set for select queries.


 '';

  DBconnect"";
 DBisError 
         getMessage;


 query""DB_OK 
         getMessage;


 query""DB_OK 
         getMessage;


query"";

 process the result set 

free;

disconnect;


Commits and Rollbacks

If you need to execute a commit or rollback, you should use the commit() and rollback() functions rather than sending a "commit" or "rollback" query. There are two reasons for this. First, it's much more efficient to call the functions. Second, if you're writing code that can run on transactional or non-transactional databases, some non-transactional databases will throw errors if they receive a "commit" or "rollback" query, but by calling the commit() and rollback() functions you instruct the database connection daemon to call the commit and rollback API functions for that database rather than issuing them as queries. If the API's have no commit or rollback functions, the calls do nothing and the database throws no error. This is especially important when using SQL Relay with ODBC.

You can also turn Autocommit on or off by passing true or false to the autoCommit() function.

The following command turns Autocommit on.

autoCommit;

The following command turns Autocommit off.

autoCommit;

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, autoCommit() 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

For most functions, you can find out if an error occurred by calling DB::isError() on the result.

When running DML or DDL queries, the query() function should return DB_OK. If it returns anything else, then that also indicates that an error has occurred. When running a select, query() returns a result set and DB::isError() must be used to determine if an error has occurred.

After determining that an error has occurred you can find out why by calling getMessage().


 '';

  DBconnect"";
 DBisError 
         getMessage;


 query""DB_OK 
         getMessage;


 query""DB_OK 
         getMessage;


query"";
 DBisError 
         getMessage;


 process the result set 

free;

disconnect;


Bind Variables

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

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

Here is an example where an associative array is used to bind a set of values to a set of variables. The values in the array are the values that will be substituted in place of the bind variables. The keys in the array may refer to either the name or (1-based) position of the bind variable. The query must be run by calling prepare() and execute(). The array is passed as the second parameter of the call to execute().


 '';

  DBconnect"";
 DBisError 
         getMessage;


prepare"";
""  ,
                ""  "",
                ""  "",
                ""  "",
                ""  "";
execute,;

disconnect;


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.

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.


 '';

  DBconnect"";
 DBisError 
         getMessage;


prepare"";

""  ,
                ""  "",
                ""  "",
                ""  "",
                ""  "";
execute,;

""  ,
                ""  "",
                ""  "",
                ""  "",
                ""  "";
execute,;

""  ,
                ""  "",
                ""  "",
                ""  "",
                ""  "";
execute,;

""  ,
                ""  "",
                ""  "",
                ""  "",
                ""  "";
execute,;


disconnect;


Accessing Fields in the Result Set

The fetchRow() and fetchInto() methods are useful for processing result sets. fetchRow() returns a row and fetchInto() populates a pre-allocated row. Both can be used to return ordered or associative arrays. The parameter to determine whether the array is ordered or associative may be passed directly into the call or (when using fetchRow()) may be set previously using setFetchMode().

Here is an example using fetchRow() to return an ordered array.


 '';

  DBconnect"";
 DBisError 
         getMessage;


query"";


setFetchModeDB_FETCHMODE_ORDERED;
fetchRow;
;
;
;
;
;

  something with the fields 


fetchRowDB_FETCHMODE_ORDERED;
;
;
;
;
;

  something with the fields 


fetchRowDB_FETCHMODE_ORDERED,;
;
;
;
;
;

  something with the fields 

free;

disconnect;


Here is an example using fetchRow() to return an associative array.


 '';

  DBconnect"";
 DBisError 
         getMessage;


query"";


setFetchModeDB_FETCHMODE_ASSOC;
fetchRow;
'';
'';
'';
'';
'';

  something with the fields 


fetchRowDB_FETCHMODE_ASSOC;
'';
'';
'';
'';
'';

  something with the fields 


fetchRowDB_FETCHMODE_ASSOC,;
'';
'';
'';
'';
'';

  something with the fields 

free;

disconnect;


Here is an example using fetchInto() to populate an ordered array.


 '';

  DBconnect"";
 DBisError 
         getMessage;


query"";


fetchInto,DB_FETCHMODE_ASSOC;
;
;
;
;
;

  something with the fields 


fetchInto,DB_FETCHMODE_ASSOC,;
;
;
;
;
;

  something with the fields 

free;

disconnect;


Here is an example using fetchInto() to populate an associative array.


 '';

  DBconnect"";
 DBisError 
         getMessage;


query"";


fetchInto,DB_FETCHMODE_ASSOC;
'';
'';
'';
'';
'';

  something with the fields 


fetchInto,DB_FETCHMODE_ASSOC,;
'';
'';
'';
'';
'';

  something with the fields 

free;

disconnect;


The Pear DB API also provides convenience functions for fetching an entire result set, single row, single column or single value from a query, all in one step. setFetchMode() applies to some of these functions as well.

getAll() runs a query and fetches the entire result set, all in one step.


 '';

  DBconnect"";
 DBisError 
         getMessage;



setFetchModeDB_FETCHMODE_ORDERED;
getAll"";

"";
  ""    "";
  ""    "";
 etc 
"";
  ""    "";
  ""    "";
 etc 



setFetchModeDB_FETCHMODE_ASSOC;
getAll"";

"";
''  ""  ''  "";
''  ""  ''  "";
 etc 
"";
''  ""  ''  "";
''  ""  ''  "";
 etc 

free;

disconnect;


getRow() runs a query and fetches the first row of the result set, all in one step.


 '';

  DBconnect"";
 DBisError 
         getMessage;



setFetchModeDB_FETCHMODE_ORDERED;
getRow"";

  ""    "";
  ""    "";



setFetchModeDB_FETCHMODE_ASSOC;
getRow"";

''  ""  ''  "";
''  ""  ''  "";

free;

disconnect;


getCol() runs a query and fetches one entire column of the result set, all in one step.


 '';

  DBconnect"";
 DBisError 
         getMessage;


getCol"";

  ""    ""    ""    "";

free;

disconnect;


getOne() runs a query and fetches the first column of the first row, all in one step.


 '';

  DBconnect"";
 DBisError 
         getMessage;


getOne"";

;

free;

disconnect;


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:


 '';

  DBconnect"";
 DBisError 
         getMessage;


"";

  ; numRows;  
        fetchRow;
         ""  ,
                                ""  ,
                                ""  ;
        prepare"";
        execute,bindvars;
        free;


free;

disconnect;


Getting Column Information

After executing a query, the column count may be retrieved using numCols(). Column data, including column name, type and length are available in the associative array returned from tableInfo(). tableInfo() takes a parameter indicating what values to populate the array with. This parameter may be empty, DB_TABLEINFO_ORDER, DB_TABLEINFO_ORDERTABLE or both values or'ed together.

An example calling numCols():


 '';

  DBconnect"";
 DBisError 
         getMessage;


"";

""  numCols  "";

disconnect


An example calling tableInfo():


 '';

  DBconnect"";
 DBisError 
         getMessage;


"";

tableInfo;
 "";
""  ''  "";
""  ''  "";
""  ''  "";
"";
 "";
""  ''  "";
""  ''  "";
""  ''  "";
"";
 "";
""  ''  "";
""  ''  "";
""  ''  "";
"";
 "";
""  ''  "";
""  ''  "";
""  ''  "";
"";
 "";
""  ''  "";
""  ''  "";
""  ''  "";
""

free;

disconnect;


An example calling tableInfo(DB_TABLEINFO_ORDER):


 '';

  DBconnect"";
 DBisError 
         getMessage;


"";

tableInfoDB_TABLEINFO_ORDER;
""  ''  "";
""  ''''  "";
""  ''''  "";
""  ''''  "";
""  ''''  "";
""  ''''  "";


free;

disconnect;


An example calling tableInfo(DB_TABLEINFO_ORDERTABLE):


 '';

  DBconnect"";
 DBisError 
         getMessage;


"";

tableInfoDB_TABLEINFO_ORDERTABLE;
""  ''''''  "";
""  ''''''  "";
""  ''''''  "";
""  ''''''  "";
""  ''''''  "";

free;

disconnect;


An example calling tableInfo(DB_TABLEINFO_ORDER|DB_TABLEINFO_ORDERTABLE):


 '';

  DBconnect"";
 DBisError 
         getMessage;


"";

""  ''  "";
""  ''''  "";
""  ''''  "";
""  ''''  "";
""  ''''  "";
""  ''''  "";

""  ''''''  "";
""  ''''''  "";
""  ''''''  "";
""  ''''''  "";
""  ''''''  "";

free;

disconnect;


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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;

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

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;
fetchRow;

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

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;
fetchRow;

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

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;
fetchRow;

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

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;
fetchRow;
fetchRow;
fetchRow;

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

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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;
fetchRow;
fetchRow;
fetchRow;

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

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 PHP Pear 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.

  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.

prepare"";
""  ,
                ""  ,
                ""  ""
execute,;
fetchRow;
fetchRow;
fetchRow;

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

  testfunc
//From: http://sqlrelay.sourceforge.net/sqlrelay/programming/phppeardb.html

深度分析DHCP relay和snooping功能

转自:http://blog.sina.com.cn/s/blog_866d4e0501016vxh.html 一、DHCP relay 首先分析一下DHCPrelay是什么协议。在使用D...
  • ahstusujian
  • ahstusujian
  • 2015年01月14日 14:31
  • 4718

relay_log 相关参数

mysql> show variables like '%relay%'; +---------------------------+----------------+ |...
  • lyj1101066558
  • lyj1101066558
  • 2016年03月18日 10:59
  • 1428

mysql 清除relay-log文件方法详解

mysql 清除relay-log文件方法详解 今天在本机的mysql数据目录下发现了许多类似hostname-relay-bin.0000*的文件,该文件一般是在mysql slave实例上存...
  • u010098331
  • u010098331
  • 2016年02月24日 13:00
  • 6599

SQL-Relay参数文档

  • 2016年09月28日 23:11
  • 91KB
  • 下载

PHP数据库连接池SQL Relay安装使用

SQL Relay按照其官网http://sqlrelay.sourceforge.net/index.html上所说是:A powerful database connection manageme...
  • unix21
  • unix21
  • 2016年03月01日 17:56
  • 542

PHP数据库连接池SQL Relay安装使用

SQL Relay按照其官网http://sqlrelay.sourceforge.net/index.html上所说是:A powerful database connection manageme...
  • 21aspnet
  • 21aspnet
  • 2016年03月01日 18:25
  • 3802

SQL Relay使用详解

http://www.ydmsh.com/www/Blog/Show/id/152/ episode01 What Is     此文算是我对这段时间学习SQL Relay的汇总和总结...
  • bestone0213
  • bestone0213
  • 2015年01月22日 10:10
  • 849

SQL-Relay学习笔记(3)

安全性保障Kerberos 和活动目录(Active Directory)加密认证SQL Relay支持Kerberos加密和认证。 当启用Kerberos和Active Directory加密认证...
  • liuyueyi1995
  • liuyueyi1995
  • 2016年09月26日 20:16
  • 230

SQL-Relay学习笔记(1)

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

SQL Relay使用

一、SQL Relay是什么? SQL Relay是一个开源的数据库池连接代理服务器 二、SQL Relay支持哪些数据库? * Oracle * MySQL * m...
  • leoking01
  • leoking01
  • 2014年11月12日 10:31
  • 627
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[SQL Relay文档] SQL Relay用于PHP Pear DB API的程序设计 (英文)
举报原因:
原因补充:

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