Skip to content

Commit 1a0a96e

Browse files
committed
Refactor ibase_execute
1 parent 7aceb55 commit 1a0a96e

File tree

1 file changed

+57
-110
lines changed

1 file changed

+57
-110
lines changed

ibase_query.c

Lines changed: 57 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -885,18 +885,37 @@ static void _php_ibase_alloc_xsqlda_vars(XSQLDA *sqlda, ISC_SHORT *nullinds) /*
885885
}
886886
/* }}} */
887887

888-
static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resultp, /* {{{ */
889-
ibase_query *ib_query, zval *args)
888+
static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_query *ib_query, zval *args, int bind_n) /* {{{ */
890889
{
891-
XSQLDA *in_sqlda = NULL, *out_sqlda = NULL;
892-
BIND_BUF *bind_buf = NULL;
893890
int i, rv = FAILURE;
894891
static char info_count[] = { isc_info_sql_records };
895892
char result[64];
896893
ISC_STATUS isc_result;
897-
int argc = ib_query->in_sqlda ? ib_query->in_sqlda->sqld : 0;
894+
int argc = ib_query->in_fields_count;
895+
896+
(void)execute_data;
898897

899898
RESET_ERRMSG;
899+
RETVAL_FALSE;
900+
901+
if (bind_n != argc) {
902+
php_error_docref(NULL, (bind_n < argc) ? E_WARNING : E_NOTICE,
903+
"Statement expects %d arguments, %d given", argc, bind_n);
904+
905+
if (bind_n < argc) {
906+
return FAILURE;
907+
}
908+
}
909+
910+
/* Have we used this cursor before and it's still open (exec proc has no cursor) ? */
911+
if (ib_query->is_open && ib_query->statement_type != isc_info_sql_stmt_exec_procedure) {
912+
IBDEBUG("Implicitly closing a cursor");
913+
if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_close)) {
914+
_php_ibase_error();
915+
return FAILURE;
916+
}
917+
ib_query->is_open = 0;
918+
}
900919

