From 74bb67e9825e06b68e2f5499b68da2323cb5bb34 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 4 Oct 2012 11:33:49 +0200 Subject: Initial support for prepared queries --- odb/sqlite/prepared-query.hxx | 33 +++++++++++++++++++++++++ odb/sqlite/query.cxx | 16 ++++--------- odb/sqlite/query.hxx | 10 +++++++- odb/sqlite/query.ixx | 6 +++++ odb/sqlite/statement.cxx | 34 ++++++++++++++++---------- odb/sqlite/statement.hxx | 56 +++++++++++++++++++++++++++++-------------- 6 files changed, 111 insertions(+), 44 deletions(-) create mode 100644 odb/sqlite/prepared-query.hxx (limited to 'odb') diff --git a/odb/sqlite/prepared-query.hxx b/odb/sqlite/prepared-query.hxx new file mode 100644 index 0000000..dd23b41 --- /dev/null +++ b/odb/sqlite/prepared-query.hxx @@ -0,0 +1,33 @@ +// file : odb/sqlite/prepared-query.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_PREPARED_QUERY_HXX +#define ODB_SQLITE_PREPARED_QUERY_HXX + +#include + +#include +#include + +#include +#include +#include + +#include + +namespace odb +{ + namespace sqlite + { + struct LIBODB_SQLITE_EXPORT prepared_query_impl: odb::prepared_query_impl + { + sqlite::query query; + details::shared_ptr stmt; + }; + } +} + +#include + +#endif // ODB_SQLITE_PREPARED_QUERY_HXX diff --git a/odb/sqlite/query.cxx b/odb/sqlite/query.cxx index 15934fb..f6a4dc1 100644 --- a/odb/sqlite/query.cxx +++ b/odb/sqlite/query.cxx @@ -92,19 +92,13 @@ namespace odb p->bind (b); } - query_params::binding_type& query_params:: - binding () + void query_params:: + init () { - size_t n (params_.size ()); - binding_type& r (binding_); - - if (n == 0) - return r; - bool inc_ver (false); sqlite::bind* b (&bind_[0]); - for (size_t i (0); i < n; ++i) + for (size_t i (0); i < params_.size (); ++i) { query_param& p (*params_[i]); @@ -119,9 +113,7 @@ namespace odb } if (inc_ver) - r.version++; - - return r; + binding_.version++; } // query_base diff --git a/odb/sqlite/query.hxx b/odb/sqlite/query.hxx index fdae3a2..eebc7c3 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -82,8 +82,11 @@ namespace odb public: typedef sqlite::binding binding_type; + void + init (); + binding_type& - binding (); + binding () {return binding_;} private: friend class query_base; @@ -202,6 +205,11 @@ namespace odb const char* clause_prefix () const; + // Initialize the by-reference parameters from bound variables. + // + void + init_parameters () const; + binding& parameters_binding () const; diff --git a/odb/sqlite/query.ixx b/odb/sqlite/query.ixx index 631749f..627ebb7 100644 --- a/odb/sqlite/query.ixx +++ b/odb/sqlite/query.ixx @@ -6,6 +6,12 @@ namespace odb { namespace sqlite { + inline void query_base:: + init_parameters () const + { + return parameters_->init (); + } + inline binding& query_base:: parameters_binding () const { diff --git a/odb/sqlite/statement.cxx b/odb/sqlite/statement.cxx index e1731ac..5955b33 100644 --- a/odb/sqlite/statement.cxx +++ b/odb/sqlite/statement.cxx @@ -245,21 +245,21 @@ namespace odb // generic_statement:: - generic_statement (connection& conn, const string& text) + generic_statement (connection_type& conn, const string& text) : statement (conn, text), result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false) { } generic_statement:: - generic_statement (connection& conn, const char* text) + generic_statement (connection_type& conn, const char* text) : statement (conn, text), result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false) { } generic_statement:: - generic_statement (connection& conn, + generic_statement (connection_type& conn, const char* text, std::size_t text_size) : statement (conn, text, text_size), @@ -319,7 +319,7 @@ namespace odb // select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const string& text, binding& param, binding& result) @@ -328,7 +328,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& param, binding& result) @@ -337,13 +337,15 @@ namespace odb } select_statement:: - select_statement (connection& conn, const string& text, binding& result) + select_statement (connection_type& conn, + const string& text, + binding& result) : statement (conn, text), param_ (0), result_ (result) { } select_statement:: - select_statement (connection& conn, const char* text, binding& result) + select_statement (connection_type& conn, const char* text, binding& result) : statement (conn, text), param_ (0), result_ (result) { } @@ -433,13 +435,15 @@ namespace odb // insert_statement:: - insert_statement (connection& conn, const string& text, binding& param) + insert_statement (connection_type& conn, + const string& text, + binding& param) : statement (conn, text), param_ (param) { } insert_statement:: - insert_statement (connection& conn, const char* text, binding& param) + insert_statement (connection_type& conn, const char* text, binding& param) : statement (conn, text), param_ (param) { } @@ -502,13 +506,15 @@ namespace odb // update_statement:: - update_statement (connection& conn, const string& text, binding& param) + update_statement (connection_type& conn, + const string& text, + binding& param) : statement (conn, text), param_ (param) { } update_statement:: - update_statement (connection& conn, const char* text, binding& param) + update_statement (connection_type& conn, const char* text, binding& param) : statement (conn, text), param_ (param) { } @@ -554,13 +560,15 @@ namespace odb // delete_statement:: - delete_statement (connection& conn, const string& text, binding& param) + delete_statement (connection_type& conn, + const string& text, + binding& param) : statement (conn, text), param_ (param) { } delete_statement:: - delete_statement (connection& conn, const char* text, binding& param) + delete_statement (connection_type& conn, const char* text, binding& param) : statement (conn, text), param_ (param) { } diff --git a/odb/sqlite/statement.hxx b/odb/sqlite/statement.hxx index 0ed8aa8..3c57c74 100644 --- a/odb/sqlite/statement.hxx +++ b/odb/sqlite/statement.hxx @@ -33,6 +33,8 @@ namespace odb class LIBODB_SQLITE_EXPORT statement: public odb::statement { public: + typedef sqlite::connection connection_type; + virtual ~statement () = 0; @@ -45,6 +47,12 @@ namespace odb virtual const char* text () const; + virtual connection_type& + connection () + { + return conn_; + } + // Cached state (public part). // public: @@ -69,19 +77,21 @@ namespace odb } protected: - statement (connection& conn, const std::string& text) + statement (connection_type& conn, const std::string& text) : conn_ (conn) { init (text.c_str (), text.size ()); } - statement (connection& conn, const char* text) + statement (connection_type& conn, const char* text) : conn_ (conn) { init (text, std::strlen (text)); } - statement (connection& conn, const char* text, std::size_t text_size) + statement (connection_type& conn, + const char* text, + std::size_t text_size) : conn_ (conn) { init (text, text_size); @@ -142,9 +152,9 @@ namespace odb finilize (); protected: - friend class connection; + friend class sqlite::connection; - connection& conn_; + connection_type& conn_; // Cached static type. auto_handle stmt_; bool active_; @@ -199,9 +209,11 @@ namespace odb class LIBODB_SQLITE_EXPORT generic_statement: public statement { public: - generic_statement (connection&, const std::string& text); - generic_statement (connection&, const char* text); - generic_statement (connection&, const char* text, std::size_t text_size); + generic_statement (connection_type&, const std::string& text); + generic_statement (connection_type&, const char* text); + generic_statement (connection_type&, + const char* text, + std::size_t text_size); unsigned long long execute (); @@ -217,21 +229,23 @@ namespace odb class LIBODB_SQLITE_EXPORT select_statement: public statement { public: - select_statement (connection& conn, + select_statement (connection_type& conn, const std::string& text, binding& param, binding& result); - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& param, binding& result); - select_statement (connection& conn, + select_statement (connection_type& conn, const std::string& text, binding& result); - select_statement (connection& conn, const char* text, binding& result); + select_statement (connection_type& conn, + const char* text, + binding& result); // Common select interface expected by the generated code. // @@ -310,11 +324,13 @@ namespace odb class LIBODB_SQLITE_EXPORT insert_statement: public statement { public: - insert_statement (connection& conn, + insert_statement (connection_type& conn, const std::string& text, binding& param); - insert_statement (connection& conn, const char* text, binding& param); + insert_statement (connection_type& conn, + const char* text, + binding& param); // Return true if successful and false if the row is a duplicate. // All other errors are reported by throwing exceptions. @@ -336,11 +352,13 @@ namespace odb class LIBODB_SQLITE_EXPORT update_statement: public statement { public: - update_statement (connection& conn, + update_statement (connection_type& conn, const std::string& text, binding& param); - update_statement (connection& conn, const char* text, binding& param); + update_statement (connection_type& conn, + const char* text, + binding& param); unsigned long long execute (); @@ -356,11 +374,13 @@ namespace odb class LIBODB_SQLITE_EXPORT delete_statement: public statement { public: - delete_statement (connection& conn, + delete_statement (connection_type& conn, const std::string& text, binding& param); - delete_statement (connection& conn, const char* text, binding& param); + delete_statement (connection_type& conn, + const char* text, + binding& param); unsigned long long execute (); -- cgit v1.1