Skip to content

Commit 94ea8e2

Browse files
committed
Refactor PDO doer handler to use zend_string
1 parent 9be0ee4 commit 94ea8e2

File tree

10 files changed

+42
-34
lines changed

10 files changed

+42
-34
lines changed

UPGRADING.INTERNALS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ PHP 8.1 INTERNALS UPGRADE NOTES
5656
of returning a boolean, and the quoted string as a pair of out params.
5757
Similarly the unquoted string is now a zend_string* instead of a pair of
5858
char* and size_t length.
59+
- The doer handler now accepts a zend_string* instead of char* + size_t
60+
pair for the SQL statement.

ext/pdo/pdo_dbh.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,23 +913,22 @@ PHP_METHOD(PDO, getAttribute)
913913
PHP_METHOD(PDO, exec)
914914
{
915915
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
916-
char *statement;
917-
size_t statement_len;
916+
zend_string *statement;
918917
zend_long ret;
919918

920919
ZEND_PARSE_PARAMETERS_START(1, 1)
921-
Z_PARAM_STRING(statement, statement_len)
920+
Z_PARAM_STR(statement)
922921
ZEND_PARSE_PARAMETERS_END();
923922

924-
if (statement_len == 0) {
923+
if (ZSTR_LEN(statement) == 0) {
925924
zend_argument_value_error(1, "cannot be empty");
926925
RETURN_THROWS();
927926
}
928927

929928
PDO_DBH_CLEAR_ERR();
930929
PDO_CONSTRUCT_CHECK;
931-
ret = dbh->methods->doer(dbh, statement, statement_len);
932-
if(ret == -1) {
930+
ret = dbh->methods->doer(dbh, statement);
931+
if (ret == -1) {
933932
PDO_HANDLE_DBH_ERR();
934933
RETURN_FALSE;
935934
} else {

ext/pdo/php_pdo_driver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ typedef void (*pdo_dbh_close_func)(pdo_dbh_t *dbh);
232232
* return true on success, false otherwise */
233233
typedef bool (*pdo_dbh_prepare_func)(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options);
234234

235-
/* execute a statement (that does not return a result set) */
236-
typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const char *sql, size_t sql_len);
235+
/* execute a statement (that does not return a result set)
236+
* Return -1 on failure, otherwise the number of affected rows */
237+
typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const zend_string *sql);
237238

238239
/* quote a string */
239240
typedef zend_string* (*pdo_dbh_quote_func)(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype);

ext/pdo_dblib/dblib_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ static bool dblib_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *
106106
return true;
107107
}
108108

109-
static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
109+
static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
110110
{
111111
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
112112
RETCODE ret, resret;
113113

114114
dbsetuserdata(H->link, (BYTE*)&H->err);
115115

116-
if (FAIL == dbcmd(H->link, sql)) {
116+
if (FAIL == dbcmd(H->link, ZSTR_VAL(sql))) {
117117
return -1;
118118
}
119119

ext/pdo_firebird/firebird_driver.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "php_pdo_firebird.h"
3030
#include "php_pdo_firebird_int.h"
3131

32-
static int firebird_alloc_prepare_stmt(pdo_dbh_t*, const char*, size_t, XSQLDA*, isc_stmt_handle*,
32+
static int firebird_alloc_prepare_stmt(pdo_dbh_t*, const zend_string*, XSQLDA*, isc_stmt_handle*,
3333
HashTable*);
3434

3535
const char CHR_LETTER = 1;
@@ -291,13 +291,13 @@ static FbTokenType getToken(const char** begin, const char* end)
291291
return ret;
292292
}
293293

294-
int preprocess(const char* sql, int sql_len, char* sql_out, HashTable* named_params)
294+
int preprocess(const zend_string* sql, char* sql_out, HashTable* named_params)
295295
{
296296
bool passAsIs = 1, execBlock = 0;
297297
zend_long pindex = -1;
298298
char pname[254], ident[253], ident2[253];
299299
unsigned int l;
300-
const char* p = sql, * end = sql + sql_len;
300+
const char* p = ZSTR_VAL(sql), * end = ZSTR_VAL(sql) + ZSTR_LEN(sql);
301301
const char* start = p;
302302
FbTokenType tok = getToken(&p, end);
303303

@@ -363,7 +363,7 @@ int preprocess(const char* sql, int sql_len, char* sql_out, HashTable* named_par
363363

364364
if (passAsIs)
365365
{
366-
strcpy(sql_out, sql);
366+
strcpy(sql_out, ZSTR_VAL(sql));
367367
return 1;
368368
}
369369

@@ -522,7 +522,7 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
522522
zend_hash_init(np, 8, NULL, NULL, 0);
523523

524524
/* allocate and prepare statement */
525-
if (!firebird_alloc_prepare_stmt(dbh, ZSTR_VAL(sql), ZSTR_LEN(sql), &num_sqlda, &s, np)) {
525+
if (!firebird_alloc_prepare_stmt(dbh, sql, &num_sqlda, &s, np)) {
526526
break;
527527
}
528528

@@ -587,7 +587,7 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
587587
/* }}} */
588588

589589
/* called by PDO to execute a statement that doesn't produce a result set */
590-
static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
590+
static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /* {{{ */
591591
{
592592
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
593593
isc_stmt_handle stmt = PDO_FIREBIRD_HANDLE_INITIALIZER;
@@ -602,7 +602,7 @@ static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sq
602602
out_sqlda.sqln = 1;
603603

604604
/* allocate and prepare statement */
605-
if (!firebird_alloc_prepare_stmt(dbh, sql, sql_len, &out_sqlda, &stmt, 0)) {
605+
if (!firebird_alloc_prepare_stmt(dbh, sql, &out_sqlda, &stmt, 0)) {
606606
return -1;
607607
}
608608

@@ -767,14 +767,14 @@ static bool firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
767767
/* }}} */
768768

769769
/* used by prepare and exec to allocate a statement handle and prepare the SQL */
770-
static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t sql_len, /* {{{ */
770+
static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const zend_string *sql,
771771
XSQLDA *out_sqlda, isc_stmt_handle *s, HashTable *named_params)
772772
{
773773
pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
774774
char *new_sql;
775775

776776
/* Firebird allows SQL statements up to 64k, so bail if it doesn't fit */
777-
if (sql_len > 65536) {
777+
if (ZSTR_LEN(sql) > 65536) {
778778
strcpy(dbh->error_code, "01004");
779779
return 0;
780780
}
@@ -797,9 +797,9 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t s
797797

798798
/* in order to support named params, which Firebird itself doesn't,
799799
we need to replace :foo by ?, and store the name we just replaced */
800-
new_sql = emalloc(sql_len+1);
800+
new_sql = emalloc(ZSTR_LEN(sql)+1);
801801
new_sql[0] = '\0';
802-
if (!preprocess(sql, sql_len, new_sql, named_params)) {
802+
if (!preprocess(sql, new_sql, named_params)) {
803803
strcpy(dbh->error_code, "07000");
804804
efree(new_sql);
805805
return 0;
@@ -815,7 +815,6 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t s
815815
efree(new_sql);
816816
return 1;
817817
}
818-
/* }}} */
819818

820819
/* called by PDO to set a driver-specific dbh attribute */
821820
static bool firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */

ext/pdo_mysql/mysql_driver.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ static bool mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *
249249
/* }}} */
250250

251251
/* {{{ mysql_handle_doer */
252-
static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
252+
static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
253253
{
254254
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
255255
PDO_DBG_ENTER("mysql_handle_doer");
256256
PDO_DBG_INF_FMT("dbh=%p", dbh);
257-
PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
257+
PDO_DBG_INF_FMT("sql=%.*s", (int)ZSTR_LEN(sql), ZSTR_VAL(sql));
258258

259-
if (mysql_real_query(H->server, sql, sql_len)) {
259+
if (mysql_real_query(H->server, ZSTR_VAL(sql), ZSTR_LEN(sql))) {
260260
pdo_mysql_error(dbh);
261261
PDO_DBG_RETURN(-1);
262262
} else {
@@ -348,9 +348,16 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
348348
/* {{{ mysql_handle_begin */
349349
static bool mysql_handle_begin(pdo_dbh_t *dbh)
350350
{
351+
zend_long return_value;
352+
zend_string *command;
353+
351354
PDO_DBG_ENTER("mysql_handle_quoter");
352355
PDO_DBG_INF_FMT("dbh=%p", dbh);
353-
PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("START TRANSACTION")));
356+
357+
command = zend_string_init("START TRANSACTION", strlen("START TRANSACTION"), 0);
358+
return_value = mysql_handle_doer(dbh, command);
359+
zend_string_release_ex(command, 0);
360+
PDO_DBG_RETURN(0 <= return_value);
354361
}
355362
/* }}} */
356363

ext/pdo_oci/oci_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static bool oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *st
306306
}
307307
/* }}} */
308308

309-
static zend_long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
309+
static zend_long oci_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /* {{{ */
310310
{
311311
pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
312312
OCIStmt *stmt;
@@ -316,7 +316,7 @@ static zend_long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len
316316

317317
OCIHandleAlloc(H->env, (dvoid*)&stmt, OCI_HTYPE_STMT, 0, NULL);
318318

319-
H->last_err = OCIStmtPrepare(stmt, H->err, (text*)sql, (ub4) sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
319+
H->last_err = OCIStmtPrepare(stmt, H->err, (text*)ZSTR_VAL(sql), (ub4) ZSTR_LEN(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
320320
if (H->last_err) {
321321
H->last_err = oci_drv_error("OCIStmtPrepare");
322322
OCIHandleFree(stmt, OCI_HTYPE_STMT);

ext/pdo_odbc/odbc_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static bool odbc_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *s
212212
return true;
213213
}
214214

215-
static zend_long odbc_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
215+
static zend_long odbc_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
216216
{
217217
pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data;
218218
RETCODE rc;
@@ -225,7 +225,7 @@ static zend_long odbc_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_le
225225
return -1;
226226
}
227227

228-
rc = SQLExecDirect(stmt, (SQLCHAR *) sql, sql_len);
228+
rc = SQLExecDirect(stmt, (SQLCHAR *) ZSTR_VAL(sql), ZSTR_LEN(sql));
229229

230230
if (rc == SQL_NO_DATA) {
231231
/* If SQLExecDirect executes a searched update or delete statement that

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *
288288
return true;
289289
}
290290

291-
static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
291+
static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
292292
{
293293
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
294294
PGresult *res;
295295
zend_long ret = 1;
296296
ExecStatusType qs;
297297

298-
if (!(res = PQexec(H->server, sql))) {
298+
if (!(res = PQexec(H->server, ZSTR_VAL(sql)))) {
299299
/* fatal error */
300300
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
301301
return -1;

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ static bool sqlite_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t
200200
return false;
201201
}
202202

203-
static zend_long sqlite_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
203+
static zend_long sqlite_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
204204
{
205205
pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
206206
char *errmsg = NULL;
207207

208-
if (sqlite3_exec(H->db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
208+
if (sqlite3_exec(H->db, ZSTR_VAL(sql), NULL, NULL, &errmsg) != SQLITE_OK) {
209209
pdo_sqlite_error(dbh);
210210
if (errmsg)
211211
sqlite3_free(errmsg);

0 commit comments

Comments
 (0)