void SQLite::exec(const String &query) { #ifndef HAVE_SQLITE3 throw UnsupportedFeatureException("SQLite"); #else if (!conn) throw NoConnectionException(); String err; double t_start; affectedrows=0; sqlite3_stmt *stmt=NULL; t_start=GetMicrotime(); int ret=sqlite3_prepare_v2((sqlite3*)conn, (const char*)query, query.size(),&stmt,NULL); if (ret!=SQLITE_OK) { throw QueryFailedException("sqlite3_prepare_v2 failed: %s",sqlite3_errmsg((sqlite3*)conn)); } if (stmt==NULL) { throw OutOfMemoryException(); } ret=sqlite3_step(stmt); if (ret!=SQLITE_DONE && ret!=SQLITE_ROW) { err.setf("sqlite3_step: %s, Query: %s",sqlite3_errmsg((sqlite3*)conn),(const char*)query); sqlite3_finalize(stmt); throw QueryFailedException(err); } ret=sqlite3_finalize(stmt); if (ret !=SQLITE_OK) { err.setf("sqlite3_finalize: %s, Query: %s",sqlite3_errmsg((sqlite3*)conn),(const char*)query); throw QueryFailedException(err); } affectedrows=sqlite3_changes((sqlite3*)conn); updateLastUse(); logQuery(query,(float)(GetMicrotime()-t_start)); #endif }
ITablePtr Db::ExecTable(std::string const& sql) const { TablePtr pRes; char** result; int nrow; int ncol; int err = sqlite3_get_table(m_pDb, sql.c_str(), &result, /* Result written to a char *[] that this points to */ &nrow, /* Number of result rows written here */ &ncol, /* Number of result columns written here */ NULL); if (SQLITE_OK != err) { assert(false); m_log << "Db::ExecTable: Failed to execute query [" << sql << "]. Error: " << err << ", " << sqlite3_errmsg(m_pDb) << std::endl; std::tstring msg = STREAM2STR("Query Failed with error code " << err); throw QueryFailedException(msg); } pRes = TablePtr(new Table(nrow, ncol, result)); sqlite3_free_table(result); return pRes; }
ResultSet *SQLite::query(const String &query) { #ifndef HAVE_SQLITE3 throw UnsupportedFeatureException("SQLite"); #else if (!conn) throw NoConnectionException(); String err; double t_start; affectedrows=0; sqlite3_stmt *stmt=NULL; t_start=GetMicrotime(); int ret=sqlite3_prepare_v2((sqlite3*)conn, (const char*)query, query.size(),&stmt,NULL); if (ret!=SQLITE_OK) { throw QueryFailedException("sqlite3_prepare_v2 failed: %s",sqlite3_errmsg((sqlite3*)conn)); } if (stmt==NULL) { throw OutOfMemoryException(); } ret=sqlite3_step(stmt); if (ret!=SQLITE_DONE && ret!=SQLITE_ROW) { err.setf("sqlite3_step: %s, Query: %s",sqlite3_errmsg((sqlite3*)conn),(const char*)query); sqlite3_finalize(stmt); throw QueryFailedException(err); } affectedrows=sqlite3_changes((sqlite3*)conn); updateLastUse(); logQuery(query,(float)(GetMicrotime()-t_start)); SQLiteResult *pr=new SQLiteResult; if (!pr) { sqlite3_finalize(stmt); throw OutOfMemoryException(); } pr->stmt=stmt; pr->last_res=ret; pr->sqlite_class=this; pr->conn=(sqlite3 *)conn; pr->affectedrows=affectedrows; pr->num_fields=sqlite3_column_count(stmt); return pr; #endif }