aboutsummaryrefslogtreecommitdiff
path: root/odb/oracle/connection.cxx
blob: bc77dfc19f7bc64551725c75101344086302ee0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// file      : odb/oracle/connection.cxx
// author    : Constantin Michael <constantin@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

#include <string>

#include <oci.h>

#include <odb/oracle/database.hxx>
#include <odb/oracle/connection.hxx>
#include <odb/oracle/transaction.hxx>
#include <odb/oracle/error.hxx>
#include <odb/oracle/exceptions.hxx>
#include <odb/oracle/auto-descriptor.hxx>
#include <odb/oracle/statement-cache.hxx>

using namespace std;

namespace odb
{
  namespace oracle
  {
    connection::
    connection (database_type& db)
        : odb::connection (db),
          db_ (db),
          statement_cache_ (new statement_cache_type (*this)),
          lob_buffer_ (0)
    {
      sword r (0);

      {
        OCIError* e (0);
        r = OCIHandleAlloc (db_.environment (),
                            reinterpret_cast<void**> (&e),
                            OCI_HTYPE_ERROR,
                            0,
                            0);

        if (r != OCI_SUCCESS)
          throw invalid_oci_handle ();

        error_.reset (e);
      }

      auto_handle<OCIAuthInfo> auth_info;
      {
        OCIAuthInfo* a (0);
        r = OCIHandleAlloc (db_.environment (),
                            reinterpret_cast<void**> (&a),
                            OCI_HTYPE_AUTHINFO,
                            0,
                            0);

        if (r != OCI_SUCCESS)
          throw invalid_oci_handle ();

        auth_info.reset (a);
      }

      r = OCIAttrSet (
        auth_info,
        OCI_HTYPE_AUTHINFO,
        reinterpret_cast<text*> (const_cast<char*> (db_.user ().c_str ())),
        static_cast <ub4> (db_.user ().size ()),
        OCI_ATTR_USERNAME,
        error_);

      if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
        translate_error (error_, r);

      r = OCIAttrSet (
        auth_info,
        OCI_HTYPE_AUTHINFO,
        reinterpret_cast<text*> (const_cast<char*> (db_.password ().c_str ())),
        static_cast<ub4> (db_.password ().size ()),
        OCI_ATTR_PASSWORD,
        error_);

      if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
        translate_error (error_, r);

      {
        OCISvcCtx* s (0);

        r = OCISessionGet (
          db_.environment (),
          error_,
          &s,
          auth_info,
          reinterpret_cast<text*> (const_cast<char*> (db_.db ().c_str ())),
          static_cast<ub4> (db_.db ().size ()),
          0,
          0,
          0,
          0,
          0,
          OCI_SESSGET_STMTCACHE);

        if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
          translate_error (error_, r);

        handle_.reset (s, error_);
      }
    }

    connection::
    connection (database_type& db, OCISvcCtx* handle)
        : odb::connection (db),
          db_ (db),
          statement_cache_ (new statement_cache_type (*this)),
          lob_buffer_ (0)
    {
      sword r (0);

      {
        OCIError* e (0);
        r = OCIHandleAlloc (db_.environment (),
                            reinterpret_cast<void**> (&e),
                            OCI_HTYPE_ERROR,
                            0,
                            0);

        if (r != OCI_SUCCESS)
          throw invalid_oci_handle ();

        error_.reset (e);
      }

      handle_.reset (handle, error_);
    }

    connection::
    ~connection ()
    {
      // Deallocate prepared statements before we close the connection.
      //
      statement_cache_.reset ();
    }

    transaction_impl* connection::
    begin ()
    {
      return new transaction_impl (connection_ptr (inc_ref (this)));
    }

