From d6518580059c6a0d34d7a1683fabc3bfcc4b5e27 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Mon, 5 Sep 2011 11:58:14 +0200 Subject: Add RAII handle management for OCI handles and descriptors --- odb/oracle/auto-descriptor.cxx | 22 ++++ odb/oracle/auto-descriptor.hxx | 95 ++++++++++++++ odb/oracle/auto-handle.cxx | 39 ++++++ odb/oracle/auto-handle.hxx | 272 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 428 insertions(+) create mode 100644 odb/oracle/auto-descriptor.cxx create mode 100644 odb/oracle/auto-descriptor.hxx create mode 100644 odb/oracle/auto-handle.cxx create mode 100644 odb/oracle/auto-handle.hxx diff --git a/odb/oracle/auto-descriptor.cxx b/odb/oracle/auto-descriptor.cxx new file mode 100644 index 0000000..17b9d4d --- /dev/null +++ b/odb/oracle/auto-descriptor.cxx @@ -0,0 +1,22 @@ +// file : odb/oracle/auto-descriptor.cxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include + +#include + +namespace odb +{ + namespace oracle + { + void + oci_descriptor_free (void* d, ub4 type) + { + OCIDescriptorFree (d, type); + } + + const ub4 descriptor_type_traits::dtype = OCI_DTYPE_PARAM; + } +} diff --git a/odb/oracle/auto-descriptor.hxx b/odb/oracle/auto-descriptor.hxx new file mode 100644 index 0000000..9e3172c --- /dev/null +++ b/odb/oracle/auto-descriptor.hxx @@ -0,0 +1,95 @@ +// file : odb/oracle/auto-descriptor.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_AUTO_DESCRIPTOR_HXX +#define ODB_ORACLE_AUTO_DESCRIPTOR_HXX + +#include + +#include +#include + +#include + +namespace odb +{ + namespace oracle + { + // + // descriptor_type_traits + // + + template + struct descriptor_type_traits; + + template <> + struct descriptor_type_traits + { static const ub4 dtype; }; + + // + // descriptor_traits + // + + LIBODB_ORACLE_EXPORT void + oci_descriptor_free (void* descriptor, ub4 type); + + template + struct descriptor_traits + { + static void + release (OCIParam* d) + { + oci_descriptor_free (d, descriptor_type_traits::dtype); + } + }; + + template + class auto_descriptor + { + public: + auto_descriptor (D* d = 0) + : d_ (d) + { + } + + ~auto_descriptor () + { + if (d_ != 0) + descriptor_traits::release (d_); + } + + operator D* () + { + return d_; + } + + D* + get () + { + return d_; + } + + void + reset (D* d = 0) + { + if (d_ != 0) + descriptor_traits::release (d_); + + d_ = d; + } + + private: + auto_descriptor (const auto_descriptor&); + auto_descriptor& operator= (const auto_descriptor&); + + private: + D* d_; + }; + } +} + +#include + +#endif // ODB_ORACLE_AUTO_DESCRIPTOR_HXX diff --git a/odb/oracle/auto-handle.cxx b/odb/oracle/auto-handle.cxx new file mode 100644 index 0000000..fea92bd --- /dev/null +++ b/odb/oracle/auto-handle.cxx @@ -0,0 +1,39 @@ +// file : odb/oracle/auto-handle.cxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include + +#include + +namespace odb +{ + namespace oracle + { + void + oci_handle_free (void* h, ub4 t) + { + OCIHandleFree (h, t); + } + + void handle_traits:: + release (OCISvcCtx* h, OCIError* e) + { + OCISessionRelease (h, e, 0, 0, OCI_DEFAULT); + } + + void handle_traits:: + release (OCIStmt* h, ub4 m, OCIError* e) + { + OCIStmtRelease (h, e, 0, 0, m); + } + + const ub4 handle_type_traits::htype = OCI_HTYPE_ENV; + const ub4 handle_type_traits::htype = OCI_HTYPE_ERROR; + const ub4 handle_type_traits::htype = OCI_HTYPE_SVCCTX; + const ub4 handle_type_traits::htype = OCI_HTYPE_STMT; + const ub4 handle_type_traits::htype = OCI_HTYPE_AUTHINFO; + const ub4 handle_type_traits::htype = OCI_HTYPE_TRANS; + } +} diff --git a/odb/oracle/auto-handle.hxx b/odb/oracle/auto-handle.hxx new file mode 100644 index 0000000..7e4b83e --- /dev/null +++ b/odb/oracle/auto-handle.hxx @@ -0,0 +1,272 @@ +// file : odb/oracle/auto-handle.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_AUTO_HANDLE_HXX +#define ODB_ORACLE_AUTO_HANDLE_HXX + +#include + +#include +#include + +#include + +namespace odb +{ + namespace oracle + { + // + // handle_type_traits + // + + template + struct handle_type_traits; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits + { static const ub4 htype; }; + + // + // handle_traits + // + + LIBODB_ORACLE_EXPORT void + oci_handle_free (void* handle, ub4 type); + + template + struct handle_traits + { + static void + release (H* h) + { + oci_handle_free (h, handle_type_traits::htype); + } + }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_traits + { + static void + release (OCISvcCtx*, OCIError*); + }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_traits + { + static void + release (OCIStmt*, ub4 release_mode, OCIError*); + }; + + // + // auto_handle + // + + template + class auto_handle + { + public: + auto_handle (H* h = 0) + : h_ (h) + { + } + + ~auto_handle () + { + if (h_ != 0) + handle_traits::release (h_); + } + + operator H* () + { + return h_; + } + + H* + get () + { + return h_; + } + + H* + release () + { + H* h (h_); + h_ = 0; + return h; + } + + void + reset (H* h = 0) + { + if (h_ != 0) + handle_traits::release (h_); + + h_ = h; + } + + private: + auto_handle (const auto_handle&); + auto_handle& operator= (const auto_handle&); + + private: + H* h_; + }; + + // + // auto_handle + // + + template <> + class LIBODB_ORACLE_EXPORT auto_handle + { + public: + auto_handle () + : h_ (0) + { + } + + auto_handle (OCISvcCtx* h, OCIError* e) + : h_ (h), e_ (e) + { + } + + ~auto_handle () + { + if (h_ != 0) + handle_traits::release (h_, e_); + } + + operator OCISvcCtx* () + { + return h_; + } + + OCISvcCtx* + get () + { + return h_; + } + + OCISvcCtx* + release () + { + OCISvcCtx* h (h_); + h_ = 0; + + return h; + } + + void + reset () + { + if (h_ != 0) + { + handle_traits::release (h_, e_); + h_ = 0; + } + } + + void + reset (OCISvcCtx* h, OCIError* e) + { + if (h_ != 0) + handle_traits::release (h_, e_); + + h_ = h; + e_ = e; + } + + private: + OCISvcCtx* h_; + OCIError* e_; + }; + + // + // auto_handle + // + + template <> + class LIBODB_ORACLE_EXPORT auto_handle + { + public: + auto_handle () + : h_ (0) + { + } + + auto_handle (OCIStmt* h, ub4 release_mode, OCIError* e) + : h_ (h), release_mode_ (release_mode), e_ (e) + { + } + + ~auto_handle () + { + if (h_ != 0) + handle_traits::release (h_, release_mode_, e_); + } + + operator OCIStmt* () + { + return h_; + } + + OCIStmt* + get () + { + return h_; + } + + void + reset () + { + if (h_ != 0) + { + handle_traits::release (h_, release_mode_, e_); + h_ = 0; + } + } + + void + reset (OCIStmt* h, ub4 release_mode, OCIError* e) + { + if (h_ != 0) + handle_traits::release (h_, release_mode_, e_); + + h_ = h; + release_mode_ = release_mode; + e_ = e; + } + + private: + OCIStmt* h_; + ub4 release_mode_; + OCIError* e_; + }; + } +} + +#include + +#endif // ODB_ORACLE_AUTO_HANDLE_HXX -- cgit v1.1