transaction::~transaction() noexcept(false) { if (db_) { auto rc = db_->execute(fcommit_ ? "COMMIT" : "ROLLBACK"); if (rc != SQLITE_OK) throw database_error(*db_); } }
database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr) { if (dbname) { auto rc = connect(dbname, flags, vfs); if (rc != SQLITE_OK) throw database_error("can't connect database"); } }
statement::statement(database& db, char const* stmt) : db_(db), stmt_(0), tail_(0) { if (stmt) { auto rc = prepare(stmt); if (rc != SQLITE_OK) throw database_error(db_); } }
database::database(char const* dbname) : db_(0) { if (dbname) { int rc = connect(dbname); if (rc != SQLITE_OK) throw database_error("can't connect database"); } }
postgresql_statement::postgresql_statement(shared_ptr<postgresql_connection_impl> conn, const string &query) : conn(conn) { postgresql_result res(PQprepare(conn->get(), "", convert_query(query).c_str(), 0, NULL)); if (!res.command_ok()) throw database_error(PQresultErrorMessage(res.get())); }
sqlite3_connection::sqlite3_connection( sqlite3 * dbh ) : m_db(0), m_name() { if( ! dbh ) { throw database_error( "sqlite3_connection(sqlite3*) ctor was passed a null db handle." ); } this->take( dbh ); }
void sqlite3_command::finalize() { if( this->stmt ) { if(sqlite3_finalize(this->stmt)!=SQLITE_OK) throw database_error(this->con); this->stmt = 0; } }
void sqlite3_command::bind(int index, const char *data, int datalen) { if(sqlite3_bind_text(this->stmt, index, data, static_cast<int>( ((-1==datalen) ? std::strlen(data) : datalen) ), SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); }
void postgresql_connection_impl::reconnect() { if (PQstatus(conn)==CONNECTION_OK) return; PQreset(conn); if (PQstatus(conn)!=CONNECTION_OK) throw database_error("fail to reconnect"); }
void update_db(const char *sql) { sqlite3 *db; int ret; char *errmsg; db = get_db(); ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { database_error(errmsg, sql); } }
table_generator::table_generator( sqlite3_connection & con, std::string const & n ) : m_pimpl( new table_generator::table_generator_impl ) { int check = con.executeint( "select count(*) from sqlite_master where type like 'table' and name like '"+n+"'" ); // ^^^ we use 'like' here because sqlite3 is case-insensitive if( 0 != check ) { throw database_error( "table_generator() db table '%s' already exists.", n.c_str() ); } this->m_pimpl->db = &con; this->m_pimpl->name = n; }
void sqlite3_command::prepare( char const * sql, int len ) { if( this->stmt ) this->finalize(); const char *tail=NULL; int rc = sqlite3_prepare( this->con.db(), sql, len, &(this->stmt), &tail ); if( SQLITE_OK != rc ) { throw database_error("sqlite3_command::prepare([%s]) failed. Reason=[%s]", sql, sqlite3_errmsg( this->con.db() ) ); } this->argc=sqlite3_column_count(this->stmt); }
/* Return False on error, True on success */ int load_add_class(char *class_name, int class_id, int superclass_id, char *superclass_name) { /* Build up a class data structure for the new class. */ id_type id = (id_type)SafeMalloc(sizeof(id_struct)); id_type temp_id = (id_type)SafeMalloc(sizeof(id_struct)); class_type c = (class_type)SafeMalloc(sizeof(class_struct)); // Adding new built-in object types will render existing kodbase.txt files // incompatible. This isn't a problem as a pre-existing kodbase.txt is only // required for reloading a live server, so a new one can be made. Check // for built-in class name/ID mismatches here and instruct the user to // delete kodbase.txt if this check fails. extern id_struct BuiltinIds[]; if ((strcmp(BuiltinIds[SETTINGS_CLASS].name, class_name) == 0 && class_id != SETTINGS_CLASS) || (strcmp(BuiltinIds[REALTIME_CLASS].name, class_name) == 0 && class_id != REALTIME_CLASS) || (strcmp(BuiltinIds[EVENTENGINE_CLASS].name, class_name) == 0 && class_id != EVENTENGINE_CLASS)) { database_error("Incompatible kodbase.txt. Delete the file and recompile."); return False; } id->name = strdup(class_name); id->idnum = class_id; id->type = I_CLASS; id->source = DBASE; c->class_id = id; c->properties = c->messages = c->resources = c->classvars = NULL; c->is_new = False; /* Don't generate code for this class */ /* Store superclass id # in pointer for now. Id # will be converted to pointer * when build_superclasses below is called. */ c->superclass = (class_type) superclass_id; /* Add to list of classes that have been read in */ st.classes = list_add_item(st.classes, (void *) c); current_class = c; st.curclass = class_id; current_message = NULL; /* Call table_insert instead of add_identifier so that our id # from * the database file is preserved. */ if (table_insert(st.globalvars, (void *) id, id_hash, id_compare) == 0) return True; else return False; }
void sqlite3_connection::open(const wchar_t *db) { if(sqlite3_open16(db, &this->m_db)!=SQLITE_OK) throw database_error("unable to open database"); try { this->on_open(); } catch(...) { try { this->close(); } catch(...) { /* ignore */ } throw; } }
void Database::open(std::string fname) { sqlite3* db=NULL; try{ //TODO UTF-8 string int rc=sqlite3_open(fname.c_str(), &db); if(rc!=SQLITE_OK) throw database_error( std::string("database error: ")+sqlite3_errmsg(db) ); con=shared_connection(new autoclosed_con(db)); }catch(std::runtime_error e){ if(db) sqlite3_close(db); throw e; } }
void router::set_nodes_total(const size_t total, vm::all *all) { nodes_per_remote = total / world_size; #ifndef USE_SIM if(nodes_per_remote == 0) throw database_error("Number of nodes is less than the number of remote machines"); #endif all->NUM_NODES_PER_PROCESS = nodes_per_remote; // cache values for each remote for(remote::remote_id i(0); i != (remote::remote_id)world_size; ++i) remote_list[i]->cache_values(world_size, nodes_per_remote, total); }
void table_generator::create() { size_t sz = this->m_pimpl->list.size(); if( ! sz ) { throw database_error( "table_generator::operator(): cannot create a table with no fields. Try using operator()(string) to add fields." ); } std::ostringstream os; os << "create table "<< this->m_pimpl->name << "("; for( size_t i = 0; i < sz; ++i ) { os << this->m_pimpl->list[i]; if( i < (sz-1) ) os << ","; } os << ");"; this->m_pimpl->db->executenonquery( os.str() ); }
/* * 从DATA_IN中读取用户的输入,比如scanf 需要的输入就从这里得到 */ void get_data_input(char *code_id) { char sql[SQL_LEN] = {0}; char *errmsg; int ret; sqlite3 *db; FILE *fp = fopen(DATA_IN, "w"); if (fp == NULL) { write_log(INT_RE, 1, "fopen error(in get_data_input)"); exit(-1); } sprintf(sql, "select data_input from code where code_id='%s'", code_id); db = get_db(); ret = sqlite3_exec(db, (const char*)sql, get_data_input_cb,\ (void*)fp, &errmsg); fclose(fp); if (ret != SQLITE_OK) { database_error(errmsg, sql); } }
void sqlite3_connection::open( char const * db) { this->close(); this->m_name = db ? db : ""; if(sqlite3_open(db, &this->m_db)!=SQLITE_OK) throw database_error("unable to open database %s", db ? db : "<null>"); try { // Potential bug: when open() is called from // the ctor of subclasses as a result of // calling the parent class ctor, the subclass // part of the subclass may not be complete, // and a less derived on_open() may // potentially be called. ??? this->on_open(); } catch(...) { try { this->close(); } catch(...) { /* ignore */ } throw; } }
int load_add_resource(char *resource_name, int resource_id) { id_type id = (id_type) SafeMalloc(sizeof(id_struct)); resource_type r = (resource_type) SafeMalloc(sizeof(resource_struct)); id->name = strdup(resource_name); id->idnum = resource_id; id->type = I_RESOURCE; id->ownernum = st.curclass; id->source = DBASE; r->lhs = id; r->rhs = NULL; /* Add resource to resource list of current class */ if (current_class == NULL) database_error("Resource appears outside of class in database file"); current_class->resources = list_add_item(current_class->resources, (void *) r); /* OK if parameter already in table; just ignore return value */ table_insert(st.globalvars, (void *) id, id_hash, id_compare); return True; }
void sqlite3_reader::reset() { if(!this->cmd) throw database_error("reader is closed"); if(sqlite3_reset(this->cmd->stmt)!=SQLITE_OK) throw database_error(&this->cmd->m_con); }
void sqlite3_command::bind(int index, const void *data, int datalen) { if(sqlite3_bind_blob(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); }
void sqlite3_command::bind(int index, double data) { if(sqlite3_bind_double(this->stmt, index, data)!=SQLITE_OK) throw database_error(this->con); }
void sqlite3_command::bind(int index, long long data) { if(sqlite3_bind_int64(this->stmt, index, data)!=SQLITE_OK) throw database_error(this->con); }
std::string sqlite3_command::executeblob() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getblob(0); }
double sqlite3_command::executedouble() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getdouble(0); }
long long sqlite3_command::executeint64() { sqlite3_reader reader=this->executereader(); if(!reader.read()) throw database_error("nothing to read"); return reader.getint64(0); }
void sqlite3_command::bind(int index, const std::wstring &data) { if(sqlite3_bind_text16(this->stmt, index, data.data(), (int)data.length()*2, SQLITE_TRANSIENT)!=SQLITE_OK) throw database_error(this->con); }
void database::execute(char const* sql) { const int rc = eexecute(sql); if (rc != SQLITE_OK) throw database_error(*this); }
void sqlite3_command::bind(int index) { if(sqlite3_bind_null(this->stmt, index)!=SQLITE_OK) throw database_error(this->con); }