php-db | 2000101

[PHP-DB] interbase module patch (updated) From: Max Derkachev (kot <email protected>)
Date: 11/10/00

I've reworked my patch to interbase php module.
The old way - ibase_query() returns affected rows - breaks php way,
where one should know, is the query succeeded.
The query is successful, if it is executed, and unsuccessful if the
query causes an error (e.g. it could not be prepared, or db link is
lost).
So, if the query is executed, it should return true, but it returns 0
(equivalent to false in php land) if affected_rows is 0 .
To prevent this behaviour, I made ibase_affected_rows() php function,
which uses global variable affected_rows, and
ibase_query now works as earlier (returning 1 on inserts, updates and
deletes).
Alo I added global constant PHP_IBASE_REC_V to options, so now one could
specify RECORD_VERSION (or NO RECORD_VERSION) option of a transaction,
when READ COMMITED is specified (for those who can not or not willing to
look at the php sources :) : these options are passed to the
undocumented ibase_trans() php functions as numbers).

DISCLAIMER:
I did not test this patch in production environment, so use it on your
own risk!.
The patch should be applied to untouched php4.0.2 interbase module
sources ( php_interbase.h ver. 1.17, interbase.c, ver. 1.43)
I compiled it in Debian Linux 2.2, Interbase 6 library (Linux).
For me, it works. Maybe ok :)).

--
Best regards,
Max A. Derkachev mailto:kot <email protected>
Symbol-Plus Publishing Ltd.
phone: +7 (812) 265-0054, 265-1228, phone/fax: 567-8775
http://www.Books.Ru -- All Books of Russia

*** php_interbase.h.orig Fri Oct 6 12:02:05 2000 --- php_interbase.h Fri Nov 10 13:41:09 2000 *************** *** 45,48 **** --- 45,49 ---- PHP_FUNCTION(ibase_close); PHP_FUNCTION(ibase_query); + PHP_FUNCTION(ibase_affected_rows); PHP_FUNCTION(ibase_fetch_row); PHP_FUNCTION(ibase_fetch_object); *************** *** 83,86 **** --- 84,88 ---- long max_links, max_persistent; long allow_persistent; + long affected_rows; int le_blob, le_link, le_plink, le_result, le_query, le_trans; char *default_user, *default_password; *************** *** 157,161 **** PHP_IBASE_TIMESTAMP = 64, PHP_IBASE_DATE = 128, ! PHP_IBASE_TIME = 256 }; --- 159,164 ---- PHP_IBASE_TIMESTAMP = 64, PHP_IBASE_DATE = 128, ! PHP_IBASE_TIME = 256, ! PHP_IBASE_REC_V = 528 }; *** interbase.c.orig Fri Oct 6 12:01:58 2000 --- interbase.c Fri Nov 10 15:34:06 2000 *************** *** 34,37 **** --- 34,49 ---- rfinish could be called repeatedly emalloc & co. replaced with malloc & co. + 2000-10-06: Maxim Derkachev <kot <email protected>> + - added int _php_ibase_affected_rows() + (used by ibase_affected_rows()). + 2000-10-10: Maxim Derkachev <kot <email protected>> + - added php function ibase_affected_rows(), + it returns affected rows in case of + INSERT, UPDATE & DELETE queries. + - added global constant PHP_IBASE_REC_V, + and it's use in ibase_trans(), so if + 'READ COMMITED' is a transaction parameter, + one can also define 'RECORD VERSION' (or 'NO + RECORD VERSION') transaction parameter. */ *************** *** 69,72 **** --- 81,85 ---- PHP_FE(ibase_close, NULL) PHP_FE(ibase_query, NULL) + PHP_FE(ibase_affected_rows, NULL) PHP_FE(ibase_fetch_row, NULL) PHP_FE(ibase_fetch_object, NULL) *************** *** 509,512 **** --- 522,526 ---- REGISTER_LONG_CONSTANT("IBASE_DATE", PHP_IBASE_DATE, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("IBASE_TIME", PHP_IBASE_TIME, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("IBASE_REC_V", PHP_IBASE_REC_V, CONST_PERSISTENT); return SUCCESS; *************** *** 1487,1490 **** --- 1501,1507 ---- if (trans_argl & PHP_IBASE_COMMITTED) { tpb[tpb_len++] = isc_tpb_read_committed; + if (trans_argl & PHP_IBASE_REC_V) + tpb[tpb_len++] = isc_tpb_rec_version; + else tpb[tpb_len++] = isc_tpb_no_rec_version; } else if (trans_argl & PHP_IBASE_CONSISTENCY) tpb[tpb_len++] = isc_tpb_consistency; *************** *** 1611,1614 **** --- 1628,1697 ---- /* }}} */ + /* int _php_ibase_affected_rows(ibase_query *ib_query) + get affected rows */ + static int _php_ibase_affected_rows ( ibase_query *ib_query ) + { + int statement_type; + int length; + char type_item[] = {isc_info_sql_stmt_type}; + char info_records[] = {isc_info_sql_records}; + char res_buffer[1024]; + long aff_rows; + IBLS_FETCH(); + + isc_dsql_sql_info( + IB_STATUS, + &ib_query->stmt, + 1, + type_item, + sizeof(res_buffer), + res_buffer); + + if (IB_STATUS[0] && IB_STATUS[1]) { /* error in fetch */ + _php_ibase_error(); + return -1; + } + + if (res_buffer[0] == isc_info_sql_stmt_type) + { + length = isc_vax_integer(&res_buffer[1], 2); + statement_type = isc_vax_integer(&res_buffer[3], length); + } + else { + return -1; + } + + isc_dsql_sql_info( + IB_STATUS, + &ib_query->stmt, + 1, + info_records, + sizeof(res_buffer), + res_buffer); + + if (IB_STATUS[0] && IB_STATUS[1]) { /* error in fetch */ + _php_ibase_error(); + return -1; + } + + switch (statement_type) { + case 2: // insert + aff_rows = isc_vax_integer(&res_buffer[27], 4); + break; + case 3: // update + aff_rows = isc_vax_integer(&res_buffer[6], 4); + break; + case 4: // delete + aff_rows = isc_vax_integer(&res_buffer[13], 4); + break; + default: // n/a + aff_rows = -1; + break; + } + + return aff_rows; + } + /* }}} */ + /* {{{ proto int ibase_query([int link_identifier,] string query [, int bind_args]) *************** *** 1619,1622 **** --- 1702,1706 ---- int i, link_id = 0, trans_n = 0, bind_n = 0, trans_id = 0; char *query; + long aff_rows; ibase_db_link *ib_link; ibase_query *ib_query; *************** *** 1686,1691 **** --- 1770,1777 ---- ib_query->stmt = NULL; /* keep stmt when free query */ _php_ibase_free_query(ib_query); + IBG(affected_rows) = 0; ZEND_REGISTER_RESOURCE(return_value, ib_result, IBG(le_result)); } else { + IBG(affected_rows) = _php_ibase_affected_rows(ib_query); _php_ibase_free_query(ib_query); RETURN_TRUE; *************** *** 1693,1697 **** --- 1779,1794 ---- } /* }}} */ + /* proto int ibase_affected_rows() + return affected rows of the last insert, update or delete + */ + PHP_FUNCTION(ibase_affected_rows) { + IBLS_FETCH(); + if (IBG(affected_rows)) { + RETURN_LONG(IBG(affected_rows)); + } + RETURN_FALSE; + } + /* }}} */ /* {{{ _php_ibase_var_pval() */

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe <email protected>
For additional commands, e-mail: php-db-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>