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

本文介绍如何使用PHP API进行SQLRelay编程,涵盖建立会话、执行查询、事务管理等内容,并详细讲解了错误处理、变量绑定及结果集处理等高级特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,;
              "";
              "";
            
1. 用户与身体信息管理模块 用户信息管理: 注册登录:支持手机号 / 邮箱注册,密码加密存储,提供第三方快捷登录(模拟) 个人资料:记录基本信息(姓名、年龄、性别、身高、体重、职业) 健康目标:用户设置目标(如 “减重 5kg”“增肌”“维持健康”)及期望周期 身体状态跟踪: 体重记录:定期录入体重数据,生成体重变化曲线(折线图) 身体指标:记录 BMI(自动计算)、体脂率(可选)、基础代谢率(根据身高体重估算) 健康状况:用户可填写特殊情况(如糖尿病、过敏食物、素食偏好),系统据此调整推荐 2. 膳食记录与食物数据库模块 食物数据库: 基础信息:包含常见食物(如米饭、鸡蛋、牛肉)的名称、类别(主食 / 肉类 / 蔬菜等)、每份重量 营养成分:记录每 100g 食物的热量(kcal)、蛋白质、脂肪、碳水化合物、维生素、矿物质含量 数据库维护:管理员可添加新食物、更新营养数据,支持按名称 / 类别检索 膳食记录功能: 快速记录:用户选择食物、输入食用量(克 / 份),系统自动计算摄入的营养成分 餐次分类:按早餐 / 午餐 / 晚餐 / 加餐分类记录,支持上传餐食照片(可选) 批量操作:提供常见套餐模板(如 “三明治 + 牛奶”),一键添加到记录 历史记录:按日期查看过往膳食记录,支持编辑 / 删除错误记录 3. 营养分析模块 每日营养摄入分析: 核心指标计算:统计当日摄入的总热量、蛋白质 / 脂肪 / 碳水化合物占比(按每日推荐量对比) 微量营养素分析:检查维生素(如维生素 C、钙、铁)的摄入是否达标 平衡评估:生成 “营养平衡度” 评分(0-100 分),指出摄入过剩或不足的营养素 趋势分析: 周 / 月营养趋势:用折线图展示近 7 天 / 30 天的热量、三大营养素摄入变化 对比分析:将实际摄入与推荐量对比(如 “蛋白质摄入仅达到推荐量的 70%”) 目标达成率:针对健
1. 用户管理模块 用户注册与认证: 注册:用户填写身份信息(姓名、身份证号、手机号)、设置登录密码(需符合复杂度要求),系统生成唯一客户号 登录:支持账号(客户号 / 手机号)+ 密码登录,提供验证码登录、忘记密码(通过手机验证码重置)功能 身份验证:注册后需完成实名认证(模拟上传身份证照片,系统标记认证状态) 个人信息管理: 基本信息:查看 / 修改联系地址、紧急联系人、邮箱等非核心信息(身份证号等关键信息不可修改) 安全设置:修改登录密码、设置交易密码(用于转账等敏感操作)、开启 / 关闭登录提醒 权限控制:普通用户仅能操作本人账户;管理员可管理用户信息、查看系统统计数据 2. 账户与资金管理模块 账户管理: 账户创建:用户可开通储蓄卡账户(默认 1 个主账户,支持最多 3 个子账户,如 “日常消费账户”“储蓄账户”) 账户查询:查看各账户余额、开户日期、状态(正常 / 冻结)、交易限额 账户操作:挂失 / 解挂账户、申请注销账户(需余额为 0) 资金操作: 转账汇款:支持同行转账(输入对方账户号 / 手机号),需验证交易密码,可添加常用收款人 存款 / 取款:模拟存款(输入金额增加余额)、取款(输入金额减少余额,需不超过可用余额) 交易记录:按时间、类型(转入 / 转出 / 存款 / 取款)查询明细,显示交易时间、金额、对方账户(脱敏显示)、交易状态 3. 账单与支付模块 账单管理: 月度账单:自动生成每月收支明细,统计总收入、总支出、余额变动 账单查询:按月份、交易类型筛选账单,支持导出为 Excel 格式 还款提醒:若有贷款(简化版可模拟),系统在还款日 3 天前发送提醒 快捷支付: 绑定支付方式:添加银行卡(系统内账户)作为支付渠道 模拟消费:支持输入商户名称和金额,完成支付(从账户余额扣减) 支付记录:保存所有消费记录,包含商户、时间、金额、支付状态 4.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值