📄 oracle.c
字号:
if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, arg, -1, "Oracle-Connection", le_conn, le_pconn); zend_list_delete(Z_LVAL_PP(arg)); RETURN_TRUE;}/* }}} *//* {{{ proto int ora_open(int connection) Open an Oracle cursor */PHP_FUNCTION(ora_open){ /* conn_index */ pval **arg; oraConnection *conn = NULL; oraCursor *cursor = NULL; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, arg, -1, "Oracle-Connection", le_conn, le_pconn); if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){ php_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)) { php_error(E_WARNING, "Unable to open new cursor (%s)", ora_error(&cursor->cda)); efree(cursor); RETURN_FALSE; } cursor->open = 1; cursor->conn_ptr = conn; ZEND_REGISTER_RESOURCE(return_value, cursor, le_cursor); cursor->conn_id = Z_LVAL_P(return_value);}/* }}} *//* {{{ proto int ora_close(int cursor) Close an Oracle cursor */PHP_FUNCTION(ora_close){ /* conn_index */ pval **arg; oraCursor *cursor; if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(cursor, oraCursor *, arg, -1, "Oracle-Cursor", le_cursor); zend_list_delete(Z_LVAL_PP(arg)); RETURN_TRUE;}/* }}} *//* {{{ proto int ora_commitoff(int connection) Disable automatic commit */PHP_FUNCTION(ora_commitoff){ /* conn_index */ pval **arg; oraConnection *conn; if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, arg, -1, "Oracle-Connection", le_conn, le_pconn); if (ocof(&conn->lda)) { php_error(E_WARNING, "Unable to turn off auto-commit (%s)", ora_error(&conn->lda)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto int ora_commiton(int connection) Enable automatic commit */PHP_FUNCTION(ora_commiton){ /* conn_index */ pval **arg; oraConnection *conn; if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, arg, -1, "Oracle-Connection", le_conn, le_pconn); if (ocon(&conn->lda)) { php_error(E_WARNING, "Unable to turn on auto-commit (%s)", ora_error(&conn->lda)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto int ora_commit(int connection) Commit an Oracle transaction */PHP_FUNCTION(ora_commit){ /* conn_index */ pval **arg; oraConnection *conn; if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, arg, -1, "Oracle-Connection", le_conn, le_pconn); if (ocom(&conn->lda)) { php_error(E_WARNING, "Unable to commit transaction (%s)", ora_error(&conn->lda)); RETURN_FALSE; } RETVAL_TRUE;}/* }}} *//* {{{ proto int ora_rollback(int connection) Roll back an Oracle transaction */PHP_FUNCTION(ora_rollback){ /* conn_index */ pval **arg; oraConnection *conn; if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, arg, -1, "Oracle-Connection", le_conn, le_pconn); if (orol(&conn->lda)) { php_error(E_WARNING, "Unable to roll back transaction (%s)", ora_error(&conn->lda)); RETURN_FALSE; } RETVAL_TRUE;}/* }}} *//* {{{ proto int ora_parse(int cursor, string sql_statement [, int defer]) Parse an Oracle SQL statement */PHP_FUNCTION(ora_parse){ pval **curs, **sql, **def; oraCursor *cursor; sword defer = 0; text *query; switch (ZEND_NUM_ARGS()) { case 3: zend_get_parameters_ex(3,&curs,&sql,&def); convert_to_long_ex(def); if (Z_LVAL_PP(def)) { defer = DEFER_PARSE; } break; case 2: zend_get_parameters_ex(2,&curs,&sql); break; default: WRONG_PARAM_COUNT; } convert_to_string_ex(sql); query = (text *) estrndup(Z_STRVAL_PP(sql),Z_STRLEN_PP(sql)); if (query == NULL) { php_error(E_WARNING, "Invalid query"); RETURN_FALSE; } if (!(cursor = ora_get_cursor(&EG(regular_list),curs TSRMLS_CC))){ efree(query); RETURN_FALSE; } if (cursor->query) { efree(cursor->query); } cursor->query = query; cursor->fetched = 0; if (cursor->params && cursor->nparams > 0){ zend_hash_destroy(cursor->params); efree(cursor->params); cursor->params = NULL; cursor->nparams = 0; } if (oparse(&cursor->cda, query, (sb4) - 1, defer, VERSION_7)) { php_error(E_WARNING, "Ora_Parse failed (%s)",ora_error(&cursor->cda)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ 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 */PHP_FUNCTION(ora_bind){ pval **curs, **pvar, **svar, **plen, **ptyp; int inout = 0; oraParam *newparam, *paramptr; oraCursor *cursor; char *paramname; switch (ZEND_NUM_ARGS()) { case 5: zend_get_parameters_ex(5,&curs,&pvar,&svar,&plen,&ptyp); convert_to_long_ex(ptyp); inout = Z_LVAL_PP(ptyp); break; case 4: zend_get_parameters_ex(4,&curs,&pvar,&svar,&plen); break; default: WRONG_PARAM_COUNT; } cursor = ora_get_cursor(&EG(regular_list), curs TSRMLS_CC); if (cursor == NULL) { RETURN_FALSE; } convert_to_string_ex(pvar); convert_to_string_ex(svar); convert_to_long_ex(plen); if (cursor->params == NULL) { ALLOC_HASHTABLE(cursor->params); if (!cursor->params || zend_hash_init(cursor->params, 19, NULL, HASH_DTOR pval_ora_param_destructor, 0) == FAILURE) { php_error(E_ERROR, "Unable to initialize parameter list"); RETURN_FALSE; } } if ((newparam = (oraParam *)emalloc(sizeof(oraParam))) == NULL) { php_error(E_WARNING, "Out of memory for parameter"); RETURN_FALSE; } if ((paramname = estrndup(Z_STRVAL_PP(pvar), Z_STRLEN_PP(pvar))) == NULL) { php_error(E_WARNING, "Out of memory for parametername"); efree(newparam); RETURN_FALSE; } if (zend_hash_add(cursor->params, paramname, Z_STRLEN_PP(pvar) + 1, newparam, sizeof(oraParam), (void **)¶mptr) == FAILURE) { /* XXX zend_hash_destroy */ efree(paramname); efree(newparam); php_error(E_ERROR, "Could not make parameter placeholder"); RETURN_FALSE; } efree(newparam); efree(paramname); paramptr->progvl = Z_LVAL_PP(plen) + 1; paramptr->inout = inout; if ((paramptr->progv = (text *)emalloc(paramptr->progvl)) == NULL) { php_error(E_WARNING, "Out of memory for parameter value"); RETURN_FALSE; }/* XXX Maximum for progvl */ paramptr->alen = paramptr->progvl; if (obndra(&cursor->cda, Z_STRVAL_PP(svar), -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)) { php_error(E_WARNING, "Ora_Bind failed (%s)", ora_error(&cursor->cda)); RETURN_FALSE; } cursor->nparams++; RETURN_TRUE;}/* }}} *//* XXX Make return values compatible with old module ? *//* {{{ proto int ora_exec(int cursor) Execute a parsed statement */PHP_FUNCTION(ora_exec){ /* cursor_index */ pval **arg; oraCursor *cursor = NULL; if (zend_get_parameters_ex(1, &arg) == FAILURE) WRONG_PARAM_COUNT; if ((cursor = ora_get_cursor(&EG(regular_list), arg TSRMLS_CC)) == NULL) { RETURN_FALSE; } if (cursor->cda.ft == FT_SELECT) { if (ora_describe_define(cursor) < 0) { /* error message is given by ora_describe_define() */ RETURN_FALSE; } } if(cursor->nparams > 0){ if(!ora_set_param_values(cursor, 0 TSRMLS_CC)){ RETURN_FALSE; } } if (oexec(&cursor->cda)) { php_error(E_WARNING, "Ora_Exec failed (%s)", ora_error(&cursor->cda)); RETURN_FALSE; } if(cursor->nparams > 0){ if(!ora_set_param_values(cursor, 1 TSRMLS_CC)){ RETURN_FALSE; } } RETURN_TRUE;}/* }}} *//* {{{ proto int ora_numcols(int cursor) Returns the numbers of columns in a result */PHP_FUNCTION(ora_numcols){ /* cursor_index */ pval **arg; oraCursor *cursor = NULL; if (zend_get_parameters_ex(1, &arg) == FAILURE) WRONG_PARAM_COUNT; if ((cursor = ora_get_cursor(&EG(regular_list), arg TSRMLS_CC)) == NULL) { RETURN_FALSE; } RETURN_LONG(cursor->ncols); }/* }}} *//* {{{ proto int ora_numrows(int cursor) Returns the number of rows in a result */PHP_FUNCTION(ora_numrows){ /* cursor_index */ pval **arg; oraCursor *cursor = NULL; if(zend_get_parameters_ex(1, &arg) == FAILURE) WRONG_PARAM_COUNT; if((cursor = ora_get_cursor(&EG(regular_list), arg TSRMLS_CC)) == NULL) { RETURN_FALSE; } RETURN_LONG(cursor->cda.rpc); }/* }}} *//* prepares/executes/fetches 1st row if avail*//* {{{ proto int ora_do(int connection, int cursor) Parse and execute a statement and fetch first result row */ PHP_FUNCTION(ora_do){ pval **con,**sql; oraConnection *conn = NULL; oraCursor *cursor = NULL; text *query; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &con,&sql) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE2(conn, oraConnection *, con, -1, "Oracle-Connection", le_conn, le_pconn); convert_to_string_ex(sql); if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){ php_error(E_WARNING, "Out of memory"); RETURN_FALSE; } memset(cursor, 0, sizeof(oraCursor)); query = (text *) estrndup(Z_STRVAL_PP(sql),Z_STRLEN_PP(sql)); if (query == NULL) { php_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)) { php_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 = Z_LVAL_PP(con); /* Prepare stmt */ if (oparse(&cursor->cda, query, (sb4) - 1, 1, VERSION_7)){ php_error(E_WARNING, "Ora_Do failed (%s)", ora_error(&cursor->cda)); _close_oracur(cursor TSRMLS_CC); 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 TSRMLS_CC); RETURN_FALSE; } if (oexfet(&cursor->cda, 1, 0, 0)) { php_error(E_WARNING, "Ora_Do failed (%s)", ora_error(&cursor->cda)); _close_oracur(cursor TSRMLS_CC); RETURN_FALSE; } cursor->fetched = 1; } else { if (oexec(&cursor->cda)) { php_error(E_WARNING, "Ora_Do failed (%s)", ora_error(&cursor->cda)); _close_oracur(cursor TSRMLS_CC); RETURN_FALSE; } } ZEND_REGISTER_RESOURCE(return_value, cursor, le_cursor);}/* }}} *//* {{{ proto int ora_fetch(int cursor) Fetch a row of result data from a cursor */PHP_FUNCTION(ora_fetch){ /* cursor_index */ pval **arg; oraCursor *cursor; if (zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } if ((cursor = ora_get_cursor(&EG(regular_list), arg TSRMLS_CC)) == NULL) { RETURN_FALSE; } if (cursor->ncols == 0){ php_error(E_WARNING, "No tuples available on this cursor"); RETURN_FALSE; } /* Get data from Oracle */ if (ofetch(&cursor->cda)) { if (cursor->cda.rc != NO_DATA_FOUND) { php_error(E_WARNING, "Ora_Fetch failed (%s)", ora_error(&cursor->cda)); } RETURN_FALSE; } cursor->fetched++; RETVAL_TRUE;}/* }}} *//* {{{ proto int ora_fetch_into(int cursor, array result [, int flags]) Fetch a row into the specified result array */PHP_FUNCTION(ora_fetch_into){ pval **curs, **arr, **flg, *tmp; oraCursor *cursor; int i; int flags = 0; switch(ZEND_NUM_ARGS()){ case 2: zend_get_parameters_ex(2, &curs, &arr); break; case 3: zend_get_parameters_ex(3, &curs, &arr, &flg); convert_to_long_ex(flg); flags = Z_LVAL_PP(flg); break; default: WRONG_PARAM_COUNT; break; } /* Find the cursor */ if ((cursor = ora_get_cursor(&EG(regular_list), curs TSRMLS_CC)) == NULL) { RETURN_FALSE; } if (cursor->ncols == 0){ php_error(E_WARNING, "No tuples available on this cursor"); RETURN_FALSE; } if (ofetch(&cursor->cda)) { if (cursor->cda.rc != NO_DATA_FOUND) { php_error(E_WARNING, "Ora_Fetch_Into failed (%s)",ora_error(&cursor->cda)); } RETURN_FALSE; } cursor->fetched++; if (Z_TYPE_PP(arr) != IS_ARRAY){ pval_destructor(*arr); if (array_init(*arr) == FAILURE){ php_error(E_WARNING, "Can't convert to type Array"); RETURN_FALSE; } } zend_hash_internal_pointer_reset(Z_ARRVAL_PP(arr)); 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 { MAKE_STD_ZVAL(tmp); ZVAL_NULL(tmp); } } 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 { MAKE_STD_ZVAL(tmp); Z_TYPE_P(tmp) = IS_STRING; Z_STRLEN_P(tmp) = 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -