/* {{{ proto int ora_open(int connection) Open an Oracle cursor */ void php3_Ora_Open(INTERNAL_FUNCTION_PARAMETERS) { /* conn_index */ pval *arg; oraConnection *conn = NULL; oraCursor *cursor = NULL; int conn_ind; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(arg); conn_ind = arg->value.lval; conn = ora_get_conn(list,plist, conn_ind); if (conn == NULL) { RETURN_FALSE; } if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){ php3_error(E_WARNING, "Out of memory"); RETURN_FALSE; } memset(cursor, 0, sizeof(oraCursor)); if (oopen(&cursor->cda, &conn->lda, (text *) 0, -1, -1, (text *) 0, -1)) { php3_error(E_WARNING, "Unable to open new cursor (%s)", ora_error(&cursor->cda)); efree(cursor); RETURN_FALSE; } cursor->open = 1; cursor->conn_ptr = conn; cursor->conn_id = conn_ind; RETURN_RESOURCE(ora_add_cursor(list, cursor)); }
/* {{{ proto string ora_error(int cursor_or_connection) Get an Oracle error message */ void php3_Ora_Error(INTERNAL_FUNCTION_PARAMETERS) { pval *arg; oraCursor *cursor; oraConnection *conn; int argc = ARG_COUNT(ht); if (argc < 0 || argc > 1 || getParametersArray(ht, argc, &arg) == FAILURE) { WRONG_PARAM_COUNT; } if (argc == 1) { convert_to_long(arg); if ((cursor = ora_get_cursor(list, arg->value.lval)) != NULL) { return_value->type = IS_STRING; return_value->value.str.val = estrdup(ora_error(&cursor->cda)); return_value->value.str.len = strlen(return_value->value.str.val); } else if ((conn = ora_get_conn(list,plist, arg->value.lval)) != NULL) { return_value->type = IS_STRING; return_value->value.str.val = estrdup(ora_error(&conn->lda)); return_value->value.str.len = strlen(return_value->value.str.val); } } else { return_value->type = IS_STRING; return_value->value.str.val = estrdup(ora_error(&db_err_conn.lda)); return_value->value.str.len = strlen(return_value->value.str.val); } }
/* {{{ proto string base_convert(string number, int frombase, int tobase) Converts a number in a string from any base <= 36 to any base <= 36. */ void php3_base_convert(INTERNAL_FUNCTION_PARAMETERS) { pval *number, *frombase, *tobase, temp; char *result; if (ARG_COUNT(ht) != 3 || getParameters(ht, 3, &number, &frombase, &tobase) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(number); convert_to_long(frombase); convert_to_long(tobase); if (frombase->value.lval < 2 || frombase->value.lval > 36) { php3_error(E_WARNING, "base_convert: invalid `from base' (%d)", frombase->value.lval); RETURN_FALSE; } if (tobase->value.lval < 2 || tobase->value.lval > 36) { php3_error(E_WARNING, "base_convert: invalid `to base' (%d)", tobase->value.lval); RETURN_FALSE; } temp.type = IS_LONG; temp.value.lval = _php3_basetolong(number, frombase->value.lval); result = _php3_longtobase(&temp, tobase->value.lval); RETVAL_STRING(result, 0); } /* }}} */
/* {{{ proto int exec(string command [, array output [, int return_value]]) Execute an external program */ void php3_exec(INTERNAL_FUNCTION_PARAMETERS) { pval *arg1, *arg2, *arg3; int arg_count = ARG_COUNT(ht); int ret; if (arg_count > 3 || getParameters(ht, arg_count, &arg1, &arg2, &arg3) == FAILURE) { WRONG_PARAM_COUNT; } switch (arg_count) { case 1: ret = _Exec(0, arg1->value.str.val, NULL, return_value); break; case 2: if (!ParameterPassedByReference(ht,2)) { php3_error(E_WARNING,"Array argument to exec() not passed by reference"); } ret = _Exec(2, arg1->value.str.val, arg2, return_value); break; case 3: if (!ParameterPassedByReference(ht,2)) { php3_error(E_WARNING,"Array argument to exec() not passed by reference"); } if (!ParameterPassedByReference(ht,3)) { php3_error(E_WARNING,"return_status argument to exec() not passed by reference"); } ret = _Exec(2, arg1->value.str.val, arg2, return_value); arg3->type = IS_LONG; arg3->value.lval=ret; break; } }
/* {{{ proto mixed com_propget(int module, string property_name) Gets properties from a COM module */ void php3_COM_propget(INTERNAL_FUNCTION_PARAMETERS) { pval *arg_idispatch, *arg_property; IDispatch *i_dispatch; int type; DISPID dispid; HRESULT hr; OLECHAR *funcname; char *error_message; VARIANTARG var_result; DISPPARAMS dispparams; if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &arg_idispatch, &arg_property)==FAILURE) { WRONG_PARAM_COUNT; } /* obtain i_dispatch interface */ convert_to_long(arg_idispatch); i_dispatch = php3_list_find(arg_idispatch->value.lval,&type); if (!i_dispatch || (type!=le_idispatch)) { php3_error(E_WARNING,"%d is not a COM object handler", arg_idispatch->value.lval); } /* obtain property/method handler */ convert_to_string(arg_property); funcname = _php3_C_str_to_unicode(arg_property->value.str.val, arg_property->value.str.len); hr = i_dispatch->lpVtbl->GetIDsOfNames(i_dispatch, &IID_NULL, &funcname, 1, LOCALE_SYSTEM_DEFAULT, &dispid); if (FAILED(hr)) { error_message = _php3_COM_error_message(hr); php3_error(E_WARNING,"Unable to lookup %s: %s\n", arg_property->value.str.val, error_message); LocalFree(error_message); efree(funcname); RETURN_FALSE; } dispparams.cArgs = 0; dispparams.cNamedArgs = 0; hr = i_dispatch->lpVtbl->Invoke(i_dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &var_result, NULL, 0); if (FAILED(hr)) { error_message = _php3_COM_error_message(hr); php3_error(E_WARNING,"Invoke() failed: %s\n", error_message); LocalFree(error_message); efree(funcname); RETURN_FALSE; } _php3_variant_to_pval(&var_result, return_value); efree(funcname); }
/* {{{ proto int com_load(string module_name) Loads a COM module */ void php3_COM_load(INTERNAL_FUNCTION_PARAMETERS) { pval *module_name; CLSID clsid; HRESULT hr; OLECHAR *ProgID; IUnknown FAR *i_unknown = NULL; IDispatch FAR *i_dispatch = NULL; char *error_message; char *clsid_str; int i; if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &module_name)==FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(module_name); ProgID = (OLECHAR *) emalloc(sizeof(OLECHAR)*(module_name->value.str.len+1)); for (i=0; i<module_name->value.str.len; i++) { ProgID[i] = module_name->value.str.val[i]; } ProgID[i] = 0; hr=CLSIDFromProgID(ProgID, &clsid); efree(ProgID); /* obtain CLSID */ if (FAILED(hr)) { error_message = _php3_COM_error_message(hr); php3_error(E_WARNING,"Invalid ProgID: %s\n", error_message); LocalFree(error_message); RETURN_FALSE; } /* obtain IUnknown */ if (FAILED(hr=CoCreateInstance(&clsid, NULL, CLSCTX_ALL, &IID_IUnknown, (void FAR* FAR*) &i_unknown))) { error_message = _php3_COM_error_message(hr); clsid_str = _php3_string_from_clsid(&clsid); php3_error(E_WARNING,"Unable to obtain IUnknown interface for CLSID %s: %s",clsid_str,error_message); LocalFree(error_message); efree(clsid_str); RETURN_FALSE; } /* obtain IDispatch */ if (FAILED(hr=i_unknown->lpVtbl->QueryInterface(i_unknown, &IID_IDispatch, (void FAR* FAR*) &i_dispatch))) { error_message = _php3_COM_error_message(hr); clsid_str = _php3_string_from_clsid(&clsid); php3_error(E_WARNING,"Unable to obtain IDispatch interface for CLSID %s: %s",clsid_str,error_message); LocalFree(error_message); efree(clsid_str); RETURN_FALSE; } i_unknown->lpVtbl->Release(i_unknown); RETURN_LONG(php3_list_insert(i_dispatch,le_idispatch)); }
/* {{{ proto double deg2rad(double number) Converts the number in degrees to the radian equivalent */ void php3_deg2rad(INTERNAL_FUNCTION_PARAMETERS) { pval *deg; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, °) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_double(deg); RETVAL_DOUBLE((deg->value.dval / 180.0) * M_PI); }
/* {{{ proto double rad2deg(double number) Converts the radian number to the equivalent number in degrees */ void php3_rad2deg(INTERNAL_FUNCTION_PARAMETERS) { pval *rad; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &rad) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_double(rad); RETVAL_DOUBLE((rad->value.dval / M_PI) * 180); }
/* {{{ proto double sqrt(double number) Returns the square root of the number */ void php3_sqrt(INTERNAL_FUNCTION_PARAMETERS) { pval *num; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_double(num); return_value->value.dval = sqrt(num->value.dval); return_value->type = IS_DOUBLE; }
/* {{{ proto int ora_parse(int cursor, string sql_statement [, int defer]) Parse an Oracle SQL statement */ void php3_Ora_Parse(INTERNAL_FUNCTION_PARAMETERS) { /* cursor_ind, sql_statement [, defer] */ int argc; pval *argv[3]; oraCursor *cursor; sword defer = 0; text *query; argc = ARG_COUNT(ht); if ((argc != 2 && argc != 3) || getParametersArray(ht, argc, argv) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(argv[0]); convert_to_string(argv[1]); if (argc == 3) { convert_to_long(argv[2]); if (argv[2]->value.lval != 0) { defer = DEFER_PARSE; } } query = (text *) estrndup(argv[1]->value.str.val,argv[1]->value.str.len); if (query == NULL) { php3_error(E_WARNING, "Invalid query"); RETURN_FALSE; } if (!(cursor = ora_get_cursor(list, argv[0]->value.lval))){ efree(query); RETURN_FALSE; } if (cursor->query) { efree(cursor->query); } cursor->query = query; cursor->fetched = 0; if(cursor->params && cursor->nparams > 0){ _php3_hash_destroy(cursor->params); efree(cursor->params); cursor->params = NULL; cursor->nparams = 0; } if (oparse(&cursor->cda, query, (sb4) - 1, defer, VERSION_7)) { php3_error(E_WARNING, "Ora_Parse failed (%s)", ora_error(&cursor->cda)); RETURN_FALSE; } RETURN_TRUE; }
/* {{{ proto double atan2(double y, double x) Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */ void php3_atan2(INTERNAL_FUNCTION_PARAMETERS) { pval *num1, *num2; if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &num1, &num2) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_double(num1); convert_to_double(num2); return_value->value.dval = atan2(num1->value.dval,num2->value.dval); return_value->type = IS_DOUBLE; }
/* {{{ proto double pow(double base, double exponent) Returns base raised to the power of expopent */ void php3_pow(INTERNAL_FUNCTION_PARAMETERS) { pval *num1, *num2; TLS_VARS; if (ARG_COUNT(ht) != 2 || getParameters(ht,2,&num1,&num2) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_double(num1); convert_to_double(num2); RETURN_DOUBLE(pow(num1->value.dval, num2->value.dval)); }
/* {{{ proto int octdec(string octal_number) Returns the decimal equivalent of an octal string */ void php3_octdec(INTERNAL_FUNCTION_PARAMETERS) { pval *arg; long ret; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(arg); ret = _php3_basetolong(arg, 8); RETVAL_LONG(ret); }
/* {{{ proto string dechex(int decimal_number) Returns a string containing a hexadecimal representation of the given number */ void php3_dechex(INTERNAL_FUNCTION_PARAMETERS) { pval *arg; char *result; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(arg); result = _php3_longtobase(arg, 16); return_value->type = IS_STRING; return_value->value.str.len = strlen(result); return_value->value.str.val = result; }
/* {{{ proto int levenshtein(string str1, string str2) Calculate Levenshtein distance between two strings */ void php3_levenshtein(INTERNAL_FUNCTION_PARAMETERS) { pval *str1, *str2; int l; if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &str1, &str2) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(str1); convert_to_string(str2); l = calc_levdist(str1->value.str.val, str2->value.str.val); if(l<0) { php3_error(E_WARNING,"levenshtein(): argument string(s) too long"); } RETURN_LONG(l); }
/* {{{ proto string number_format(double number [, int num_decimal_places [, string dec_separator, string thousands_separator]]) Formats a number with grouped thousands */ void php3_number_format(INTERNAL_FUNCTION_PARAMETERS) { pval *num,*dec,*t_s,*d_p; char thousand_sep=',', dec_point='.'; switch(ARG_COUNT(ht)) { case 1: if (getParameters(ht, 1, &num)==FAILURE) { RETURN_FALSE; } convert_to_double(num); RETURN_STRING(_php3_number_format(num->value.dval,0,dec_point,thousand_sep),0); break; case 2: if (getParameters(ht, 2, &num, &dec)==FAILURE) { RETURN_FALSE; } convert_to_double(num); convert_to_long(dec); RETURN_STRING(_php3_number_format(num->value.dval,dec->value.lval,dec_point,thousand_sep),0); break; case 4: if (getParameters(ht, 4, &num, &dec, &d_p, &t_s)==FAILURE) { RETURN_FALSE; } convert_to_double(num); convert_to_long(dec); convert_to_string(d_p); convert_to_string(t_s); if (d_p->value.str.len==1) { dec_point=d_p->value.str.val[0]; } if (t_s->value.str.len==1) { thousand_sep=t_s->value.str.val[0]; } RETURN_STRING(_php3_number_format(num->value.dval,dec->value.lval,dec_point,thousand_sep),0); break; default: WRONG_PARAM_COUNT; break; } }
/* {{{ proto array parse_url(string url) Parse a URL and return its components */ void php3_parse_url(INTERNAL_FUNCTION_PARAMETERS) { pval *string; url *resource; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &string) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(string); resource = url_parse(string->value.str.val); if (resource == NULL) { php3_error(E_WARNING, "unable to parse url (%s)", string->value.str.val); RETURN_FALSE; } /* allocate an array for return */ if (array_init(return_value) == FAILURE) { free_url(resource); RETURN_FALSE; } /* add the various elements to the array */ if (resource->scheme != NULL) add_assoc_string(return_value, "scheme", resource->scheme, 1); if (resource->host != NULL) add_assoc_string(return_value, "host", resource->host, 1); if (resource->port != 0) add_assoc_long(return_value, "port", resource->port); if (resource->user != NULL) add_assoc_string(return_value, "user", resource->user, 1); if (resource->pass != NULL) add_assoc_string(return_value, "pass", resource->pass, 1); if (resource->path != NULL) add_assoc_string(return_value, "path", resource->path, 1); if (resource->query != NULL) add_assoc_string(return_value, "query", resource->query, 1); if (resource->fragment != NULL) add_assoc_string(return_value, "fragment", resource->fragment, 1); free_url(resource); }
/* {{{ proto double floor(double number) Returns the next lowest integer value from the number */ void php3_floor(INTERNAL_FUNCTION_PARAMETERS) { pval *value; TLS_VARS; if (ARG_COUNT(ht)!=1||getParameters(ht,1,&value)==FAILURE) { WRONG_PARAM_COUNT; } if (value->type == IS_STRING) { convert_string_to_number(value); } if (value->type == IS_DOUBLE) { RETURN_DOUBLE(floor(value->value.dval)); } else if (value->type == IS_LONG) { RETURN_LONG(value->value.lval); } RETURN_FALSE; }
/* {{{ proto int round(double number) Returns the rounded value of the number */ void php3_round(INTERNAL_FUNCTION_PARAMETERS) { pval *value; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &value) == FAILURE) { WRONG_PARAM_COUNT; } if (value->type == IS_STRING) { convert_string_to_number(value); } if (value->type == IS_DOUBLE) { double d; d=rint(value->value.dval); if(d==0.0) d=0.0; /* workaround for rint() returning -0 instead of 0 */ RETURN_DOUBLE(d); } if (value->type == IS_LONG) { RETURN_DOUBLE((double)value->value.lval); } RETURN_FALSE; }
/* {{{ proto int ora_errorcode(int cursor_or_connection) Get an Oracle error code */ void php3_Ora_ErrorCode(INTERNAL_FUNCTION_PARAMETERS) { pval *arg; oraCursor *cursor; oraConnection *conn; int argc = ARG_COUNT(ht); if (argc < 0 || argc > 1 || getParametersArray(ht, argc, &arg) == FAILURE) { WRONG_PARAM_COUNT; } if (argc == 1) { convert_to_long(arg); if ((cursor = ora_get_cursor(list, arg->value.lval)) != NULL) { RETURN_LONG(cursor->cda.rc); } else if ((conn = ora_get_conn(list,plist, arg->value.lval)) != NULL) { RETURN_LONG(conn->lda.rc); } } else { RETURN_LONG(db_err_conn.lda.rc); } }
/* {{{ proto string ora_columnname(int cursor, int column) Get the name of an Oracle result column */ void php3_Ora_ColumnName(INTERNAL_FUNCTION_PARAMETERS) { /* cursor_index, column_index */ pval *argv[2]; int cursor_ind; oraCursor *cursor = NULL; if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(argv[0]); cursor_ind = argv[0]->value.lval; /* Find the cursor */ if ((cursor = ora_get_cursor(list, cursor_ind)) == NULL) { RETURN_FALSE; } convert_to_long(argv[1]); if (cursor->ncols == 0){ php3_error(E_WARNING, "No tuples available at this cursor index"); RETURN_FALSE; } if (argv[1]->value.lval >= cursor->ncols){ php3_error(E_WARNING, "Column index larger than number of columns"); RETURN_FALSE; } if (argv[1]->value.lval < 0){ php3_error(E_WARNING, "Column numbering starts at 0"); RETURN_FALSE; } RETURN_STRINGL(cursor->columns[argv[1]->value.lval].cbuf, cursor->columns[argv[1]->value.lval].cbufl,1); }
/* {{{ proto mixed com_invoke(int module, string handler_name [, mixed arg [, ...]]) Invokes a COM module */ void php3_COM_invoke(INTERNAL_FUNCTION_PARAMETERS) { pval **arguments; IDispatch *i_dispatch; int type; DISPID dispid; HRESULT hr; OLECHAR *funcname; char *error_message; VARIANTARG *variant_args; int arg_count = ARG_COUNT(ht), current_arg, current_variant; DISPPARAMS dispparams; VARIANTARG var_result; if (arg_count<2) { WRONG_PARAM_COUNT; } arguments = (pval **) emalloc(sizeof(pval *) * arg_count); if (getParametersArray(ht, arg_count, arguments)==FAILURE) { efree(arguments); RETURN_FALSE; } /* obtain i_dispatch interface */ convert_to_long(arguments[0]); i_dispatch = php3_list_find(arguments[0]->value.lval,&type); if (!i_dispatch || (type!=le_idispatch)) { php3_error(E_WARNING,"%d is not a COM object handler", arguments[0]->value.lval); efree(arguments); RETURN_FALSE; } /* obtain property/method handler */ convert_to_string(arguments[1]); funcname = _php3_C_str_to_unicode(arguments[1]->value.str.val, arguments[1]->value.str.len); hr = i_dispatch->lpVtbl->GetIDsOfNames(i_dispatch, &IID_NULL, &funcname, 1, LOCALE_SYSTEM_DEFAULT, &dispid); if (FAILED(hr)) { error_message = _php3_COM_error_message(hr); php3_error(E_WARNING,"Unable to lookup %s: %s\n", arguments[1]->value.str.val, error_message); LocalFree(error_message); efree(funcname); efree(arguments); RETURN_FALSE; } arg_count -= 2; variant_args = (VARIANTARG *) emalloc(sizeof(VARIANTARG)*arg_count); for (current_arg=0; current_arg<arg_count; current_arg++) { current_variant = arg_count - current_arg - 1; _php3_pval_to_variant(arguments[2+current_arg], &variant_args[current_variant]); } dispparams.rgvarg = variant_args; dispparams.rgdispidNamedArgs = NULL; dispparams.cArgs = arg_count; dispparams.cNamedArgs = 0; hr = i_dispatch->lpVtbl->Invoke(i_dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparams, &var_result, NULL, 0); if (FAILED(hr)) { error_message = _php3_COM_error_message(hr); php3_error(E_WARNING,"Invoke() failed: %s\n", error_message); LocalFree(error_message); efree(funcname); efree(arguments); efree(variant_args); RETURN_FALSE; } _php3_variant_to_pval(&var_result, return_value); efree(arguments); efree(variant_args); efree(funcname); }
/* {{{ proto string ora_columntype(int cursor, int column) Get the type of an Oracle result column */ void php3_Ora_ColumnType(INTERNAL_FUNCTION_PARAMETERS) { /* cursor_index, column_index */ pval *argv[2]; int cursor_ind, colno; oraCursor *cursor = NULL; if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(argv[0]); /* don't convert the column index yet, it might be the column name */ cursor_ind = argv[0]->value.lval; /* Find the cursor */ if ((cursor = ora_get_cursor(list, cursor_ind)) == NULL) { RETURN_FALSE; } convert_to_long(argv[1]); colno = argv[1]->value.lval; if (cursor->ncols == 0){ php3_error(E_WARNING, "No tuples available at this cursor index"); RETURN_FALSE; } if (colno >= cursor->ncols){ php3_error(E_WARNING, "Column index larger than number of columns"); RETURN_FALSE; } if (colno < 0){ php3_error(E_WARNING, "Column numbering starts at 0"); RETURN_FALSE; } switch (cursor->columns[colno].dbtype) { case SQLT_CHR: RETURN_STRINGL("VARCHAR2", 8, 1); case SQLT_VCS: case SQLT_AVC: RETURN_STRINGL("VARCHAR", 7, 1); case SQLT_STR: case SQLT_AFC: RETURN_STRINGL("CHAR", 4, 1); case SQLT_NUM: case SQLT_INT: case SQLT_FLT: case SQLT_UIN: RETURN_STRINGL("NUMBER", 6, 1); case SQLT_LNG: RETURN_STRINGL("LONG", 4, 1); case SQLT_LBI: RETURN_STRINGL("LONG RAW", 8, 1); case SQLT_RID: RETURN_STRINGL("ROWID", 5, 1); case SQLT_DAT: RETURN_STRINGL("DATE", 4, 1); #ifdef SQLT_CUR case SQLT_CUR: RETURN_STRINGL("CURSOR", 6, 1); #endif default: { char numbuf[21]; snprintf(numbuf, 20, "UNKNOWN(%d)", cursor->columns[colno].dbtype); numbuf[20] = '\0'; RETVAL_STRING(numbuf,1); } } }
/* {{{ proto mixed ora_getcolumn(int cursor, int column) Get data from a fetched row */ void php3_Ora_GetColumn(INTERNAL_FUNCTION_PARAMETERS) { /* cursor_index, column_index */ pval *argv[2]; int colno; oraCursor *cursor = NULL; oraColumn *column = NULL; sb2 type; if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(argv[0]); /* Find the cursor */ if ((cursor = ora_get_cursor(list, argv[0]->value.lval)) == NULL) { RETURN_FALSE; } if (cursor->ncols == 0){ php3_error(E_WARNING, "No tuples available at this cursor index"); RETURN_FALSE; } convert_to_long(argv[1]); colno = argv[1]->value.lval; if (colno >= cursor->ncols){ php3_error(E_WARNING, "Column index larger than number of columns"); RETURN_FALSE; } if (colno < 0){ php3_error(E_WARNING, "Column numbering starts at 0"); RETURN_FALSE; } if (cursor->fetched == 0){ if (ofetch(&cursor->cda)) { if (cursor->cda.rc != NO_DATA_FOUND) { php3_error(E_WARNING, "Ora_Fetch failed (%s)", ora_error(&cursor->cda)); } RETURN_FALSE; } cursor->fetched++; } column = &cursor->columns[colno]; type = column->dbtype; if (column->col_retcode != 0 && column->col_retcode != 1406) { /* So error fetching column. The most common is 1405, a NULL * was retreived. 1406 is ASCII or string buffer data was * truncated. The converted data from the database did not fit * into the buffer. Since we allocated the buffer to be large * enough, this should not occur. Anyway, we probably want to * return what we did get, in that case */ RETURN_FALSE; } else { switch(type) { case SQLT_CHR: case SQLT_NUM: case SQLT_INT: case SQLT_FLT: case SQLT_STR: case SQLT_UIN: case SQLT_AFC: case SQLT_AVC: case SQLT_DAT: RETURN_STRINGL(column->buf, min(column->col_retlen, column->dsize), 1); case SQLT_LNG: case SQLT_LBI: #if 0 { ub4 ret_len; /* XXX 64k max for LONG and LONG RAW */ oflng(&cursor->cda, (sword)(colno + 1), column->buf, DB_SIZE, 1, &ret_len, 0); RETURN_STRINGL(column->buf, ret_len, 1); } #else { ub4 ret_len; int offset = column->col_retlen; sb2 result; if (column->col_retcode == 1406) { /* truncation -> get the rest! */ while (1) { column->buf = erealloc(column->buf,offset + DB_SIZE + 1); if (! column->buf) { offset = 0; break; } result = oflng(&cursor->cda, (sword)(colno + 1), column->buf + offset, DB_SIZE, 1, &ret_len, offset); if (result) { break; } if (ret_len <= 0) { break; } offset += ret_len; } } if (column->buf && offset) { RETURN_STRINGL(column->buf, offset, 1); } else { RETURN_FALSE; } } #endif default: php3_error(E_WARNING, "Ora_GetColumn found invalid type (%d)", type); RETURN_FALSE; } } }
static value_t* cljc_core_apply (int nargs, environment_t *env, value_t *f, value_t *arg1, value_t *arg2, value_t *argrest) { int ndirect = 0; int nrest; assert (nargs > 1); switch (nargs) { case 2: ndirect = 0; argrest = arg1; break; case 3: ndirect = 1; argrest = arg2; break; default: ndirect = 2; argrest = ARG_FLATTEN_TAIL (argrest); break; } nrest = ARG_COUNT (argrest); switch (ndirect) { case 0: switch (nrest) { case 0: arg1 = arg2 = argrest = VALUE_NONE; break; case 1: arg1 = ARG_FIRST (argrest); arg2 = argrest = VALUE_NONE; break; case 2: arg1 = ARG_FIRST (argrest); argrest = ARG_NEXT (argrest); arg2 = ARG_FIRST (argrest); argrest = VALUE_NONE; break; default: arg1 = ARG_FIRST (argrest); argrest = ARG_NEXT (argrest); arg2 = ARG_FIRST (argrest); argrest = ARG_NEXT (argrest); break; } break; case 1: switch (nrest) { case 0: arg2 = argrest = VALUE_NONE; break; case 1: arg2 = ARG_FIRST (argrest); argrest = VALUE_NONE; break; default: arg2 = ARG_FIRST (argrest); argrest = ARG_NEXT (argrest); break; } break; case 2: switch (nrest) { case 0: argrest = VALUE_NONE; break; default: argrest = argrest; break; } break; default: assert (false); } return invoken (f, ndirect + nrest, arg1, arg2, argrest); }
/* {{{ proto int ora_bind(int cursor, string php_variable_name, string sql_parameter_name, int length [, int type]) Bind a PHP variable to an Oracle parameter */ void php3_Ora_Bind(INTERNAL_FUNCTION_PARAMETERS) { /* cursor_ind, php_var_name, sql_var_name, data_len [, inout]*/ /* inout: 0 = in/out, 1 = in, 2 = out */ int argc; pval *argv[5]; oraParam *newparam, *paramptr; oraCursor *cursor; char *paramname; argc = ARG_COUNT(ht); if (argc < 4 || argc > 5 || getParametersArray(ht, argc, argv) == FAILURE){ WRONG_PARAM_COUNT; } convert_to_long(argv[0]); convert_to_string(argv[1]); convert_to_string(argv[2]); convert_to_long(argv[3]); cursor = ora_get_cursor(list, argv[0]->value.lval); if (cursor == NULL) { php3_error(E_WARNING, "Invalid cursor index %d", argv[0]->value.lval); RETURN_FALSE; } if(cursor->params == NULL){ cursor->params = (HashTable *)emalloc(sizeof(HashTable)); if (!cursor->params || _php3_hash_init(cursor->params, 19, NULL, HASH_DTOR pval_ora_param_destructor, 0) == FAILURE) { php3_error(E_ERROR, "Unable to initialize parameter list"); RETURN_FALSE; } } if((newparam = (oraParam *)emalloc(sizeof(oraParam))) == NULL){ php3_error(E_WARNING, "Out of memory for parameter"); RETURN_FALSE; } if((paramname = estrndup(argv[1]->value.str.val, argv[1]->value.str.len)) == NULL){ php3_error(E_WARNING, "Out of memory for parametername"); efree(newparam); RETURN_FALSE; } if (_php3_hash_add(cursor->params, paramname, argv[1]->value.str.len + 1, newparam, sizeof(oraParam), (void **)¶mptr) == FAILURE) { /* XXX _php3_hash_destroy */ efree(paramname); efree(newparam); php3_error(E_ERROR, "Could not make parameter placeholder"); RETURN_FALSE; } efree(newparam); efree(paramname); paramptr->progvl = argv[3]->value.lval + 1; if(argc > 4){ convert_to_long(argv[4]); paramptr->inout = (short)argv[4]->value.lval; }else{ paramptr->inout = 0; } if((paramptr->progv = (text *)emalloc(paramptr->progvl)) == NULL){ php3_error(E_WARNING, "Out of memory for parameter value"); RETURN_FALSE; } /* XXX Maximum for progvl */ paramptr->alen = paramptr->progvl; if (obndra(&cursor->cda, argv[2]->value.str.val, -1, (ub1 *)paramptr->progv, paramptr->progvl, SQLT_STR, /* ftype */ -1, /* scale */ 0/*¶mptr->ind*/, /* ind */ ¶mptr->alen, /* alen */ 0 /*¶mptr->arcode*/, 0, /* maxsize */ 0, 0, -1, -1)) { php3_error(E_WARNING, "Ora_Bind failed (%s)", ora_error(&cursor->cda)); RETURN_FALSE; } cursor->nparams++; RETURN_TRUE; }
/* {{{ proto int ora_do(int connection, int cursor) Parse and execute a statement and fetch first result row */ void php3_Ora_Do(INTERNAL_FUNCTION_PARAMETERS) { pval *argv[2]; oraConnection *conn = NULL; oraCursor *cursor = NULL; text *query; if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(argv[0]); convert_to_string(argv[1]); conn = ora_get_conn(list,plist, argv[0]->value.lval); if (conn == NULL) { RETURN_FALSE; } if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){ php3_error(E_WARNING, "Out of memory"); RETURN_FALSE; } memset(cursor, 0, sizeof(oraCursor)); query = (text *) estrndup(argv[1]->value.str.val,argv[1]->value.str.len); if (query == NULL) { php3_error(E_WARNING, "Invalid query in Ora_Do"); RETURN_FALSE; } cursor->query = query; if (oopen(&cursor->cda, &conn->lda, (text *) 0, -1, -1, (text *) 0, -1)) { php3_error(E_WARNING, "Unable to open new cursor (%s)", ora_error(&cursor->cda)); efree(cursor); RETURN_FALSE; } cursor->open = 1; cursor->conn_ptr = conn; cursor->conn_id = argv[0]->value.lval; /* Prepare stmt */ if (oparse(&cursor->cda, query, (sb4) - 1, 1, VERSION_7)){ php3_error(E_WARNING, "Ora_Do failed (%s)", ora_error(&cursor->cda)); _close_oracur(cursor); RETURN_FALSE; } /* Execute stmt (and fetch 1st row for selects) */ if (cursor->cda.ft == FT_SELECT) { if (ora_describe_define(cursor) < 0){ /* error message is given by ora_describe_define() */ _close_oracur(cursor); RETURN_FALSE; } if (oexfet(&cursor->cda, 1, 0, 0)) { php3_error(E_WARNING, "Ora_Do failed (%s)", ora_error(&cursor->cda)); _close_oracur(cursor); RETURN_FALSE; } cursor->fetched = 1; } else { if (oexec(&cursor->cda)) { php3_error(E_WARNING, "Ora_Do failed (%s)", ora_error(&cursor->cda)); _close_oracur(cursor); RETURN_FALSE; } } RETURN_RESOURCE(ora_add_cursor(list, cursor)); }
/* {{{ proto int ora_fetch_into(int cursor, array result [, int flags]) Fetch a row into the specified result array */ void php3_Ora_FetchInto(INTERNAL_FUNCTION_PARAMETERS) { pval *arg1, *arr, *flg, *tmp; oraCursor *cursor; int i; int flags = 0; switch(ARG_COUNT(ht)){ case 2: if (getParameters(ht, 2, &arg1, &arr) == FAILURE) { WRONG_PARAM_COUNT; } break; case 3: if (getParameters(ht, 3, &arg1, &arr, &flg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(flg); flags = flg->value.lval; break; default: WRONG_PARAM_COUNT; break; } if (!ParameterPassedByReference(ht, 2)){ php3_error(E_WARNING, "Array not passed by reference in call to ora_fetch_into()"); RETURN_FALSE; } convert_to_long(arg1); /* Find the cursor */ if ((cursor = ora_get_cursor(list, arg1->value.lval)) == NULL) { RETURN_FALSE; } if (cursor->ncols == 0){ php3_error(E_WARNING, "No tuples available on this cursor"); RETURN_FALSE; } if (ofetch(&cursor->cda)) { if (cursor->cda.rc != NO_DATA_FOUND) { php3_error(E_WARNING, "Ora_Fetch_Into failed (%s)", ora_error(&cursor->cda)); } RETURN_FALSE; } cursor->fetched++; if (arr->type != IS_ARRAY){ php3tls_pval_destructor(arr); if (array_init(arr) == FAILURE){ php3_error(E_WARNING, "Can't convert to type Array"); RETURN_FALSE; } } _php3_hash_internal_pointer_reset(arr->value.ht); #if PHP_API_VERSION < 19990421 tmp = emalloc(sizeof(pval)); #endif for (i = 0; i < cursor->ncols; i++) { if (cursor->columns[i].col_retcode == 1405) { if (!(flags&ORA_FETCHINTO_NULLS)){ continue; /* don't add anything for NULL columns, unless the calles wants it */ } else { tmp->value.str.val = empty_string; tmp->value.str.len = 0; } } else if (cursor->columns[i].col_retcode != 0 && cursor->columns[i].col_retcode != 1406) { /* So error fetching column. The most common is 1405, a NULL */ /* was retreived. 1406 is ASCII or string buffer data was */ /* truncated. The converted data from the database did not fit */ /* into the buffer. Since we allocated the buffer to be large */ /* enough, this should not occur. Anyway, we probably want to */ /* return what we did get, in that case */ RETURN_FALSE; } else { #if PHP_API_VERSION >= 19990421 MAKE_STD_ZVAL(tmp); #endif tmp->type = IS_STRING; tmp->value.str.len = 0; switch(cursor->columns[i].dbtype) { case SQLT_LNG: case SQLT_LBI: { ub4 ret_len; int offset = cursor->columns[i].col_retlen; sb2 result; if (cursor->columns[i].col_retcode == 1406) { /* truncation -> get the rest! */ while (1) { cursor->columns[i].buf = erealloc(cursor->columns[i].buf,offset + DB_SIZE + 1); if (! cursor->columns[i].buf) { offset = 0; break; } result = oflng(&cursor->cda, (sword)(i + 1), cursor->columns[i].buf + offset, DB_SIZE, 1, &ret_len, offset); if (result) { break; } if (ret_len <= 0) { break; } offset += ret_len; } } if (cursor->columns[i].buf && offset) { tmp->value.str.len = offset; } else { tmp->value.str.len = 0; } } break; default: tmp->value.str.len = min(cursor->columns[i].col_retlen, cursor->columns[i].dsize); break; } tmp->value.str.val = estrndup(cursor->columns[i].buf,tmp->value.str.len); } if (flags&ORA_FETCHINTO_ASSOC){ #if PHP_API_VERSION >= 19990421 _php3_hash_update(arr->value.ht, cursor->columns[i].cbuf, cursor->columns[i].cbufl+1, (void *) &tmp, sizeof(pval*), NULL); #else _php3_hash_update(arr->value.ht, cursor->columns[i].cbuf, cursor->columns[i].cbufl+1, (void *) tmp, sizeof(pval), NULL); #endif } else { #if PHP_API_VERSION >= 19990421 _php3_hash_index_update(arr->value.ht, i, (void *) &tmp, sizeof(pval*), NULL); #else _php3_hash_index_update(arr->value.ht, i, (void *) tmp, sizeof(pval), NULL); #endif } } #if PHP_API_VERSION < 19990421 efree(tmp); #endif RETURN_LONG(cursor->ncols); }