void open(const std::string& service, const std::string& username, const std::string& password, int mode) { if (OCIServerAttach(server, error, (text*) service.c_str(), service.length(), OCI_DEFAULT)) throw_error(); if (OCIAttrSet(svcctx, OCI_HTYPE_SVCCTX, server, 0, OCI_ATTR_SERVER, error)) throw_error(); if (OCIAttrSet(session, OCI_HTYPE_SESSION, (dvoid*) username.c_str(), username.length(), OCI_ATTR_USERNAME, error)) throw_error(); if (OCIAttrSet(session, OCI_HTYPE_SESSION, (dvoid*) password.c_str(), password.length(), OCI_ATTR_PASSWORD, error)) throw_error(); if (OCISessionBegin(svcctx, error, session, OCI_CRED_RDBMS, mode)) throw_error(); if (OCIAttrSet(svcctx, OCI_HTYPE_SVCCTX, session, 0, OCI_ATTR_SESSION, error)) throw_error(); }
static int _doConnect(T C, URL_T url, char** error) { #undef ERROR #define ERROR(e) do {*error = Str_dup(e); return false;} while (0) #define ORAERROR(e) do{ *error = Str_dup(OracleConnection_getLastError(e)); return false;} while(0) const char *database, *username, *password; const char *host = URL_getHost(url); int port = URL_getPort(url); if (! (username = URL_getUser(url))) if (! (username = URL_getParameter(url, "user"))) ERROR("no username specified in URL"); if (! (password = URL_getPassword(url))) if (! (password = URL_getParameter(url, "password"))) ERROR("no password specified in URL"); if (! (database = URL_getPath(url))) ERROR("no database specified in URL"); ++database; /* Create a thread-safe OCI environment with N' substitution turned on. */ if (OCIEnvCreate(&C->env, OCI_THREADED | OCI_OBJECT | OCI_NCHAR_LITERAL_REPLACE_ON, 0, 0, 0, 0, 0, 0)) ERROR("Create a OCI environment failed"); /* allocate an error handle */ if (OCI_SUCCESS != OCIHandleAlloc(C->env, (dvoid**)&C->err, OCI_HTYPE_ERROR, 0, 0)) ERROR("Allocating error handler failed"); /* server contexts */ if (OCI_SUCCESS != OCIHandleAlloc(C->env, (dvoid**)&C->srv, OCI_HTYPE_SERVER, 0, 0)) ERROR("Allocating server context failed"); /* allocate a service handle */ if (OCI_SUCCESS != OCIHandleAlloc(C->env, (dvoid**)&C->svc, OCI_HTYPE_SVCCTX, 0, 0)) ERROR("Allocating service handle failed"); StringBuffer_clear(C->sb); /* Oracle connect string is on the form: //host[:port]/service name */ if (host) { StringBuffer_append(C->sb, "//%s", host); if (port > 0) StringBuffer_append(C->sb, ":%d", port); StringBuffer_append(C->sb, "/%s", database); } else /* Or just service name */ StringBuffer_append(C->sb, "%s", database); /* Create a server context */ C->lastError = OCIServerAttach(C->srv, C->err, StringBuffer_toString(C->sb), StringBuffer_length(C->sb), 0); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); /* Set attribute server context in the service context */ C->lastError = OCIAttrSet(C->svc, OCI_HTYPE_SVCCTX, C->srv, 0, OCI_ATTR_SERVER, C->err); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCIHandleAlloc(C->env, (void**)&C->usr, OCI_HTYPE_SESSION, 0, NULL); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCIAttrSet(C->usr, OCI_HTYPE_SESSION, (dvoid *)username, (int)strlen(username), OCI_ATTR_USERNAME, C->err); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCIAttrSet(C->usr, OCI_HTYPE_SESSION, (dvoid *)password, (int)strlen(password), OCI_ATTR_PASSWORD, C->err); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCISessionBegin(C->svc, C->err, C->usr, OCI_CRED_RDBMS, OCI_DEFAULT); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); OCIAttrSet(C->svc, OCI_HTYPE_SVCCTX, C->usr, 0, OCI_ATTR_SESSION, C->err); return true; }
int main(int argc, char *argv[]) { OCIError *errhp = NULL; printf("stage4: Demonstrating OCI statement caching \n"); /* parse command line options */ parse_options(argc, argv); checkenv(envhp, OCIEnvCreate(&envhp, /* returned env handle */ OCI_THREADED, /* initialization modes */ NULL, NULL, NULL, NULL, /* callbacks, context */ (size_t) 0, /* extra memory size: optional */ (void **) NULL)); /* returned extra memory */ /* allocate error handle * note: for OCIHandleAlloc(), we always check error on environment handle */ checkenv(envhp, OCIHandleAlloc(envhp, /* environment handle */ (void **) &errhp, /* returned err handle */ OCI_HTYPE_ERROR,/*type of handle to allocate*/ (size_t) 0, /* extra memory size: optional */ (void **) NULL)); /* returned extra memory */ create_session_pool(envhp, errhp, &poolName, &poolNameLen); /* allocate auth handle * note: for OCIHandleAlloc(), we check error on environment handle */ checkenv(envhp, OCIHandleAlloc(envhp, (void **) &authp, OCI_HTYPE_AUTHINFO, (size_t) 0, (void **) NULL)); /* setup username and password */ checkerr(errhp, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO, (void *) username, strlen((char *)username), OCI_ATTR_USERNAME, errhp)); checkerr(errhp, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO, apppassword, strlen((char *) apppassword), OCI_ATTR_PASSWORD, errhp)); spawn_threads(envhp, errhp, &thread_function); /* Destroy the session pool */ OCISessionPoolDestroy(spoolhp, errhp, OCI_DEFAULT); /* clean up */ if (authp) OCIHandleFree(authp, OCI_HTYPE_AUTHINFO); if (spoolhp) OCIHandleFree(spoolhp, OCI_HTYPE_SPOOL); if (errhp) OCIHandleFree(errhp, OCI_HTYPE_ERROR); if (envhp) OCIHandleFree(envhp, OCI_HTYPE_ENV); return 0; }
ocisession::ocisession(const char * connect_str, const int connect_str_len, const char * user_name, const int user_name_len, const char * password, const int password_len) { intf_ret r; OCIAuthInfo *authp = NULL; init(); r.handle = envhp; // allocate error handle checkenv(&r, OCIHandleAlloc((OCIEnv*)envhp, /* environment handle */ (void **) &_errhp, /* returned err handle */ OCI_HTYPE_ERROR, /* typ of handle to allocate */ (size_t) 0, /* optional extra memory size */ (void **) NULL)); /* returned extra memeory */ if(r.fn_ret != SUCCESS) { REMOTE_LOG("failed OCISessionGet %s\n", r.gerrbuf); throw r; } // allocate auth handle checkenv(&r, OCIHandleAlloc((OCIEnv*)envhp, (void**)&authp, OCI_HTYPE_AUTHINFO, (size_t)0, (void **) NULL)); r.handle = _errhp; // usrname and password checkerr(&r, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO, (void*) user_name, user_name_len, OCI_ATTR_USERNAME, (OCIError *)_errhp)); checkerr(&r, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO, (void*) password, password_len, OCI_ATTR_PASSWORD, (OCIError *)_errhp)); /* get the database connection */ checkerr(&r, OCISessionGet((OCIEnv*)envhp, (OCIError *)_errhp, (OCISvcCtx**)&_svchp, /* returned database connection */ authp, /* initialized authentication handle */ (OraText *) connect_str, connect_str_len,/* connect string */ NULL, 0, NULL, NULL, NULL, /* session tagging parameters: optional */ OCI_SESSGET_STMTCACHE)); /* modes */ if(r.fn_ret != SUCCESS) { REMOTE_LOG("failed OCISessionGet %s\n", r.gerrbuf); throw r; } (void) OCIHandleFree(authp, OCI_HTYPE_AUTHINFO); REMOTE_LOG("got session %p\n", _svchp); _sessions.push_back(this); }
static void bind_string_post_bind_hook(oci8_bind_t *obind) { oci8_bind_string_t *obs = (oci8_bind_string_t *)obind; if (obs->charlen != 0) { chker2(OCIAttrSet(obind->base.hp.ptr, obind->base.type, (void*)&obs->charlen, 0, OCI_ATTR_MAXCHAR_SIZE, oci8_errhp), &obind->base); } chker2(OCIAttrSet(obind->base.hp.ptr, obind->base.type, (void*)&obs->csfrm, 0, OCI_ATTR_CHARSET_FORM, oci8_errhp), &obind->base); }
// Begins an Oracle session. void OraSession::startSession(const std::string& sConnectAsUser, const std::string& sConnectAsUserPwd, const std::string& sConnectDBLink) { dvoid* tmp; sword status; status = OCIInitialize((ub4) OCI_THREADED | OCI_OBJECT, 0, 0, 0, 0); status = OCIHandleAlloc((dvoid *)NULL, (dvoid **)&ociHandles.envhp, (ub4)OCI_HTYPE_ENV, 52, (dvoid **)&tmp); status = OCIEnvInit(&ociHandles.envhp, (ub4)OCI_DEFAULT, 21, (dvoid **)&tmp); status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.errhp, (ub4)OCI_HTYPE_ERROR, 52, (dvoid **)&tmp); status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.srvhp, (ub4)OCI_HTYPE_SERVER, 52, (dvoid **)&tmp); if(checkerr(&ociHandles, OCIServerAttach(ociHandles.srvhp, ociHandles.errhp, (text *)sConnectDBLink.c_str(), (sb4)sConnectDBLink.length(), (ub4)OCI_DEFAULT)) != 0) { std::cout << "OCIServerAttach failed." << std::endl << std::endl; return; } status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.svchp, (ub4)OCI_HTYPE_SVCCTX, 52, (dvoid **)&tmp); /* set attribute server context in the service context */ status = OCIAttrSet((dvoid *) ociHandles.svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)ociHandles.srvhp, (ub4)0, (ub4)OCI_ATTR_SERVER, (OCIError *)ociHandles.errhp); /* allocate a user context handle */ status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.usrhp, (ub4)OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0); status = OCIAttrSet((dvoid *)ociHandles.usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)sConnectAsUser.c_str(), (ub4)sConnectAsUser.length(), OCI_ATTR_USERNAME, ociHandles.errhp); status = OCIAttrSet((dvoid *)ociHandles.usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)sConnectAsUserPwd.c_str(), (ub4)sConnectAsUserPwd.length(), OCI_ATTR_PASSWORD, ociHandles.errhp); if(checkerr(&ociHandles, OCISessionBegin(ociHandles.svchp, ociHandles.errhp, ociHandles.usrhp, OCI_CRED_RDBMS, OCI_DEFAULT)) != 0) { std::cout << "OCISessionBegin failed." << std::endl << std::endl; return; } status = OCIAttrSet((dvoid *)ociHandles.svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)ociHandles.usrhp, (ub4)0, OCI_ATTR_SESSION, ociHandles.errhp); status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.stmthp, (ub4)OCI_HTYPE_STMT, 50, (dvoid **)&tmp); connectedUser = sConnectAsUser; }
sword ConnectToDb(const char* const uid, const char* const pwd) { sword orc; /* allocate Server and Authentication (Session) handles */ orc = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, (ub4) OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0); orc = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &authp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0); /* attach to the server */ orc = OCIServerAttach(srvhp, errhp, (text *) 0, 0, (ub4) OCI_DEFAULT); orc = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) uid, (ub4) strlen((char *)uid), (ub4) OCI_ATTR_USERNAME, errhp); orc = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) pwd, (ub4) strlen((char *)pwd), (ub4) OCI_ATTR_PASSWORD, errhp); /* set the server attribute in the service context */ orc = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER, errhp); /* log on */ orc = OCISessionBegin(svchp, errhp, authp, (ub4) OCI_CRED_RDBMS, (ub4) OCI_DEFAULT); CheckErr(errhp, orc); /* set the session attribute in the service context */ orc = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) authp, (ub4) 0, (ub4) OCI_ATTR_SESSION, errhp); return (orc); } /* ConnectToDb */
static void bind_lob_post_bind_hook_for_nclob(oci8_bind_t *obind) { ub1 csfrm = SQLCS_NCHAR; chker2(OCIAttrSet(obind->base.hp.ptr, obind->base.type, (void*)&csfrm, 0, OCI_ATTR_CHARSET_FORM, oci8_errhp), &obind->base); }
/* * call-seq: * session_begin(cred, mode) * * <b>internal use only</b> * * Begins the session by the OCI function OCISessionBegin(). */ static VALUE oci8_session_begin(VALUE self, VALUE cred, VALUE mode) { oci8_svcctx_t *svcctx = DATA_PTR(self); if (svcctx->logoff_strategy != &complex_logoff) { rb_raise(rb_eRuntimeError, "Use this method only for the service context handle created by OCI8#server_handle()."); } if (svcctx->state & OCI8_STATE_SESSION_BEGIN_WAS_CALLED) { rb_raise(rb_eRuntimeError, "Could not use this method twice."); } /* check arguments */ Check_Type(cred, T_FIXNUM); Check_Type(mode, T_FIXNUM); /* begin session */ oci_lc(OCISessionBegin_nb(svcctx, svcctx->base.hp.ptr, oci8_errhp, svcctx->usrhp, FIX2UINT(cred), FIX2UINT(mode))); oci_lc(OCIAttrSet(svcctx->base.hp.ptr, OCI_HTYPE_SVCCTX, svcctx->usrhp, 0, OCI_ATTR_SESSION, oci8_errhp)); svcctx->state |= OCI8_STATE_SESSION_BEGIN_WAS_CALLED; return Qnil; }
boolean OCI_API OCI_MsgSetSender ( OCI_Msg *msg, OCI_Agent *sender ) { boolean res = TRUE; OCI_CHECK_PTR(OCI_IPC_MSG, msg, FALSE); OCI_CALL2 ( res, msg->typinf->con, OCIAttrSet((dvoid *) msg->proph, (ub4 ) OCI_DTYPE_AQMSG_PROPERTIES, (dvoid *) (sender ? sender->handle : NULL), (ub4 ) 0, (ub4 ) OCI_ATTR_SENDER_ID, msg->typinf->con->err) ) OCI_RESULT(res); return res; }
boolean OCI_API OCI_DequeueSetVisibility ( OCI_Dequeue *dequeue, unsigned int visibility ) { boolean res = TRUE; ub4 value = (ub4) visibility; OCI_CHECK_PTR(OCI_IPC_DEQUEUE, dequeue, FALSE); OCI_CALL2 ( res, dequeue->typinf->con, OCIAttrSet((dvoid *) dequeue->opth, (ub4 ) OCI_DTYPE_AQDEQ_OPTIONS, (dvoid *) &value, (ub4 ) 0, (ub4 ) OCI_ATTR_VISIBILITY, dequeue->typinf->con->err) ) OCI_RESULT(res); return res; }
boolean OCI_API OCI_MsgSetPriority ( OCI_Msg *msg, int value ) { boolean res = TRUE; sb4 sval = (sb4) value; OCI_CHECK_PTR(OCI_IPC_MSG, msg, FALSE); OCI_CALL2 ( res, msg->typinf->con, OCIAttrSet((dvoid *) msg->proph, (ub4 ) OCI_DTYPE_AQMSG_PROPERTIES, (dvoid *) &sval, (ub4 ) sizeof(sval), (ub4 ) OCI_ATTR_PRIORITY, msg->typinf->con->err) ) OCI_RESULT(res); return res; }
boolean OCI_API OCI_DequeueSetNavigation ( OCI_Dequeue *dequeue, unsigned int position ) { boolean res = TRUE; ub4 value = (ub4) position; OCI_CHECK_PTR(OCI_IPC_DEQUEUE, dequeue, FALSE); OCI_CALL2 ( res, dequeue->typinf->con, OCIAttrSet((dvoid *) dequeue->opth, (ub4 ) OCI_DTYPE_AQDEQ_OPTIONS, (dvoid *) &value, (ub4 ) 0, (ub4 ) OCI_ATTR_NAVIGATION, dequeue->typinf->con->err) ) OCI_RESULT(res); return res; }
boolean OCI_API OCI_DequeueSetMode ( OCI_Dequeue *dequeue, unsigned int mode ) { boolean res = TRUE; ub4 value = (ub4) mode; OCI_CHECK_PTR(OCI_IPC_DEQUEUE, dequeue, FALSE); OCI_CALL2 ( res, dequeue->typinf->con, OCIAttrSet((dvoid *) dequeue->opth, (ub4 ) OCI_DTYPE_AQDEQ_OPTIONS, (dvoid *) &value, (ub4 ) 0, (ub4 ) OCI_ATTR_DEQ_MODE, dequeue->typinf->con->err) ) OCI_RESULT(res); return res; }
boolean OCI_API OCI_DequeueSetWaitTime ( OCI_Dequeue *dequeue, int timeout ) { boolean res = TRUE; sb4 value = (ub4) timeout; OCI_CHECK_PTR(OCI_IPC_DEQUEUE, dequeue, FALSE); OCI_CALL2 ( res, dequeue->typinf->con, OCIAttrSet((dvoid *) dequeue->opth, (ub4 ) OCI_DTYPE_AQDEQ_OPTIONS, (dvoid *) &value, (ub4 ) 0, (ub4 ) OCI_ATTR_WAIT, dequeue->typinf->con->err) ) OCI_RESULT(res); return res; }
/* * call-seq: * client_info = string or nil * * <b>(new in 2.0.3)</b> * * Sets additional information about the client application. * This information is stored in the V$SESSION view. * * === Oracle 10g client or upper * * This doesn't perform network round trips. The change is reflected * to the server by the next round trip such as OCI8#exec, OCI8#ping, * etc. * * === Oracle 9i client or lower * * This executes the following PL/SQL block internally. * The change is reflected immediately by a network round trip. * * BEGIN * DBMS_APPLICATION_INFO.SET_CLIENT_INFO(:client_info); * END; * * See {Oracle Manual: Oracle Database PL/SQL Packages and Types Reference}[http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_appinf.htm#CHEJCFGG] */ static VALUE oci8_set_client_info(VALUE self, VALUE val) { char *ptr; ub4 size; if (!NIL_P(val)) { OCI8SafeStringValue(val); ptr = RSTRING_PTR(val); size = RSTRING_LEN(val); } else { ptr = ""; size = 0; } if (oracle_client_version >= ORAVER_10_1) { /* Oracle 10g or upper */ oci_lc(OCIAttrSet(oci8_get_oci_session(self), OCI_HTYPE_SESSION, ptr, size, OCI_ATTR_CLIENT_INFO, oci8_errhp)); } else { /* Oracle 9i or lower */ oci8_exec_sql_var_t bind_vars[1]; /* :client_info */ bind_vars[0].valuep = ptr; bind_vars[0].value_sz = size; bind_vars[0].dty = SQLT_CHR; bind_vars[0].indp = NULL; bind_vars[0].alenp = NULL; oci8_exec_sql(oci8_get_svcctx(self), "BEGIN\n" " DBMS_APPLICATION_INFO.SET_CLIENT_INFO(:client_info);\n" "END;\n", 0, NULL, 1, bind_vars, 1); } return val; }
/* * call-seq: * server_attach(dbname, mode) * * <b>internal use only</b> * * Attachs to the server by the OCI function OCIServerAttach(). */ static VALUE oci8_server_attach(VALUE self, VALUE dbname, VALUE mode) { oci8_svcctx_t *svcctx = oci8_get_svcctx(self); if (svcctx->logoff_strategy != &complex_logoff) { rb_raise(rb_eRuntimeError, "Use this method only for the service context handle created by OCI8#server_handle()."); } if (svcctx->state & OCI8_STATE_SERVER_ATTACH_WAS_CALLED) { rb_raise(rb_eRuntimeError, "Could not use this method twice."); } /* check arguments */ if (!NIL_P(dbname)) { OCI8SafeStringValue(dbname); } Check_Type(mode, T_FIXNUM); /* attach to the server */ oci_lc(OCIServerAttach_nb(svcctx, svcctx->srvhp, oci8_errhp, NIL_P(dbname) ? NULL : RSTRING_ORATEXT(dbname), NIL_P(dbname) ? 0 : RSTRING_LEN(dbname), FIX2UINT(mode))); oci_lc(OCIAttrSet(svcctx->base.hp.ptr, OCI_HTYPE_SVCCTX, svcctx->srvhp, 0, OCI_ATTR_SERVER, oci8_errhp)); svcctx->state |= OCI8_STATE_SERVER_ATTACH_WAS_CALLED; return self; }
/* ** Execute an SQL statement. ** Return a Cursor object if the statement is a query, otherwise ** return the number of tuples affected by the statement. */ static int conn_execute (lua_State *L) { env_data *env; conn_data *conn = getconnection (L); const char *statement = luaL_checkstring (L, 2); sword status; ub4 prefetch = 0; ub4 iters; ub4 mode; ub2 type; OCIStmt *stmthp; /* get environment */ lua_rawgeti (L, LUA_REGISTRYINDEX, conn->env); if (!lua_isuserdata (L, -1)) luaL_error(L,LUASQL_PREFIX"invalid environment in connection!"); env = (env_data *)lua_touserdata (L, -1); /* statement handle */ ASSERT (L, OCIHandleAlloc ((dvoid *)env->envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0), conn->errhp); ASSERT (L, OCIAttrSet ((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT, (dvoid *)&prefetch, (ub4)0, (ub4)OCI_ATTR_PREFETCH_ROWS, conn->errhp), conn->errhp); ASSERT (L, OCIStmtPrepare (stmthp, conn->errhp, (text *)statement, (ub4) strlen(statement), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT), conn->errhp); /* statement type */ ASSERT (L, OCIAttrGet ((dvoid *)stmthp, (ub4) OCI_HTYPE_STMT, (dvoid *)&type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, conn->errhp), conn->errhp); if (type == OCI_STMT_SELECT) iters = 0; else iters = 1; if (conn->auto_commit) mode = OCI_COMMIT_ON_SUCCESS; else mode = OCI_DEFAULT; /* execute statement */ status = OCIStmtExecute (conn->svchp, stmthp, conn->errhp, iters, (ub4)0, (CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, mode); if (status && (status != OCI_NO_DATA)) { OCIHandleFree ((dvoid *)stmthp, OCI_HTYPE_STMT); return checkerr (L, status, conn->errhp); } if (type == OCI_STMT_SELECT) { /* create cursor */ return create_cursor (L, 1, conn, stmthp, statement); } else { /* return number of rows */ int rows_affected; ASSERT (L, OCIAttrGet ((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT, (dvoid *)&rows_affected, (ub4 *)0, (ub4)OCI_ATTR_ROW_COUNT, conn->errhp), conn->errhp); OCIHandleFree ((dvoid *)stmthp, OCI_HTYPE_STMT); lua_pushnumber (L, rows_affected); return 1; } }
boolean OCI_API OCI_MsgSetConsumers ( OCI_Msg *msg, OCI_Agent **consumers, unsigned int count ) { boolean res = TRUE; OCIAQAgent **handles = NULL; OCI_CHECK_PTR(OCI_IPC_MSG, msg, FALSE); /* allocate local array of OCIAQAgent handles if needed */ if ((consumers != NULL) && (count > 0)) { handles = (OCIAQAgent **) OCI_MemAlloc(OCI_IPC_ARRAY,sizeof(OCIAQAgent *), count, FALSE); if (handles != NULL) { unsigned int i; for(i = 0; i < count; i++) { handles[i] = consumers[i]->handle; } } } else { count = 0; } OCI_CALL2 ( res, msg->typinf->con, OCIAttrSet((dvoid *) msg->proph, (ub4 ) OCI_DTYPE_AQMSG_PROPERTIES, (dvoid *) handles, (ub4 ) count, (ub4 ) OCI_ATTR_RECIPIENT_LIST, msg->typinf->con->err) ) /* free local array of OCIAQAgent handles if needed */ if (handles != NULL) { OCI_FREE(handles); } OCI_RESULT(res); return res; }
bool oci_create_tns_seesion_pool(const unsigned char * connect_str, const int connect_str_len, const unsigned char * user_name, const int user_name_len, const unsigned char * password, const int password_len, const unsigned char * options, const int options_len) { function_success = SUCCESS; poolName = NULL; poolNameLen = 0; oci_free_session_pool(); /* allocate session pool handle * note: for OCIHandleAlloc() we check error on environment handle */ checkenv(envhp, OCIHandleAlloc(envhp, (void **)&spoolhp, OCI_HTYPE_SPOOL, (size_t) 0, (void **) NULL)); /* set the statement cache size for all sessions in the pool * note: this can also be set per session after obtaining the session from the pool */ checkerr(errhp, OCIAttrSet(spoolhp, OCI_HTYPE_SPOOL, &stmt_cachesize, 0, OCI_ATTR_SPOOL_STMTCACHESIZE, errhp)); checkerr(errhp, OCISessionPoolCreate(envhp, errhp, spoolhp, (OraText **) &poolName, &poolNameLen, (OraText *) connect_str, connect_str_len, POOL_MIN, POOL_MAX, POOL_INCR, (OraText*)user_name, user_name_len, /* h**o pool user specified */ (OraText*)password, password_len, /* h**o pool password specified */ OCI_SPC_STMTCACHE)); /* modes */ if(function_success != SUCCESS)return false; sprintf_s(session_pool_name, sizeof(session_pool_name), "%.*s", poolNameLen, (char *)poolName); ub1 spoolMode = OCI_SPOOL_ATTRVAL_NOWAIT; checkerr(errhp, OCIAttrSet(spoolhp, OCI_HTYPE_SPOOL, (void*)&spoolMode, sizeof(ub1), OCI_ATTR_SPOOL_GETMODE, errhp)); if(function_success != SUCCESS)return false; return true; }
OCI_Transaction * OCI_API OCI_TransactionCreate ( OCI_Connection *con, unsigned int timeout, unsigned int mode, OCI_XID *pxid ) { OCI_Item *item = NULL; OCI_Transaction *trans = NULL; boolean res = TRUE; OCI_CHECK_INITIALIZED(NULL); OCI_CHECK_PTR(OCI_IPC_CONNECTION, con, NULL); /* create transaction object */ item = OCI_ListAppend(con->trsns, sizeof(*trans)); if (item != NULL) { trans = (OCI_Transaction *) item->data; trans->con = con; trans->mode = mode; trans->timeout = timeout; trans->local = (pxid == NULL); /* allocate transaction handle */ if (res == TRUE) { res = (OCI_SUCCESS == OCI_HandleAlloc((dvoid *) trans->con->env, (dvoid **) (void *) &trans->htr, (ub4) OCI_HTYPE_TRANS, (size_t) 0, (dvoid **) NULL)); } /* set XID attribute for global transaction */ if (pxid != NULL) { memcpy(&trans->xid, pxid, sizeof(trans->xid)); OCI_CALL2 ( res, con, OCIAttrSet((dvoid *) trans->htr, (ub4) OCI_HTYPE_TRANS, (dvoid *) &trans->xid, (ub4) sizeof(trans->xid), (ub4) OCI_ATTR_XID, trans->con->err) ) }
/* * call-seq: * attr_set_binary(attr_type, attr_value) * * Sets the value of an attribute as `ub1 *' datatype. * * @param [Fixnum] attr_type * @param [String] attr_value * @return [self] * * @since 2.0.4 * @private */ static VALUE attr_set_binary(VALUE self, VALUE attr_type, VALUE val) { oci8_base_t *base = DATA_PTR(self); /* validate arguments */ Check_Type(attr_type, T_FIXNUM); SafeStringValue(val); /* set attribute */ chker2(OCIAttrSet(base->hp.ptr, base->type, RSTRING_PTR(val), RSTRING_LEN(val), FIX2INT(attr_type), oci8_errhp), base); return self; }
/* * call-seq: * attr_set_boolean(attr_type, attr_value) * * Sets the value of an attribute as `boolean' datatype. * * @note If the specified attr_type's datatype is a * pointer type, it causes a segmentation fault. * * @param [Fixnum] attr_type * @param [true or false] attr_value * @return [self] * * @since 2.0.4 * @private */ static VALUE attr_set_boolean(VALUE self, VALUE attr_type, VALUE val) { oci8_base_t *base = DATA_PTR(self); boolean value; /* validate arguments */ Check_Type(attr_type, T_FIXNUM); value = RTEST(val) ? TRUE : FALSE; /* set attribute */ chker2(OCIAttrSet(base->hp.ptr, base->type, &value, sizeof(value), FIX2INT(attr_type), oci8_errhp), base); return self; }
/* * call-seq: * attr_set_integer(attr_type, number) * * Sets the value of an attribute as `ub1 *' datatype. * +number+ is converted to internal Oracle NUMBER format before * it is set. * * @param [Fixnum] attr_type * @param [Numeric] number * @return [self] * * @since 2.0.4 * @private */ static VALUE attr_set_integer(VALUE self, VALUE attr_type, VALUE val) { oci8_base_t *base = DATA_PTR(self); OCINumber value; /* validate arguments */ Check_Type(attr_type, T_FIXNUM); oci8_set_integer(&value, val, oci8_errhp); /* set attribute */ chker2(OCIAttrSet(base->hp.ptr, base->type, &value, sizeof(value), FIX2INT(attr_type), oci8_errhp), base); return self; }
/* * call-seq: * attr_set_sb2(attr_type, attr_value) * * Sets the value of an attribute as `sb2' datatype. * * @note If the specified attr_type's datatype is a * pointer type, it causes a segmentation fault. * * @param [Fixnum] attr_type * @param [Fixnum] attr_value * @return [self] * * @since 2.0.4 * @private */ static VALUE attr_set_sb2(VALUE self, VALUE attr_type, VALUE val) { oci8_base_t *base = DATA_PTR(self); sb2 value; /* validate arguments */ Check_Type(attr_type, T_FIXNUM); value = (sb2)check_data_range(val, SHRT_MIN, SHRT_MAX, "sb2"); /* set attribute */ chker2(OCIAttrSet(base->hp.ptr, base->type, &value, sizeof(value), FIX2INT(attr_type), oci8_errhp), base); return self; }
/* * call-seq: * attr_set_ub8(attr_type, attr_value) * * Sets the value of an attribute as `ub8' datatype. * * @note If the specified attr_type's datatype is a * pointer type, it causes a segmentation fault. * * @param [Fixnum] attr_type * @param [Integer] attr_value * @return [self] * * @since 2.0.4 * @private */ static VALUE attr_set_ub8(VALUE self, VALUE attr_type, VALUE val) { oci8_base_t *base = DATA_PTR(self); ub8 value; /* validate arguments */ Check_Type(attr_type, T_FIXNUM); value = NUM2ULL(val); /* set attribute */ chker2(OCIAttrSet(base->hp.ptr, base->type, &value, sizeof(value), FIX2INT(attr_type), oci8_errhp), base); return self; }
/* * call-seq: * attr_set_ub1(attr_type, attr_value) * * <b>(new in 2.0.4)</b> * * Sets the value of an attribute as `ub1' datatype. * * <b>Caution:</b> If the specified attr_type's datatype is a * pointer type, it causes a segmentation fault. */ static VALUE attr_set_ub1(VALUE self, VALUE attr_type, VALUE val) { oci8_base_t *base = DATA_PTR(self); ub1 value; /* validate arguments */ Check_Type(attr_type, T_FIXNUM); value = (ub1)check_data_range(val, 0, UCHAR_MAX, "ub1"); /* set attribute */ oci_lc(OCIAttrSet(base->hp.ptr, base->type, &value, sizeof(value), FIX2INT(attr_type), oci8_errhp)); return self; }
int OracleConnection_beginTransaction(T C) { assert(C); if (C->txnhp == NULL) /* Allocate handler only once, if it is necessary */ { /* allocate transaction handle and set it in the service handle */ C->lastError = OCIHandleAlloc(C->env, (void **)&C->txnhp, OCI_HTYPE_TRANS, 0, 0); if (C->lastError != OCI_SUCCESS) return false; OCIAttrSet(C->svc, OCI_HTYPE_SVCCTX, (void *)C->txnhp, 0, OCI_ATTR_TRANS, C->err); } C->lastError = OCITransStart (C->svc, C->err, ORACLE_TRANSACTION_PERIOD, OCI_TRANS_NEW); return (C->lastError == OCI_SUCCESS); }
/*--------------------------------------------------------------------- * attach - Attach to XStream server specified in connection info *---------------------------------------------------------------------*/ static void attach_session(oci_t * ocip, conn_info_t *conn, boolean outbound) { sword err; ub4 mode = OCI_DEFAULT; ub4 ack_interval = 30; ub4 idle_tmout = 5; printf (">>> attach_session server=%.*s \n", conn->svrnmlen, conn->svrnm); OCICALL(ocip, OCIAttrSet((dvoid *) ocip->srvp, (ub4) OCI_HTYPE_SERVER, (dvoid *) &ack_interval, (ub4) 0, (ub4) OCI_ATTR_XSTREAM_ACK_INTERVAL, ocip->errp)); if (outbound) { OCICALL(ocip, OCIAttrSet((dvoid *) ocip->srvp, (ub4) OCI_HTYPE_SERVER, (dvoid *) &idle_tmout, (ub4) 0, (ub4) OCI_ATTR_XSTREAM_IDLE_TIMEOUT, ocip->errp)); OCICALL(ocip, OCIXStreamOutAttach(ocip->svcp, ocip->errp, conn->svrnm, (ub2)conn->svrnmlen, (ub1 *)0, 0, OCI_DEFAULT)); } else { OCICALL(ocip, OCIXStreamInAttach(ocip->svcp, ocip->errp, conn->svrnm, (ub2)conn->svrnmlen, (oratext *)"From_XOUT", 9, (ub1 *)0, 0, OCI_DEFAULT)); } ocip->attached = TRUE; ocip->outbound = outbound; printf (">>> attach_session -- Done. \n"); }
int db_init(char *servername, char *username, char *passwd) { OCIEnvCreate(&myenvhp, OCI_THREADED|OCI_OBJECT, (dvoid *)0, 0, 0, 0, (size_t)0, (dvoid **)0); OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&mysrvhp, OCI_HTYPE_SERVER, 0, (dvoid **)0); OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&myerrhp, OCI_HTYPE_ERROR, 0, (dvoid **)0); /* 服务名 */ status = OCIServerAttach(mysrvhp, myerrhp, (const OraText*)servername, strlen(servername), OCI_DEFAULT); if (status != OCI_SUCCESS && status != OCI_SUCCESS_WITH_INFO) { checkerr(status, myerrhp); OCIServerDetach(mysrvhp, myerrhp, OCI_DEFAULT); return -1; } OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&mysvchp, OCI_HTYPE_SVCCTX, 0, (dvoid **)0); OCIAttrSet((dvoid *)mysvchp, OCI_HTYPE_SVCCTX, (dvoid *)mysrvhp, (ub4)0, OCI_ATTR_SERVER, myerrhp); OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&myusrhp, OCI_HTYPE_SESSION, 0, (dvoid **)0); OCIAttrSet((dvoid *)myusrhp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen(username), OCI_ATTR_USERNAME, myerrhp); OCIAttrSet((dvoid *)myusrhp, OCI_HTYPE_SESSION, (dvoid *)passwd, (ub4)strlen(passwd), OCI_ATTR_PASSWORD, myerrhp); if(OCISessionBegin((dvoid *)mysvchp, myerrhp, myusrhp, OCI_CRED_RDBMS, OCI_DEFAULT) != OCI_SUCCESS) { DEBUG("Build the talking fail!\n"); OCIHandleFree((void *)myenvhp, OCI_HTYPE_ENV); return -1; } OCIAttrSet((dvoid *)mysvchp, OCI_HTYPE_SVCCTX, (dvoid *)myusrhp, (ub4)0, OCI_ATTR_SESSION, myerrhp); INFO("Oracle connect successful!\n"); return 0; }