Hi:
Attached are three patches for ext/sybase. The first two fix bugs. The
third adds a function, while at the same time removing a lot of
redundant code. In order:
php5_sybase-fetch_object.diff:
The sybase extension does not currently compile, since
sybase_fetch_object was never converted to ZE2's new object API.
This patch converts sybase_fetch_object, fixes three warnings, and
checks the return value of malloc() in the one place it's used.
Please apply this - ext/sybase is broken in PHP5 without it.
php_sybase-string-termination:
With --enable-debug turned on, both ZE1 and ZE2 emit warnings about
null-termination for each call to sybase_query. These warnings are
caused by php_sybase_get_column_content, which returns a zval whose
internal string is not null-terminated. The original logic which
terminated the string was #ifdef'ed out; this patch replaces it
with a greatly simplified version. Tested with FreeTDS 0.60 against
Microsoft SQL Server 2000; also applies cleanly to PHP4.
php5_sybase-fetch_assoc.diff:
Most of the other PHP database extensions have three ways for rows
to be fetched: fetch_row (numeric keys), fetch_assoc (string keys),
or fetch_array (both numeric and string keys). The ext/sybase
extension currently has no sybase_fetch_assoc function. This patch
adds sybase_fetch_assoc, while at the same time removing the
duplication between sybase_fetch_row and php_sybase_fetch_hash. The
result is a unified php_sybase_fetch_hash capable of fetching in
either of the three modes, and three userland functions which pass
through to it. (Depends: php5_sybase-fetch_object.diff)
If these look okay, please apply. If not, I'll gladly fix and resend -
just let me know.
Thanks a lot,
- Dave
[EMAIL PROTECTED]
diff -ur php-5.0-rc1-dist/ext/sybase/php_sybase_db.c
php-5.0-rc1/ext/sybase/php_sybase_db.c
--- php-5.0-rc1-dist/ext/sybase/php_sybase_db.c 2004-01-27 20:43:39.000000000 -0500
+++ php-5.0-rc1/ext/sybase/php_sybase_db.c 2004-03-19 08:32:47.000000000 -0500
@@ -439,11 +439,11 @@
list_entry new_le;
if (php_sybase_module.max_links!=-1 &&
php_sybase_module.num_links>=php_sybase_module.max_links) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase:
Too many open links (%d)",php_sybase_module.num_links);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase:
Too many open links (%ld)",php_sybase_module.num_links);
goto err_login;
}
if (php_sybase_module.max_persistent!=-1 &&
php_sybase_module.num_persistent>=php_sybase_module.max_persistent) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase:
Too many open persistent links (%d)",php_sybase_module.num_persistent);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase:
Too many open persistent links (%ld)",php_sybase_module.num_persistent);
goto err_login;
}
/* create the link */
@@ -457,7 +457,9 @@
}
/* hash it up */
- sybase_ptr = (sybase_link *) malloc(sizeof(sybase_link));
+ if ((sybase_ptr = (sybase_link *) malloc(sizeof(sybase_link)))
== NULL) {
+ goto err_link;
+ }
memcpy(sybase_ptr,&sybase,sizeof(sybase_link));
Z_TYPE(new_le) = php_sybase_module.le_plink;
new_le.ptr = sybase_ptr;
@@ -517,7 +519,7 @@
}
}
if (php_sybase_module.max_links!=-1 &&
php_sybase_module.num_links>=php_sybase_module.max_links) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Too many
open links (%d)",php_sybase_module.num_links);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Too many
open links (%ld)",php_sybase_module.num_links);
goto err_login;
}
@@ -711,7 +713,6 @@
char *res_buf;
int res_length = dbdatlen(sybase_ptr->link,offset);
int src_length = res_length;
- register char *p;
switch (coltype(offset)) {
case SYBBINARY:
@@ -1036,7 +1037,6 @@
zval **sybase_result_index;
int type,i,id;
sybase_result *result;
- pval *field_content;
if (ZEND_NUM_ARGS() !=1 || zend_get_parameters_ex(1,
&sybase_result_index)==FAILURE) {
WRONG_PARAM_COUNT;
@@ -1105,11 +1105,9 @@
PHP_FUNCTION(sybase_fetch_object)
{
php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
- if (Z_TYPE_P(return_value)==IS_ARRAY) {
- Z_TYPE_P(return_value)=IS_OBJECT;
- Z_OBJPROP_P(return_value) = Z_ARRVAL_P(return_value);
- Z_OBJCE_P(return_value) = ZEND_STANDARD_CLASS_DEF_PTR;
- }
+
+ if (Z_TYPE_P(return_value)==IS_ARRAY)
+ object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR,
Z_ARRVAL_P(return_value));
}
/* }}} */
diff -ur php-5.0-rc1-dist/ext/sybase/php_sybase_db.c
php-5.0-rc1/ext/sybase/php_sybase_db.c
--- php-5.0-rc1-dist/ext/sybase/php_sybase_db.c 2004-03-19 10:17:50.000000000 -0500
+++ php-5.0-rc1/ext/sybase/php_sybase_db.c 2004-03-19 10:17:02.000000000 -0500
@@ -731,21 +731,15 @@
}
res_buf = (char *) emalloc(res_length+1);
- memset(res_buf,' ',res_length+1); /* XXX i'm sure
there's a better way
-
but i don't have sybase here to test
-
991105 [EMAIL PROTECTED] */
-
dbconvert(NULL,coltype(offset),dbdata(sybase_ptr->link,offset),
src_length,SYBCHAR,res_buf,res_length);
-#if ilia_0
- /* get rid of trailing spaces */
- p = res_buf + res_length;
- while (p >= res_buf && (*p == ' ' || *p == '\0')) {
- p--;
- }
- *(++p) = 0; /* put a trailing NULL */
- res_length = p - res_buf;
-#endif
- Z_STRLEN_P(result) = res_length;
+ memset(res_buf,0,res_length+1);
+
dbconvert(NULL,coltype(offset),dbdata(sybase_ptr->link,offset),src_length,SYBCHAR,res_buf,res_length);
+
+ /* Get rid of extra nulls */
+ while (res_length > 0 && res_buf[res_length] == '\0')
+ res_length--;
+
Z_STRVAL_P(result) = res_buf;
+ Z_STRLEN_P(result) = res_length+1;
Z_TYPE_P(result) = IS_STRING;
} else {
TSRMLS_FETCH();
diff -ur php-5.0-rc1-dist/ext/sybase/php_sybase_db.c
php-5.0-rc1/ext/sybase/php_sybase_db.c
--- php-5.0-rc1-dist/ext/sybase/php_sybase_db.c 2004-03-19 10:21:57.000000000 -0500
+++ php-5.0-rc1/ext/sybase/php_sybase_db.c 2004-03-19 10:32:37.000000000 -0500
@@ -92,6 +92,7 @@
PHP_FE(sybase_num_fields, NULL)
PHP_FE(sybase_fetch_row, NULL)
PHP_FE(sybase_fetch_array, NULL)
+ PHP_FE(sybase_fetch_assoc, NULL)
PHP_FE(sybase_fetch_object, NULL)
PHP_FE(sybase_data_seek, NULL)
PHP_FE(sybase_fetch_field, NULL)
@@ -111,6 +112,7 @@
PHP_FALIAS(mssql_num_fields, sybase_num_fields, NULL)
PHP_FALIAS(mssql_fetch_row, sybase_fetch_row, NULL)
PHP_FALIAS(mssql_fetch_array, sybase_fetch_array, NULL)
+ PHP_FALIAS(mssql_fetch_assoc, sybase_fetch_assoc, NULL)
PHP_FALIAS(mssql_fetch_object, sybase_fetch_object, NULL)
PHP_FALIAS(mssql_data_seek, sybase_data_seek, NULL)
PHP_FALIAS(mssql_fetch_field, sybase_fetch_field, NULL)
@@ -934,7 +936,7 @@
}
/* }}} */
-
+
/* {{{ proto bool sybase_free_result(int result)
Free result memory */
PHP_FUNCTION(sybase_free_result)
@@ -1024,42 +1026,7 @@
}
/* }}} */
-/* {{{ proto array sybase_fetch_row(int result)
- Get row as enumerated array */
-PHP_FUNCTION(sybase_fetch_row)
-{
- zval **sybase_result_index;
- int type,i,id;
- sybase_result *result;
-
- if (ZEND_NUM_ARGS() !=1 || zend_get_parameters_ex(1,
&sybase_result_index)==FAILURE) {
- WRONG_PARAM_COUNT;
- }
-
- convert_to_long_ex(sybase_result_index);
- id = Z_LVAL_PP(sybase_result_index);
-
- result = (sybase_result *) zend_list_find(id,&type);
- if (type!=php_sybase_module.le_result) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,"%d is not a Sybase result
index",id);
- RETURN_FALSE;
- }
-
- if (result->cur_row >= result->num_rows) {
- RETURN_FALSE;
- }
-
- array_init(return_value);
- for (i=0; i<result->num_fields; i++) {
- ZVAL_ADDREF(result->data[result->cur_row][i]);
- zend_hash_index_update(Z_ARRVAL_P(return_value), i, (void *)
&result->data[result->cur_row][i], sizeof(pval *), NULL);
- }
- result->cur_row++;
-}
-/* }}} */
-
-
-static void php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS)
+static void php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int numeric_keys, int
string_keys)
{
zval **sybase_result_index;
sybase_result *result;
@@ -1085,31 +1052,50 @@
array_init(return_value);
for (i=0; i<result->num_fields; i++) {
- ZVAL_ADDREF(result->data[result->cur_row][i]);
- zend_hash_index_update(Z_ARRVAL_P(return_value), i, (void *)
&result->data[result->cur_row][i], sizeof(pval *), NULL);
- ZVAL_ADDREF(result->data[result->cur_row][i]);
- zend_hash_update(Z_ARRVAL_P(return_value), result->fields[i].name,
strlen(result->fields[i].name)+1, (void *) &result->data[result->cur_row][i],
sizeof(pval *), NULL);
+ if (numeric_keys) {
+ ZVAL_ADDREF(result->data[result->cur_row][i]);
+ zend_hash_index_update(Z_ARRVAL_P(return_value), i, (void *)
&result->data[result->cur_row][i], sizeof(pval *), NULL);
+ }
+ if (string_keys) {
+ ZVAL_ADDREF(result->data[result->cur_row][i]);
+ zend_hash_update(Z_ARRVAL_P(return_value),
result->fields[i].name, strlen(result->fields[i].name)+1, (void *)
&result->data[result->cur_row][i], sizeof(pval *), NULL);
+ }
}
result->cur_row++;
}
-
/* {{{ proto object sybase_fetch_object(int result)
Fetch row as object */
PHP_FUNCTION(sybase_fetch_object)
{
- php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
if (Z_TYPE_P(return_value)==IS_ARRAY)
object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR,
Z_ARRVAL_P(return_value));
}
/* }}} */
+/* {{{ proto array sybase_fetch_row(int result)
+ Get row as enumerated array */
+PHP_FUNCTION(sybase_fetch_row)
+{
+ php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
+}
+/* }}} */
+
/* {{{ proto array sybase_fetch_array(int result)
Fetch row as array */
PHP_FUNCTION(sybase_fetch_array)
{
- php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 1);
+}
+/* }}} */
+
+/* {{{ proto array sybase_fetch_assoc(int result)
+ Fetch row as array */
+PHP_FUNCTION(sybase_fetch_assoc)
+{
+ php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
}
/* }}} */
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php