2004-05-17 23:43:01 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
2007-01-01 17:36:18 +08:00
|
|
|
| Copyright (c) 1997-2007 The PHP Group |
|
2004-05-17 23:43:01 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.0 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_0.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Wez Furlong <wez@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_ini.h"
|
|
|
|
#include "ext/standard/info.h"
|
|
|
|
#include "pdo/php_pdo.h"
|
|
|
|
#include "pdo/php_pdo_driver.h"
|
|
|
|
#include "php_pdo_odbc.h"
|
|
|
|
#include "php_pdo_odbc_int.h"
|
2005-01-21 12:43:06 +08:00
|
|
|
#include "zend_exceptions.h"
|
2004-05-17 23:43:01 +08:00
|
|
|
|
2004-05-22 08:30:00 +08:00
|
|
|
static int pdo_odbc_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
|
|
|
pdo_odbc_errinfo *einfo = &H->einfo;
|
|
|
|
pdo_odbc_stmt *S = NULL;
|
|
|
|
char *message = NULL;
|
|
|
|
|
|
|
|
if (stmt) {
|
|
|
|
S = (pdo_odbc_stmt*)stmt->driver_data;
|
|
|
|
einfo = &S->einfo;
|
|
|
|
}
|
|
|
|
|
2005-01-21 12:43:06 +08:00
|
|
|
spprintf(&message, 0, "%s (%s[%ld] at %s:%d)",
|
2005-01-17 13:28:13 +08:00
|
|
|
einfo->last_err_msg,
|
|
|
|
einfo->what, einfo->last_error,
|
|
|
|
einfo->file, einfo->line);
|
2004-05-22 08:30:00 +08:00
|
|
|
|
|
|
|
add_next_index_long(info, einfo->last_error);
|
|
|
|
add_next_index_string(info, message, 0);
|
|
|
|
add_next_index_string(info, einfo->last_state, 1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void pdo_odbc_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, PDO_ODBC_HSTMT statement, char *what, const char *file, int line TSRMLS_DC) /* {{{ */
|
2004-05-17 23:43:01 +08:00
|
|
|
{
|
2005-12-14 12:56:22 +08:00
|
|
|
SQLRETURN rc;
|
|
|
|
SQLSMALLINT errmsgsize = 0;
|
|
|
|
SQLHANDLE eh;
|
|
|
|
SQLSMALLINT htype, recno = 1;
|
2004-05-17 23:43:01 +08:00
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle*)dbh->driver_data;
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_errinfo *einfo = &H->einfo;
|
|
|
|
pdo_odbc_stmt *S = NULL;
|
2005-01-07 13:25:35 +08:00
|
|
|
pdo_error_type *pdo_err = &dbh->error_code;
|
2004-05-22 08:30:00 +08:00
|
|
|
|
|
|
|
if (stmt) {
|
|
|
|
S = (pdo_odbc_stmt*)stmt->driver_data;
|
2004-05-17 23:43:01 +08:00
|
|
|
|
2004-05-22 08:30:00 +08:00
|
|
|
einfo = &S->einfo;
|
|
|
|
pdo_err = &stmt->error_code;
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
|
2004-05-22 08:30:00 +08:00
|
|
|
if (statement == SQL_NULL_HSTMT && S) {
|
|
|
|
statement = S->stmt;
|
|
|
|
}
|
2005-12-14 12:56:22 +08:00
|
|
|
|
|
|
|
if (statement) {
|
|
|
|
htype = SQL_HANDLE_STMT;
|
|
|
|
eh = statement;
|
|
|
|
} else if (H->dbc) {
|
|
|
|
htype = SQL_HANDLE_DBC;
|
|
|
|
eh = H->dbc;
|
|
|
|
} else {
|
|
|
|
htype = SQL_HANDLE_ENV;
|
|
|
|
eh = H->env;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = SQLGetDiagRec(htype, eh, recno++, einfo->last_state, &einfo->last_error,
|
2004-05-22 08:30:00 +08:00
|
|
|
einfo->last_err_msg, sizeof(einfo->last_err_msg)-1, &errmsgsize);
|
|
|
|
|
2005-09-27 05:37:33 +08:00
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
errmsgsize = 0;
|
|
|
|
}
|
|
|
|
|
2004-05-22 08:30:00 +08:00
|
|
|
einfo->last_err_msg[errmsgsize] = '\0';
|
|
|
|
einfo->file = file;
|
|
|
|
einfo->line = line;
|
|
|
|
einfo->what = what;
|
|
|
|
|
2005-01-17 13:28:13 +08:00
|
|
|
strcpy(*pdo_err, einfo->last_state);
|
2005-07-07 20:49:21 +08:00
|
|
|
/* printf("@@ SQLSTATE[%s] %s\n", *pdo_err, einfo->last_err_msg); */
|
2004-05-22 08:30:00 +08:00
|
|
|
if (!dbh->methods) {
|
2005-01-07 13:25:35 +08:00
|
|
|
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] %s: %d %s",
|
|
|
|
*pdo_err, what, einfo->last_error, einfo->last_err_msg);
|
2004-05-22 08:30:00 +08:00
|
|
|
}
|
2005-12-14 12:56:22 +08:00
|
|
|
|
|
|
|
/* just like a cursor, once you start pulling, you need to keep
|
|
|
|
* going until the end; SQL Server (at least) will mess with the
|
|
|
|
* actual cursor state if you don't finish retrieving all the
|
|
|
|
* diagnostic records (which can be generated by PRINT statements
|
|
|
|
* in the query, for instance). */
|
|
|
|
while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
|
|
|
|
char discard_state[5];
|
|
|
|
char discard_buf[1024];
|
|
|
|
SQLINTEGER code;
|
|
|
|
rc = SQLGetDiagRec(htype, eh, recno++, discard_state, &code,
|
|
|
|
discard_buf, sizeof(discard_buf)-1, &errmsgsize);
|
|
|
|
}
|
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static int odbc_handle_closer(pdo_dbh_t *dbh TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle*)dbh->driver_data;
|
2005-07-10 22:56:36 +08:00
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
if (H->dbc != SQL_NULL_HANDLE) {
|
|
|
|
SQLEndTran(SQL_HANDLE_DBC, H->dbc, SQL_ROLLBACK);
|
2004-05-22 23:04:26 +08:00
|
|
|
SQLDisconnect(H->dbc);
|
|
|
|
SQLFreeHandle(SQL_HANDLE_DBC, H->dbc);
|
2005-06-11 22:48:06 +08:00
|
|
|
H->dbc = NULL;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
SQLFreeHandle(SQL_HANDLE_ENV, H->env);
|
2005-06-11 22:48:06 +08:00
|
|
|
H->env = NULL;
|
2004-05-17 23:43:01 +08:00
|
|
|
pefree(H, dbh->is_persistent);
|
2005-07-07 20:49:21 +08:00
|
|
|
dbh->driver_data = NULL;
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-21 08:39:03 +08:00
|
|
|
static int odbc_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
|
2004-05-17 23:43:01 +08:00
|
|
|
{
|
|
|
|
RETCODE rc;
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
|
|
|
pdo_odbc_stmt *S = ecalloc(1, sizeof(*S));
|
2004-05-26 02:57:50 +08:00
|
|
|
enum pdo_cursor_type cursor_type = PDO_CURSOR_FWDONLY;
|
2005-01-21 12:22:06 +08:00
|
|
|
int ret;
|
|
|
|
char *nsql = NULL;
|
|
|
|
int nsql_len = 0;
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
S->H = H;
|
2005-01-21 12:22:06 +08:00
|
|
|
|
|
|
|
/* before we prepare, we need to peek at the query; if it uses named parameters,
|
|
|
|
* we want PDO to rewrite them for us */
|
|
|
|
stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
|
|
|
|
ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
|
|
|
|
|
|
|
|
if (ret == 1) {
|
|
|
|
/* query was re-written */
|
|
|
|
sql = nsql;
|
|
|
|
} else if (ret == -1) {
|
|
|
|
/* couldn't grok it */
|
2005-01-21 12:43:06 +08:00
|
|
|
strcpy(dbh->error_code, stmt->error_code);
|
|
|
|
efree(S);
|
2005-01-21 12:22:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
rc = SQLAllocHandle(SQL_HANDLE_STMT, H->dbc, &S->stmt);
|
|
|
|
|
|
|
|
if (rc == SQL_INVALID_HANDLE || rc == SQL_ERROR) {
|
|
|
|
efree(S);
|
2005-01-21 12:22:06 +08:00
|
|
|
if (nsql) {
|
|
|
|
efree(nsql);
|
|
|
|
}
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_drv_error("SQLAllocStmt");
|
2004-05-17 23:43:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-26 02:57:50 +08:00
|
|
|
cursor_type = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, PDO_CURSOR_FWDONLY TSRMLS_CC);
|
|
|
|
if (cursor_type != PDO_CURSOR_FWDONLY) {
|
2005-01-17 13:28:13 +08:00
|
|
|
rc = SQLSetStmtAttr(S->stmt, SQL_ATTR_CURSOR_SCROLLABLE, (void*)SQL_SCROLLABLE, 0);
|
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
pdo_odbc_stmt_error("SQLSetStmtAttr: SQL_ATTR_CURSOR_SCROLLABLE");
|
|
|
|
SQLFreeHandle(SQL_HANDLE_STMT, S->stmt);
|
2005-01-21 12:22:06 +08:00
|
|
|
if (nsql) {
|
|
|
|
efree(nsql);
|
|
|
|
}
|
2005-01-17 13:28:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-26 02:57:50 +08:00
|
|
|
}
|
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
rc = SQLPrepare(S->stmt, (char*)sql, SQL_NTS);
|
2005-01-21 12:22:06 +08:00
|
|
|
if (nsql) {
|
|
|
|
efree(nsql);
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
|
2006-04-30 08:52:28 +08:00
|
|
|
stmt->driver_data = S;
|
|
|
|
stmt->methods = &odbc_stmt_methods;
|
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
if (rc != SQL_SUCCESS) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_stmt_error("SQLPrepare");
|
2006-04-30 08:52:28 +08:00
|
|
|
if (rc != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
/* clone error information into the db handle */
|
|
|
|
strcpy(H->einfo.last_err_msg, S->einfo.last_err_msg);
|
|
|
|
H->einfo.file = S->einfo.file;
|
|
|
|
H->einfo.line = S->einfo.line;
|
|
|
|
H->einfo.what = S->einfo.what;
|
|
|
|
strcpy(dbh->error_code, stmt->error_code);
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-21 23:33:18 +08:00
|
|
|
static long odbc_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
|
2004-05-17 23:43:01 +08:00
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
2004-05-22 00:17:22 +08:00
|
|
|
RETCODE rc;
|
2004-05-22 08:30:00 +08:00
|
|
|
long row_count = -1;
|
|
|
|
PDO_ODBC_HSTMT stmt;
|
|
|
|
|
|
|
|
rc = SQLAllocHandle(SQL_HANDLE_STMT, H->dbc, &stmt);
|
|
|
|
if (rc != SQL_SUCCESS) {
|
|
|
|
pdo_odbc_drv_error("SQLAllocHandle: STMT");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = SQLExecDirect(stmt, (char *)sql, sql_len);
|
2004-05-17 23:43:01 +08:00
|
|
|
|
2006-04-30 09:27:33 +08:00
|
|
|
if (rc == SQL_NO_DATA) {
|
|
|
|
/* If SQLExecDirect executes a searched update or delete statement that
|
|
|
|
* does not affect any rows at the data source, the call to
|
|
|
|
* SQLExecDirect returns SQL_NO_DATA. */
|
|
|
|
row_count = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:17:22 +08:00
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_doer_error("SQLExecDirect");
|
|
|
|
goto out;
|
2004-05-22 00:17:22 +08:00
|
|
|
}
|
|
|
|
|
2004-05-22 08:30:00 +08:00
|
|
|
rc = SQLRowCount(stmt, &row_count);
|
2004-05-22 00:17:22 +08:00
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_doer_error("SQLRowCount");
|
|
|
|
goto out;
|
2004-05-22 00:17:22 +08:00
|
|
|
}
|
2005-07-20 00:54:51 +08:00
|
|
|
if (row_count == -1) {
|
|
|
|
row_count = 0;
|
|
|
|
}
|
2004-05-22 08:30:00 +08:00
|
|
|
out:
|
|
|
|
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
2004-05-22 00:17:22 +08:00
|
|
|
return row_count;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
|
2005-02-07 01:49:48 +08:00
|
|
|
static int odbc_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type param_type TSRMLS_DC)
|
2004-05-17 23:43:01 +08:00
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
2005-07-10 22:56:36 +08:00
|
|
|
/* TODO: figure it out */
|
2004-05-17 23:43:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int odbc_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
|
|
|
|
{
|
2005-06-11 20:38:12 +08:00
|
|
|
if (dbh->auto_commit) {
|
|
|
|
/* we need to disable auto-commit now, to be able to initiate a transaction */
|
|
|
|
RETCODE rc;
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
|
|
|
|
2005-07-10 22:56:36 +08:00
|
|
|
rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_IS_INTEGER);
|
2005-06-11 20:38:12 +08:00
|
|
|
if (rc != SQL_SUCCESS) {
|
|
|
|
pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = OFF");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int odbc_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
|
|
|
RETCODE rc;
|
|
|
|
|
|
|
|
rc = SQLEndTran(SQL_HANDLE_DBC, H->dbc, SQL_COMMIT);
|
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_drv_error("SQLEndTran: Commit");
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-06-11 20:38:12 +08:00
|
|
|
|
|
|
|
if (dbh->auto_commit) {
|
|
|
|
/* turn auto-commit back on again */
|
2005-07-10 22:56:36 +08:00
|
|
|
rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_IS_INTEGER);
|
2005-06-11 20:38:12 +08:00
|
|
|
if (rc != SQL_SUCCESS) {
|
2005-06-11 22:48:06 +08:00
|
|
|
pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = ON");
|
2005-06-11 20:38:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int odbc_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
|
|
|
|
RETCODE rc;
|
2005-07-10 22:56:36 +08:00
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
rc = SQLEndTran(SQL_HANDLE_DBC, H->dbc, SQL_ROLLBACK);
|
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_drv_error("SQLEndTran: Rollback");
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS_WITH_INFO) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-06-11 22:48:06 +08:00
|
|
|
if (dbh->auto_commit && H->dbc) {
|
2005-06-11 20:38:12 +08:00
|
|
|
/* turn auto-commit back on again */
|
2005-07-10 22:56:36 +08:00
|
|
|
rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_IS_INTEGER);
|
2005-06-11 20:38:12 +08:00
|
|
|
if (rc != SQL_SUCCESS) {
|
2005-06-11 22:48:06 +08:00
|
|
|
pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = ON");
|
2005-06-11 20:38:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-02-07 01:49:48 +08:00
|
|
|
static int odbc_handle_get_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
|
|
|
|
{
|
|
|
|
switch (attr) {
|
|
|
|
case PDO_ATTR_CLIENT_VERSION:
|
|
|
|
ZVAL_STRING(val, "ODBC-" PDO_ODBC_TYPE, 1);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case PDO_ATTR_SERVER_VERSION:
|
|
|
|
case PDO_ATTR_PREFETCH:
|
|
|
|
case PDO_ATTR_TIMEOUT:
|
|
|
|
case PDO_ATTR_SERVER_INFO:
|
|
|
|
case PDO_ATTR_CONNECTION_STATUS:
|
|
|
|
break;
|
2004-05-17 23:43:01 +08:00
|
|
|
|
2005-02-07 01:49:48 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
static struct pdo_dbh_methods odbc_methods = {
|
|
|
|
odbc_handle_closer,
|
|
|
|
odbc_handle_preparer,
|
|
|
|
odbc_handle_doer,
|
|
|
|
odbc_handle_quoter,
|
|
|
|
odbc_handle_begin,
|
|
|
|
odbc_handle_commit,
|
|
|
|
odbc_handle_rollback,
|
2004-05-22 08:30:00 +08:00
|
|
|
NULL, /* set attr */
|
|
|
|
NULL, /* last id */
|
|
|
|
pdo_odbc_fetch_error_func,
|
2005-02-07 01:49:48 +08:00
|
|
|
odbc_handle_get_attr, /* get attr */
|
2004-09-24 04:43:11 +08:00
|
|
|
NULL, /* check_liveness */
|
2004-05-17 23:43:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
pdo_odbc_db_handle *H;
|
|
|
|
RETCODE rc;
|
|
|
|
int use_direct = 0;
|
2004-05-26 01:44:36 +08:00
|
|
|
SQLUINTEGER cursor_lib;
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
H = pecalloc(1, sizeof(*H), dbh->is_persistent);
|
|
|
|
|
|
|
|
dbh->driver_data = H;
|
|
|
|
|
|
|
|
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &H->env);
|
|
|
|
rc = SQLSetEnvAttr(H->env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
|
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_drv_error("SQLSetEnvAttr: ODBC3");
|
2005-07-07 20:49:21 +08:00
|
|
|
goto fail;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
2004-05-22 22:51:26 +08:00
|
|
|
|
2005-07-10 22:56:36 +08:00
|
|
|
#ifdef SQL_ATTR_CONNECTION_POOLING
|
2004-05-22 22:51:26 +08:00
|
|
|
if (pdo_odbc_pool_on != SQL_CP_OFF) {
|
|
|
|
rc = SQLSetEnvAttr(H->env, SQL_ATTR_CP_MATCH, (void*)pdo_odbc_pool_mode, 0);
|
|
|
|
if (rc != SQL_SUCCESS) {
|
|
|
|
pdo_odbc_drv_error("SQLSetEnvAttr: SQL_ATTR_CP_MATCH");
|
2005-07-07 20:49:21 +08:00
|
|
|
goto fail;
|
2004-05-22 22:51:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
rc = SQLAllocHandle(SQL_HANDLE_DBC, H->env, &H->dbc);
|
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_drv_error("SQLAllocHandle (DBC)");
|
2005-07-07 20:49:21 +08:00
|
|
|
goto fail;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
|
2005-06-11 22:48:06 +08:00
|
|
|
rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT,
|
2005-07-10 22:56:36 +08:00
|
|
|
(SQLPOINTER)(dbh->auto_commit ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF), SQL_IS_INTEGER);
|
2005-06-11 22:48:06 +08:00
|
|
|
if (rc != SQL_SUCCESS) {
|
|
|
|
pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT");
|
2005-07-07 20:49:21 +08:00
|
|
|
goto fail;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
2004-05-26 01:44:36 +08:00
|
|
|
|
|
|
|
/* set up the cursor library, if needed, or if configured explicitly */
|
|
|
|
cursor_lib = pdo_attr_lval(driver_options, PDO_ODBC_ATTR_USE_CURSOR_LIBRARY, SQL_CUR_USE_IF_NEEDED TSRMLS_CC);
|
2005-07-10 22:56:36 +08:00
|
|
|
rc = SQLSetConnectAttr(H->dbc, SQL_ODBC_CURSORS, (void*)cursor_lib, SQL_IS_INTEGER);
|
2005-01-17 13:28:13 +08:00
|
|
|
if (rc != SQL_SUCCESS && cursor_lib != SQL_CUR_USE_IF_NEEDED) {
|
2004-05-26 01:44:36 +08:00
|
|
|
pdo_odbc_drv_error("SQLSetConnectAttr SQL_ODBC_CURSORS");
|
2005-07-07 20:49:21 +08:00
|
|
|
goto fail;
|
2004-05-26 01:44:36 +08:00
|
|
|
}
|
2004-05-26 02:57:50 +08:00
|
|
|
|
2004-05-17 23:43:01 +08:00
|
|
|
if (strchr(dbh->data_source, ';')) {
|
|
|
|
char dsnbuf[1024];
|
|
|
|
short dsnbuflen;
|
|
|
|
|
|
|
|
use_direct = 1;
|
|
|
|
|
|
|
|
/* Force UID and PWD to be set in the DSN */
|
2005-01-19 11:45:21 +08:00
|
|
|
if (dbh->username && *dbh->username && !strstr(dbh->data_source, "uid")
|
|
|
|
&& !strstr(dbh->data_source, "UID")) {
|
2004-05-17 23:43:01 +08:00
|
|
|
char *dsn = pemalloc(strlen(dbh->data_source) + strlen(dbh->username) + strlen(dbh->password) + sizeof(";UID=;PWD="), dbh->is_persistent);
|
|
|
|
sprintf(dsn, "%s;UID=%s;PWD=%s", dbh->data_source, dbh->username, dbh->password);
|
|
|
|
pefree((char*)dbh->data_source, dbh->is_persistent);
|
|
|
|
dbh->data_source = dsn;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = SQLDriverConnect(H->dbc, NULL, (char*)dbh->data_source, strlen(dbh->data_source),
|
|
|
|
dsnbuf, sizeof(dsnbuf)-1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
|
|
|
|
}
|
|
|
|
if (!use_direct) {
|
|
|
|
rc = SQLConnect(H->dbc, (char*)dbh->data_source, SQL_NTS, dbh->username, SQL_NTS, dbh->password, SQL_NTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
2004-05-22 08:30:00 +08:00
|
|
|
pdo_odbc_drv_error(use_direct ? "SQLDriverConnect" : "SQLConnect");
|
2005-07-07 20:49:21 +08:00
|
|
|
goto fail;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
|
2004-05-21 22:15:03 +08:00
|
|
|
/* TODO: if we want to play nicely, we should check to see if the driver really supports ODBC v3 or not */
|
2004-05-17 23:43:01 +08:00
|
|
|
|
|
|
|
dbh->methods = &odbc_methods;
|
|
|
|
dbh->alloc_own_columns = 1;
|
|
|
|
|
|
|
|
return 1;
|
2005-07-07 20:49:21 +08:00
|
|
|
|
|
|
|
fail:
|
|
|
|
dbh->methods = &odbc_methods;
|
|
|
|
return 0;
|
2004-05-17 23:43:01 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
pdo_driver_t pdo_odbc_driver = {
|
|
|
|
PDO_DRIVER_HEADER(odbc),
|
|
|
|
pdo_odbc_handle_factory
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|