From dac72baef46897b80fc98632cef182fb266a5d60 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 21 Mar 2011 17:24:35 +0200 Subject: Add base SQLite database classes --- odb/sqlite/statement.cxx | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 odb/sqlite/statement.cxx (limited to 'odb/sqlite/statement.cxx') diff --git a/odb/sqlite/statement.cxx b/odb/sqlite/statement.cxx new file mode 100644 index 0000000..e7d197b --- /dev/null +++ b/odb/sqlite/statement.cxx @@ -0,0 +1,99 @@ +// file : odb/sqlite/statement.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include +#include + +using namespace std; + +namespace odb +{ + namespace sqlite + { + // statement + // + + statement:: + statement (connection& conn, const string& s) + : conn_ (conn) + { + if (int e = sqlite3_prepare_v2 ( + conn_.handle (), + s.c_str (), + static_cast (s.size () + 1), + &stmt_, + 0)) + { + translate_error (e, conn_); + } + } + + statement:: + statement (connection& conn, const char* s, std::size_t n) + : conn_ (conn) + { + if (int e = sqlite3_prepare_v2 ( + conn_.handle (), + s, + static_cast (n), + &stmt_, + 0)) + { + translate_error (e, conn_); + } + } + + + + statement:: + ~statement () + { + sqlite3_finalize (stmt_); + } + + // simple_statement + // + + simple_statement:: + simple_statement (connection& conn, const string& s) + : statement (conn, s), + result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false) + { + } + + simple_statement:: + simple_statement (connection& conn, const char* s, std::size_t n) + : statement (conn, s, n), + result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false) + { + } + + unsigned long long simple_statement:: + execute () + { + if (stmt_ == 0) // Empty statement or comment. + return 0; + + if (int e = sqlite3_reset (stmt_)) + translate_error (e, conn_); + + unsigned long long r (0); + + int e; + for (e = sqlite3_step (stmt_); e == SQLITE_ROW; e = sqlite3_step (stmt_)) + r++; + + if (e != SQLITE_DONE) + translate_error (e, conn_); + + if (!result_set_) + r = static_cast ( + sqlite3_changes (conn_.handle ())); + + return r; + } + } +} -- cgit v1.1