// file : odb/mysql/statement.hxx // author : Boris Kolpackov // copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #ifndef ODB_MYSQL_STATEMENT_HXX #define ODB_MYSQL_STATEMENT_HXX #include #include #include // std::size_t #include #include #include #include #include #include #include namespace odb { namespace mysql { class connection; class LIBODB_MYSQL_EXPORT statement: public odb::statement { public: virtual ~statement () = 0; MYSQL_STMT* handle () const { return stmt_; } virtual const char* text () const; // Cancel the statement execution (e.g., result fetching) so // that another statement can be executed on the connection. // virtual void cancel (); protected: statement (connection&, const std::string& text); protected: connection& conn_; const std::string text_; auto_handle stmt_; }; class LIBODB_MYSQL_EXPORT select_statement: public statement { public: virtual ~select_statement (); select_statement (connection& conn, const std::string& statement, binding& param, binding& result); select_statement (connection& conn, const std::string& statement, binding& result); enum result { success, no_data, truncated }; void execute (); void cache (); bool cached () const { return cached_; } // Can only be called on a cached result. // std::size_t result_size () const { return size_; } // Number of rows already fetched. // std::size_t fetched () const { return rows_; } // Fetch next or current row depending on the next argument. // Note that fetching of the current row is only supported // if the result is cached. // result fetch (bool next = true); void refetch (); void free_result (); virtual void cancel (); private: select_statement (const select_statement&); select_statement& operator= (const select_statement&); private: bool end_; bool cached_; std::size_t rows_; std::size_t size_; binding* param_; std::size_t param_version_; binding& result_; std::size_t result_version_; }; class LIBODB_MYSQL_EXPORT insert_statement: public statement { public: virtual ~insert_statement (); insert_statement (connection& conn, const std::string& statement, binding& param); // Return true if successful and false if the row is a duplicate. // All other errors are reported by throwing exceptions. // bool execute (); unsigned long long id (); private: insert_statement (const insert_statement&); insert_statement& operator= (const insert_statement&); private: binding& param_; std::size_t param_version_; }; class LIBODB_MYSQL_EXPORT update_statement: public statement { public: virtual ~update_statement (); // Asssumes that param.bind is a suffix of data.bind. // update_statement (connection& conn, const std::string& statement, binding& param); unsigned long long execute (); private: update_statement (const update_statement&); update_statement& operator= (const update_statement&); private: binding& param_; std::size_t param_version_; }; class LIBODB_MYSQL_EXPORT delete_statement: public statement { public: virtual ~delete_statement (); delete_statement (connection& conn, const std::string& statement, binding& param); unsigned long long execute (); private: delete_statement (const delete_statement&); delete_statement& operator= (const delete_statement&); private: binding& param_; std::size_t param_version_; }; } } #include #endif // ODB_MYSQL_STATEMENT_HXX