aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/statement.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-03-21 17:24:35 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-03-21 17:24:35 +0200
commitdac72baef46897b80fc98632cef182fb266a5d60 (patch)
treea90f40a5fac59456c6fecf3d31a008c5a061b955 /odb/sqlite/statement.cxx
parent3af997a875e439e71754fddb67fd60de9f60307b (diff)
Add base SQLite database classes
Diffstat (limited to 'odb/sqlite/statement.cxx')
-rw-r--r--odb/sqlite/statement.cxx99
1 files changed, 99 insertions, 0 deletions
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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/sqlite/statement.hxx>
+#include <odb/sqlite/connection.hxx>
+#include <odb/sqlite/error.hxx>
+
+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<int> (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<int> (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<unsigned long long> (
+ sqlite3_changes (conn_.handle ()));
+
+ return r;
+ }
+ }
+}