Skip to content

Commit

Permalink
Boolify PDO's preparer handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Jan 6, 2021
1 parent 53ba72e commit 60a61af
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 deletions.
5 changes: 3 additions & 2 deletions ext/pdo/php_pdo_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ typedef struct {
/* close or otherwise disconnect the database */
typedef void (*pdo_dbh_close_func)(pdo_dbh_t *dbh);

/* prepare a statement and stash driver specific portion into stmt */
typedef int (*pdo_dbh_prepare_func)(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options);
/* prepare a statement and stash driver specific portion into stmt
* return true on success, false otherwise */
typedef bool (*pdo_dbh_prepare_func)(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options);

/* execute a statement (that does not return a result set) */
typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const char *sql, size_t sql_len);
Expand Down
4 changes: 2 additions & 2 deletions ext/pdo_dblib/dblib_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void dblib_handle_closer(pdo_dbh_t *dbh)
}
}

static int dblib_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
static bool dblib_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
Expand All @@ -105,7 +105,7 @@ static int dblib_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s
S->computed_column_name_count = 0;
S->err.sqlstate = stmt->error_code;

return 1;
return true;
}

static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
Expand Down
6 changes: 3 additions & 3 deletions ext/pdo_firebird/firebird_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ static void firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
/* }}} */

/* called by PDO to prepare an SQL query */
static int firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
pdo_stmt_t *stmt, zval *driver_options)
{
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
Expand Down Expand Up @@ -566,7 +566,7 @@ static int firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
stmt->methods = &firebird_stmt_methods;
stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;

return 1;
return true;

} while (0);

Expand All @@ -582,7 +582,7 @@ static int firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
efree(S);
}

return 0;
return false;
}
/* }}} */

Expand Down
12 changes: 6 additions & 6 deletions ext/pdo_mysql/mysql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static void mysql_handle_closer(pdo_dbh_t *dbh)
/* }}} */

/* {{{ mysql_handle_preparer */
static int mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
static bool mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt));
Expand Down Expand Up @@ -194,15 +194,15 @@ static int mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s
} else if (ret == -1) {
/* failed to parse */
strcpy(dbh->error_code, stmt->error_code);
PDO_DBG_RETURN(0);
PDO_DBG_RETURN(false);
}

if (!(S->stmt = mysql_stmt_init(H->server))) {
pdo_mysql_error(dbh);
if (nsql) {
zend_string_release(nsql);
}
PDO_DBG_RETURN(0);
PDO_DBG_RETURN(false);
}

if (mysql_stmt_prepare(S->stmt, ZSTR_VAL(sql), ZSTR_LEN(sql))) {
Expand All @@ -217,7 +217,7 @@ static int mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s
goto fallback;
}
pdo_mysql_error(dbh);
PDO_DBG_RETURN(0);
PDO_DBG_RETURN(false);
}
if (nsql) {
zend_string_release(nsql);
Expand All @@ -238,13 +238,13 @@ static int mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s

S->max_length = pdo_attr_lval(driver_options, PDO_ATTR_MAX_COLUMN_LEN, 0);

PDO_DBG_RETURN(1);
PDO_DBG_RETURN(true);

fallback:
end:
stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;

PDO_DBG_RETURN(1);
PDO_DBG_RETURN(true);
}
/* }}} */

Expand Down
8 changes: 4 additions & 4 deletions ext/pdo_oci/oci_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static void oci_handle_closer(pdo_dbh_t *dbh) /* {{{ */
}
/* }}} */

static int oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options) /* {{{ */
static bool oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options) /* {{{ */
{
pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
pdo_oci_stmt *S = ecalloc(1, sizeof(*S));
Expand All @@ -263,7 +263,7 @@ static int oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stm
/* couldn't grok it */
strcpy(dbh->error_code, stmt->error_code);
efree(S);
return 0;
return false;
}

/* create an OCI statement handle */
Expand All @@ -283,7 +283,7 @@ static int oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stm
OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
OCIHandleFree(S->err, OCI_HTYPE_ERROR);
efree(S);
return 0;
return false;
}

}
Expand All @@ -304,7 +304,7 @@ static int oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stm
nsql = NULL;
}

return 1;
return true;
}
/* }}} */

Expand Down
12 changes: 6 additions & 6 deletions ext/pdo_odbc/odbc_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static void odbc_handle_closer(pdo_dbh_t *dbh)
dbh->driver_data = NULL;
}

static int odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
static bool odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
RETCODE rc;
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
Expand All @@ -160,7 +160,7 @@ static int odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *st
/* couldn't grok it */
strcpy(dbh->error_code, stmt->error_code);
efree(S);
return 0;
return false;
}

rc = SQLAllocHandle(SQL_HANDLE_STMT, H->dbc, &S->stmt);
Expand All @@ -171,7 +171,7 @@ static int odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *st
zend_string_release(nsql);
}
pdo_odbc_drv_error("SQLAllocStmt");
return 0;
return false;
}

stmt->driver_data = S;
Expand All @@ -185,7 +185,7 @@ static int odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *st
if (nsql) {
zend_string_release(nsql);
}
return 0;
return false;
}
}

Expand All @@ -209,9 +209,9 @@ static int odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *st
}

if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
return 0;
return false;
}
return 1;
return true;
}

static zend_long odbc_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
Expand Down
6 changes: 3 additions & 3 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static void pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
}
/* }}} */

static int pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
Expand Down Expand Up @@ -272,7 +272,7 @@ static int pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s
if (ret == -1) {
/* couldn't grok it */
strcpy(dbh->error_code, stmt->error_code);
return 0;
return false;
} else if (ret == 1) {
/* query was re-written */
S->query = nsql;
Expand All @@ -286,7 +286,7 @@ static int pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s
spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
}

return 1;
return true;
}

static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
Expand Down
8 changes: 4 additions & 4 deletions ext/pdo_sqlite/sqlite_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void sqlite_handle_closer(pdo_dbh_t *dbh) /* {{{ */
}
/* }}} */

static int sqlite_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
static bool sqlite_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
pdo_sqlite_stmt *S = ecalloc(1, sizeof(pdo_sqlite_stmt));
Expand All @@ -189,17 +189,17 @@ static int sqlite_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *
if (PDO_CURSOR_FWDONLY != pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, PDO_CURSOR_FWDONLY)) {
H->einfo.errcode = SQLITE_ERROR;
pdo_sqlite_error(dbh);
return 0;
return false;
}

i = sqlite3_prepare_v2(H->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &S->stmt, &tail);
if (i == SQLITE_OK) {
return 1;
return true;
}

pdo_sqlite_error(dbh);

return 0;
return false;
}

static zend_long sqlite_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
Expand Down

0 comments on commit 60a61af

Please sign in to comment.