VALUE cConnection_initialize(VALUE self, VALUE uri) { rb_iv_set(self, "@using_socket", Qfalse); rb_iv_set(self, "@ssl_cipher", Qnil); VALUE r_host = rb_funcall(uri, rb_intern("host"), 0); if (r_host != Qnil) { rb_iv_set(self, "@host", r_host); } VALUE r_user = rb_funcall(uri, rb_intern("user"), 0); if (r_user != Qnil) { rb_iv_set(self, "@user", r_user); } VALUE r_password = rb_funcall(uri, rb_intern("password"), 0); if (r_password != Qnil) { rb_iv_set(self, "@password", r_password); } VALUE r_path = rb_funcall(uri, rb_intern("path"), 0); if (r_path != Qnil) { rb_iv_set(self, "@path", r_path); } VALUE r_port = rb_funcall(uri, rb_intern("port"), 0); if (r_port != Qnil) { rb_iv_set(self, "@port", r_port); } // Pull the querystring off the URI VALUE r_query = rb_funcall(uri, rb_intern("query"), 0); rb_iv_set(self, "@query", r_query); const char *encoding = get_uri_option(r_query, "encoding"); if (!encoding) { encoding = get_uri_option(r_query, "charset"); if (!encoding) { encoding = "UTF-8"; } } rb_iv_set(self, "@encoding", rb_str_new2(encoding)); MYSQL *db = mysql_init(NULL); full_connect(self, db); rb_iv_set(self, "@uri", uri); return Qtrue; }
static VALUE cConnection_initialize(VALUE self, VALUE uri) { VALUE r_host, r_user, r_password, r_path, r_query, r_port; PGconn *db = NULL; rb_iv_set(self, "@using_socket", Qfalse); r_host = rb_funcall(uri, rb_intern("host"), 0); if (Qnil != r_host) { rb_iv_set(self, "@host", r_host); } r_user = rb_funcall(uri, rb_intern("user"), 0); if (Qnil != r_user) { rb_iv_set(self, "@user", r_user); } r_password = rb_funcall(uri, rb_intern("password"), 0); if (Qnil != r_password) { rb_iv_set(self, "@password", r_password); } r_path = rb_funcall(uri, rb_intern("path"), 0); if (Qnil != r_path) { rb_iv_set(self, "@path", r_path); } r_port = rb_funcall(uri, rb_intern("port"), 0); if (Qnil != r_port) { r_port = rb_funcall(r_port, rb_intern("to_s"), 0); rb_iv_set(self, "@port", r_port); } // Pull the querystring off the URI r_query = rb_funcall(uri, rb_intern("query"), 0); rb_iv_set(self, "@query", r_query); const char* encoding = get_uri_option(r_query, "encoding"); if (!encoding) { encoding = get_uri_option(r_query, "charset"); } if (!encoding) { encoding = "UTF-8"; } rb_iv_set(self, "@encoding", rb_str_new2(encoding)); full_connect(self, db); rb_iv_set(self, "@uri", uri); return Qtrue; }
static VALUE cDO_OracleConnection_initialize(VALUE self, VALUE uri) { VALUE r_host, r_port, r_path, r_user, r_password; VALUE r_query, r_time_zone; char *non_blocking = NULL; char *time_zone = NULL; char set_time_zone_command[80]; char *host = "localhost", *port = "1521", *path = NULL; char *connect_string; long connect_string_length; VALUE oci8_conn; r_user = rb_funcall(uri, rb_intern("user"), 0); r_password = rb_funcall(uri, rb_intern("password"), 0); r_host = rb_funcall(uri, rb_intern("host"), 0); if ( Qnil != r_host && RSTRING_LEN(r_host) > 0) { host = StringValuePtr(r_host); } r_port = rb_funcall(uri, rb_intern("port"), 0); if ( Qnil != r_port ) { r_port = rb_funcall(r_port, DO_ID_TO_S, 0); port = StringValuePtr(r_port); } r_path = rb_funcall(uri, rb_intern("path"), 0); if ( Qnil != r_path ) { path = StringValuePtr(r_path); } // If just host name is specified then use it as TNS names alias if ((r_host != Qnil && RSTRING_LEN(r_host) > 0) && (r_port == Qnil) && (r_path == Qnil || RSTRING_LEN(r_path) == 0)) { connect_string = host; // If database name is specified in path (in format "/database") } else if (path != NULL && strlen(path) > 1) { connect_string_length = strlen(host) + strlen(port) + strlen(path) + 4; connect_string = (char *)calloc(connect_string_length, sizeof(char)); snprintf(connect_string, connect_string_length, "//%s:%s%s", host, port, path); } else { rb_raise(eDO_ConnectionError, "Database must be specified"); } // oci8_conn = rb_funcall(cOCI8, DO_ID_NEW, 3, r_user, r_password, RUBY_STRING(connect_string)); oci8_conn = rb_funcall(cDO_OracleConnection, rb_intern("oci8_new"), 3, r_user, r_password, RUBY_STRING(connect_string)); // Pull the querystring off the URI r_query = rb_funcall(uri, rb_intern("query"), 0); non_blocking = get_uri_option(r_query, "non_blocking"); // Enable non-blocking mode if (non_blocking != NULL && strcmp(non_blocking, "true") == 0) rb_funcall(oci8_conn, rb_intern("non_blocking="), 1, Qtrue); // By default enable auto-commit mode rb_funcall(oci8_conn, rb_intern("autocommit="), 1, Qtrue); // Set prefetch rows to 100 to increase fetching performance SELECTs with many rows rb_funcall(oci8_conn, rb_intern("prefetch_rows="), 1, INT2NUM(100)); // Set session time zone // at first look for option in connection string time_zone = get_uri_option(r_query, "time_zone"); rb_iv_set(self, "@uri", uri); rb_iv_set(self, "@connection", oci8_conn); // if no option specified then look in ENV['TZ'] if (time_zone == NULL) { r_time_zone = rb_funcall(cDO_OracleConnection, rb_intern("ruby_time_zone"), 0); if (!NIL_P(r_time_zone)) time_zone = StringValuePtr(r_time_zone); } if (time_zone) { snprintf(set_time_zone_command, 80, "alter session set time_zone = '%s'", time_zone); execute_sql(self, RUBY_STRING(set_time_zone_command)); } execute_sql(self, RUBY_STRING("alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'")); execute_sql(self, RUBY_STRING("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS.FF'")); execute_sql(self, RUBY_STRING("alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM'")); return Qtrue; }
static void full_connect(VALUE self, PGconn *db) { PGresult *result = NULL; VALUE r_host, r_user, r_password, r_path, r_port, r_query, r_options; char *host = NULL, *user = NULL, *password = NULL, *path; char *database = "", *port = "5432"; VALUE encoding = Qnil; char *search_path = NULL; char *search_path_query = NULL; char *backslash_off = "SET backslash_quote = off"; char *standard_strings_on = "SET standard_conforming_strings = on"; char *warning_messages = "SET client_min_messages = warning"; if((r_host = rb_iv_get(self, "@host")) != Qnil) { host = StringValuePtr(r_host); } if((r_user = rb_iv_get(self, "@user")) != Qnil) { user = StringValuePtr(r_user); } if((r_password = rb_iv_get(self, "@password")) != Qnil) { password = StringValuePtr(r_password); } if((r_port = rb_iv_get(self, "@port")) != Qnil) { port = StringValuePtr(r_port); } if((r_path = rb_iv_get(self, "@path")) != Qnil) { path = StringValuePtr(r_path); database = strtok(path, "/"); } if (NULL == database || 0 == strlen(database)) { rb_raise(eConnectionError, "Database must be specified"); } r_query = rb_iv_get(self, "@query"); search_path = get_uri_option(r_query, "search_path"); db = PQsetdbLogin( host, port, NULL, NULL, database, user, password ); if ( PQstatus(db) == CONNECTION_BAD ) { rb_raise(eConnectionError, PQerrorMessage(db)); } if (search_path != NULL) { search_path_query = (char *)calloc(256, sizeof(char)); snprintf(search_path_query, 256, "set search_path to %s;", search_path); r_query = rb_str_new2(search_path_query); result = cCommand_execute(self, db, r_query); if (PQresultStatus(result) != PGRES_COMMAND_OK) { free(search_path_query); raise_error(self, result, r_query); } free(search_path_query); } r_options = rb_str_new2(backslash_off); result = cCommand_execute(self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn(PQresultErrorMessage(result)); } r_options = rb_str_new2(standard_strings_on); result = cCommand_execute(self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn(PQresultErrorMessage(result)); } r_options = rb_str_new2(warning_messages); result = cCommand_execute(self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn(PQresultErrorMessage(result)); } encoding = rb_iv_get(self, "@encoding"); #ifdef HAVE_PQSETCLIENTENCODING VALUE pg_encoding = rb_hash_aref(CONST_GET(mEncoding, "MAP"), encoding); if(pg_encoding != Qnil) { if(PQsetClientEncoding(db, RSTRING_PTR(pg_encoding))) { rb_raise(eConnectionError, "Couldn't set encoding: %s", RSTRING_PTR(encoding)); } else { #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index(RSTRING_PTR(encoding)))); #endif rb_iv_set(self, "@pg_encoding", pg_encoding); } } else { rb_warn("Encoding %s is not a known Ruby encoding for PostgreSQL\n", RSTRING_PTR(encoding)); rb_iv_set(self, "@encoding", rb_str_new2("UTF-8")); #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index("UTF-8"))); #endif rb_iv_set(self, "@pg_encoding", rb_str_new2("UTF8")); } #endif rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db)); }
static void full_connect(VALUE self, MYSQL* db) { // Check to see if we're on the db machine. If so, try to use the socket VALUE r_host, r_user, r_password, r_path, r_query, r_port; const char *host = "localhost", *user = "******"; char *database = NULL, *socket = NULL, *password = NULL, *path = NULL; VALUE encoding = Qnil; MYSQL *result; int port = 3306; unsigned long client_flags = 0; int encoding_error; if((r_host = rb_iv_get(self, "@host")) != Qnil) { host = StringValuePtr(r_host); } if((r_user = rb_iv_get(self, "@user")) != Qnil) { user = StringValuePtr(r_user); } if((r_password = rb_iv_get(self, "@password")) != Qnil) { password = StringValuePtr(r_password); } if((r_port = rb_iv_get(self, "@port")) != Qnil) { port = NUM2INT(r_port); } if((r_path = rb_iv_get(self, "@path")) != Qnil) { path = StringValuePtr(r_path); database = strtok(path, "/"); } if (NULL == database || 0 == strlen(database)) { rb_raise(eConnectionError, "Database must be specified"); } r_query = rb_iv_get(self, "@query"); if (0 == strcasecmp(host, "localhost")) { socket = get_uri_option(r_query, "socket"); if (NULL != socket) { rb_iv_set(self, "@using_socket", Qtrue); } } #ifdef HAVE_MYSQL_SSL_SET char *ssl_client_key, *ssl_client_cert, *ssl_ca_cert, *ssl_ca_path, *ssl_cipher; VALUE r_ssl; if(rb_obj_is_kind_of(r_query, rb_cHash)) { r_ssl = rb_hash_aref(r_query, rb_str_new2("ssl")); if(rb_obj_is_kind_of(r_ssl, rb_cHash)) { ssl_client_key = get_uri_option(r_ssl, "client_key"); ssl_client_cert = get_uri_option(r_ssl, "client_cert"); ssl_ca_cert = get_uri_option(r_ssl, "ca_cert"); ssl_ca_path = get_uri_option(r_ssl, "ca_path"); ssl_cipher = get_uri_option(r_ssl, "cipher"); assert_file_exists(ssl_client_key, "client_key doesn't exist"); assert_file_exists(ssl_client_cert, "client_cert doesn't exist"); assert_file_exists(ssl_ca_cert, "ca_cert doesn't exist"); mysql_ssl_set(db, ssl_client_key, ssl_client_cert, ssl_ca_cert, ssl_ca_path, ssl_cipher); } else if(r_ssl != Qnil) { rb_raise(rb_eArgError, "ssl must be passed a hash"); } } #endif result = (MYSQL *)mysql_real_connect( db, host, user, password, database, port, socket, client_flags ); if (NULL == result) { raise_error(self, db, Qnil); } #ifdef HAVE_MYSQL_SSL_SET const char *ssl_cipher_used = mysql_get_ssl_cipher(db); if (NULL != ssl_cipher_used) { rb_iv_set(self, "@ssl_cipher", rb_str_new2(ssl_cipher_used)); } #endif #ifdef MYSQL_OPT_RECONNECT my_bool reconnect = 1; mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect); #endif // Set the connections character set encoding = rb_iv_get(self, "@encoding"); VALUE my_encoding = rb_hash_aref(CONST_GET(mEncoding, "MAP"), encoding); if(my_encoding != Qnil) { encoding_error = mysql_set_character_set(db, rb_str_ptr_readonly(my_encoding)); if (0 != encoding_error) { raise_error(self, db, Qnil); } else { #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index(rb_str_ptr_readonly(encoding)))); #endif rb_iv_set(self, "@my_encoding", my_encoding); } } else { rb_warn("Encoding %s is not a known Ruby encoding for MySQL\n", rb_str_ptr_readonly(encoding)); rb_iv_set(self, "@encoding", rb_str_new2("UTF-8")); #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index("UTF-8"))); #endif rb_iv_set(self, "@my_encoding", rb_str_new2("utf8")); } // Disable sql_auto_is_null cCommand_execute(Qnil, self, db, rb_str_new2("SET sql_auto_is_null = 0")); // removed NO_AUTO_VALUE_ON_ZERO because of MySQL bug http://bugs.mysql.com/bug.php?id=42270 // added NO_BACKSLASH_ESCAPES so that backslashes should not be escaped as in other databases //4.x versions do not support certain session parameters if(mysql_get_server_version(db) < 50000 ){ cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_DIR_IN_CREATE,NO_UNSIGNED_SUBTRACTION'")); }else{ cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'")); } rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db)); }
void full_connect(VALUE self, MYSQL *db) { VALUE r_host = rb_iv_get(self, "@host"); const char *host = "localhost"; if (r_host != Qnil) { host = StringValuePtr(r_host); } VALUE r_user = rb_iv_get(self, "@user"); const char *user = "******"; if (r_user != Qnil) { user = StringValuePtr(r_user); } VALUE r_password = rb_iv_get(self, "@password"); char *password = NULL; if (r_password != Qnil) { password = StringValuePtr(r_password); } VALUE r_port = rb_iv_get(self, "@port"); int port = 3306; if (r_port != Qnil) { port = NUM2INT(r_port); } VALUE r_path = rb_iv_get(self, "@path"); char *path = NULL; char *database = NULL; if (r_path != Qnil) { path = StringValuePtr(r_path); database = strtok(path, "/"); // not threadsafe } if (!database || !*database) { rb_raise(eConnectionError, "Database must be specified"); } VALUE r_query = rb_iv_get(self, "@query"); char *socket = NULL; // Check to see if we're on the db machine. If so, try to use the socket if (strcasecmp(host, "localhost") == 0) { socket = get_uri_option(r_query, "socket"); if (socket) { rb_iv_set(self, "@using_socket", Qtrue); } } #ifdef HAVE_MYSQL_SSL_SET char *ssl_client_key, *ssl_client_cert, *ssl_ca_cert, *ssl_ca_path, *ssl_cipher; VALUE r_ssl; if (rb_obj_is_kind_of(r_query, rb_cHash)) { r_ssl = rb_hash_aref(r_query, rb_str_new2("ssl")); if (rb_obj_is_kind_of(r_ssl, rb_cHash)) { ssl_client_key = get_uri_option(r_ssl, "client_key"); ssl_client_cert = get_uri_option(r_ssl, "client_cert"); ssl_ca_cert = get_uri_option(r_ssl, "ca_cert"); ssl_ca_path = get_uri_option(r_ssl, "ca_path"); ssl_cipher = get_uri_option(r_ssl, "cipher"); assert_file_exists(ssl_client_key, "client_key doesn't exist"); assert_file_exists(ssl_client_cert, "client_cert doesn't exist"); assert_file_exists(ssl_ca_cert, "ca_cert doesn't exist"); mysql_ssl_set(db, ssl_client_key, ssl_client_cert, ssl_ca_cert, ssl_ca_path, ssl_cipher); } else if (r_ssl != Qnil) { rb_raise(rb_eArgError, "ssl must be passed a hash"); } } #endif unsigned long client_flags = 0; MYSQL *result = mysql_real_connect( db, host, user, password, database, port, socket, client_flags ); if (!result) { raise_error(self, db, Qnil); } #ifdef HAVE_MYSQL_GET_SSL_CIPHER const char *ssl_cipher_used = mysql_get_ssl_cipher(db); if (ssl_cipher_used) { rb_iv_set(self, "@ssl_cipher", rb_str_new2(ssl_cipher_used)); } #endif #ifdef MYSQL_OPT_RECONNECT my_bool reconnect = 1; mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect); #endif // We only support encoding for MySQL versions providing mysql_set_character_set. // Without this function there are potential issues with mysql_real_escape_string // since that doesn't take the character set into consideration when setting it // using a SET CHARACTER SET query. Since we don't want to stimulate these possible // issues we simply ignore it and assume the user has configured this correctly. #ifdef HAVE_MYSQL_SET_CHARACTER_SET // Set the connections character set VALUE encoding = rb_iv_get(self, "@encoding"); VALUE my_encoding = rb_hash_aref(do_const_get(mEncoding, "MAP"), encoding); if (my_encoding != Qnil) { int encoding_error = mysql_set_character_set(db, rb_str_ptr_readonly(my_encoding)); if (encoding_error != 0) { raise_error(self, db, Qnil); } else { #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index(rb_str_ptr_readonly(encoding)))); #endif rb_iv_set(self, "@my_encoding", my_encoding); } } else { rb_warn("Encoding %s is not a known Ruby encoding for MySQL\n", rb_str_ptr_readonly(encoding)); rb_iv_set(self, "@encoding", rb_str_new2("UTF-8")); #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index("UTF-8"))); #endif rb_iv_set(self, "@my_encoding", rb_str_new2("utf8")); } #endif // Disable sql_auto_is_null cCommand_execute(Qnil, self, db, rb_str_new2("SET sql_auto_is_null = 0")); // removed NO_AUTO_VALUE_ON_ZERO because of MySQL bug http://bugs.mysql.com/bug.php?id=42270 // added NO_BACKSLASH_ESCAPES so that backslashes should not be escaped as in other databases // For really anscient MySQL versions we don't attempt any strictness #ifdef HAVE_MYSQL_GET_SERVER_VERSION //4.x versions do not support certain session parameters if (mysql_get_server_version(db) < 50000) { cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_DIR_IN_CREATE,NO_UNSIGNED_SUBTRACTION'")); } else { cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'")); } #endif rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db)); }
static VALUE cConnection_initialize(VALUE self, VALUE uri) { VALUE r_host, r_user, r_password, r_path, r_query, r_port; char *host = "localhost", *user = "******", *password = NULL, *path; char *database = "", *socket = NULL; char *encoding = NULL; int port = 3306; unsigned long client_flags = 0; int encoding_error; MYSQL *db = 0, *result; db = (MYSQL *)mysql_init(NULL); rb_iv_set(self, "@using_socket", Qfalse); r_host = rb_funcall(uri, rb_intern("host"), 0); if (Qnil != r_host) { host = StringValuePtr(r_host); } r_user = rb_funcall(uri, rb_intern("user"), 0); if (Qnil != r_user) { user = StringValuePtr(r_user); } r_password = rb_funcall(uri, rb_intern("password"), 0); if (Qnil != r_password) { password = StringValuePtr(r_password); } r_path = rb_funcall(uri, rb_intern("path"), 0); path = StringValuePtr(r_path); if (Qnil != r_path) { database = strtok(path, "/"); } if (NULL == database || 0 == strlen(database)) { rb_raise(eMysqlError, "Database must be specified"); } // Pull the querystring off the URI r_query = rb_funcall(uri, rb_intern("query"), 0); // Check to see if we're on the db machine. If so, try to use the socket if (0 == strcasecmp(host, "localhost")) { socket = get_uri_option(r_query, "socket"); if (NULL != socket) { rb_iv_set(self, "@using_socket", Qtrue); } } r_port = rb_funcall(uri, rb_intern("port"), 0); if (Qnil != r_port) { port = NUM2INT(r_port); } encoding = get_uri_option(r_query, "encoding"); if (!encoding) { encoding = get_uri_option(r_query, "charset"); } if (!encoding) { encoding = "utf8"; } // If ssl? { // mysql_ssl_set(db, key, cert, ca, capath, cipher) // } my_bool reconnect = 1; mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect); result = (MYSQL *)mysql_real_connect( db, host, user, password, database, port, socket, client_flags ); if (NULL == result) { raise_mysql_error(Qnil, db, -1, NULL); } // Set the connections character set encoding_error = mysql_set_character_set(db, encoding); if (0 != encoding_error) { raise_mysql_error(Qnil, db, encoding_error, NULL); } rb_iv_set(self, "@uri", uri); rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db)); return Qtrue; }