Oracle ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
statement.hxx
Go to the documentation of this file.
1 // file : odb/oracle/statement.hxx
2 // copyright : Copyright (c) 2005-2013 Code Synthesis Tools CC
3 // license : ODB NCUEL; see accompanying LICENSE file
4 
5 #ifndef ODB_ORACLE_STATEMENT_HXX
6 #define ODB_ORACLE_STATEMENT_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <string>
11 #include <cstddef> // std::size_t
12 
13 #include <odb/statement.hxx>
14 
15 #include <odb/oracle/version.hxx>
16 #include <odb/oracle/forward.hxx>
17 #include <odb/oracle/binding.hxx>
21 
22 #include <odb/oracle/details/export.hxx>
23 
24 namespace odb
25 {
26  namespace oracle
27  {
28  class LIBODB_ORACLE_EXPORT statement: public odb::statement
29  {
30  public:
32 
33  virtual
34  ~statement () = 0;
35 
36  OCIStmt*
37  handle () const
38  {
39  return stmt_;
40  }
41 
42  virtual const char*
43  text () const;
44 
45  virtual connection_type&
47  {
48  return conn_;
49  }
50 
51  // A statement can be empty. This is used to handle situations
52  // where a SELECT or UPDATE statement ends up not having any
53  // columns after processing. An empty statement cannot be
54  // executed.
55  //
56  bool
57  empty () const
58  {
59  return stmt_ == 0;
60  }
61 
62  protected:
63  // We keep two versions to take advantage of std::string COW.
64  //
65  statement (connection_type&,
66  const std::string& text,
68  const binding* process,
69  bool optimize);
70 
71  statement (connection_type&,
72  const char* text,
74  const binding* process,
75  bool optimize);
76 
77  private:
78  void
79  init (const char* text,
80  std::size_t text_size,
82  const binding* process,
83  bool optimize);
84 
85  protected:
86  struct unbind
87  {
89  oracle::bind* bind; // Corresponding bind entry.
90  void* value; // Actual value passed to OCIBindByPos.
91  };
92 
93  // Bind parameters for this statement. This function must only
94  // be called once. Multiple calls to it will result in memory
95  // leaks due to lost OCIBind resources. Return the actual number
96  // of columns bound.
97  //
98  ub4
99  bind_param (bind*, std::size_t count);
100 
101  // Bind results for this statement. This function must only be
102  // called once. Multiple calls to it will result in memory leaks
103  // due to lost OCIDefine resources. Return the actual number of
104  // columns bound.
105  //
106  ub4
107  bind_result (bind*,
108  std::size_t count,
109  std::size_t lob_prefetch_size = 0);
110 
111  // Rebind LOB input parameters. If a query has made a private copy of
112  // the shared image, any LOB handles that were previously owned by the
113  // shared image are now owned by the private image of the query. These
114  // LOB handles need to be reallocated and redefined so that any unfetched
115  // results may be fetched.
116  //
117  void
118  rebind_result (bind*,
119  std::size_t count,
120  std::size_t lob_prefetch_size = 0);
121 
122  // Stream the result LOBs, calling user callbacks where necessary.
123  // The old_base and new_base arguments can be used to "re-base" the
124  // lob_callback struct pointer (stored in bind::callback), the lob
125  // struct pointer (stored in bind::buffer), and the indicator value
126  // pointer (stored in bind::indicator). This is used by the query
127  // machinery to cause stream_result() to use the callback information
128  // from a copy of the image instead of the bound image.
129  //
130  void
131  stream_result (bind*,
132  std::size_t count,
133  void* old_base = 0,
134  void* new_base = 0);
135 
136  protected:
139 
141  std::size_t usize_;
142  };
143 
144  class LIBODB_ORACLE_EXPORT generic_statement: public statement
145  {
146  public:
147  virtual
148  ~generic_statement ();
149 
150  generic_statement (connection_type&, const std::string& text);
151  generic_statement (connection_type&, const char* text);
152 
153  unsigned long long
154  execute ();
155 
156  private:
158  generic_statement& operator= (const generic_statement&);
159 
160  private:
161  void
162  init ();
163 
164  private:
165  ub2 stmt_type_;
166  bool bound_;
167  };
168 
169  class LIBODB_ORACLE_EXPORT select_statement: public statement
170  {
171  public:
172  virtual
173  ~select_statement ();
174 
176  const std::string& text,
177  bool process_text,
178  bool optimize_text,
179  binding& param,
180  binding& result,
181  std::size_t lob_prefetch_size = 0);
182 
184  const char* text,
185  bool process_text,
186  bool optimize_text,
187  binding& param,
188  binding& result,
189  std::size_t lob_prefetch_size = 0);
190 
192  const std::string& text,
193  bool process_text,
194  bool optimize_text,
195  binding& result,
196  std::size_t lob_prefetch_size = 0);
197 
199  const char* text,
200  bool process_text,
201  bool optimize_text,
202  binding& result,
203  std::size_t lob_prefetch_size = 0);
204 
205  enum result
206  {
208  no_data
209  };
210 
211  void
212  execute ();
213 
214  result
215  fetch ();
216 
217  void
218  stream_result (void* old_base = 0, void* new_base = 0)
219  {
220  statement::stream_result (result_.bind,
221  result_.count,
222  old_base,
223  new_base);
224  }
225 
226  void
227  free_result ();
228 
229  private:
231  select_statement& operator= (const select_statement&);
232 
233  private:
234  binding& result_;
235  std::size_t result_version_;
236  ub4 result_count_; // Actual number of bound columns.
237  const std::size_t lob_prefetch_size_;
238  bool done_;
239  };
240 
241  struct LIBODB_ORACLE_EXPORT auto_result
242  {
243  explicit auto_result (select_statement& s): s_ (s) {}
244  ~auto_result () {s_.free_result ();}
245 
246  private:
247  auto_result (const auto_result&);
248  auto_result& operator= (const auto_result&);
249 
250  private:
251  select_statement& s_;
252  };
253 
254  class LIBODB_ORACLE_EXPORT insert_statement: public statement
255  {
256  public:
257  virtual
258  ~insert_statement ();
259 
261  const std::string& text,
262  bool process_text,
263  binding& param,
264  bool returning);
265 
267  const char* text,
268  bool process_text,
269  binding& param,
270  bool returning);
271 
272  // Return true if successful and false if the row is a duplicate. All
273  // other errors are reported by throwing exceptions.
274  //
275  bool
276  execute ();
277 
278  unsigned long long
279  id ();
280 
281  private:
283  insert_statement& operator= (const insert_statement&);
284 
285  // Only OCI versions 11.2 and greater support conversion of the internal
286  // Oracle type NUMBER to an external 64-bit integer type. If we detect
287  // version 11.2 or greater we provide an unsigned long long image.
288  // Otherwise, we revert to using a NUMBER image and manipulate it using
289  // the custom conversion algorithms found in details/number.hxx.
290  //
291  public:
293  {
294  union
295  {
296  struct
297  {
298  char buffer[21];
300  } number;
301 
302  unsigned long long integer;
303  } id;
304 
306  };
307 
308  private:
309  void
310  init (binding& param, bool returning);
311 
312  private:
313  id_bind_type id_bind_;
314  };
315 
316  class LIBODB_ORACLE_EXPORT update_statement: public statement
317  {
318  public:
319  virtual
320  ~update_statement ();
321 
323  const std::string& text,
324  bool process_text,
325  binding& param);
326 
328  const char* text,
329  bool process_text,
330  binding& param);
331 
332  unsigned long long
333  execute ();
334 
335  private:
337  update_statement& operator= (const update_statement&);
338  };
339 
340  class LIBODB_ORACLE_EXPORT delete_statement: public statement
341  {
342  public:
343  virtual
344  ~delete_statement ();
345 
347  const std::string& text,
348  binding& param);
349 
351  const char* text,
352  binding& param);
353 
354  unsigned long long
355  execute ();
356 
357  private:
359  delete_statement& operator= (const delete_statement&);
360  };
361  }
362 }
363 
364 #include <odb/post.hxx>
365 
366 #endif // ODB_ORACLE_STATEMENT_HXX
auto_result(select_statement &s)
Definition: statement.hxx:243
Definition: connection.hxx:35
connection_type & conn_
Definition: statement.hxx:137
buffer_type
Definition: oracle-types.hxx:90
Definition: statement.hxx:292
OCIStmt * handle() const
Definition: statement.hxx:37
result
Definition: statement.hxx:205
bool empty() const
Definition: statement.hxx:57
virtual connection_type & connection()
Definition: statement.hxx:46
ub4 size
Definition: statement.hxx:299
oracle::connection connection_type
Definition: statement.hxx:31
Definition: statement.hxx:340
sb2 indicator
Definition: statement.hxx:305
unsigned int ub4
Definition: oracle-fwd.hxx:20
void stream_result(bind *, std::size_t count, void *old_base=0, void *new_base=0)
void * value
Definition: statement.hxx:90
statement_kind
Definition: forward.hxx:42
Definition: statement.hxx:316
void stream_result(void *old_base=0, void *new_base=0)
Definition: statement.hxx:218
Definition: statement.hxx:28
Definition: statement.hxx:169
struct OCIStmt OCIStmt
Definition: oracle-fwd.hxx:25
Definition: binding.hxx:21
auto_handle< OCIStmt > stmt_
Definition: statement.hxx:138
Definition: oracle-types.hxx:78
Definition: statement.hxx:241
Definition: statement.hxx:86
unsigned short ub2
Definition: oracle-fwd.hxx:18
Definition: statement.hxx:254
Definition: statement.hxx:207
oracle::bind * bind
Definition: statement.hxx:89
unsigned long long integer
Definition: statement.hxx:302
~auto_result()
Definition: statement.hxx:244
signed short sb2
Definition: oracle-fwd.hxx:17
std::size_t usize_
Definition: statement.hxx:141
unbind * udata_
Definition: statement.hxx:140
oracle::bind::buffer_type type
Definition: statement.hxx:88
Definition: statement.hxx:144
Definition: auto-handle.hxx:210