aboutsummaryrefslogtreecommitdiff
path: root/odb/oracle/database.cxx
blob: 375739df3747a83238ff0a6cc1788bfaa5049560 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// file      : odb/oracle/database.cxx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

#include <oci.h>

#include <sstream>

#include <odb/oracle/traits.hxx>
#include <odb/oracle/database.hxx>
#include <odb/oracle/statement.hxx>
#include <odb/oracle/transaction.hxx>
#include <odb/oracle/exceptions.hxx>
#include <odb/oracle/error.hxx>

#include <odb/oracle/details/options.hxx>

using namespace std;

namespace odb
{
  namespace oracle
  {
    using odb::details::transfer_ptr;

    database::
    database (const string& user,
              const string& password,
              const string& db,
              ub2 charset,
              ub2 ncharset,
              OCIEnv* environment,
              transfer_ptr<connection_factory> factory)
        : odb::database (id_oracle),
          user_ (user),
          password_ (password),
          db_ (db),
          port_ (0),
          charset_ (charset),
          ncharset_ (ncharset),
          environment_ (environment),
          factory_ (factory.transfer ())
    {
      if (environment_ == 0)
      {
        sword s (OCIEnvNlsCreate (&environment_,
                                  OCI_THREADED,
                                  0, 0, 0, 0, 0, 0,
                                  charset,
                                  ncharset));

        if (s == OCI_ERROR)
          translate_error (environment_);

        auto_environment_.reset (environment_);
      }

      if (!factory_)
        factory_.reset (new connection_pool_factory ());

      factory_->database (*this);
    }

    database::
    database (const string& user,
              const string& password,
              const string& service,
              const string& host,
              unsigned int port,
              ub2 charset,
              ub2 ncharset,
              OCIEnv* environment,
              transfer_ptr<connection_factory> factory)
        : odb::database (id_oracle),
          user_ (user),
          password_ (password),
          service_ (service),
          host_ (host),
          port_ (port),
          charset_ (charset),
          ncharset_ (ncharset),
          environment_ (environment),
          factory_ (factory.transfer ())
    {
      if (environment_ == 0)
      {
        sword s (OCIEnvNlsCreate (&environment_,
                                  OCI_THREADED,
                                  0, 0, 0, 0, 0, 0,
                                  charset,
                                  ncharset));

        if (s == OCI_ERROR)
          translate_error (environment_);

        auto_environment_.reset (environment_);
      }

      ostringstream ss;

      if (!host.empty ())
      {
        ss << "//" << host_;

        if (port != 0)
          ss << ":" << port;
      }

      if (!service_.empty ())
      {
        if (!host.empty ())
          ss << "/" << service_;
        else
          ss << service_;
      }

      db_ = ss.str ();

      if (!factory_)
        factory_.reset (new connection_pool_factory ());

      factory_->database (*this);
    }

    database::
    database (int& argc,
              char* argv[],
              bool erase,
              ub2 charset,
              ub2 ncharset,
              OCIEnv* environment,
              transfer_ptr<connection_factory> factory)
        : odb::database (id_oracle),
          port_ (0),
          charset_ (charset),
          ncharset_ (ncharset),
          environment_ (environment),
          factory_ (factory.transfer ())
    {
      if (environment_ == 0)
      {
        sword s (OCIEnvNlsCreate (&environment_,
                                  OCI_THREADED,
                                  0, 0, 0, 0, 0, 0,
                                  charset,
                                  ncharset));

        if (s == OCI_ERROR)
          translate_error (environment_);

        auto_environment_.reset (environment_);
      }

      using namespace details;

      try
      {
        cli::argv_file_scanner scan (argc, argv, "--options-file", erase);
        options ops (scan, cli::unknown_mode::skip, cli::unknown_mode::skip);

        if (ops.user_specified ())
          user_ = ops.user ();

        if (ops.password_specified ())
          password_ = ops.password ();

        if (ops.database_specified ())
        {
          if (ops.host_specified () ||
              ops.port_specified () ||
              ops.service_specified ())

            throw cli_exception ("--host, --port, or --service options "
                                 "cannot be specified together with "
                                 "--database option");
          db_ = ops.database ();
        }
        else
        {
          bool host_present (false);
          ostringstream oss;

          if (ops.host_specified () && !ops.host ().empty ())
          {
            host_present = true;

            host_ = ops.host ();
            oss << "//" << host_;

            if (ops.port_specified ())
            {
              port_ = ops.port ();

              if (port_ != 0)
                oss << ":" << port_;
            }
          }

          if (ops.service_specified () && !ops.service ().empty ())
          {
            service_ = ops.service ();

            if (host_present)
              oss << "/" << service_;
            else
              oss << service_;
          }

          db_ = oss.str ();
        }
      }
      catch (const cli::exception& e)
      {
        ostringstream oss;
        oss << e;
        throw cli_exception (oss.str ());
      }

      if (!factory_)
        factory_.reset (new connection_pool_factory ());

      factory_->database (*this);
    }

