OCILIB (C and C++ Driver for Oracle)  4.0.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-2014 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 static void Check();
93 
99 template<class TResultType>
100 static TResultType Check(TResultType result);
101 
106 ostring MakeString(const otext *result);
107 
112 Raw MakeRaw(void *result, unsigned int size);
113 
118 template <class THandleType>
120 
125 template <class TEnum>
126 class Enum
127 {
128 public:
129 
130  typedef TEnum type;
131 
132  Enum();
133  Enum(TEnum value);
134 
135  TEnum GetValue();
136 
137  operator TEnum ();
138  operator unsigned int ();
139 
140  bool operator == (const Enum& other) const;
141  bool operator != (const Enum& other) const;
142 
143  bool operator == (const TEnum& other) const;
144  bool operator != (const TEnum& other) const;
145 
146 private:
147 
148  TEnum _value;
149 };
150 
155 template <class TEnum>
156 class Flags
157 {
158 public:
159 
160  typedef TEnum type;
161 
162  Flags();
163  Flags(TEnum flag);
164  Flags(const Flags& other);
165  Flags operator~ () const;
166 
167  Flags operator | (TEnum other) const;
168  Flags operator & (TEnum other) const;
169  Flags operator ^ (TEnum other) const;
170 
171  Flags operator | (const Flags& other) const;
172  Flags operator & (const Flags& other) const;
173  Flags operator ^ (const Flags& other) const;
174 
175  Flags& operator |= (TEnum other);
176  Flags& operator &= (TEnum other);
177  Flags& operator ^= (TEnum other);
178 
179  Flags& operator |= (const Flags& other);
180  Flags& operator &= (const Flags& other);
181  Flags& operator ^= (const Flags& other);
182 
183  bool operator == (TEnum other) const;
184  bool operator == (const Flags& other) const;
185 
186  unsigned int GetValues() const;
187 
188  bool IsSet(TEnum other) const;
189 
190 private:
191 
192  Flags(unsigned int flags);
193 
194  unsigned int _flags;
195 };
196 
197 template< typename TBufferType>
198 class ManagedBuffer
199 {
200 public:
201  ManagedBuffer();
202  ManagedBuffer(size_t size);
203  ManagedBuffer(TBufferType *buffer, size_t size);
204 
205  ~ManagedBuffer();
206 
207  operator TBufferType* () const;
208  operator const TBufferType* () const;
209 
210 private:
211 
212  TBufferType* _buffer;
213  size_t _size;
214 };
215 
216 template <class TKey, class TValue>
217 class ConcurrentPool
218 {
219 public:
220 
221  ConcurrentPool();
222 
223  void Initialize(unsigned int envMode);
224  void Release();
225 
226  void Remove(TKey key);
227  TValue Get(TKey key) const;
228  void Set(TKey key, TValue value);
229 
230 private:
231 
232  typedef std::map< TKey, TValue > ConcurrentPoolMap;
233 
234  ConcurrentPoolMap _map;
235  MutexHandle _mutex;
236 
237  void Lock() const;
238  void Unlock() const;
239 };
240 
241 class Handle
242 {
243 public:
244 
245  virtual ~Handle() {};
246  virtual std::list<Handle *> & GetChildren() = 0;
247  virtual void DetachFromHolders() = 0;
248  virtual void DetachFromParent() = 0;
249 };
250 
255 template<class THandleType>
256 class HandleHolder
257 {
258 public:
259 
260  bool IsNull() const;
261 
262  operator bool() const;
263  operator THandleType();
264  operator THandleType() const;
265 
266 protected:
267 
268  class SmartHandle;
269 
270  HandleHolder(const HandleHolder &other);
271  HandleHolder();
272  ~HandleHolder();
273 
274  HandleHolder& operator= (const HandleHolder &other);
275 
276  typedef boolean(OCI_API *HandleFreeFunc)(AnyPointer handle);
277 
278  Handle* GetHandle() const;
279 
280  void Acquire(THandleType handle, HandleFreeFunc func, Handle *parent);
281  void Acquire(HandleHolder &other);
282  void Release();
283 
284  class SmartHandle : public Handle
285  {
286  public:
287 
288  SmartHandle(HandleHolder *holder, THandleType handle, HandleFreeFunc func, Handle *parent);
289  virtual ~SmartHandle();
290 
291  void Acquire(HandleHolder *holder);
292  void Release(HandleHolder *holder);
293 
294  THandleType GetHandle() const;
295 
296  Handle *GetParent() const;
297 
298  AnyPointer GetExtraInfos() const;
299  void SetExtraInfos(AnyPointer extraInfo);
300 
301  bool IsLastHolder(HandleHolder *holder) const;
302 
303  std::list<Handle *> & GetChildren();
304  void DetachFromHolders();
305  void DetachFromParent();
306 
307  private:
308 
309  std::list<HandleHolder *> _holders;
310  std::list<Handle *> _children;
311 
312  THandleType _handle;
313  HandleFreeFunc _func;
314  Handle *_parent;
315  AnyPointer _extraInfo;
316  };
317 
318 protected:
319 
320  SmartHandle *_smartHandle;
321  };
322 
323 template <class TValueType>
324 class BindValue
325 {
326 public:
327 
328  BindValue();
329  BindValue(TValueType value);
330 
331  operator TValueType() const;
332 
333 private:
334 
335  TValueType _value;
336 };
337 
338 class BindObject
339 {
340 public:
341 
342  BindObject(const Statement &statement, const ostring& name);
343 
344  virtual ~BindObject();
345 
346  ostring GetName() const;
347 
348  Statement GetStatement() const;
349 
350  virtual void SetInData() = 0;
351  virtual void SetOutData() = 0;
352 
353 private:
354 
355  ostring _name;
356  OCI_Statement *_pStatement;
357 };
358 
359 class BindArray : public BindObject
360 {
361 public:
362 
363  BindArray(const Statement &statement, const ostring& name);
364  virtual ~BindArray();
365 
366  template <class TObjectType, class TDataType>
367  void SetVector(std::vector<TObjectType> & vector, unsigned int mode, unsigned int elemSize);
368 
369  template <class TObjectType, class TDataType>
370  TDataType * GetData () const;
371 
372  void SetInData();
373  void SetOutData();
374 
375 private:
376 
377  class AbstractBindArrayObject
378  {
379  public:
380  AbstractBindArrayObject() { }
381  virtual ~AbstractBindArrayObject() { }
382  virtual void SetInData() = 0;
383  virtual void SetOutData() = 0;
384  ostring GetName();
385  };
386 
387  template <class TObjectType, class TDataType>
388  class BindArrayObject : public AbstractBindArrayObject
389  {
390  private:
391 
392  OCI_Statement *_pStatement;
393  ostring _name;
394  std::vector<TObjectType> & _vector;
395  TDataType *_data;
396  unsigned int _mode;
397  unsigned int _elemCount;
398  unsigned int _elemSize;
399 
400  public:
401 
402  BindArrayObject(const Statement &statement, const ostring& name, std::vector<TObjectType> &vector, unsigned int mode, unsigned int elemSize);
403  virtual ~BindArrayObject();
404  void SetInData();
405  void SetOutData();
406  ostring GetName();
407 
408  operator std::vector<TObjectType> & () const;
409  operator TDataType * () const;
410 
411  private:
412 
413  void AllocData();
414  void FreeData();
415  };
416 
417  AbstractBindArrayObject * _object;
418 };
419 
420 template <class TNativeType, class TObjectType>
421 class BindAdaptor : public BindObject
422 {
423  friend class Statement;
424 
425 public:
426 
427  operator TNativeType *() const;
428 
429  void SetInData();
430  void SetOutData();
431 
432  BindAdaptor(const Statement &statement, const ostring& name, TObjectType &object, unsigned int size);
433  virtual ~BindAdaptor();
434 
435 private:
436 
437  TObjectType& _object;
438  TNativeType* _data;
439  unsigned int _size;
440 };
441 
442 class BindsHolder
443 {
444 public:
445 
446  BindsHolder(const Statement &statement);
447  ~BindsHolder();
448 
449  void Clear();
450 
451  void AddBindObject(BindObject *bindObject);
452 
453  void SetOutData();
454  void SetInData();
455 
456 private:
457 
458  std::vector<BindObject *> _bindObjects;
459  OCI_Statement * _pStatement;
460 };
461 
462 }
ostring MakeString(const otext *result)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Definition: ocilib_impl.hpp:70
OCI_Mutex * MutexHandle
Alias for an OCI_Mutex pointer.
Definition: ocilib.hpp:192
Raw MakeRaw(void *result, unsigned int size)
Internal usage. Constructs a C++ Raw object from the given OCILIB raw buffer.
Definition: ocilib_impl.hpp:76
Template Enum template class providing some type safety to some extends for manipulating enum variabl...
Oracle SQL or PL/SQL statement.
void * AnyPointer
Alias for the generic void pointer.
Definition: ocilib.hpp:174
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:183
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:165
static void Check()
Internal usage. Checks if the last OCILIB method call has raised an error. If so, it raises a C++ exc...
Definition: ocilib_impl.hpp:52