/* * call-seq: * OCI8::ConnectionPool.new(conn_min, conn_max, conn_incr, username = nil, password = nil, dbname = nil) -> connection pool * OCI8::ConnectionPool.new(conn_min, conn_max, conn_incr, connect_string) -> connection pool * * Creates a connection pool. * * <i>conn_min</i> specifies the minimum number of connections in the * connection pool. Valid values are 0 and higher. * * <i>conn_max</i> specifies the maximum number of connections that * can be opened to the database. Once this value is reached, no more * connections are opened. Valid values are 1 and higher. * * <i>conn_incr</i> allows the application to set the next increment * for connections to be opened to the database if the current number * of connections are less than <i>conn_max</i>. Valid values are 0 * and higher. * * <i>username</i> and <i>password</i> are required to establish an * implicit primary session. When both are nil, external * authentication is used. * * <i>dbname</i> specifies the database server to connect to. * * If the number of arguments is four, <i>username</i>, * <i>password</i> and <i>dbname</i> are extracted from the fourth * argument <i>connect_string</i>. The syntax is "username/password" or * "username/password@dbname". */ static VALUE oci8_cpool_initialize(int argc, VALUE *argv, VALUE self) { VALUE conn_min; VALUE conn_max; VALUE conn_incr; VALUE username; VALUE password; VALUE dbname; oci8_cpool_t *cpool = DATA_PTR(self); OraText *pool_name; sb4 pool_name_len; sword rv; /* check arguments */ rb_scan_args(argc, argv, "42", &conn_min, &conn_max, &conn_incr, &username, &password, &dbname); Check_Type(conn_min, T_FIXNUM); Check_Type(conn_max, T_FIXNUM); Check_Type(conn_incr, T_FIXNUM); if (argc == 4) { VALUE mode; VALUE conn_str = username; OCI8SafeStringValue(conn_str); oci8_do_parse_connect_string(conn_str, &username, &password, &dbname, &mode); if (!NIL_P(mode)) { rb_raise(rb_eArgError, "invalid connect string \"%s\": Connection pooling doesn't support sysdba and sysoper privileges.", RSTRING_PTR(conn_str)); } } else { if (!NIL_P(username)) { OCI8SafeStringValue(username); } if (!NIL_P(password)) { OCI8SafeStringValue(password); } if (!NIL_P(dbname)) { OCI8SafeStringValue(dbname); } } rv = OCIHandleAlloc(oci8_envhp, &cpool->base.hp.ptr, OCI_HTYPE_CPOOL, 0, NULL); if (rv != OCI_SUCCESS) oci8_env_raise(oci8_envhp, rv); cpool->base.type = OCI_HTYPE_CPOOL; oci_lc(OCIConnectionPoolCreate(oci8_envhp, oci8_errhp, cpool->base.hp.poolhp, &pool_name, &pool_name_len, NIL_P(dbname) ? NULL : RSTRING_ORATEXT(dbname), NIL_P(dbname) ? 0 : RSTRING_LEN(dbname), FIX2UINT(conn_min), FIX2UINT(conn_max), FIX2UINT(conn_incr), NIL_P(username) ? NULL : RSTRING_ORATEXT(username), NIL_P(username) ? 0 : RSTRING_LEN(username), NIL_P(password) ? NULL : RSTRING_ORATEXT(password), NIL_P(password) ? 0 : RSTRING_LEN(password), OCI_DEFAULT)); cpool->pool_name = rb_str_new(TO_CHARPTR(pool_name), pool_name_len); rb_str_freeze(cpool->pool_name); return Qnil; }
/* * call-seq: * parse_connect_string(string) -> [username, password, dbname, privilege] * * Extracts +username+, +password+, +dbname+ and +privilege+ from the specified +string+. * * example: * "scott/tiger" -> ["scott", "tiger", nil, nil], * "scott/[email protected]" -> ["scott", "tiger", "oradb.example.com", nil] * "sys/change_on_install as sysdba" -> ["sys", "change_on_install", nil, :SYSDBA] * */ static VALUE oci8_parse_connect_string(VALUE self, VALUE conn_str) { VALUE user; VALUE pass; VALUE dbname; VALUE mode; oci8_do_parse_connect_string(conn_str, &user, &pass, &dbname, &mode); return rb_ary_new3(4, user, pass, dbname, mode); }
/* * call-seq: * new(username, password, dbname = nil, privilege = nil) * * Connects to an Oracle database server by +username+ and +password+ * at +dbname+ as +privilege+. * * === connecting to the local server * * Set +username+ and +password+ or pass "username/password" as a * single argument. * * OCI8.new('scott', 'tiger') * or * OCI8.new('scott/tiger') * * === connecting to a remote server * * Set +username+, +password+ and +dbname+ or pass * "username/password@dbname" as a single argument. * * OCI8.new('scott', 'tiger', 'orcl.world') * or * OCI8.new('scott/[email protected]') * * The +dbname+ is a net service name or an easy connectection * identifier. The former is a name listed in the file tnsnames.ora. * Ask to your DBA if you don't know what it is. The latter has the * syntax as "//host:port/service_name". * * OCI8.new('scott', 'tiger', '//remote-host:1521/XE') * or * OCI8.new('scott/tiger@//remote-host:1521/XE') * * === connecting as a privileged user * * Set :SYSDBA or :SYSOPER to +privilege+, otherwise * "username/password as sysdba" or "username/password as sysoper" * as a single argument. * * OCI8.new('sys', 'change_on_install', nil, :SYSDBA) * or * OCI8.new('sys/change_on_install as sysdba') * * === external OS authentication * * Set nil to +username+ and +password+, or "/" as a single argument. * * OCI8.new(nil, nil) * or * OCI8.new('/') * * To connect to a remote host: * * OCI8.new(nil, nil, 'dbname') * or * OCI8.new('/@dbname') * * === proxy authentication * * Enclose end user's username with square brackets and add it at the * end of proxy user's username. * * OCI8.new('proxy_user_name[end_user_name]', 'proxy_password') * or * OCI8.new('proxy_user_name[end_user_name]/proxy_password') * */ static VALUE oci8_svcctx_initialize(int argc, VALUE *argv, VALUE self) { VALUE vusername; VALUE vpassword; VALUE vdbname; VALUE vmode; oci8_svcctx_t *svcctx = DATA_PTR(self); sword rv; enum logon_type_t logon_type = T_IMPLICIT; ub4 cred = OCI_CRED_RDBMS; ub4 mode = OCI_DEFAULT; OCISvcCtx *svchp = NULL; svcctx->executing_thread = Qnil; if (argc == 1) { oci8_do_parse_connect_string(argv[0], &vusername, &vpassword, &vdbname, &vmode); } else { rb_scan_args(argc, argv, "22", &vusername, &vpassword, &vdbname, &vmode); } rb_ivar_set(self, id_at_prefetch_rows, Qnil); rb_ivar_set(self, id_at_username, Qnil); if (NIL_P(vusername) && NIL_P(vpassword)) { /* external credential */ logon_type = T_EXPLICIT; cred = OCI_CRED_EXT; } else { /* RDBMS credential */ OCI8SafeStringValue(vusername); /* 1 */ OCI8SafeStringValue(vpassword); /* 2 */ } if (!NIL_P(vdbname)) { OCI8SafeStringValue(vdbname); /* 3 */ } if (!NIL_P(vmode)) { /* 4 */ logon_type = T_EXPLICIT; Check_Type(vmode, T_SYMBOL); if (vmode == sym_SYSDBA) { mode = OCI_SYSDBA; } else if (vmode == sym_SYSOPER) { mode = OCI_SYSOPER; } else { rb_raise(rb_eArgError, "invalid privilege name %s (expect :SYSDBA or :SYSOPER)", rb_id2name(SYM2ID(vmode))); } } switch (logon_type) { case T_IMPLICIT: rv = OCILogon_nb(svcctx, oci8_envhp, oci8_errhp, &svchp, RSTRING_ORATEXT(vusername), RSTRING_LEN(vusername), RSTRING_ORATEXT(vpassword), RSTRING_LEN(vpassword), NIL_P(vdbname) ? NULL : RSTRING_ORATEXT(vdbname), NIL_P(vdbname) ? 0 : RSTRING_LEN(vdbname)); svcctx->base.hp.svc = svchp; svcctx->base.type = OCI_HTYPE_SVCCTX; svcctx->logon_type = T_IMPLICIT; if (rv != OCI_SUCCESS) { oci8_raise(oci8_errhp, rv, NULL); } break; case T_EXPLICIT: /* allocate OCI handles. */ rv = OCIHandleAlloc(oci8_envhp, &svcctx->base.hp.ptr, OCI_HTYPE_SVCCTX, 0, 0); if (rv != OCI_SUCCESS) oci8_env_raise(oci8_envhp, rv); svcctx->base.type = OCI_HTYPE_SVCCTX; rv = OCIHandleAlloc(oci8_envhp, (void*)&svcctx->authhp, OCI_HTYPE_SESSION, 0, 0); if (rv != OCI_SUCCESS) oci8_env_raise(oci8_envhp, rv); rv = OCIHandleAlloc(oci8_envhp, (void*)&svcctx->srvhp, OCI_HTYPE_SERVER, 0, 0); if (rv != OCI_SUCCESS) oci8_env_raise(oci8_envhp, rv); /* set username and password to OCISession. */ if (cred == OCI_CRED_RDBMS) { oci_lc(OCIAttrSet(svcctx->authhp, OCI_HTYPE_SESSION, RSTRING_PTR(vusername), RSTRING_LEN(vusername), OCI_ATTR_USERNAME, oci8_errhp)); oci_lc(OCIAttrSet(svcctx->authhp, OCI_HTYPE_SESSION, RSTRING_PTR(vpassword), RSTRING_LEN(vpassword), OCI_ATTR_PASSWORD, oci8_errhp)); } /* attach to server and set to OCISvcCtx. */ rv = OCIServerAttach_nb(svcctx, svcctx->srvhp, oci8_errhp, NIL_P(vdbname) ? NULL : RSTRING_ORATEXT(vdbname), NIL_P(vdbname) ? 0 : RSTRING_LEN(vdbname), OCI_DEFAULT); if (rv != OCI_SUCCESS) oci8_raise(oci8_errhp, rv, NULL); oci_lc(OCIAttrSet(svcctx->base.hp.ptr, OCI_HTYPE_SVCCTX, svcctx->srvhp, 0, OCI_ATTR_SERVER, oci8_errhp)); /* begin session. */ rv = OCISessionBegin_nb(svcctx, svcctx->base.hp.ptr, oci8_errhp, svcctx->authhp, cred, mode); if (rv != OCI_SUCCESS) oci8_raise(oci8_errhp, rv, NULL); oci_lc(OCIAttrSet(svcctx->base.hp.ptr, OCI_HTYPE_SVCCTX, svcctx->authhp, 0, OCI_ATTR_SESSION, oci8_errhp)); svcctx->logon_type = T_EXPLICIT; break; default: break; } svcctx->pid = getpid(); svcctx->is_autocommit = 0; #ifdef RUBY_VM svcctx->non_blocking = 1; #endif svcctx->long_read_len = INT2FIX(65535); return Qnil; }