34 #include <system_error>
42 static const std::string::size_type npos = std::string::npos;
44 using namespace scc::sqld;
48 int r = sqlite3_open(uri.c_str(), &m_db);
51 throw std::runtime_error(sqlite3_errstr(r));
73 int r = sqlite3_open(uri.c_str(), &m_db);
76 throw std::runtime_error(sqlite3_errstr(r));
80 Trans::Trans(
Conn& conn) : m_conn(conn), m_active(false)
96 throw std::runtime_error(
"begin() transaction when already active");
108 throw std::runtime_error(
"commit() transaction when not active");
111 r.
sql() <<
"COMMIT;";
120 throw std::runtime_error(
"abort() transaction when not active");
123 r.
sql() <<
"ROLLBACK;";
128 Req::Req(
Conn& conn) : m_conn(conn), m_stmt(nullptr), m_pos(0), m_cols(0)
141 sqlite3_finalize(m_stmt);
169 if (m_pos >= m_sql.str().size())
180 const char *head = &(m_sql.str()[m_pos]), *tail =
nullptr;
182 int r = sqlite3_prepare_v2(m_conn.m_db, head, m_sql.str().size()+1, &m_stmt, &tail);
185 throw std::runtime_error(sqlite3_errstr(r));
196 throw std::runtime_error(
"exec_select() called with current row data");
208 int r = sqlite3_step(m_stmt);
210 if (r == SQLITE_DONE)
217 throw std::runtime_error(sqlite3_errstr(r));
220 m_cols = sqlite3_column_count(m_stmt);
232 throw std::runtime_error(
"exec() called with current row data");
256 throw std::runtime_error(
"next_row() called with invalid statement");
260 throw std::runtime_error(
"next_row() called without current row data");
263 int r = sqlite3_step(m_stmt);
265 if (r == SQLITE_DONE)
273 throw std::runtime_error(sqlite3_errstr(r));
276 return sqlite3_column_count(m_stmt);
283 throw std::runtime_error(
"column operation called with invalid statement");
287 throw std::runtime_error(
"column operation called when row not available");
289 if (col < 0 || col >= m_cols)
291 throw std::runtime_error(
"column operation called with invalid column number");
294 return sqlite3_column_name(m_stmt, col);
301 throw std::runtime_error(
"column operation called with invalid statement");
305 throw std::runtime_error(
"column operation called when row not available");
307 if (col < 0 || col >= m_cols)
309 throw std::runtime_error(
"column operation called with invalid column number");
312 str.assign(sqlite3_column_name(m_stmt, col));
319 throw std::runtime_error(
"column operation called with invalid statement");
323 throw std::runtime_error(
"column operation called when row not available");
325 if (col < 0 || col >= m_cols)
327 throw std::runtime_error(
"column operation called with invalid column number");
330 return reinterpret_cast<const char*
>(sqlite3_column_text(m_stmt, col));
337 throw std::runtime_error(
"column operation called with invalid statement");
341 throw std::runtime_error(
"column operation called when row not available");
343 if (col < 0 || col >= m_cols)
345 throw std::runtime_error(
"column operation called with invalid column number");
348 str.assign(
reinterpret_cast<const char*
>(sqlite3_column_text(m_stmt, col)));
355 throw std::runtime_error(
"column operation called with invalid statement");
359 throw std::runtime_error(
"column operation called when row not available");
361 if (col < 0 || col >= m_cols)
363 throw std::runtime_error(
"column operation called with invalid column number");
366 return sqlite3_column_int(m_stmt, col);
373 throw std::runtime_error(
"column operation called with invalid statement");
377 throw std::runtime_error(
"column operation called when row not available");
379 if (col < 0 || col >= m_cols)
381 throw std::runtime_error(
"column operation called with invalid column number");
384 return sqlite3_column_int64(m_stmt, col);
391 throw std::runtime_error(
"column operation called with invalid statement");
395 throw std::runtime_error(
"column operation called when row not available");
397 if (col < 0 || col >= m_cols)
399 throw std::runtime_error(
"column operation called with invalid column number");
402 return sqlite3_column_double(m_stmt, col);
409 throw std::runtime_error(
"column operation called with invalid statement");
413 throw std::runtime_error(
"column operation called when row not available");
415 if (col < 0 || col >= m_cols)
417 throw std::runtime_error(
"column operation called with invalid column number");
420 int sz = sqlite3_column_bytes(m_stmt, col);
422 const char* v =
reinterpret_cast<const char*
>(sqlite3_column_blob(m_stmt, col));