OcciConn.cpp 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19
#include "common/exception/Exception.hpp"
20
#include "common/exception/Errnum.hpp"
21
#include "common/exception/LostDatabaseConnection.hpp"
22
#include "common/make_unique.hpp"
23
#include "common/threading/MutexLocker.hpp"
24
25
26
#include "common/threading/RWLockRdLocker.hpp"
#include "common/threading/RWLockWrLocker.hpp"
#include "common/utils/utils.hpp"
27
28
29
#include "rdbms/wrapper/OcciConn.hpp"
#include "rdbms/wrapper/OcciEnv.hpp"
#include "rdbms/wrapper/OcciStmt.hpp"
30
31
32

#include <stdexcept>
#include <string>
33

34
namespace cta {
35
namespace rdbms {
36
namespace wrapper {
37

38
39
40
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
41
OcciConn::OcciConn(oracle::occi::Environment *const env, oracle::occi::Connection *const conn):
42
  m_env(env),
43
44
  m_occiConn(conn),
  m_autocommitMode(AutocommitMode::AUTOCOMMIT_ON) {
45
  if(nullptr == conn) {
46
    throw exception::Exception(std::string(__FUNCTION__) + " failed"
47
      ": The OCCI connection is a nullptr pointer");
Steven Murray's avatar
Steven Murray committed
48
  }
49
50
51
52
53
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
54
OcciConn::~OcciConn() {
55
  try {
56
    close(); // Idempotent close() method
57
58
59
60
61
62
63
64
  } catch(...) {
    // Destructor should not throw any exceptions
  }
}

//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
65
void OcciConn::close() {
66
  threading::MutexLocker locker(m_mutex);
67

68
  if(nullptr != m_occiConn) {
69
70
    m_env->terminateConnection(m_occiConn);
    m_occiConn = nullptr;
71
72
73
  }
}

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//------------------------------------------------------------------------------
// setAutocommitMode
//------------------------------------------------------------------------------
void OcciConn::setAutocommitMode(const AutocommitMode autocommitMode) {
  threading::RWLockWrLocker wrLocker(m_autocommitModeRWLock);
  m_autocommitMode = autocommitMode;
}

//------------------------------------------------------------------------------
// getAutocommitMode
//------------------------------------------------------------------------------
AutocommitMode OcciConn::getAutocommitMode() const noexcept{
  threading::RWLockRdLocker rdLocker(m_autocommitModeRWLock);
  return m_autocommitMode;
}

90
91
92
//------------------------------------------------------------------------------
// executeNonQuery
//------------------------------------------------------------------------------
93
void OcciConn::executeNonQuery(const std::string &sql) {
94
95
  try {
    auto stmt = createStmt(sql);
96
    stmt->executeNonQuery();
97
98
99
100
101
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

102
103
104
//------------------------------------------------------------------------------
// createStmt
//------------------------------------------------------------------------------
105
std::unique_ptr<StmtWrapper> OcciConn::createStmt(const std::string &sql) {
106
  try {
107
    threading::MutexLocker locker(m_mutex);
108
109
110
111
112
113
114
115
116

    if(nullptr == m_occiConn) {
       throw exception::Exception("Connection is closed");
    }

    oracle::occi::Statement *const stmt = m_occiConn->createStatement(sql);
    if (nullptr == stmt) {
      throw exception::Exception("oracle::occi::createStatement() returned a nullptr pointer");
    }
117
    return cta::make_unique<OcciStmt>(sql, *this, stmt);
118
119
120
121
122
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + sql + ": " +
      ex.getMessage().str());
  } catch(std::exception &se) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + sql + ": " + se.what());
123
  }
124
}
125

126
127
128
129
//------------------------------------------------------------------------------
// commit
//------------------------------------------------------------------------------
void OcciConn::commit() {
130
  try {
131
    threading::MutexLocker locker(m_mutex);
132
133
134
135
136

    if(nullptr == m_occiConn) {
       throw exception::Exception("Connection is closed");
    }

137
    m_occiConn->commit();
138
139
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
140
141
142
  } catch(std::exception &se) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + se.what());
  }
143
144
145
}

//------------------------------------------------------------------------------
146
// rollback
147
148
//------------------------------------------------------------------------------
void OcciConn::rollback() {
149
  try {
150
    threading::MutexLocker locker(m_mutex);
151
152
153
154
155

    if(nullptr == m_occiConn) {
       throw exception::Exception("Connection is closed");
    }

156
    m_occiConn->rollback();
157
158
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
159
160
161
  } catch(std::exception &se) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + se.what());
  }
162
163
}

164
165
166
167
//------------------------------------------------------------------------------
// getColumns
//------------------------------------------------------------------------------
std::map<std::string, std::string> OcciConn::getColumns(const std::string &tableName) {
168
169
  try {
    std::map<std::string, std::string> columnNamesAndTypes;
170
    const char *const sql =
171
172
173
174
175
176
      "SELECT "
        "COLUMN_NAME, "
        "DATA_TYPE "
      "FROM "
        "USER_TAB_COLUMNS "
      "WHERE "
177
        "TABLE_NAME = :TABLE_NAME";
178
179

    auto stmt = createStmt(sql);
180
    stmt->bindString(":TABLE_NAME", tableName);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("COLUMN_NAME");
      auto type = rset->columnOptionalString("DATA_TYPE");
      if(name && type) {
         if ("NUMBER" == type.value()) {
           type = "NUMERIC";
         }
        columnNamesAndTypes.insert(std::make_pair(name.value(), type.value()));
      }
    }
    return columnNamesAndTypes;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }         