    void database::
    print_usage (ostream& os)
    {
      details::options::print_usage (os);
    }

    database::
    ~database ()
    {
    }

    transaction_impl* database::
    begin ()
    {
      return new transaction_impl (*this);
    }

    odb::connection* database::
    connection_ ()
    {
      connection_ptr c (factory_->connect ());
      return c.release ();
    }

    const database::schema_version_info& database::
    load_schema_version (const string& name) const
    {
      schema_version_info& svi (schema_version_map_[name]);

      // Construct the SELECT statement text.
      //
      string text ("SELECT \"version\", \"migration\" FROM ");

      if (!svi.version_table.empty ())
        text += svi.version_table; // Already quoted.
      else if (!schema_version_table_.empty ())
        text += schema_version_table_; // Already quoted.
      else
        text += "\"schema_version\"";

      text += " WHERE \"name\" = :1";

      // Bind parameters and results. If the schema name is empty, replace
      // it with a single space to workaround the VARCHAR2 empty/NULL issue.
      //
      string n (name.empty () ? string (" ") : name);
      ub2 psize[1] = {static_cast<ub2> (n.size ())};
      sb2 pind[1] = {0};
      bind pbind[1] = {{bind::string,
                        const_cast<char*> (n.c_str ()),
                        &psize[0],
                        psize[0],
                        &pind[0],
                        0}};
      binding param (pbind, 1);
      param.version++;

      char version[12];
      unsigned int migration;
      ub2 rsize[1];
      sb2 rind[2];
      bind rbind[2] = {
        {bind::number,
         version,
         &rsize[0],
         static_cast<ub4> (sizeof (version)),
         &rind[0],
         0},

        {bind::uinteger, &migration, 0, 4, &rind[1], 0}
      };
      binding result (rbind, 2);
      result.version++;

      // If we are not in transaction, then OCI will start an implicit one
      // but only if we try to modify anything. Since our statement is read-
      // only, we can run without a transaction.
      //
      connection_ptr cp;
      if (!transaction::has_current ())
        cp = factory_->connect ();

      oracle::connection& c (
        cp != 0 ? *cp : transaction::current ().connection ());

      try
      {
        select_statement st (c,
                             text,
                             false, // Don't process.
                             false, // Don't optimize.
                             param,
                             result);
        st.execute ();
        auto_result ar (st);

        switch (st.fetch ())
        {
        case select_statement::success:
          {
            value_traits<unsigned long long, id_big_int>::set_value (
              svi.version, version, rsize[0], rind[0] == -1);
            svi.migration = migration != 0;
            assert (st.fetch () == select_statement::no_data);
            break;
          }
        case select_statement::no_data:
          {
            svi.version = 0; // No schema.
            break;
          }
        }
      }
      catch (const database_exception& e)
      {
        // Detect the case where there is no version table.
        //
        if (e.size () != 0 && e.begin ()->error () == 942)
          svi.version = 0; // No schema.
        else
          throw;
      }

      return svi;
    }
  }
}