void sqlite_database::on_open(const std::string &db) { int ret = sqlite3_open(db.c_str(), &sqlite_db_); if (ret != SQLITE_OK) { throw sqlite_exception("couldn't open database: " + db); } }
void throw_error(int ec, sqlite3 *db, const std::string &source) { if (ec == SQLITE_OK) { return; } std::stringstream msg; msg << source << ": " << sqlite3_errmsg(db); throw sqlite_exception(msg.str()); }
result* sqlite_database::on_execute(const std::string &sql) { #ifdef WIN32 std::auto_ptr<sqlite_result> res(new sqlite_result); #else std::unique_ptr<sqlite_result> res(new sqlite_result); #endif char *errmsg = 0; int ret = sqlite3_exec(sqlite_db_, sql.c_str(), parse_result, res.get(), &errmsg); if (ret != SQLITE_OK) { std::string error(errmsg); sqlite3_free(errmsg); throw sqlite_exception(error); } return res.release(); }
/* ctor */ sqlite_statement::sqlite_statement ( sqlite _database , char const * _query ) /* * index starts at 1 and then * increments. */ : stmt (NULL) /* * The following state cannot be set only thrown, and * signifies a new un-stepped statement. */ , state (SQLITE_MISUSE) , db (_database) , column_count (0) , stepped(false) , bind_parameter_count (0) // will be set to 1 for when auto binding. , index (0) { sqlite_check_error ( sqlite3_prepare_v2 ( _database.get() , _query , -1 /* Query must be alawys null terminated. */ , & (this->stmt) , 0 /* There is never an unused portion of the statement. */ )); if (this->stmt == NULL) throw sqlite_exception("Null statement", SQLITE_MISUSE); this->bind_parameter_count = sqlite3_bind_parameter_count(this->stmt); /* * If the statement has no parameters to bind, then step * right away. * if step finds a result, the statement will be treated * like a input statement. otherwise complete. */ if (0 == bind_parameter_count) this->step(); }