static unsigned char a3_arg3_force_ref[] = { 3, BYREF_NONE, BYREF_ALLOW, BYREF_FORCE };
function_entry odbc_functions[] = {
+ PHP_FE(odbc_error, NULL)
+ PHP_FE(odbc_errormsg, NULL)
PHP_FE(odbc_setoption, NULL)
PHP_FE(odbc_autocommit, NULL)
PHP_FE(odbc_close, NULL)
{
ODBCLS_D;
#ifdef SQLANY_BUG
- HDBC foobar;
+ ODBC_SQL_CONN_T foobar;
RETCODE rc;
#endif
ODBCG(defConn) = -1;
ODBCG(num_links) = ODBCG(num_persistent);
+ memset(ODBCG(laststate), '\0', 6);
+ memset(ODBCG(lasterrormsg), '\0', SQL_MAX_MESSAGE_LENGTH);
return SUCCESS;
}
char errormsg[SQL_MAX_MESSAGE_LENGTH];
SWORD errormsgsize; /* Not used */
RETCODE rc;
+ ODBC_SQL_ENV_T henv;
+ ODBC_SQL_CONN_T conn;
ODBCLS_FETCH();
+ if (conn_resource) {
+ henv = conn_resource->henv;
+ conn = conn_resource->hdbc;
+ } else {
+ henv = SQL_NULL_HENV;
+ conn = SQL_NULL_HDBC;
+ }
+
/* This leads to an endless loop in many drivers!
*
while(henv != SQL_NULL_HENV){
do {
*/
- rc = SQLError(henv, conn, stmt, state,
- &error, errormsg, sizeof(errormsg)-1, &errormsgsize);
- if (func) {
- php_error(E_WARNING, "SQL error: %s, SQL state %s in %s",
- errormsg, state, func);
- } else {
- php_error(E_WARNING, "SQL error: %s, SQL state %s",
- errormsg, state);
- }
+ rc = SQLError(henv, conn, stmt, state,
+ &error, errormsg, sizeof(errormsg)-1, &errormsgsize);
+ if (conn_resource) {
+ memcpy(conn_resource->laststate, state, sizeof(state));
+ memcpy(conn_resource->lasterrormsg, errormsg, sizeof(errormsg));
+ }
+ memcpy(ODBCG(laststate), state, sizeof(state));
+ memcpy(ODBCG(lasterrormsg), errormsg, sizeof(errormsg));
+ if (func) {
+ php_error(E_WARNING, "SQL error: %s, SQL state %s in %s",
+ errormsg, state, func);
+ } else {
+ php_error(E_WARNING, "SQL error: %s, SQL state %s",
+ errormsg, state);
+ }
/*
} while (SQL_SUCCEEDED(rc));
}
rc = SQLTransact(conn->henv, conn->hdbc, (UWORD)((type)?SQL_COMMIT:SQL_ROLLBACK));
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLTransact");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTransact");
RETURN_FALSE;
}
}
/* }}} */
+
/* {{{ proto int odbc_prepare(int connection_id, string query)
Prepares a statement for execution */
PHP_FUNCTION(odbc_prepare)
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
*/
if (SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, SQL_CURSOR_DYNAMIC)
== SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, result->stmt, " SQLSetStmtOption");
+ odbc_sql_error(conn, result->stmt, " SQLSetStmtOption");
SQLFreeStmt(result->stmt, SQL_DROP);
efree(result);
RETURN_FALSE;
case SQL_SUCCESS:
break;
case SQL_SUCCESS_WITH_INFO:
- odbc_sql_error(conn->henv, conn->hdbc, result->stmt, "SQLPrepare");
+ odbc_sql_error(conn, result->stmt, "SQLPrepare");
break;
default:
- odbc_sql_error(conn->henv, conn->hdbc, result->stmt, "SQLPrepare");
+ odbc_sql_error(conn, result->stmt, "SQLPrepare");
RETURN_FALSE;
}
rc = SQLFreeStmt(result->stmt, SQL_CLOSE);
if (rc == SQL_ERROR) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLFreeStmt");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLFreeStmt");
}
rc = SQLExecute(result->stmt);
break;
case SQL_NO_DATA_FOUND:
case SQL_SUCCESS_WITH_INFO:
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLExecute");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
break;
default:
RETVAL_FALSE;
if (!strncmp(state,"S1015",5)) {
sprintf(cursorname,"php_curs_%d", (int)result->stmt);
if (SQLSetCursorName(result->stmt,cursorname,SQL_NTS) != SQL_SUCCESS) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc,
- result->stmt, "SQLSetCursorName");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLSetCursorName");
RETVAL_FALSE;
} else {
RETVAL_STRING(cursorname,1);
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
*/
if (SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, SQL_CURSOR_DYNAMIC)
== SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, result->stmt, " SQLSetStmtOption");
+ odbc_sql_error(conn, result->stmt, " SQLSetStmtOption");
SQLFreeStmt(result->stmt, SQL_DROP);
efree(result);
RETURN_FALSE;
/* XXX FIXME we should really check out SQLSTATE with SQLError
* in case rc is SQL_SUCCESS_WITH_INFO here.
*/
- odbc_sql_error(conn->henv, conn->hdbc, result->stmt, "SQLExecDirect");
+ odbc_sql_error(conn, result->stmt, "SQLExecDirect");
SQLFreeStmt(result->stmt, SQL_DROP);
efree(result);
RETURN_FALSE;
buf, result->longreadlen + 1, &result->values[i].vallen);
if (rc == SQL_ERROR) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLGetData");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
efree(buf);
RETURN_FALSE;
}
buf, result->longreadlen + 1, &result->values[i].vallen);
if (rc == SQL_ERROR) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLGetData");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
efree(buf);
RETURN_FALSE;
}
field, fieldsize, &result->values[field_ind].vallen);
if (rc == SQL_ERROR) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLGetData");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
efree(field);
RETURN_FALSE;
}
field, fieldsize, &result->values[field_ind].vallen);
if (rc == SQL_ERROR) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLGetData");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
efree(field);
RETURN_FALSE;
}
php_printf("<td>");
if (rc == SQL_ERROR) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SQLGetData");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
php_printf("</td></tr></table>");
efree(buf);
RETURN_FALSE;
if (cur_opt != SQL_CUR_DEFAULT) {
rc = SQLSetConnectOption((*conn)->hdbc, SQL_ODBC_CURSORS, cur_opt);
if (rc != SQL_SUCCESS) { /* && rc != SQL_SUCCESS_WITH_INFO ? */
- odbc_sql_error((*conn)->henv, (*conn)->hdbc, SQL_NULL_HSTMT, "SQLSetConnectOption");
+ odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLSetConnectOption");
SQLFreeConnect((*conn)->hdbc);
pefree(*conn, persistent);
return FALSE;
#endif
#endif
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- odbc_sql_error((*conn)->henv, (*conn)->hdbc, SQL_NULL_HSTMT, "SQLConnect");
+ odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLConnect");
SQLFreeConnect((*conn)->hdbc);
pefree((*conn), persistent);
return FALSE;
((*pv_onoff)->value.lval) ?
SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "Set autocommit");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "Set autocommit");
RETURN_FALSE;
}
RETVAL_TRUE;
rc = SQLGetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (PTR)&status);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "Get commit status");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "Get commit status");
RETURN_FALSE;
}
RETVAL_LONG((long)status);
}
/* }}} */
+static void php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS, int mode)
+{
+ odbc_connection *conn;
+ pval **pv_handle;
+ char *ptr, *dest;
+ int argc, len;
+
+ argc = ZEND_NUM_ARGS();
+
+ if (argc > 1 || zend_get_parameters_ex(argc, &pv_handle)) {
+ WRONG_PARAM_COUNT;
+ }
+
+ if (mode == 0) { /* last state */
+ len = 6;
+ } else { /* last error message */
+ len = SQL_MAX_MESSAGE_LENGTH;
+ }
+ ptr = ecalloc(len + 1, 1);
+ if (argc == 1) {
+ ZEND_FETCH_RESOURCE2(conn, odbc_connection *, pv_handle, -1, "ODBC-Link", le_conn, le_pconn);
+ if (mode == 0) {
+ strncpy(ptr, conn->laststate, len);
+ } else {
+ strncpy(ptr, conn->lasterrormsg, len);
+ }
+ } else {
+ if (mode == 0) {
+ strncpy(ptr, ODBCG(laststate), len);
+ } else {
+ strncpy(ptr, ODBCG(lasterrormsg), len);
+ }
+ }
+ RETVAL_STRING(ptr, 0);
+}
+
+/* {{{ proto string odbc_error([int connection_id])
+ Get the last error code */
+PHP_FUNCTION(odbc_error)
+{
+ php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+}
+/* }}} */
+
+/* {{{ proto string odbc_errormsg([int connection_id])
+ Get the last error message */
+PHP_FUNCTION(odbc_errormsg)
+{
+ php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+}
+/* }}} */
/* {{{ proto int odbc_setoption(int conn_id|result_id, int which, int option, int value)
Sets connection or statement options */
}
rc = SQLSetConnectOption(conn->hdbc, (unsigned short)((*pv_opt)->value.lval), (*pv_val)->value.lval);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SetConnectOption");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SetConnectOption");
RETURN_FALSE;
}
break;
rc = SQLSetStmtOption(result->stmt, (unsigned short)((*pv_opt)->value.lval), ((*pv_val)->value.lval));
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
- odbc_sql_error(result->conn_ptr->henv, result->conn_ptr->hdbc, result->stmt, "SetStmtOption");
+ odbc_sql_error(result->conn_ptr, result->stmt, "SetStmtOption");
RETURN_FALSE;
}
break;
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
type, SAFE_SQL_NTS(type));
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLTables");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTables");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
column, SAFE_SQL_NTS(column));
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLColumns");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumns");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
column, SAFE_SQL_NTS(column));
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLColumnPrivileges");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumnPrivileges");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
ftable, SAFE_SQL_NTS(ftable) );
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLForeignKeys");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLForeignKeys");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
rc = SQLGetTypeInfo(result->stmt, data_type );
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLGetTypeInfo");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLGetTypeInfo");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
table, SAFE_SQL_NTS(table) );
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLPrimaryKeys");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLPrimaryKeys");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
col, SAFE_SQL_NTS(col) );
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLProcedureColumns");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedureColumns");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
proc, SAFE_SQL_NTS(proc) );
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLProcedures");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedures");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
nullable);
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLSpecialColumns");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLSpecialColumns");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
reserved);
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLStatistics");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLStatistics");
efree(result);
RETURN_FALSE;
}
}
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLAllocStmt");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
efree(result);
RETURN_FALSE;
}
table, SAFE_SQL_NTS(table));
if (rc == SQL_ERROR) {
- odbc_sql_error(conn->henv, conn->hdbc, SQL_NULL_HSTMT, "SQLTablePrivileges");
+ odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTablePrivileges");
efree(result);
RETURN_FALSE;
}