SQLStatementImpl::SQLStatementImpl ( SQLiteDBConn& p_sqlite_dbconn, string const& str ): m_statement(nullptr), m_sqlite_dbconn(p_sqlite_dbconn), m_is_locked(false) { if (!p_sqlite_dbconn.is_valid()) { JEWEL_THROW ( InvalidConnection, "Attempt to initialize SQLStatementImpl with invalid " "DatabaseConnection." ); } char const* cstr = str.c_str(); char const** tail = &cstr; JEWEL_ASSERT (p_sqlite_dbconn.is_valid()); throw_on_failure ( sqlite3_prepare_v2 ( m_sqlite_dbconn.m_connection, cstr, str.length() + 1, &m_statement, tail ) ); for (char const* it = *tail; *it != '\0'; ++it) { switch (*it) { case ';': case ' ': // The character is harmless. break; default: // The character is bad. sqlite3_finalize(m_statement); // Always succeeds. m_statement = nullptr; // Note this will have thrown already if first statement is // ungrammatical. JEWEL_THROW ( TooManyStatements, "Compound SQL statement passed to constructor of " "SQLStatementImpl - which can handle only single statements." ); } } return; }
bool SQLStatementImpl::step() { if (!m_sqlite_dbconn.is_valid()) { JEWEL_THROW(InvalidConnection, "Invalid database connection."); } int code = SQLITE_OK; try { // Intentional assignment throw_on_failure(code = sqlite3_step(m_statement)); } catch (SQLiteException&) { reset(); clear_bindings(); throw; } switch (code) { case SQLITE_DONE: // After SQLite version 3.6.23.1, the statement is // reset automatically. #if SQLITE_VERSION_NUMBER < 3007000 sqlite3_reset(m_statement); #endif return false; case SQLITE_ROW: return true; default: ; // Do nothing } JEWEL_HARD_ASSERT (false); // Execution should never reach here. return false; // Silence compiler re. return from non-void function. }
void* sk_calloc_throw(size_t size) { return prevent_overcommit(0, size, throw_on_failure(size, calloc(size, 1))); }
void* sk_malloc_throw(size_t size) { return prevent_overcommit(0x42, size, throw_on_failure(size, malloc(size))); }
void* sk_realloc_throw(void* addr, size_t size) { return throw_on_failure(size, realloc(addr, size)); }
void* sk_calloc_throw(size_t size) { return throw_on_failure(size, calloc(size, 1)); }
void* sk_malloc_throw(size_t size) { return throw_on_failure(size, malloc(size)); }