diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-09-05 11:58:14 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-09-05 11:58:14 +0200 |
commit | d6518580059c6a0d34d7a1683fabc3bfcc4b5e27 (patch) | |
tree | 1d0cb1b8fd8f7fb98b52a8433c5cc4d1ef4383f1 | |
parent | 83d687748558cb4f138cecfff2ccbe5cbbc0d761 (diff) |
Add RAII handle management for OCI handles and descriptors
-rw-r--r-- | odb/oracle/auto-descriptor.cxx | 22 | ||||
-rw-r--r-- | odb/oracle/auto-descriptor.hxx | 95 | ||||
-rw-r--r-- | odb/oracle/auto-handle.cxx | 39 | ||||
-rw-r--r-- | odb/oracle/auto-handle.hxx | 272 |
4 files changed, 428 insertions, 0 deletions
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 <constantin@codesynthesis.com> +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include <oci.h> + +#include <odb/oracle/auto-descriptor.hxx> + +namespace odb +{ + namespace oracle + { + void + oci_descriptor_free (void* d, ub4 type) + { + OCIDescriptorFree (d, type); + } + + const ub4 descriptor_type_traits<OCIParam>::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 <constantin@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <odb/oracle/version.hxx> +#include <odb/oracle/oracle-fwd.hxx> + +#include <odb/oracle/details/export.hxx> + +namespace odb +{ + namespace oracle + { + // + // descriptor_type_traits + // + + template <typename D> + struct descriptor_type_traits; + + template <> + struct descriptor_type_traits<OCIParam> + { static const ub4 dtype; }; + + // + // descriptor_traits + // + + LIBODB_ORACLE_EXPORT void + oci_descriptor_free (void* descriptor, ub4 type); + + template <typename D> + struct descriptor_traits + { + static void + release (OCIParam* d) + { + oci_descriptor_free (d, descriptor_type_traits<D>::dtype); + } + }; + + template <typename D> + class auto_descriptor + { + public: + auto_descriptor (D* d = 0) + : d_ (d) + { + } + + ~auto_descriptor () + { + if (d_ != 0) + descriptor_traits<D>::release (d_); + } + + operator D* () + { + return d_; + } + + D* + get () + { + return d_; + } + + void + reset (D* d = 0) + { + if (d_ != 0) + descriptor_traits<D>::release (d_); + + d_ = d; + } + + private: + auto_descriptor (const auto_descriptor&); + auto_descriptor& operator= (const auto_descriptor&); + + private: + D* d_; + }; + } +} + +#include <odb/post.hxx> + +#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 <constantin@codesynthesis.com> +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include <oci.h> + +#include <odb/oracle/auto-handle.hxx> + +namespace odb +{ + namespace oracle + { + void + oci_handle_free (void* h, ub4 t) + { + OCIHandleFree (h, t); + } + + void handle_traits<OCISvcCtx>:: + release (OCISvcCtx* h, OCIError* e) + { + OCISessionRelease (h, e, 0, 0, OCI_DEFAULT); + } + + void handle_traits<OCIStmt>:: + release (OCIStmt* h, ub4 m, OCIError* e) + { + OCIStmtRelease (h, e, 0, 0, m); + } + + const ub4 handle_type_traits<OCIEnv>::htype = OCI_HTYPE_ENV; + const ub4 handle_type_traits<OCIError>::htype = OCI_HTYPE_ERROR; + const ub4 handle_type_traits<OCISvcCtx>::htype = OCI_HTYPE_SVCCTX; + const ub4 handle_type_traits<OCIStmt>::htype = OCI_HTYPE_STMT; + const ub4 handle_type_traits<OCIAuthInfo>::htype = OCI_HTYPE_AUTHINFO; + const ub4 handle_type_traits<OCITrans>::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 <constantin@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <odb/oracle/version.hxx> +#include <odb/oracle/oracle-fwd.hxx> + +#include <odb/oracle/details/export.hxx> + +namespace odb +{ + namespace oracle + { + // + // handle_type_traits + // + + template <typename H> + struct handle_type_traits; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits<OCIEnv> + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits<OCIError> + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits<OCISvcCtx> + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits<OCIStmt> + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits<OCIAuthInfo> + { static const ub4 htype; }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_type_traits<OCITrans> + { static const ub4 htype; }; + + // + // handle_traits + // + + LIBODB_ORACLE_EXPORT void + oci_handle_free (void* handle, ub4 type); + + template <typename H> + struct handle_traits + { + static void + release (H* h) + { + oci_handle_free (h, handle_type_traits<H>::htype); + } + }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_traits<OCISvcCtx> + { + static void + release (OCISvcCtx*, OCIError*); + }; + + template <> + struct LIBODB_ORACLE_EXPORT handle_traits<OCIStmt> + { + static void + release (OCIStmt*, ub4 release_mode, OCIError*); + }; + + // + // auto_handle + // + + template <typename H> + class auto_handle + { + public: + auto_handle (H* h = 0) + : h_ (h) + { + } + + ~auto_handle () + { + if (h_ != 0) + handle_traits<H>::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<H>::release (h_); + + h_ = h; + } + + private: + auto_handle (const auto_handle&); + auto_handle& operator= (const auto_handle&); + + private: + H* h_; + }; + + // + // auto_handle<OCISvcCtx> + // + + template <> + class LIBODB_ORACLE_EXPORT auto_handle<OCISvcCtx> + { + public: + auto_handle () + : h_ (0) + { + } + + auto_handle (OCISvcCtx* h, OCIError* e) + : h_ (h), e_ (e) + { + } + + ~auto_handle () + { + if (h_ != 0) + handle_traits<OCISvcCtx>::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<OCISvcCtx>::release (h_, e_); + h_ = 0; + } + } + + void + reset (OCISvcCtx* h, OCIError* e) + { + if (h_ != 0) + handle_traits<OCISvcCtx>::release (h_, e_); + + h_ = h; + e_ = e; + } + + private: + OCISvcCtx* h_; + OCIError* e_; + }; + + // + // auto_handle<OCIStmt> + // + + template <> + class LIBODB_ORACLE_EXPORT auto_handle<OCIStmt> + { + 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<OCIStmt>::release (h_, release_mode_, e_); + } + + operator OCIStmt* () + { + return h_; + } + + OCIStmt* + get () + { + return h_; + } + + void + reset () + { + if (h_ != 0) + { + handle_traits<OCIStmt>::release (h_, release_mode_, e_); + h_ = 0; + } + } + + void + reset (OCIStmt* h, ub4 release_mode, OCIError* e) + { + if (h_ != 0) + handle_traits<OCIStmt>::release (h_, release_mode_, e_); + + h_ = h; + release_mode_ = release_mode; + e_ = e; + } + + private: + OCIStmt* h_; + ub4 release_mode_; + OCIError* e_; + }; + } +} + +#include <odb/post.hxx> + +#endif // ODB_ORACLE_AUTO_HANDLE_HXX |