196
197
}

Steven Murray's avatar
Steven Murray committed
198
199
200
201
202
203
204
205
206
207
208
209
210
//------------------------------------------------------------------------------
// getTableNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getTableNames() {
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "TABLE_NAME "
      "FROM "
        "USER_TABLES "
      "ORDER BY "
        "TABLE_NAME";
211
    auto stmt = createStmt(sql);
212
    auto rset = stmt->executeQuery();
213
214
215
216
217
    while (rset->next()) {
      auto name = rset->columnOptionalString("TABLE_NAME");
      if(name) {
        names.push_back(name.value());
      }
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
    }

    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

//------------------------------------------------------------------------------
// getIndexNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getIndexNames() {
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "INDEX_NAME "
      "FROM "
        "USER_INDEXES "
      "ORDER BY "
        "INDEX_NAME";
    auto stmt = createStmt(sql);
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("INDEX_NAME");
      if(name) {
        names.push_back(name.value());
      }
Steven Murray's avatar
Steven Murray committed
246
247
248
249
250
251
252
253
    }

    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

254
//------------------------------------------------------------------------------
255
256
257
258
259
260
261
262
263
264
265
266
// getSequenceNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getSequenceNames() {
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "SEQUENCE_NAME "
      "FROM "
        "USER_SEQUENCES "
      "ORDER BY "
        "SEQUENCE_NAME";
267
    auto stmt = createStmt(sql);
268
    auto rset = stmt->executeQuery();
269
270
271
    while (rset->next()) {
      auto name = rset->columnOptionalString("SEQUENCE_NAME");
      names.push_back(name.value());
272
273
274
275
276
277
278
279
280
    }

    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

//------------------------------------------------------------------------------
281
282
283
284
285
286
287
// getTriggerNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getTriggerNames() {
  return std::list<std::string>();
}

//------------------------------------------------------------------------------
288
// getParallelTableNames
289
290
291
292
293
294
295
296
297
298
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getParallelTableNames() {
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "TABLE_NAME "
      "FROM "
        "USER_TABLES "
      "WHERE "
299
        "TRIM(DEGREE) NOT LIKE '1' "
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
      "ORDER BY "
        "TABLE_NAME";
    auto stmt = createStmt(sql);
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("TABLE_NAME");
      names.push_back(name.value());
    }

    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

//------------------------------------------------------------------------------
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
// getConstraintNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getConstraintNames(const std::string& tableName){
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "CONSTRAINT_NAME "
      "FROM "
        "USER_CONSTRAINTS "
      "WHERE "
        "TABLE_NAME=:TABLE_NAME";
    auto stmt = createStmt(sql);
    stmt->bindString(":TABLE_NAME",tableName);
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("CONSTRAINT_NAME");
      names.push_back(name.value());
    }
    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

341
342
343
344
//------------------------------------------------------------------------------
// getStoredProcedureNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getStoredProcedureNames() {
345
  try {
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "OBJECT_NAME "
      "FROM "
        "USER_PROCEDURES";
    auto stmt = createStmt(sql);
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("OBJECT_NAME");
      names.push_back(name.value());
    }
    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//------------------------------------------------------------------------------
// getSynonymNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getSynonymNames() {
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "SYNONYM_NAME "
      "FROM "
        "USER_SYNONYMS";
    auto stmt = createStmt(sql);
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("SYNONYM_NAME");
      names.push_back(name.value());
    }
    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

//------------------------------------------------------------------------------
// getTypeNames
//------------------------------------------------------------------------------
std::list<std::string> OcciConn::getTypeNames() {
  try {
    std::list<std::string> names;
    const char *const sql =
      "SELECT "
        "TYPE_NAME "
      "FROM "
        "USER_TYPES";
    auto stmt = createStmt(sql);
    auto rset = stmt->executeQuery();
    while (rset->next()) {
      auto name = rset->columnOptionalString("TYPE_NAME");
      names.push_back(name.value());
    }
    return names;
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}
409

410
//------------------------------------------------------------------------------
411
412
413
// isOpen
//------------------------------------------------------------------------------
bool OcciConn::isOpen() const {
414
  return nullptr != m_occiConn;
415
416
}

417
418
419
420
421
//------------------------------------------------------------------------------
// closeStmt
//------------------------------------------------------------------------------
void OcciConn::closeStmt(oracle::occi::Statement *const stmt) {
  try {
422
    threading::MutexLocker locker(m_mutex);
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

    if(nullptr == m_occiConn) {
       throw exception::Exception("Connection is closed");
    }

    if(nullptr == stmt) {
      throw exception::Exception("stmt is a nullptr");
    }

    m_occiConn->terminateStatement(stmt);
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  } catch(std::exception &se) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + se.what());
  }
}

440
} // namespace wrapper
441
} // namespace rdbms
442
} // namespace cta