    unsigned long long connection::
    execute (const char* s, std::size_t n)
    {
      sword r (0);

      // OCI requires statement text to be NULL terminated.
      //
      string sql (s, n);

      auto_handle<OCIStmt> stmt;
      {
        OCIStmt* s (0);
        r = OCIStmtPrepare2 (handle_,
                             &s,
                             error_,
                             reinterpret_cast<const text*> (sql.c_str ()),
                             static_cast<ub4> (sql.size ()),
                             0,
                             0,
                             OCI_NTV_SYNTAX,
                             OCI_DEFAULT);

        if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
          translate_error (error_, r);

        stmt.reset (s, OCI_STRLS_CACHE_DELETE, error_);
      }

      ub2 stmt_type;
      r = OCIAttrGet (stmt,
                      OCI_HTYPE_STMT,
                      &stmt_type,
                      0,
                      OCI_ATTR_STMT_TYPE,
                      error_);

      if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
        translate_error (error_, r);

      ub4 row_count (0);

      if (stmt_type == OCI_STMT_SELECT)
      {
        // Do not prefetch any rows.
        //
        r = OCIStmtExecute (handle_, stmt, error_, 0, 0, 0, 0, OCI_DEFAULT);

        if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
          translate_error (error_, r);

        // In order to succesfully execute a select statement, OCI/Oracle
        // requires that there be OCIDefine handles provided for all select
        // list columns. Since we are not interested in any data returned by
        // the select statement, all buffer pointers, indication variable
        // pointers, and data length pointers are specified as NULL (we still
        // specify a valid data type identifier as not doing so results in
        // undefined behaviour). This action results in truncation errors
        // being returned for all attempted row fetches. However, cursor
        // behaviour is normal, thus allowing us to return the row count for
        // a select statement.
        //
        for (ub4 i (1); ; ++i)
        {
          auto_descriptor<OCIParam> param;
          {
            OCIParam* p (0);
            r = OCIParamGet (stmt,
                             OCI_HTYPE_STMT,
                             error_,
                             reinterpret_cast<void**> (&p),
                             i);

            if (r == OCI_ERROR)
              break;

            param.reset (p);
          }

          ub2 data_type;
          r = OCIAttrGet (param,
                          OCI_DTYPE_PARAM,
                          &data_type,
                          0,
                          OCI_ATTR_DATA_TYPE,
                          error_);

          if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
            translate_error (error_, r);

          // No need to keep track of the OCIDefine handles - these will
          // be deallocated with the statement.
          //
          OCIDefine* define (0);
          r = OCIDefineByPos (stmt,
                              &define,
                              error_,
                              i,
                              0,    // NULL value buffer pointer
                              0,    // zero length value buffer
                              data_type,
                              0,    // NULL indicator pointer
                              0,    // NULL length data pointer
                              0,    // NULL column level return code pointer
                              OCI_DEFAULT);

          if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
            translate_error (error_, r);
        }

        while (1)
        {
          r = OCIStmtFetch2 (stmt, error_, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT);

          if (r == OCI_NO_DATA)
            break;
          else if (r == OCI_ERROR)
          {
            sb4 e;
            r = OCIErrorGet (error_, 1, 0, &e, 0, 0, OCI_HTYPE_ERROR);

            // ORA-01406 is returned if there is a truncation error. We expect
            // and ignore all truncation errors.
            //
            if (e != 1406)
              translate_error (error_, r);
          }
          else if (r ==  OCI_INVALID_HANDLE)
            translate_error (error_, r);
        }
      }
      else
      {
        // OCIStmtExecute requires a non-zero iters param for DML statements.
        //
        r = OCIStmtExecute (handle_, stmt, error_, 1, 0, 0, 0, OCI_DEFAULT);

        if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
          translate_error (error_, r);
      }

      r = OCIAttrGet (stmt,
                      OCI_HTYPE_STMT,
                      &row_count,
                      0,
                      OCI_ATTR_ROW_COUNT,
                      error_);

      if (r == OCI_ERROR || r == OCI_INVALID_HANDLE)
        translate_error (error_, r);

      return row_count;
    }
  }
}