901920
for (i = 0; i < argc; ++i) {
902921
SEPARATE_ZVAL(&args[i]);
@@ -915,7 +934,7 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul
915934
if (isc_dsql_execute_immediate(IB_STATUS, &ib_query->link->handle, &tr, 0,
916935
ib_query->query, ib_query->dialect, NULL)) {
917936
_php_ibase_error();
918-
goto _php_ibase_exec_error;
937+
goto _php_ibase_ex_error;
919938
}
920939

921940
trans = (ibase_trans *) emalloc(sizeof(ibase_trans));
@@ -947,7 +966,7 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul
947966
if (isc_dsql_execute_immediate(IB_STATUS, &ib_query->link->handle,
948967
&ib_query->trans->handle, 0, ib_query->query, ib_query->dialect, NULL)) {
949968
_php_ibase_error();
950-
goto _php_ibase_exec_error;
969+
goto _php_ibase_ex_error;
951970
}
952971

953972
if (ib_query->trans->handle == 0 && ib_query->trans_res != NULL) {
@@ -965,55 +984,41 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul
965984
RETVAL_FALSE;
966985
}
967986

968-
/* allocate sqlda and output buffers */
969-
if (ib_query->out_sqlda) { /* output variables in select, select for update */
970-
ibase_result *res;
971-
972-
IBDEBUG("Query wants XSQLDA for output");
973-
res = emalloc(sizeof(ibase_result)+sizeof(ibase_array)*max(0,ib_query->out_array_cnt-1));
974-
res->link = ib_query->link;
975-
res->trans = ib_query->trans;
976-
res->stmt = ib_query->stmt;
977-
GC_ADDREF(res->stmt_res = ib_query->stmt_res);
978-
979-
res->statement_type = ib_query->statement_type;
980-
res->has_more_rows = 1;
981-
982-
out_sqlda = res->out_sqlda = emalloc(XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
983-
memcpy(out_sqlda, ib_query->out_sqlda, XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
984-
_php_ibase_alloc_xsqlda(out_sqlda);
985-
986-
if (ib_query->out_array) {
987-
memcpy(&res->out_array, ib_query->out_array, sizeof(ibase_array)*ib_query->out_array_cnt);
988-
}
989-
*ib_resultp = res;
990-
}
991-
992-
if (ib_query->in_sqlda) { /* has placeholders */
987+
if (ib_query->in_fields_count) { /* has placeholders */
993988
IBDEBUG("Query wants XSQLDA for input");
994-
in_sqlda = emalloc(XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
995-
memcpy(in_sqlda, ib_query->in_sqlda, XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
996-
bind_buf = safe_emalloc(sizeof(BIND_BUF), ib_query->in_sqlda->sqld, 0);
997-
if (_php_ibase_bind(in_sqlda, args, bind_buf, ib_query) == FAILURE) {
989+
if (_php_ibase_bind(ib_query, args) == FAILURE) {
998990
IBDEBUG("Could not bind input XSQLDA");
999-
goto _php_ibase_exec_error;
991+
goto _php_ibase_ex_error;
1000992
}
1001993
}
1002994

1003995
if (ib_query->statement_type == isc_info_sql_stmt_exec_procedure) {
1004996
isc_result = isc_dsql_execute2(IB_STATUS, &ib_query->trans->handle,
1005-
&ib_query->stmt, SQLDA_CURRENT_VERSION, in_sqlda, out_sqlda);
997+
&ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda, ib_query->out_sqlda);
1006998
} else {
1007999
isc_result = isc_dsql_execute(IB_STATUS, &ib_query->trans->handle,
1008-
&ib_query->stmt, SQLDA_CURRENT_VERSION, in_sqlda);
1000+
&ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda);
10091001
}
1002+
10101003
if (isc_result) {
10111004
IBDEBUG("Could not execute query");
10121005
_php_ibase_error();
1013-
goto _php_ibase_exec_error;
1006+
goto _php_ibase_ex_error;
10141007
}
1008+
10151009
ib_query->trans->affected_rows = 0;
10161010

1011+
// TODO: test INSERT / UPDATE / UPDATE OR INSERT with ... RETURNING
1012+
if (ib_query->out_sqlda) { /* output variables in select, select for update */
1013+
ib_query->has_more_rows = 1;
1014+
ib_query->is_open = 1;
1015+
1016+
RETVAL_RES(ib_query->res);
1017+
Z_TRY_ADDREF_P(return_value);
1018+
1019+
return SUCCESS;
1020+
}
1021+
10171022
switch (ib_query->statement_type) {
10181023

10191024
unsigned long affected_rows;
@@ -1026,7 +1031,7 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul
10261031
if (isc_dsql_sql_info(IB_STATUS, &ib_query->stmt, sizeof(info_count),
10271032
info_count, sizeof(result), result)) {
10281033
_php_ibase_error();
1029-
goto _php_ibase_exec_error;
1034+
goto _php_ibase_ex_error;
10301035
}
10311036

10321037
affected_rows = 0;
@@ -1059,24 +1064,7 @@ static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resul
10591064

10601065
rv = SUCCESS;
10611066

1062-
_php_ibase_exec_error:
1063-
1064-
if (in_sqlda) {
1065-
efree(in_sqlda);
1066-
}
1067-
if (bind_buf)
1068-
efree(bind_buf);
1069-
1070-
if (rv == FAILURE) {
1071-
if (*ib_resultp) {
1072-
efree(*ib_resultp);
1073-
*ib_resultp = NULL;
1074-
}
1075-
if (out_sqlda) {
1076-
_php_ibase_free_xsqlda(out_sqlda);
1077-
}
1078-
}
1079-
1067+
_php_ibase_ex_error:
10801068
return rv;
10811069
}
10821070
/* }}} */
@@ -1856,69 +1844,28 @@ PHP_FUNCTION(ibase_execute)
18561844
{
18571845
zval *query, *args = NULL;
18581846
ibase_query *ib_query;
1859-
ibase_result *result = NULL;
18601847
int bind_n = 0;
18611848

18621849
RESET_ERRMSG;
18631850

18641851
RETVAL_FALSE;
18651852

1866-
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|r*", &query, &args, &bind_n)) {
1853+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r*", &query, &args, &bind_n)) {
18671854
return;
18681855
}
18691856

1870-
ib_query = (ibase_query *)zend_fetch_resource_ex(query, LE_QUERY, le_query);
1871-
1872-
do {
1873-
int expected_n = ib_query->in_sqlda ? ib_query->in_sqlda->sqld : 0;
1874-
1875-
if (bind_n != expected_n) {
1876-
php_error_docref(NULL, (bind_n < expected_n) ? E_WARNING : E_NOTICE,
1877-
"Statement expects %d arguments, %d given", expected_n, bind_n);
1878-
1879-
if (bind_n < expected_n) {
1880-
break;
1881-
}
1882-
}
1883-
1884-
/* Have we used this cursor before and it's still open (exec proc has no cursor) ? */
1885-
if (ib_query->result_res != NULL) {
1886-
if (ib_query->statement_type != isc_info_sql_stmt_exec_procedure) {
1887-
IBDEBUG("Implicitly closing a cursor");
1888-
1889-
if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_close)) {
1890-
_php_ibase_error();
1891-
break;
1892-
}
1893-
}
1894-
zend_list_delete(ib_query->result_res);
1895-
ib_query->result_res = NULL;
1896-
}
1857+
if(_php_ibase_fetch_query_res(query, &ib_query)) {
1858+
return;
1859+
}
18971860

1898-
if (FAILURE == _php_ibase_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, &result, ib_query, args)) {
1899-
break;
1900-
}
1861+
// was do {
1862+
_php_ibase_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, ib_query, args, bind_n);
19011863

19021864
/* free the query if trans handle was released */
1903-
if (ib_query->trans->handle == 0) {
1904-
zend_list_delete(Z_RES_P(query));
1905-
}
1906-
1907-
if (result != NULL) {
1908-
zval *ret;
1909-
1910-
result->type = EXECUTE_RESULT;
1911-
if (ib_query->statement_type == isc_info_sql_stmt_exec_procedure) {
1912-
result->stmt = 0;
1913-
}
1914-
1915-
ret = zend_list_insert(result, le_result);
1916-
ib_query->result_res = Z_RES_P(ret);
1917-
ZVAL_COPY_VALUE(return_value, ret);
1918-
Z_TRY_ADDREF_P(return_value);
1919-
Z_TRY_ADDREF_P(return_value);
1920-
}
1921-
} while (0);
1865+
// if (ib_query->trans->handle == 0) {
1866+
// zend_list_delete(Z_RES_P(query));
1867+
// }
1868+
// } while (0);
19221869
}
19231870
/* }}} */
19241871

0 commit comments

Comments
 (0)