static PGresult* cCommand_execute_sync(VALUE self, PGconn *db, VALUE query) { PGresult *response; struct timeval start; char* str = StringValuePtr(query); while ((response = PQgetResult(db)) != NULL) { PQclear(response); } gettimeofday(&start, NULL); response = PQexec(db, str); if (response == NULL) { if(PQstatus(db) != CONNECTION_OK) { PQreset(db); if (PQstatus(db) == CONNECTION_OK) { response = PQexec(db, str); } else { VALUE connection = rb_iv_get(self, "@connection"); full_connect(connection, db); response = PQexec(db, str); } } if(response == NULL) { rb_raise(eConnectionError, PQerrorMessage(db)); } } data_objects_debug(query, &start); return response; }
static PGresult* cCommand_execute_async(VALUE self, PGconn *db, VALUE query) { int socket_fd; int retval; fd_set rset; PGresult *response; struct timeval start; char* str = StringValuePtr(query); while ((response = PQgetResult(db)) != NULL) { PQclear(response); } retval = PQsendQuery(db, str); if (!retval) { if(PQstatus(db) != CONNECTION_OK) { PQreset(db); if (PQstatus(db) == CONNECTION_OK) { retval = PQsendQuery(db, str); } else { VALUE connection = rb_iv_get(self, "@connection"); full_connect(connection, db); retval = PQsendQuery(db, str); } } if(!retval) { rb_raise(eConnectionError, "%s", PQerrorMessage(db)); } } gettimeofday(&start, NULL); socket_fd = PQsocket(db); data_objects_debug(query, &start); for(;;) { FD_ZERO(&rset); FD_SET(socket_fd, &rset); retval = rb_thread_select(socket_fd + 1, &rset, NULL, NULL, NULL); if (retval < 0) { rb_sys_fail(0); } if (retval == 0) { continue; } if (PQconsumeInput(db) == 0) { rb_raise(eConnectionError, "%s", PQerrorMessage(db)); } if (PQisBusy(db) == 0) { break; } } return PQgetResult(db); }
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; }
MYSQL_RES *cCommand_execute_async(VALUE self, VALUE connection, MYSQL *db, VALUE query) { int retval; if ((retval = mysql_ping(db)) && mysql_errno(db) == CR_SERVER_GONE_ERROR) { full_connect(connection, db); } struct timeval start; const char *str = rb_str_ptr_readonly(query); size_t len = rb_str_len(query); gettimeofday(&start, NULL); retval = mysql_send_query(db, str, len); CHECK_AND_RAISE(retval, query); int socket_fd = db->net.fd; fd_set rset; while (1) { FD_ZERO(&rset); FD_SET(socket_fd, &rset); retval = rb_thread_select(socket_fd + 1, &rset, NULL, NULL, NULL); if (retval < 0) { rb_sys_fail(0); } if (retval == 0) { continue; } if (db->status == MYSQL_STATUS_READY) { break; } } retval = mysql_read_query_result(db); CHECK_AND_RAISE(retval, query); data_objects_debug(connection, query, &start); MYSQL_RES *result = mysql_store_result(db); if (!result) { CHECK_AND_RAISE(mysql_errno(db), query); } return result; }
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 MYSQL_RES* cCommand_execute_sync(VALUE self, VALUE connection, MYSQL* db, VALUE query) { int retval; struct timeval start; const char* str = rb_str_ptr_readonly(query); int len = rb_str_len(query); if(mysql_ping(db) && mysql_errno(db) == CR_SERVER_GONE_ERROR) { // Ok, we do one more try here by doing a full connect VALUE connection = rb_iv_get(self, "@connection"); full_connect(connection, db); } gettimeofday(&start, NULL); retval = mysql_real_query(db, str, len); data_objects_debug(connection, query, &start); CHECK_AND_RAISE(retval, query); return mysql_store_result(db); }