OCILIB (C and C++ Driver for Oracle)  4.1.0
ocilib_core.hpp
1 /*
2  +-----------------------------------------------------------------------------------------+
3  | |
4  | |
5  | OCILIB ++ - C++ wrapper around OCILIB |
6  | |
7  | (C Wrapper for Oracle OCI) |
8  | |
9  | Website : http://www.ocilib.net |
10  | |
11  | Copyright (c) 2007-2015 Vincent ROGIER <vince.rogier@ocilib.net> |
12  | |
13  +-----------------------------------------------------------------------------------------+
14  | |
15  | This library is free software; you can redistribute it and/or |
16  | modify it under the terms of the GNU Lesser General Public |
17  | License as published by the Free Software Foundation; either |
18  | version 2 of the License, or (at your option) any later version. |
19  | |
20  | This library is distributed in the hope that it will be useful, |
21  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
23  | Lesser General Public License for more details. |
24  | |
25  | You should have received a copy of the GNU Lesser General Public |
26  | License along with this library; if not, write to the Free |
27  | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
28  | |
29  +-----------------------------------------------------------------------------------------+
30 
31  +-----------------------------------------------------------------------------------------+
32  | IMPORTANT NOTICE |
33  +-----------------------------------------------------------------------------------------+
34  | |
35  | This C++ header defines C++ wrapper classes around the OCILIB C API |
36  | It requires a compatible version of OCILIB |
37  +-----------------------------------------------------------------------------------------+
38 
39  */
40 
41 /* --------------------------------------------------------------------------------------------- *
42  * $Id: ocilib_core.hpp, Vincent Rogier $
43  * --------------------------------------------------------------------------------------------- */
44 
45 namespace ocilib
46 {
47 
48 #define ARG_NOT_USED(a) (a) = (a)
49 
52 class Exception;
53 class Connection;
54 class Transaction;
55 class Environment;
56 class Statement;
57 class Resultset;
58 class Date;
59 class Timestamp;
60 class Interval;
61 class TypeInfo;
62 class Reference;
63 class Object;
64 template <class TDataType>
65 class Collection;
66 template<class TLobObjectType, int TLobOracleType>
67 class Lob;
68 class File;
69 class Pool;
70 template<class TLongObjectType, int TLongOracleType>
71 class Long;
72 class Column;
73 class Subscription;
74 class Event;
75 class Agent;
76 class Message;
77 class Enqueue;
78 class Dequeue;
79 class Queue;
80 class QueueTable;
81 class DirectPath;
82 class Thread;
83 class ThreadKey;
84 class Mutex;
85 class BindInfo;
86 
92 template<class TResultType>
93 static TResultType Check(TResultType result);
94 
99 ostring MakeString(const otext *result);
100 
105 Raw MakeRaw(void *result, unsigned int size);
106 
111 template <class THandleType>
113 
118 template <class TEnum>
119 class Enum
120 {
121 public:
122 
123  typedef TEnum type;
124 
125  Enum();
126  Enum(TEnum value);
127 
128  TEnum GetValue();
129 
130  operator TEnum ();
131  operator unsigned int ();
132 
133  bool operator == (const Enum& other) const;
134  bool operator != (const Enum& other) const;
135 
136  bool operator == (const TEnum& other) const;
137  bool operator != (const TEnum& other) const;
138 
139 private:
140 
141  TEnum _value;
142 };
143 
148 template <class TEnum>
149 class Flags
150 {
151 public:
152 
153  typedef TEnum type;
154 
155  Flags();
156  Flags(TEnum flag);
157  Flags(const Flags& other);
158  Flags operator~ () const;
159 
160  Flags operator | (TEnum other) const;
161  Flags operator & (TEnum other) const;
162  Flags operator ^ (TEnum other) const;
163 
164  Flags operator | (const Flags& other) const;
165  Flags operator & (const Flags& other) const;
166  Flags operator ^ (const Flags& other) const;
167 
168  Flags& operator |= (TEnum other);
169  Flags& operator &= (TEnum other);
170  Flags& operator ^= (TEnum other);
171 
172  Flags& operator |= (const Flags& other);
173  Flags& operator &= (const Flags& other);
174  Flags& operator ^= (const Flags& other);
175 
176  bool operator == (TEnum other) const;
177  bool operator == (const Flags& other) const;
178 
179  unsigned int GetValues() const;
180 
181  bool IsSet(TEnum other) const;
182 
183 private:
184 
185  Flags(unsigned int flags);
186 
187  unsigned int _flags;
188 };
189 
190 template< typename TBufferType>
191 class ManagedBuffer
192 {
193 public:
194  ManagedBuffer();
195  ManagedBuffer(size_t size);
196  ManagedBuffer(TBufferType *buffer, size_t size);
197 
198  ~ManagedBuffer();
199 
200  operator TBufferType* () const;
201  operator const TBufferType* () const;
202 
203 private:
204 
205  TBufferType* _buffer;
206  size_t _size;
207 };
208 
209 class Locker
210 {
211 public:
212 
213  Locker();
214  virtual ~Locker();
215 
216  void Lock();
217  void Unlock();
218 
219  void SetAccessMode(bool threaded);
220 
221 private:
222 
223  MutexHandle _mutex;
224 };
225 
226 class Lockable
227 {
228 public:
229 
230  Lockable();
231  virtual ~Lockable();
232 
233  void SetLocker(Locker *locker);
234 
235  void Lock();
236  void Unlock();
237 
238 private:
239 
240  Locker *_locker;
241 };
242 
243 template <class TKey, class TValue>
244 class ConcurrentMap : public Lockable
245 {
246 public:
247 
248  ConcurrentMap();
249  virtual ~ConcurrentMap();
250 
251  void Remove(TKey key);
252  TValue Get(TKey key);
253  void Set(TKey key, TValue value);
254  void Clear();
255  size_t GetSize();
256 
257 private:
258 
259  std::map<TKey, TValue> _map;
260 
261 };
262 
263 template <class TValue>
264 class ConcurrentList : public Lockable
265 {
266 public:
267 
268  ConcurrentList();
269  virtual ~ConcurrentList();
270 
271  void Add(TValue value);
272  void Remove(TValue value);
273  void Clear();
274  size_t GetSize();
275  bool Exists(TValue value);
276 
277  template<class TPredicate>
278  bool FindIf(TPredicate predicate, TValue &value);
279 
280  template<class TAction>
281  void ForEach(TAction action);
282 
283 private:
284 
285  std::list<TValue> _list;
286 };
287 
288 class Handle
289 {
290 public:
291 
292  virtual ~Handle() {}
293  virtual ConcurrentList<Handle *> & GetChildren() = 0;
294  virtual void DetachFromHolders() = 0;
295  virtual void DetachFromParent() = 0;
296 };
297 
302 template<class THandleType>
303 class HandleHolder
304 {
305 public:
306 
307  bool IsNull() const;
308 
309  operator bool();
310  operator bool() const;
311 
312  operator THandleType();
313  operator THandleType() const;
314 
315 protected:
316 
317  class SmartHandle;
318 
319  HandleHolder(const HandleHolder &other);
320  HandleHolder();
321  ~HandleHolder();
322 
323  HandleHolder& operator= (const HandleHolder &other);
324 
325  typedef boolean(OCI_API *HandleFreeFunc)(AnyPointer handle);
326 
327  Handle* GetHandle() const;
328 
329  void Acquire(THandleType handle, HandleFreeFunc func, Handle *parent);
330  void Acquire(HandleHolder &other);
331  void Release();
332 
333  class SmartHandle : public Handle
334  {
335  public:
336 
337  SmartHandle(HandleHolder *holder, THandleType handle, HandleFreeFunc func, Handle *parent);
338  virtual ~SmartHandle();
339 
340  void Acquire(HandleHolder *holder);
341  void Release(HandleHolder *holder);
342 
343  const THandleType GetHandle() const;
344 
345  Handle *GetParent() const;
346 
347  AnyPointer GetExtraInfos() const;
348  void SetExtraInfos(AnyPointer extraInfo);
349 
350  bool IsLastHolder(HandleHolder *holder);
351 
352  ConcurrentList<Handle *> & GetChildren();
353  void DetachFromHolders();
354  void DetachFromParent();
355 
356  private:
357 
358  static void DeleteHandle(Handle *handle);
359  static void ResetHolder(HandleHolder *holder);
360 
361  ConcurrentList<HandleHolder *> _holders;
362  ConcurrentList<Handle *> _children;
363 
364  Locker _locker;
365 
366  THandleType _handle;
367  HandleFreeFunc _func;
368  Handle *_parent;
369  AnyPointer _extraInfo;
370  };
371 
372 protected:
373 
374  SmartHandle *_smartHandle;
375  };
376 
383 {
384 public:
385 
386  virtual ~Streamable() {}
387 
388  operator ostring() const
389  {
390  return ToString();
391  }
392 
393  virtual ostring ToString() const = 0;
394 
395  template <class TStream>
396  friend TStream& operator << (TStream &lhs, const Streamable &rhs)
397  {
398  lhs << static_cast<ostring>(rhs);
399  return lhs;
400  }
401 };
402 
403 template <class TValueType>
404 class BindValue
405 {
406 public:
407 
408  BindValue();
409  BindValue(TValueType value);
410 
411  operator TValueType() const;
412 
413 private:
414 
415  TValueType _value;
416 };
417 
418 class BindObject
419 {
420 public:
421 
422  BindObject(const Statement &statement, const ostring& name);
423 
424  virtual ~BindObject();
425 
426  ostring GetName() const;
427 
428  Statement GetStatement() const;
429 
430  virtual void SetInData() = 0;
431  virtual void SetOutData() = 0;
432 
433 protected:
434 
435  ostring _name;
436  OCI_Statement *_pStatement;
437 };
438 
439 class BindArray : public BindObject
440 {
441 public:
442 
443  BindArray(const Statement &statement, const ostring& name);
444  virtual ~BindArray();
445 
446  template <class TObjectType, class TDataType>
447  void SetVector(std::vector<TObjectType> & vector, unsigned int mode, unsigned int elemSize);
448 
449  template <class TObjectType, class TDataType>
450  TDataType * GetData () const;
451 
452  void SetInData();
453  void SetOutData();
454 
455 private:
456 
457  class AbstractBindArrayObject
458  {
459  public:
460  AbstractBindArrayObject() { }
461  virtual ~AbstractBindArrayObject() { }
462  virtual void SetInData() = 0;
463  virtual void SetOutData() = 0;
464  ostring GetName();
465  };
466 
467  template <class TObjectType, class TDataType>
468  class BindArrayObject : public AbstractBindArrayObject
469  {
470  private:
471 
472  OCI_Statement *_pStatement;
473  ostring _name;
474  std::vector<TObjectType> & _vector;
475  TDataType *_data;
476  unsigned int _mode;
477  unsigned int _elemCount;
478  unsigned int _elemSize;
479 
480  public:
481 
482  BindArrayObject(const Statement &statement, const ostring& name, std::vector<TObjectType> &vector, unsigned int mode, unsigned int elemSize);
483  virtual ~BindArrayObject();
484  void SetInData();
485  void SetOutData();
486  ostring GetName();
487 
488  operator std::vector<TObjectType> & () const;
489  operator TDataType * () const;
490 
491  private:
492 
493  void AllocData();
494  void FreeData();
495  };
496 
497  AbstractBindArrayObject * _object;
498 };
499 
500 template <class TNativeType, class TObjectType>
501 class BindAdaptor : public BindObject
502 {
503  friend class Statement;
504 
505 public:
506 
507  operator TNativeType *() const;
508 
509  void SetInData();
510  void SetOutData();
511 
512  BindAdaptor(const Statement &statement, const ostring& name, TObjectType &object, unsigned int size);
513  virtual ~BindAdaptor();
514 
515 private:
516 
517  TObjectType& _object;
518  TNativeType* _data;
519  unsigned int _size;
520 };
521 
522 class BindsHolder
523 {
524 public:
525 
526  BindsHolder(const Statement &statement);
527  ~BindsHolder();
528 
529  void Clear();
530 
531  void AddBindObject(BindObject *bindObject);
532 
533  void SetOutData();
534  void SetInData();
535 
536 private:
537 
538  std::vector<BindObject *> _bindObjects;
539  OCI_Statement * _pStatement;
540 };
541 
542 }
ostring MakeString(const otext *result)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Definition: ocilib_impl.hpp:65
OCI_Mutex * MutexHandle
Alias for an OCI_Mutex pointer.
Definition: ocilib.hpp:200
struct OCI_Statement OCI_Statement
Oracle SQL or PL/SQL statement.
Definition: ocilib.h:455
Raw MakeRaw(void *result, unsigned int size)
Internal usage. Constructs a C++ Raw object from the given OCILIB raw buffer.
Definition: ocilib_impl.hpp:70
Template Enum template class providing some type safety to some extends for manipulating enum variabl...
void * AnyPointer
Alias for the generic void pointer.
Definition: ocilib.hpp:182
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:191
static TResultType Check(TResultType 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.
Definition: ocilib_impl.hpp:53
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:173