Skip to content

Commit 1528cb3

Browse files
committed
Refactor _php_ibase_alloc_query
1 parent e51da3a commit 1528cb3

File tree

1 file changed

+59
-109
lines changed

1 file changed

+59
-109
lines changed

ibase_query.c

Lines changed: 59 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -261,126 +261,92 @@ static int _php_ibase_alloc_array(ibase_array **ib_arrayp, XSQLDA *sqlda, /* {{{
261261
/* }}} */
262262

263263
/* allocate and prepare query */
264-
static int _php_ibase_alloc_query(ibase_query *ib_query, ibase_db_link *link, /* {{{ */
265-
ibase_trans *trans, char *query, unsigned short dialect, zend_resource *trans_res)
264+
static int _php_ibase_prepare(ibase_query **new_query, ibase_db_link *link, /* {{{ */
265+
ibase_trans *trans, char *query)
266266
{
267-
static char info_type[] = {isc_info_sql_stmt_type};
268-
char result[8];
269-
270267
/* Return FAILURE, if querystring is empty */
271268
if (*query == '\0') {
272269
php_error_docref(NULL, E_WARNING, "Querystring empty.");
273270
return FAILURE;
274271
}
275272

273+
ibase_query *ib_query = ecalloc(1, sizeof(ibase_query));
274+
275+
ib_query->res = zend_register_resource(ib_query, le_query);
276276
ib_query->link = link;
277277
ib_query->trans = trans;
278-
ib_query->result_res = NULL;
279-
ib_query->stmt = 0;
280-
ib_query->stmt_res = NULL;
281-
ib_query->in_array = NULL;
282-
ib_query->out_array = NULL;
283-
ib_query->dialect = dialect;
278+
ib_query->dialect = link->dialect;
284279
ib_query->query = estrdup(query);
285-
ib_query->trans_res = trans_res;
286-
ib_query->out_sqlda = NULL;
287-
ib_query->in_sqlda = NULL;
288280

289281
if (isc_dsql_allocate_statement(IB_STATUS, &link->handle, &ib_query->stmt)) {
290282
_php_ibase_error();
291283
goto _php_ibase_alloc_query_error;
292284
}
293-
{ /* allocate zend resource for statement*/
294-
ibase_statement* ib_stmt = emalloc(sizeof(ibase_statement));
295-
ib_stmt->link = link;
296-
ib_stmt->stmt = ib_query->stmt;
297-
ib_query->stmt_res = zend_register_resource(ib_stmt, le_statement);
298-
}
299-
300-
ib_query->out_sqlda = (XSQLDA *) emalloc(XSQLDA_LENGTH(1));
301-
ib_query->out_sqlda->sqln = 1;
302-
ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
303285

304286
if (isc_dsql_prepare(IB_STATUS, &ib_query->trans->handle, &ib_query->stmt,
305-
0, query, dialect, ib_query->out_sqlda)) {
287+
0, query, link->dialect, NULL)) {
288+
IBDEBUG("isc_dsql_prepare() failed\n");
306289
_php_ibase_error();
307290
goto _php_ibase_alloc_query_error;
308291
}
309292

310-
/* find out what kind of statement was prepared */
311-
if (isc_dsql_sql_info(IB_STATUS, &ib_query->stmt, sizeof(info_type),
312-
info_type, sizeof(result), result)) {
313-
_php_ibase_error();
293+
294+
// TODO: Rename. It also sets statement_type
295+
if(_php_ibase_get_vars_count(ib_query)){
314296
goto _php_ibase_alloc_query_error;
315297
}
316-
ib_query->statement_type = result[3];
317298

318-
/* not enough output variables ? */
319-
if (ib_query->out_sqlda->sqld > ib_query->out_sqlda->sqln) {
320-
ib_query->out_sqlda = erealloc(ib_query->out_sqlda, XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
321-
ib_query->out_sqlda->sqln = ib_query->out_sqlda->sqld;
299+
if(ib_query->out_fields_count) {
300+
ib_query->out_sqlda = (XSQLDA *) emalloc(XSQLDA_LENGTH(ib_query->out_fields_count));
301+
ib_query->out_sqlda->sqln = ib_query->out_fields_count;
322302
ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
303+
323304
if (isc_dsql_describe(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->out_sqlda)) {
305+
IBDEBUG("isc_dsql_describe() failed\n");
324306
_php_ibase_error();
325307
goto _php_ibase_alloc_query_error;
326308
}
327-
}
328309

329-
/* maybe have input placeholders? */
330-
ib_query->in_sqlda = emalloc(XSQLDA_LENGTH(1));
331-
ib_query->in_sqlda->sqln = 1;
332-
ib_query->in_sqlda->version = SQLDA_CURRENT_VERSION;
333-
if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
334-
_php_ibase_error();
335-
goto _php_ibase_alloc_query_error;
310+
assert(ib_query->out_sqlda->sqln == ib_query->out_sqlda->sqld);
311+
assert(ib_query->out_sqlda->sqld == ib_query->out_fields_count);
312+
313+
ib_query->out_nullind = safe_emalloc(sizeof(*ib_query->out_nullind), ib_query->out_sqlda->sqld, 0);
314+
_php_ibase_alloc_xsqlda_vars(ib_query->out_sqlda, ib_query->out_nullind);
315+
if (FAILURE == _php_ibase_alloc_array(&ib_query->out_array, ib_query->out_sqlda,
316+
link->handle, trans->handle, &ib_query->out_array_cnt)) {
317+
goto _php_ibase_alloc_query_error;
318+
}
336319
}
337320

338-
/* not enough input variables ? */
339-
if (ib_query->in_sqlda->sqln < ib_query->in_sqlda->sqld) {
340-
ib_query->in_sqlda = erealloc(ib_query->in_sqlda, XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
341-
ib_query->in_sqlda->sqln = ib_query->in_sqlda->sqld;
321+
if(ib_query->in_fields_count) {
322+
ib_query->in_sqlda = emalloc(XSQLDA_LENGTH(ib_query->in_fields_count));
323+
ib_query->in_sqlda->sqln = ib_query->in_fields_count;
342324
ib_query->in_sqlda->version = SQLDA_CURRENT_VERSION;
343325

344-
if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt,
345-
SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
326+
if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
327+
IBDEBUG("isc_dsql_describe_bind() failed\n");
346328
_php_ibase_error();
347329
goto _php_ibase_alloc_query_error;
348330
}
349-
}
350331

351-
/* no, haven't placeholders at all */
352-
if (ib_query->in_sqlda->sqld == 0) {
353-
efree(ib_query->in_sqlda);
354-
ib_query->in_sqlda = NULL;
355-
} else if (FAILURE == _php_ibase_alloc_array(&ib_query->in_array, ib_query->in_sqlda,
332+
assert(ib_query->in_sqlda->sqln == ib_query->in_sqlda->sqld);
333+
assert(ib_query->in_sqlda->sqld == ib_query->in_fields_count);
334+
335+
ib_query->bind_buf = safe_emalloc(sizeof(BIND_BUF), ib_query->in_sqlda->sqld, 0);
336+
ib_query->in_nullind = safe_emalloc(sizeof(*ib_query->in_nullind), ib_query->in_sqlda->sqld, 0);
337+
if (FAILURE == _php_ibase_alloc_array(&ib_query->in_array, ib_query->in_sqlda,
356338
link->handle, trans->handle, &ib_query->in_array_cnt)) {
357-
goto _php_ibase_alloc_query_error;
339+
goto _php_ibase_alloc_query_error;
340+
}
358341
}
359342

360-
if (ib_query->out_sqlda->sqld == 0) {
361-
efree(ib_query->out_sqlda);
362-
ib_query->out_sqlda = NULL;
363-
} else if (FAILURE == _php_ibase_alloc_array(&ib_query->out_array, ib_query->out_sqlda,
364-
link->handle, trans->handle, &ib_query->out_array_cnt)) {
365-
goto _php_ibase_alloc_query_error;
366-
}
343+
*new_query = ib_query;
367344

368345
return SUCCESS;
369346

370347
_php_ibase_alloc_query_error:
348+
zend_list_delete(ib_query->res);
371349

372-
if (ib_query->out_sqlda) {
373-
efree(ib_query->out_sqlda);
374-
}
375-
if (ib_query->in_sqlda) {
376-
efree(ib_query->in_sqlda);
377-
}
378-
if (ib_query->out_array) {
379-
efree(ib_query->out_array);
380-
}
381-
if (ib_query->query) {
382-
efree(ib_query->query);
383-
}
384350
return FAILURE;
385351
}
386352
/* }}} */
@@ -1206,46 +1172,32 @@ PHP_FUNCTION(ibase_query)
12061172
}
12071173

12081174
/* open default transaction */
1209-
if (ib_link == NULL || FAILURE == _php_ibase_def_trans(ib_link, &trans)
1210-
|| FAILURE == _php_ibase_alloc_query(&ib_query, ib_link, trans, query, ib_link->dialect, trans_res)) {
1175+
if (ib_link == NULL || FAILURE == _php_ibase_def_trans(ib_link, &trans)){
12111176
return;
12121177
}
12131178

1214-
do {
1215-
int bind_n = ZEND_NUM_ARGS() - bind_i,
1216-
expected_n = ib_query.in_sqlda ? ib_query.in_sqlda->sqld : 0;
1179+
ibase_query *ib_query;
1180+
if(FAILURE == _php_ibase_prepare(&ib_query, ib_link, trans, query)) {
1181+
return;
1182+
}
12171183

1218-
if (bind_n != expected_n) {
1219-
php_error_docref(NULL, (bind_n < expected_n) ? E_WARNING : E_NOTICE,
1220-
"Statement expects %d arguments, %d given", expected_n, bind_n);
1221-
if (bind_n < expected_n) {
1222-
break;
1223-
}
1224-
} else if (bind_n > 0) {
1225-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &bind_args, &bind_num) == FAILURE) {
1226-
return;
1227-
}
1184+
{ // was while
1185+
int bind_n = ZEND_NUM_ARGS() - bind_i;
1186+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &bind_args, &bind_num) == FAILURE) {
1187+
goto _php_ibase_query_error;
12281188
}
12291189

1230-
if (FAILURE == _php_ibase_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, &result, &ib_query,
1231-
&bind_args[bind_i])) {
1232-
break;
1190+
if (FAILURE == _php_ibase_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, ib_query, &bind_args[bind_i], bind_n)) {
1191+
goto _php_ibase_query_error;
12331192
}
12341193

1235-
if (result != NULL) { /* statement returns a result */
1236-
result->type = QUERY_RESULT;
1237-
1238-
/* EXECUTE PROCEDURE returns only one row => statement can be released immediately */
1239-
if (ib_query.statement_type != isc_info_sql_stmt_exec_procedure) {
1240-
ib_query.stmt = 0; /* keep stmt when free query */
1241-
}
1242-
RETVAL_RES(zend_register_resource(result, le_result));
1243-
Z_TRY_ADDREF_P(return_value);
1244-
}
1245-
} while (0);
1194+
return;
1195+
}
12461196

1247-
_php_ibase_free_query(&ib_query);
1197+
assert(false && "UNREACHABLE");
12481198

1199+
_php_ibase_query_error:
1200+
zend_list_delete(ib_query->res);
12491201
}
12501202
/* }}} */
12511203

@@ -1888,13 +1840,11 @@ PHP_FUNCTION(ibase_prepare)
18881840
RETURN_FALSE;
18891841
}
18901842

1891-
ib_query = (ibase_query *) emalloc(sizeof(ibase_query));
1892-
1893-
if (FAILURE == _php_ibase_alloc_query(ib_query, ib_link, trans, query, ib_link->dialect, trans_res)) {
1894-
efree(ib_query);
1843+
if(FAILURE == _php_ibase_prepare(&ib_query, ib_link, trans, query)){
18951844
RETURN_FALSE;
18961845
}
1897-
RETVAL_RES(zend_register_resource(ib_query, le_query));
1846+
1847+
RETVAL_RES(ib_query->res);
18981848
Z_TRY_ADDREF_P(return_value);
18991849
}
19001850
/* }}} */

0 commit comments

Comments
 (0)