From 26e36b3a9d7b49d46ecfa69b447482251acba8ac Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 24 Jan 2024 16:53:38 +0300 Subject: Turn libodb repository into package for muti-package repository --- libodb/odb/connection.hxx | 228 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 libodb/odb/connection.hxx (limited to 'libodb/odb/connection.hxx') diff --git a/libodb/odb/connection.hxx b/libodb/odb/connection.hxx new file mode 100644 index 0000000..8ce4544 --- /dev/null +++ b/libodb/odb/connection.hxx @@ -0,0 +1,228 @@ +// file : odb/connection.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_CONNECTION_HXX +#define ODB_CONNECTION_HXX + +#include + +#include +#include +#include // std::auto_ptr, std::unique_ptr +#include // std::size_t +#include + +#include +#include +#include +#include + +#include // ODB_CXX11 +#include +#include +#include + +namespace odb +{ + class transaction_impl; + class connection_factory; + + class connection; + typedef details::shared_ptr connection_ptr; + + class LIBODB_EXPORT connection: public details::shared_base + { + public: + typedef odb::database database_type; + + database_type& + database (); + + // Transactions. + // + public: + virtual transaction_impl* + begin () = 0; + + // Native database statement execution. Note that unlike the + // versions in the database class, these can be executed + // without a transaction. + // + public: + unsigned long long + execute (const char* statement); + + unsigned long long + execute (const std::string& statement); + + virtual unsigned long long + execute (const char* statement, std::size_t length) = 0; + + // Query preparation. + // + public: + template + prepared_query + prepare_query (const char* name, const char*); + + template + prepared_query + prepare_query (const char* name, const std::string&); + + template + prepared_query + prepare_query (const char* name, const query&); + + template + void + cache_query (const prepared_query&); + +#ifdef ODB_CXX11 + template + void + cache_query (const prepared_query&, std::unique_ptr

params); +#else + template + void + cache_query (const prepared_query&, std::auto_ptr

params); +#endif + + template + prepared_query + lookup_query (const char* name); + + template + prepared_query + lookup_query (const char* name, P*& params); + + // SQL statement tracing. + // + public: + typedef odb::tracer tracer_type; + + void + tracer (tracer_type&); + + void + tracer (tracer_type*); + + tracer_type* + tracer () const; + + public: + // Store the transaction-spacific tracer in the connection. If we + // were to store it in the transaction, then in order to check if + // it was set, we would need to get the transaction instance using + // the current() API. But that requires a TLS lookup, which can be + // slow. + // + tracer_type* + transaction_tracer () const; + + public: + virtual + ~connection (); + + // Recycle the connection to be used by another thread. This call + // invalidates uncached prepared queries. + // + void + recycle (); + + protected: + connection (connection_factory&); + + template ::kind> + struct query_; + + virtual void + cache_query_ (prepared_query_impl* pq, + const std::type_info& ti, + void* params, + const std::type_info* params_info, + void (*params_deleter) (void*)); + + prepared_query_impl* + lookup_query_ (const char* name, + const std::type_info& ti, + void** params, // out + const std::type_info* params_info) const; + + template + static void + params_deleter (void*); + + private: + connection (const connection&); + connection& operator= (const connection&); + + // Prepared query cache. + // + protected: + struct prepared_entry_type + { + details::shared_ptr prep_query; + const std::type_info* type_info; + void* params; + const std::type_info* params_info; + void (*params_deleter) (void*); + }; + + typedef + std::map + prepared_map_type; + + prepared_map_type prepared_map_; + + void + clear_prepared_map (); + + protected: + connection_factory& factory_; + tracer_type* tracer_; + + // Active query result list. + // + protected: + friend class result_impl; + result_impl* results_; + + void + invalidate_results (); + + // Prepared but uncached query list (cached ones are stored in + // prepared_map_). + // + protected: + friend class prepared_query_impl; + prepared_query_impl* prepared_queries_; + + // Implementation details. + // + public: + tracer_type* transaction_tracer_; + }; + + class connection_factory + { + public: + typedef odb::database database_type; + + connection_factory (): db_ (0) {} + + database_type& + database () {return *db_;} + + protected: + database_type* db_; + }; +} + +#include +#include + +#include + +#endif // ODB_CONNECTION_HXX -- cgit v1.1