📄 dbx.c
字号:
if (object_init(return_value) != SUCCESS) { zend_error(E_ERROR, "dbx: unable to create resulting object..."); FREE_ZVAL(dbx_module); zval_dtor(db_name); /* to free stringvalue memory */ FREE_ZVAL(db_name); FREE_ZVAL(rv_dbx_handle); RETURN_LONG(0); } zend_hash_update(Z_OBJPROP_P(return_value), "handle", 7, (void *)&(rv_dbx_handle), sizeof(zval *), NULL); zend_hash_update(Z_OBJPROP_P(return_value), "module", 7, (void *)&(dbx_module), sizeof(zval *), NULL); zend_hash_update(Z_OBJPROP_P(return_value), "database", 9, (void *)&(db_name), sizeof(zval *), NULL);}/* }}} *//* {{{ proto bool dbx_close(dbx_link_object dbx_link) Returns success or failure */ZEND_FUNCTION(dbx_close){ int number_of_arguments=1; zval **arguments[1]; int result; zval **dbx_handle; zval **dbx_module; zval **dbx_database; zval *rv_success; if (ZEND_NUM_ARGS() !=number_of_arguments || zend_get_parameters_array_ex(number_of_arguments, arguments) == FAILURE) { WRONG_PARAM_COUNT; } if (!split_dbx_handle_object(arguments[0], &dbx_handle, &dbx_module, &dbx_database TSRMLS_CC)) { zend_error(E_WARNING, "dbx_close: not a valid dbx_handle-object..."); RETURN_LONG(0); } MAKE_STD_ZVAL(rv_success); ZVAL_LONG(rv_success, 0); result = switch_dbx_close(&rv_success, dbx_handle, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); result = (result && Z_LVAL_P(rv_success))?1:0; FREE_ZVAL(rv_success); RETURN_LONG(result?1:0);}/* }}} *//* {{{ proto dbx_result_object dbx_query(dbx_link_object dbx_link, string sql_statement [, long flags]) Returns a dbx_link_object on success and returns 0 on failure */ZEND_FUNCTION(dbx_query){ int min_number_of_arguments=2; int number_of_arguments=3; zval **arguments[3]; int result; zval **dbx_handle; zval **dbx_module; zval **dbx_database; zval *rv_result_handle; zval *rv_column_count; long col_index; long row_count; zval *info; long query_flags; long result_flags; zval *data; zval **row_ptr; zval **inforow_ptr; /* default values for colname-case */ char * colnames_case = INI_STR("dbx.colnames_case"); long colcase = DBX_COLNAMES_UNCHANGED; if (!strcmp(colnames_case, "uppercase")) { colcase = DBX_COLNAMES_UPPERCASE; } if (!strcmp(colnames_case, "lowercase")) { colcase = DBX_COLNAMES_LOWERCASE; } if (ZEND_NUM_ARGS()<min_number_of_arguments || ZEND_NUM_ARGS()>number_of_arguments || zend_get_parameters_array_ex(ZEND_NUM_ARGS(), arguments) == FAILURE) { WRONG_PARAM_COUNT; } if (!split_dbx_handle_object(arguments[0], &dbx_handle, &dbx_module, &dbx_database TSRMLS_CC)) { zend_error(E_WARNING, "dbx_query: not a valid dbx_handle-object..."); RETURN_LONG(0); } /* default values */ result_flags = DBX_RESULT_INFO | DBX_RESULT_INDEX | DBX_RESULT_ASSOC; /* parameter overrides */ if (ZEND_NUM_ARGS()>2) { convert_to_long_ex(arguments[2]); query_flags = Z_LVAL_PP(arguments[2]); /* fieldnames are needed for association! */ result_flags = (query_flags & DBX_RESULT_INFO) | (query_flags & DBX_RESULT_INDEX) | (query_flags & DBX_RESULT_ASSOC); if (result_flags & DBX_RESULT_ASSOC) { result_flags |= DBX_RESULT_INFO; } if (!result_flags) result_flags = DBX_RESULT_INFO | DBX_RESULT_INDEX | DBX_RESULT_ASSOC; /* override ini-setting for colcase */ if (query_flags & DBX_COLNAMES_UNCHANGED) { colcase = DBX_COLNAMES_UNCHANGED; } if (query_flags & DBX_COLNAMES_UPPERCASE) { colcase = DBX_COLNAMES_UPPERCASE; } if (query_flags & DBX_COLNAMES_LOWERCASE) { colcase = DBX_COLNAMES_LOWERCASE; } } MAKE_STD_ZVAL(rv_result_handle); ZVAL_LONG(rv_result_handle, 0); convert_to_string_ex(arguments[1]); result = switch_dbx_query(&rv_result_handle, dbx_handle, dbx_database, arguments[1], INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); /* boolean return value means either failure for any query or success for queries that don't return anything */ if (!result || (rv_result_handle && Z_TYPE_P(rv_result_handle)==IS_BOOL)) { result = (result && Z_LVAL_P(rv_result_handle))?1:0; FREE_ZVAL(rv_result_handle); RETURN_LONG(result?1:0); } /* if you get here, the query succeeded and returned results, so we'll return them * rv_result_handle holds a resource */ /* init return_value as object (of rows) */ if (object_init(return_value) != SUCCESS) { zend_error(E_ERROR, "dbx_query: unable to create resulting object..."); FREE_ZVAL(rv_result_handle); RETURN_LONG(0); } /* add result_handle property to return_value */ zend_hash_update(Z_OBJPROP_P(return_value), "handle", 7, (void *)&(rv_result_handle), sizeof(zval *), NULL); /* init info property as array and add to return_value as a property */ if (result_flags & DBX_RESULT_INFO) { MAKE_STD_ZVAL(info); if (array_init(info) != SUCCESS) { zend_error(E_ERROR, "dbx_query: unable to create info-array for results..."); FREE_ZVAL(info); RETURN_LONG(0); } zend_hash_update(Z_OBJPROP_P(return_value), "info", 5, (void *)&(info), sizeof(zval *), NULL); } /* init data property as array and add to return_value as a property */ MAKE_STD_ZVAL(data); if (array_init(data) != SUCCESS) { zend_error(E_ERROR, "dbx_query: unable to create data-array for results..."); FREE_ZVAL(data); RETURN_LONG(0); } zend_hash_update(Z_OBJPROP_P(return_value), "data", 5, (void *)&(data), sizeof(zval *), NULL); /* get columncount and add to returnvalue as property */ MAKE_STD_ZVAL(rv_column_count); ZVAL_LONG(rv_column_count, 0); result = switch_dbx_getcolumncount(&rv_column_count, &rv_result_handle, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); if (!result) { zend_error(E_ERROR, "dbx_query: get column_count failed..."); FREE_ZVAL(rv_column_count); RETURN_LONG(0); } zend_hash_update(Z_OBJPROP_P(return_value), "cols", 5, (void *)&(rv_column_count), sizeof(zval *), NULL); /* fill the info array with columnnames and types (indexed and assoc) */ if (result_flags & DBX_RESULT_INFO) { zval *info_row_name; zval *info_row_type; MAKE_STD_ZVAL(info_row_name); MAKE_STD_ZVAL(info_row_type); if (array_init(info_row_name) != SUCCESS) { zend_error(E_ERROR, "dbx_query: unable to create info_row_name-array for results..."); FREE_ZVAL(info_row_name); FREE_ZVAL(info_row_type); RETURN_LONG(0); } if (array_init(info_row_type) != SUCCESS) { zend_error(E_ERROR, "dbx_query: unable to create info_row_type-array for results..."); FREE_ZVAL(info_row_name); FREE_ZVAL(info_row_type); RETURN_LONG(0); } for (col_index=0; col_index<Z_LVAL_P(rv_column_count); ++col_index) { zval *rv_column_name; zval *rv_column_type; /* get name */ MAKE_STD_ZVAL(rv_column_name); ZVAL_LONG(rv_column_name, 0); result = switch_dbx_getcolumnname(&rv_column_name, &rv_result_handle, col_index, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); /* modify case if requested */ if (colcase==DBX_COLNAMES_UPPERCASE) { php_strtoupper(Z_STRVAL_P(rv_column_name), Z_STRLEN_P(rv_column_name)); } if (colcase==DBX_COLNAMES_LOWERCASE) { php_strtolower(Z_STRVAL_P(rv_column_name), Z_STRLEN_P(rv_column_name)); } if (result) { zend_hash_index_update(Z_ARRVAL_P(info_row_name), col_index, (void *)&(rv_column_name), sizeof(zval *), NULL); } else { FREE_ZVAL(rv_column_name); } /* get type */ MAKE_STD_ZVAL(rv_column_type); ZVAL_LONG(rv_column_type, 0); result = switch_dbx_getcolumntype(&rv_column_type, &rv_result_handle, col_index, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); if (result) { zend_hash_index_update(Z_ARRVAL_P(info_row_type), col_index, (void *)&(rv_column_type), sizeof(zval *), NULL); } else { FREE_ZVAL(rv_column_type); } } zend_hash_update(Z_ARRVAL_P(info), "name", 5, (void *) &info_row_name, sizeof(zval *), (void **) &inforow_ptr); zend_hash_update(Z_ARRVAL_P(info), "type", 5, (void *) &info_row_type, sizeof(zval *), NULL); } /* fill each row array with fieldvalues (indexed (and assoc)) */ row_count=0; result=1; while (result) { zval *rv_row; MAKE_STD_ZVAL(rv_row); ZVAL_LONG(rv_row, 0); result = switch_dbx_getrow(&rv_row, &rv_result_handle, row_count, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); if (result) { zend_hash_index_update(Z_ARRVAL_P(data), row_count, (void *)&(rv_row), sizeof(zval *), (void **) &row_ptr); /* associate results with fieldnames */ if (result_flags & DBX_RESULT_ASSOC) { zval **columnname_ptr, **actual_ptr; for (col_index=0; col_index<Z_LVAL_P(rv_column_count); ++col_index) { zend_hash_index_find(Z_ARRVAL_PP(inforow_ptr), col_index, (void **) &columnname_ptr); zend_hash_index_find(Z_ARRVAL_PP(row_ptr), col_index, (void **) &actual_ptr); (*actual_ptr)->refcount+=1; (*actual_ptr)->is_ref=1; zend_hash_update(Z_ARRVAL_PP(row_ptr), Z_STRVAL_PP(columnname_ptr), Z_STRLEN_PP(columnname_ptr) + 1, actual_ptr, sizeof(zval *), NULL); } } ++row_count; } else { FREE_ZVAL(rv_row); } } /* add row_count property */ add_property_long(return_value, "rows", row_count);}/* }}} *//* {{{ proto void dbx_error(dbx_link_object dbx_link) Returns success or failure */ZEND_FUNCTION(dbx_error){ int number_of_arguments=1; zval **arguments[1]; int result; zval **dbx_handle; zval **dbx_module; zval **dbx_database; zval *rv_errormsg; if (ZEND_NUM_ARGS() !=number_of_arguments || zend_get_parameters_array_ex(number_of_arguments, arguments) == FAILURE) { WRONG_PARAM_COUNT; } if (!split_dbx_handle_object(arguments[0], &dbx_handle, &dbx_module, &dbx_database TSRMLS_CC)) { zend_error(E_WARNING, "dbx_error: not a valid dbx_handle-object..."); RETURN_LONG(0); } MAKE_STD_ZVAL(rv_errormsg); ZVAL_LONG(rv_errormsg, 0); result = switch_dbx_error(&rv_errormsg, dbx_handle, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); if (!result) { FREE_ZVAL(rv_errormsg); RETURN_STRING("", 1); } MOVE_RETURNED_TO_RV(&return_value, rv_errormsg);}/* }}} *//* {{{ proto string dbx_esc(dbx_link_object dbx_link, string sz) Returns escaped string or NULL on error*/ZEND_FUNCTION(dbx_escape_string){ int number_of_arguments=2; zval **arguments[2]; int result; zval **dbx_handle; zval **dbx_module; zval **dbx_database; zval *rv; if (ZEND_NUM_ARGS() !=number_of_arguments || zend_get_parameters_array_ex(number_of_arguments, arguments) == FAILURE) { WRONG_PARAM_COUNT; } if (!split_dbx_handle_object(arguments[0], &dbx_handle, &dbx_module, &dbx_database TSRMLS_CC)) { zend_error(E_WARNING, "dbx_esc: not a valid dbx_handle-object..."); RETURN_NULL(); } convert_to_string_ex(arguments[1]); MAKE_STD_ZVAL(rv); ZVAL_LONG(rv, 0); result = switch_dbx_esc(&rv, dbx_handle, arguments[1], INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); if (!result) { /* this will probably never happen */ FREE_ZVAL(rv); RETURN_NULL(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -