OCILIB (C and C++ Driver for Oracle)  4.6.3
ocilib_core.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2019 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 /*
22  * IMPORTANT NOTICE
23  *
24  * This C++ header defines C++ wrapper classes around the OCILIB C API
25  * It requires a compatible version of OCILIB
26  *
27  */
28 
29 #pragma once
30 
31 #include <map>
32 
33 namespace ocilib
34 {
35 
36 /* Try to guess C++ Compiler capabilities ... */
37 
38 #define CPP_98 199711L
39 #define CPP_11 201103L
40 #define CPP_14 201402L
41 
42 #if __cplusplus < CPP_11
43  #if defined(__GNUC__)
44  #if defined(__GXX_EXPERIMENTAL_CXX0X__)
45  #define HAVE_NULLPTR
46  #define HAVE_MOVE_SEMANTICS
47  #else
48  #define override
49  #endif
50  #elif defined(_MSC_VER)
51  #if _MSC_VER >= 1600
52  #define HAVE_NULLPTR
53  #define HAVE_MOVE_SEMANTICS
54  #else
55  #define override
56  #endif
57  #endif
58 #else
59  #define HAVE_NULLPTR
60  #define HAVE_MOVE_SEMANTICS
61 #endif
62 
63 
64 /* guessing if nullptr is supported */
65 
66 #ifndef HAVE_NULLPTR
67  #define nullptr 0
68 #endif
69 
70 #define ARG_NOT_USED(a) (a) = (a)
71 
72 /* class forward declarations */
73 
74 class Exception;
75 class Connection;
76 class Transaction;
77 class Environment;
78 class Statement;
79 class Resultset;
80 class Date;
81 class Timestamp;
82 class Interval;
83 class TypeInfo;
84 class Reference;
85 class Object;
86 template<class>
87 class Element;
88 template<class>
89 class Iterator;
90 template<class>
91 class Collection;
92 template<class, int>
93 class Lob;
94 class File;
95 class Pool;
96 template<class, int>
97 class Long;
98 class Column;
99 class Subscription;
100 class Event;
101 class Agent;
102 class Message;
103 class Enqueue;
104 class Dequeue;
105 class Queue;
106 class QueueTable;
107 class DirectPath;
108 class Thread;
109 class ThreadKey;
110 class Mutex;
111 class BindInfo;
112 
117 template<class T> struct BindResolver {};
118 
124 template<class T>
125 static T Check(T result);
126 
131 ostring MakeString(const otext *result, int size = -1);
132 
137 Raw MakeRaw(void *result, unsigned int size);
138 
143 template<class>
145 
150 template<class T>
151 class Enum
152 {
153 public:
154 
155  typedef T Type;
156 
157  Enum();
158  Enum(T value);
159 
160  T GetValue();
161 
162  operator T ();
163  operator unsigned int () const;
164 
165  bool operator == (const Enum& other) const;
166  bool operator != (const Enum& other) const;
167 
168  bool operator == (const T& other) const;
169  bool operator != (const T& other) const;
170 
171 private:
172 
173  T _value;
174 };
175 
180 template<class T>
181 class Flags
182 {
183 public:
184 
185  typedef T Type;
186 
187  Flags();
188  Flags(T flag);
189  Flags(const Flags& other);
190  Flags operator~ () const;
191 
192  Flags operator | (T other) const;
193  Flags operator & (T other) const;
194  Flags operator ^ (T other) const;
195 
196  Flags operator | (const Flags& other) const;
197  Flags operator & (const Flags& other) const;
198  Flags operator ^ (const Flags& other) const;
199 
200  Flags& operator |= (T other);
201  Flags& operator &= (T other);
202  Flags& operator ^= (T other);
203 
204  Flags& operator |= (const Flags& other);
205  Flags& operator &= (const Flags& other);
206  Flags& operator ^= (const Flags& other);
207 
208  bool operator == (T other) const;
209  bool operator == (const Flags& other) const;
210 
211  unsigned int GetValues() const;
212 
213  bool IsSet(T other) const;
214 
215 private:
216 
217  Flags(unsigned int flags);
218 
219  unsigned int _flags;
220 };
221 
222 template< typename T>
223 class ManagedBuffer
224 {
225 public:
226  ManagedBuffer();
227  ManagedBuffer(size_t size);
228  ManagedBuffer(T *buffer, size_t size);
229 
230  ~ManagedBuffer();
231 
232  operator T* () const;
233  operator const T* () const;
234 
235 private:
236 
237  T* _buffer;
238  size_t _size;
239 };
240 
241 class Locker
242 {
243 public:
244 
245  Locker();
246  virtual ~Locker();
247 
248  void Lock() const;
249  void Unlock() const;
250 
251  void SetAccessMode(bool threaded);
252 
253 private:
254 
255  MutexHandle _mutex;
256 };
257 
258 class Lockable
259 {
260 public:
261 
262  Lockable();
263  virtual ~Lockable();
264 
265  void SetLocker(Locker *locker);
266 
267  void Lock() const;
268  void Unlock() const;
269 
270 private:
271 
272  Locker *_locker;
273 };
274 
275 template<class K, class V>
276 class ConcurrentMap : public Lockable
277 {
278 public:
279 
280  ConcurrentMap();
281  virtual ~ConcurrentMap();
282 
283  void Remove(K key);
284  V Get(K key);
285  void Set(K key, V value);
286  void Clear();
287  size_t GetSize();
288 
289 private:
290 
291  std::map<K, V> _map;
292 
293 };
294 
295 template<class T>
296 class ConcurrentList : public Lockable
297 {
298 public:
299 
300  ConcurrentList();
301  virtual ~ConcurrentList();
302 
303  void Add(T value);
304  void Remove(T value);
305  void Clear();
306  size_t GetSize();
307  bool Exists(const T &value);
308 
309  template<class P>
310  bool FindIf(P predicate, T &value);
311 
312  template<class A>
313  void ForEach(A action);
314 
315 private:
316 
317  std::list<T> _list;
318 };
319 
320 class Handle
321 {
322 public:
323 
324  virtual ~Handle() {}
325  virtual ConcurrentList<Handle *> & GetChildren() = 0;
326  virtual void DetachFromHolders() = 0;
327  virtual void DetachFromParent() = 0;
328 };
329 
334 template<class T>
335 class HandleHolder
336 {
337 public:
338 
339  bool IsNull() const;
340 
341  operator bool();
342  operator bool() const;
343 
344  operator T();
345  operator T() const;
346 
347 protected:
348 
349  class SmartHandle;
350 
351  HandleHolder(const HandleHolder &other);
352  HandleHolder();
353  ~HandleHolder();
354 
355  HandleHolder& operator= (const HandleHolder &other);
356 
357  typedef boolean(OCI_API *HandleFreeFunc)(AnyPointer handle);
358 
359  typedef void(*SmartHandleFreeNotifyFunc)(SmartHandle *smartHandle);
360 
361  Handle* GetHandle() const;
362 
363  void Acquire(T handle, HandleFreeFunc handleFreefunc, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle *parent);
364  void Acquire(HandleHolder &other);
365  void Release();
366 
367  class SmartHandle : public Handle
368  {
369  public:
370 
371  SmartHandle(HandleHolder *holder, T handle, HandleFreeFunc handleFreefunc, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle *parent);
372  virtual ~SmartHandle();
373 
374  void Acquire(HandleHolder *holder);
375  void Release(HandleHolder *holder);
376 
377  T GetHandle() const;
378 
379  Handle *GetParent() const;
380 
381  AnyPointer GetExtraInfos() const;
382  void SetExtraInfos(AnyPointer extraInfo);
383 
384  ConcurrentList<Handle *> & GetChildren() override;
385  void DetachFromHolders() override;
386  void DetachFromParent() override;
387 
388  private:
389 
390  static void DeleteHandle(Handle *handle);
391  static void ResetHolder(HandleHolder *holder);
392 
393  ConcurrentList<HandleHolder *> _holders;
394  ConcurrentList<Handle *> _children;
395 
396  Locker _locker;
397 
398  T _handle;
399  HandleFreeFunc _handleFreeFunc;
400  SmartHandleFreeNotifyFunc _freeNotifyFunc;
401  Handle *_parent;
402  AnyPointer _extraInfo;
403  };
404 
405  SmartHandle *_smartHandle;
406  };
407 
414 {
415 public:
416 
417  virtual ~Streamable() {}
418 
419  operator ostring() const
420  {
421  return ToString();
422  }
423 
424  virtual ostring ToString() const = 0;
425 
426  template<class T>
427  friend T& operator << (T &lhs, const Streamable &rhs)
428  {
429  lhs << static_cast<ostring>(rhs);
430  return lhs;
431  }
432 };
433 
434 class BindObject
435 {
436 public:
437 
438  BindObject(const Statement &statement, const ostring& name, unsigned int mode);
439 
440  virtual ~BindObject();
441 
442  ostring GetName() const;
443 
444  Statement GetStatement() const;
445 
446  unsigned int GetMode() const;
447 
448  virtual void SetInData() = 0;
449  virtual void SetOutData() = 0;
450 
451 protected:
452 
453  const Statement& _statement;
454  ostring _name;
455  unsigned int _mode;
456 };
457 
458 class BindArray : public BindObject
459 {
460 public:
461 
462  BindArray(const Statement &statement, const ostring& name, unsigned int mode);
463  virtual ~BindArray();
464 
465  template<class T>
466  void SetVector(std::vector<T> & vector, bool isPlSqlTable, unsigned int elemSize);
467 
468  template<class T>
469  typename BindResolver<T>::OutputType * GetData() const;
470 
471  void SetInData() override;
472  void SetOutData() override;
473 
474  unsigned int GetSize();
475  unsigned int GetSizeForBindCall();
476 
477 private:
478 
479  class AbstractBindArrayObject
480  {
481  public:
482  AbstractBindArrayObject() { }
483  virtual ~AbstractBindArrayObject() { }
484  virtual void SetInData() = 0;
485  virtual void SetOutData() = 0;
486  virtual ostring GetName() = 0;
487  virtual bool IsHandleObject() = 0;
488  virtual unsigned int GetSize() = 0;
489  virtual unsigned int GetSizeForBindCall() = 0;
490  };
491 
492  template<class T>
493  class BindArrayObject : public AbstractBindArrayObject
494  {
495  public:
496 
497  typedef T ObjectType;
498  typedef std::vector<ObjectType> ObjectVector;
499  typedef typename BindResolver<ObjectType>::OutputType NativeType;
500 
501  BindArrayObject(const Statement &statement, const ostring& name, ObjectVector &vector, bool isPlSqlTable, unsigned int mode, unsigned int elemSize);
502  virtual ~BindArrayObject();
503  void SetInData() override;
504  void SetOutData() override;
505  ostring GetName() override;
506  bool IsHandleObject() override;
507  unsigned int GetSize() override;
508  unsigned int GetSizeForBindCall() override;
509 
510  operator ObjectVector & () const;
511  operator NativeType * () const;
512 
513  private:
514 
515  void AllocData();
516  void FreeData() const;
517 
518  const Statement& _statement;
519  ostring _name;
520  ObjectVector& _vector;
521  NativeType *_data;
522  bool _isPlSqlTable;
523  unsigned int _mode;
524  unsigned int _elemCount;
525  unsigned int _elemSize;
526  };
527 
528  AbstractBindArrayObject * _object;
529 };
530 
531 template<class T>
532 class BindObjectAdaptor : public BindObject
533 {
534  friend class Statement;
535 
536 public:
537 
538  typedef T ObjectType;
539  typedef typename BindResolver<ObjectType>::OutputType NativeType;
540 
541  operator NativeType *() const;
542 
543  void SetInData() override;
544  void SetOutData() override;
545 
546  BindObjectAdaptor(const Statement &statement, const ostring& name, unsigned int mode, ObjectType &object, unsigned int size);
547  virtual ~BindObjectAdaptor();
548 
549 private:
550 
551  ObjectType& _object;
552  NativeType* _data;
553  unsigned int _size;
554 };
555 
556 template<class T>
557 class BindTypeAdaptor : public BindObject
558 {
559  friend class Statement;
560 
561 public:
562 
563  typedef T ObjectType;
564  typedef typename BindResolver<ObjectType>::OutputType NativeType;
565 
566  operator NativeType *() const;
567 
568  void SetInData() override;
569  void SetOutData() override;
570 
571  BindTypeAdaptor(const Statement &statement, const ostring& name, unsigned int mode, ObjectType &object);
572  virtual ~BindTypeAdaptor();
573 
574 private:
575 
576  ObjectType& _object;
577  NativeType* _data;
578 };
579 
580 class BindsHolder
581 {
582 public:
583 
584  BindsHolder(const Statement &statement);
585  ~BindsHolder();
586 
587  void Clear();
588 
589  void AddBindObject(BindObject *bindObject);
590 
591  void SetOutData();
592  void SetInData();
593 
594 private:
595 
596  std::vector<BindObject *> _bindObjects;
597  const Statement& _statement;
598 };
599 
600 }
OCILIB ++ Namespace.
OCI_Mutex * MutexHandle
Alias for an OCI_Mutex pointer.
Definition: ocilib.hpp:187
Object used for executing SQL or PL/SQL statement and returning the produced results.
Definition: ocilib.hpp:5689
Raw MakeRaw(void *result, unsigned int size)
Internal usage. Constructs a C++ Raw object from the given OCILIB raw buffer.
Template Enumeration template class providing some type safety to some extends for manipulating enume...
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so, it raises a C++ exception using the retrieved error handle.
void * AnyPointer
Alias for the generic void pointer.
Definition: ocilib.hpp:169
Abstract class allowing derived classes to be compatible with any type supporting the operator << oci...
Template class providing OCILIB handles auto memory, life cycle and scope management.
Template Flags template class providing some type safety to some extends for manipulating flags set v...
std::vector< unsigned char > Raw
C++ counterpart of SQL RAW data type.
Definition: ocilib.hpp:178
ostring MakeString(const otext *result, int size=-1)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Internal usage. Allow resolving a native type used by C API from a C++ type in binding operations...
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets ) ...
Definition: ocilib.hpp:160