📄 php_sybase_db.c
字号:
if (dbsetopt(sybase_ptr->link,DBBUFFER,"2",-1)==FAIL) { zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length+1); goto err_login; } } } Z_LVAL_P(return_value) = zend_list_insert(sybase_ptr,php_sybase_module.le_plink); Z_TYPE_P(return_value) = IS_LONG; } else { /* non persistent */ list_entry *index_ptr,new_index_ptr; /* first we check the hash for the hashed_details key. if it exists, * it should point us to the right offset where the actual sybase link sits. * if it doesn't, open a new sybase link, add it to the resource list, * and add a pointer to it with hashed_details as the key. */ if (zend_hash_find(&EG(regular_list),hashed_details,hashed_details_length+1,(void **) &index_ptr)==SUCCESS) { int type,link; void *ptr; if (Z_TYPE_P(index_ptr) != le_index_ptr) { goto err_login; } link = (int) index_ptr->ptr; ptr = zend_list_find(link,&type); /* check if the link is still there */ if (ptr && (type==php_sybase_module.le_link || type==php_sybase_module.le_plink)) { Z_LVAL_P(return_value) = php_sybase_module.default_link = link; Z_TYPE_P(return_value) = IS_LONG; efree(hashed_details); dbloginfree(sybase.login); return; } else { zend_hash_del(&EG(regular_list),hashed_details,hashed_details_length+1); } } 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); goto err_login; } if ((sybase.link=PHP_SYBASE_DBOPEN(sybase.login,host))==NULL) { /*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to connect to server: %s",sybase_error(sybase));*/ goto err_login; } if (dbsetopt(sybase.link,DBBUFFER,"2",-1)==FAIL) { goto err_link; } /* add it to the list */ sybase_ptr = (sybase_link *) emalloc(sizeof(sybase_link)); memcpy(sybase_ptr,&sybase,sizeof(sybase_link)); Z_LVAL_P(return_value) = zend_list_insert(sybase_ptr,php_sybase_module.le_link); Z_TYPE_P(return_value) = IS_LONG; /* add it to the hash */ new_index_ptr.ptr = (void *) Z_LVAL_P(return_value); Z_TYPE(new_index_ptr) = le_index_ptr; if (zend_hash_update(&EG(regular_list),hashed_details,hashed_details_length+1,(void *) &new_index_ptr, sizeof(list_entry),NULL)==FAILURE) { goto err_link; } php_sybase_module.num_links++; } efree(hashed_details); php_sybase_module.default_link=Z_LVAL_P(return_value); return;err_link: dbclose(sybase.link);err_login: dbloginfree(sybase.login);err: efree(hashed_details); RETURN_FALSE;}static int php_sybase_get_default_link(INTERNAL_FUNCTION_PARAMETERS){ if (php_sybase_module.default_link==-1) { /* no link opened yet, implicitly open one */ ht = 0; php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0); } return php_sybase_module.default_link;}/* {{{ proto int sybase_connect([string host [, string user [, string password [, string charset [, string appname]]]]]) Open Sybase server connection */PHP_FUNCTION(sybase_connect){ php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);}/* }}} *//* {{{ proto int sybase_pconnect([string host [, string user [, string password [, string charset [, string appname]]]]]) Open persistent Sybase connection */PHP_FUNCTION(sybase_pconnect){ php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);}/* }}} *//* {{{ proto bool sybase_close([int link_id]) Close Sybase connection */PHP_FUNCTION(sybase_close){ zval **sybase_link_index; int id,type; switch (ZEND_NUM_ARGS()) { case 0: id = php_sybase_module.default_link; break; case 1: if (zend_get_parameters_ex(1, &sybase_link_index) == FAILURE) { RETURN_FALSE; } convert_to_long_ex(sybase_link_index); id = Z_LVAL_PP(sybase_link_index); break; default: WRONG_PARAM_COUNT; break; } zend_list_find(id,&type); if (type!=php_sybase_module.le_link && type!=php_sybase_module.le_plink) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"%d is not a Sybase link index",id); RETURN_FALSE; } zend_list_delete(id); RETURN_TRUE;}/* }}} */ /* {{{ proto bool sybase_select_db(string database [, int link_id]) Select Sybase database */PHP_FUNCTION(sybase_select_db){ zval **db, **sybase_link_index; int id,type; sybase_link *sybase_ptr; switch(ZEND_NUM_ARGS()) { case 1: if (zend_get_parameters_ex(1, &db) == FAILURE) { RETURN_FALSE; } id = php_sybase_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU); break; case 2: if (zend_get_parameters_ex(2, &db, &sybase_link_index) == FAILURE) { RETURN_FALSE; } convert_to_long_ex(sybase_link_index); id = Z_LVAL_PP(sybase_link_index); break; default: WRONG_PARAM_COUNT; break; } CHECK_LINK(id); sybase_ptr = (sybase_link *) zend_list_find(id,&type); if (type!=php_sybase_module.le_link && type!=php_sybase_module.le_plink) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"%d is not a Sybase link index",id); RETURN_FALSE; } convert_to_string_ex(db); if (dbuse(sybase_ptr->link,Z_STRVAL_PP(db))==FAIL) { /*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to select database: %s",sybase_error(sybase));*/ RETURN_FALSE; } else { RETURN_TRUE; }}/* }}} */ static void php_sybase_get_column_content(sybase_link *sybase_ptr,int offset,pval **result_ptr, int column_type){ zval *result; ALLOC_INIT_ZVAL(result); *result_ptr = result; if (dbdatlen(sybase_ptr->link,offset) == 0) { ZVAL_FALSE(result); return; } switch (column_type) { case SYBINT2: case SYBINT4: { Z_LVAL_P(result) = (long) anyintcol(offset); Z_TYPE_P(result) = IS_LONG; break; } case SYBCHAR: case SYBTEXT: { int length; char *data = charcol(offset); length=dbdatlen(sybase_ptr->link,offset); while (length>0 && charcol(offset)[length-1] == ' ') { /* nuke trailing whitespace */ length--; } Z_STRVAL_P(result) = estrndup(data,length); Z_STRLEN_P(result) = length; Z_TYPE_P(result) = IS_STRING; break; } /*case SYBFLT8:*/ case SYBREAL: { Z_DVAL_P(result) = (double) floatcol(offset); Z_TYPE_P(result) = IS_DOUBLE; break; } default: { if (dbwillconvert(coltype(offset),SYBCHAR)) { char *res_buf; int res_length = dbdatlen(sybase_ptr->link,offset); int src_length = res_length; register char *p; switch (coltype(offset)) { case SYBBINARY: case SYBVARBINARY: case SYBIMAGE: res_length *= 2; break; case SYBCHAR: case SYBVARCHAR: case SYBTEXT: break; default: /* take no chances, no telling how big the result would really be */ res_length += 20; break; } 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 thies@thieso.net */ 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; Z_STRVAL_P(result) = res_buf; Z_TYPE_P(result) = IS_STRING; } else { TSRMLS_FETCH(); php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: column %d has unknown data type (%d)", offset, coltype(offset)); ZVAL_FALSE(result); } } }}/* {{{ proto int sybase_query(string query [, int link_id]) Send Sybase query */PHP_FUNCTION(sybase_query){ zval **query, **sybase_link_index; int id,type,retvalue; sybase_link *sybase_ptr; sybase_result *result; int num_fields; int blocks_initialized=1; int i,j; int *column_types; RETCODE dbresults_retval; switch(ZEND_NUM_ARGS()) { case 1: if (zend_get_parameters_ex(1, &query) == FAILURE) { RETURN_FALSE; } id = php_sybase_module.default_link; break; case 2: if (zend_get_parameters_ex(2, &query, &sybase_link_index) == FAILURE) { RETURN_FALSE; } convert_to_long_ex(sybase_link_index); id = Z_LVAL_PP(sybase_link_index); break; default: WRONG_PARAM_COUNT; break; } sybase_ptr = (sybase_link *) zend_list_find(id,&type); if (type!=php_sybase_module.le_link && type!=php_sybase_module.le_plink) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"%d is not a Sybase link index",id); RETURN_FALSE; } convert_to_string_ex(query); if (dbcmd(sybase_ptr->link,Z_STRVAL_PP(query))==FAIL) { /*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to set query");*/ RETURN_FALSE; } if (dbsqlexec(sybase_ptr->link)==FAIL) { /*php_error(E_WARNING,"Sybase: Query failed");*/ RETURN_FALSE; } dbresults_retval = dbresults(sybase_ptr->link); if (dbresults_retval==FAIL) { /*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Query failed");*/ RETURN_FALSE; } /* The following is more or less the equivalent of mysql_store_result(). * fetch all rows from the server into the row buffer, thus: * 1) Being able to fire up another query without explicitly reading all rows * 2) Having numrows accessible */ retvalue=dbnextrow(sybase_ptr->link); if (retvalue==FAIL) { RETURN_FALSE; } num_fields = dbnumcols(sybase_ptr->link); if (num_fields<=0) { RETURN_TRUE; } column_types = (int *) emalloc(sizeof(int) * num_fields); for (i=0; i<num_fields; i++) { column_types[i] = coltype(i+1); } result = (sybase_result *) emalloc(sizeof(sybase_result)); result->data = (pval ***) emalloc(sizeof(pval **)*SYBASE_ROWS_BLOCK); result->sybase_ptr = sybase_ptr; result->cur_field=result->cur_row=result->num_rows=0; result->num_fields = num_fields; i=0; while (retvalue!=FAIL && retvalue!=NO_MORE_ROWS) { result->num_rows++; if (result->num_rows > blocks_initialized*SYBASE_ROWS_BLOCK) { result->data = (pval ***) erealloc(result->data,sizeof(pval **)*SYBASE_ROWS_BLOCK*(++blocks_initialized)); } result->data[i] = (pval **) emalloc(sizeof(pval *)*num_fields); for (j=1; j<=num_fields; j++) { php_sybase_get_column_content(sybase_ptr, j, &result->data[i][j-1], column_types[j-1]); if (!php_sybase_module.compatability_mode) { zval *cur_value = result->data[i][j-1]; convert_to_string(cur_value); if (PG(magic_quotes_runtime)) { Z_STRVAL_P(cur_value) = php_addslashes(Z_STRVAL_P(cur_value), Z_STRLEN_P(cur_value), &Z_STRLEN_P(cur_value),0 TSRMLS_CC); } } } retvalue=dbnextrow(sybase_ptr->link); dbclrbuf(sybase_ptr->link,DBLASTROW(sybase_ptr->link)-1); i++; } result->num_rows = DBCOUNT(sybase_ptr->link); result->fields = (sybase_field *) emalloc(sizeof(sybase_field)*num_fields); j=0; for (i=0; i<num_fields; i++) { char *fname = dbcolname(sybase_ptr->link,i+1); char computed_buf[16]; if (*fname) { result->fields[i].name = estrdup(fname); } else { if (j>0) { snprintf(computed_buf,16,"computed%d",j); } else { strcpy(computed_buf,"computed"); } result->fields[i].name = estrdup(computed_buf); j++; } result->fields[i].max_length = dbcollen(sybase_ptr->link,i+1); result->fields[i].column_source = estrdup(dbcolsource(sybase_ptr->link,i+1)); if (!result->fields[i].column_source) { result->fields[i].column_source = empty_string; } Z_TYPE(result->fields[i]) = column_types[i]; /* set numeric flag */ switch (column_types[i]) { case SYBINT2: case SYBINT4: case SYBFLT8: case SYBREAL: result->fields[i].numeric = 1; break; case SYBCHAR: case SYBTEXT: default: result->fields[i].numeric = 0; break; } } efree(column_types); Z_LVAL_P(return_value) = zend_list_insert(result,php_sybase_module.le_result); Z_TYPE_P(return_value) = IS_LONG; /** * mgruetzner@rw3.com -- 3-6/2003 * * If you do a query that calls a stored procedure which returns * data AND calls raiserror to generated a user-defined error message, * then after retrieving the first set of results above, the call * to dbresults will have returned SUCCESS, instead of NO_MORE_RESULTS * which indicates that the message generated by raiserror is still * waiting to be read. If this is the case, then call dbresults * again to force the reading of the message. This will ensure the * get_last_message call will return the message properly if called * after mssql_query, like it should. * * One thing to note, we are assuming that no more data should follow * in this case--only the message will be read. To make sure, I have * added a call to dbnextrow. The assumption is that it will return * NO_MORE_ROWS in this case. If the assumption is false, generate * a PHP error so we can debug this case. * */ if (dbresults_retval != NO_MORE_RESULTS) { /* Call dbresults again to read message */ dbresults_retval = dbresults(sybase_ptr->link); /* Check assumption that dbnextrow returns NO_MORE_ROWS */ retvalue = dbnextrow(sybase_ptr->link); if (retvalue != NO_MORE_ROWS) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Expected dbnextrow() to return NO_MORE_ROWS."); } }}/* }}} */ /* {{{ proto bool sybase_free_result(int result) Free result memory */PHP_FUNCTION(sybase_free_result){ zval **sybase_result_index; sybase_result *result; int type; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &sybase_result_index) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(sybase_result_index); if (Z_LVAL_PP(sybase_result_index)==0) { RETURN_FALSE; } result = (sybase_result *) zend_list_find(Z_LVAL_PP(sybase_result_index),&type); if (type!=php_sybase_module.le_result) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"%ld is not a Sybase result index", Z_LVAL_PP(sybase_result_index)); RETURN_FALSE; } zend_list_delete(Z_LVAL_PP(sybase_result_index)); RETURN_TRUE;}/* }}} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -