diff options
Diffstat (limited to 'odb/mysql/statement.hxx')
-rw-r--r-- | odb/mysql/statement.hxx | 223 |
1 files changed, 201 insertions, 22 deletions
diff --git a/odb/mysql/statement.hxx b/odb/mysql/statement.hxx index ab6ab9b..4925269 100644 --- a/odb/mysql/statement.hxx +++ b/odb/mysql/statement.hxx @@ -10,6 +10,7 @@ #include <map> #include <string> +#include <cstddef> // std::size_t #include <typeinfo> #include <odb/forward.hxx> @@ -22,6 +23,23 @@ namespace odb { class connection; + class binding + { + public: + binding (MYSQL_BIND* b, std::size_t n) + : bind (b), count (n), version (0) + { + } + + MYSQL_BIND* bind; + std::size_t count; + std::size_t version; + + private: + binding (const binding&); + binding& operator= (const binding&); + }; + class statement: public shared_base { public: @@ -36,27 +54,15 @@ namespace odb MYSQL_STMT* stmt_; }; - template <typename T> class insert_statement: public statement { public: - typedef T object_type; - typedef odb::object_traits<T> object_traits; - typedef typename object_traits::image_type image_type; - - public: virtual ~insert_statement (); insert_statement (connection& conn, const std::string& query, - image_type&); - - image_type& - image () - { - return image_; - } + binding& image); void execute (); @@ -66,8 +72,97 @@ namespace odb insert_statement& operator= (const insert_statement&); private: - image_type& image_; - MYSQL_BIND bind_[object_traits::column_count]; + binding& image_; + std::size_t version_; + }; + + class select_statement: public statement + { + public: + virtual + ~select_statement (); + + select_statement (connection& conn, + const std::string& query, + binding& id, + binding& image); + enum result + { + success, + no_data, + truncated + }; + + // You are expected to call free_result() if this function + // returns success or truncated. + // + result + execute (); + + void + refetch (); + + void + free_result (); + + + private: + select_statement (const select_statement&); + select_statement& operator= (const select_statement&); + + private: + binding& id_; + std::size_t id_version_; + + binding& image_; + std::size_t image_version_; + }; + + class update_statement: public statement + { + public: + virtual + ~update_statement (); + + update_statement (connection& conn, + const std::string& query, + binding& id, + binding& image); + void + execute (); + + private: + update_statement (const update_statement&); + update_statement& operator= (const update_statement&); + + private: + binding& id_; + std::size_t id_version_; + + binding& image_; + std::size_t image_version_; + }; + + class delete_statement: public statement + { + public: + virtual + ~delete_statement (); + + delete_statement (connection& conn, + const std::string& query, + binding& id); + + void + execute (); + + private: + delete_statement (const delete_statement&); + delete_statement& operator= (const delete_statement&); + + private: + binding& id_; + std::size_t version_; }; // Statement cache. @@ -93,27 +188,111 @@ namespace odb class object_statements: public object_statements_base { public: - typedef typename object_traits<T>::image_type image_type; + typedef odb::object_traits<T> object_traits; + typedef typename object_traits::image_type image_type; + typedef typename object_traits::id_image_type id_image_type; + + object_statements (connection&); + + image_type& + image () + { + return image_; + } + + binding& + image_binding () + { + return image_binding_; + } + + my_bool* + image_error () + { + return image_error_; + } + + id_image_type& + id_image () + { + return id_image_; + } - object_statements (connection& conn) - : object_statements_base (conn) + binding& + id_image_binding () { + return id_image_binding_; } - insert_statement<T>& + insert_statement& insert () { if (insert_ == 0) insert_.reset ( - new (shared) insert_statement<T> ( - conn_, object_traits<T>::insert_query, image_)); + new (shared) insert_statement ( + conn_, object_traits::insert_query, image_binding_)); return *insert_; } + select_statement& + select () + { + if (select_ == 0) + select_.reset ( + new (shared) select_statement ( + conn_, + object_traits::select_query, + id_image_binding_, + image_binding_)); + + return *select_; + } + + update_statement& + update () + { + if (update_ == 0) + update_.reset ( + new (shared) update_statement ( + conn_, + object_traits::update_query, + id_image_binding_, + image_binding_)); + + return *update_; + } + + delete_statement& + delete_ () + { + if (del_ == 0) + del_.reset ( + new (shared) delete_statement ( + conn_, + object_traits::delete_query, + id_image_binding_)); + + return *del_; + } + private: + // The last element is the id parameter. The update statement + // depends on this being one contiguous arrays. + // + MYSQL_BIND image_bind_[object_traits::column_count + 1]; + image_type image_; - odb::shared_ptr<insert_statement<T> > insert_; + my_bool image_error_[object_traits::column_count]; + binding image_binding_; + + id_image_type id_image_; + binding id_image_binding_; + + odb::shared_ptr<insert_statement> insert_; + odb::shared_ptr<select_statement> select_; + odb::shared_ptr<update_statement> update_; + odb::shared_ptr<delete_statement> del_; }; struct type_info_comparator |