StmtTest.cpp 13.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
20
#include "common/exception/DatabaseCheckConstraintError.hpp"
#include "common/exception/DatabasePrimaryKeyError.hpp"
21
#include "common/exception/DatabaseUniqueError.hpp"
22
23
#include "common/exception/Exception.hpp"
#include "common/make_unique.hpp"
24
#include "common/utils/utils.hpp"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "rdbms/ConnPool.hpp"
#include "rdbms/StmtTest.hpp"

#include <gtest/gtest.h>

namespace unitTests {

//------------------------------------------------------------------------------
// Setup
//------------------------------------------------------------------------------
void cta_rdbms_StmtTest::SetUp() {
  using namespace cta::rdbms;

  if(!m_connPool) {
39
    m_login = GetParam()->create();
40
    const uint64_t maxNbConns = 1;
41
    m_connPool = cta::make_unique<ConnPool>(m_login, maxNbConns);
42
43
44
45
46
47
48
49
50
51
52
  }

  m_conn = m_connPool->getConn();
  ASSERT_EQ(AutocommitMode::AUTOCOMMIT_ON, m_conn.getAutocommitMode());

  try {
    m_conn.executeNonQuery("DROP TABLE STMT_TEST");
  } catch(...) {
    // Do nothing
  }

53
54
55
56
57
  const std::string createTableSql = getCreateStmtTestTableSql();
  m_conn.executeNonQuery(createTableSql);
}

//------------------------------------------------------------------------------
58
// getCreateStmtTestTableSql
59
60
61
62
63
64
65
//------------------------------------------------------------------------------
std::string cta_rdbms_StmtTest::getCreateStmtTestTableSql() {
  using namespace cta;
  using namespace cta::rdbms;

  try {
    std::string sql =
66
67
68
69
70
71
72
73
      "CREATE TABLE STMT_TEST("                                      "\n"
      "  ID         UINT64TYPE CONSTRAINT STMT_TEST_ID_NN NOT NULL," "\n"
      "  DOUBLE_COL FLOAT,"                                          "\n"
      "  UINT64_COL UINT64TYPE,"                                     "\n"
      "  STRING_COL VARCHAR(100),"                                   "\n"
      "  BOOL_COL   CHAR(1) DEFAULT '0',"                            "\n"
      "  CONSTRAINT STMT_TEST_PK PRIMARY KEY(ID),"                   "\n"
      "  CONSTRAINT BOOL_COL_BOOL_CK CHECK(BOOL_COL IN ('0', '1'))"  "\n"
74
75
76
77
78
79
      ")";

    switch(m_login.dbType) {
    case Login::DBTYPE_IN_MEMORY:
      break;
    case Login::DBTYPE_ORACLE:
80
      utils::searchAndReplace(sql, "UINT64TYPE", "NUMERIC(20, 0)");
81
82
83
      utils::searchAndReplace(sql, "VARCHAR", "VARCHAR2");
      break;
    case Login::DBTYPE_SQLITE:
84
      utils::searchAndReplace(sql, "UINT64TYPE", "INTEGER");
85
86
      break;
    case Login::DBTYPE_MYSQL:
87
      utils::searchAndReplace(sql, "UINT64TYPE", "BIGINT UNSIGNED");
88
89
      break;
    case Login::DBTYPE_POSTGRESQL:
90
      utils::searchAndReplace(sql, "UINT64TYPE", "NUMERIC(20, 0)");
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
      break;
    case Login::DBTYPE_NONE:
      {
        throw exception::Exception("Cannot create SQL for database type DBTYPE_NONE");
      }
    default:
      {
        std::ostringstream msg;
        msg << "Unknown database type: intVal=" << m_login.dbType;
        throw exception::Exception(msg.str());
      }
    }

    return sql;
  } catch(exception::Exception &ex) {
    std::ostringstream msg;
    msg << __FUNCTION__ << " failed: " << ex.getMessage().str();
    ex.getMessage().str(msg.str());
    throw ex;
  }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}

//------------------------------------------------------------------------------
// TearDown
//------------------------------------------------------------------------------
void cta_rdbms_StmtTest::TearDown() {
  using namespace cta::rdbms;
  try {
    m_conn.executeNonQuery("DROP TABLE STMT_TEST");
  } catch(...) {
    // Do nothing
  }
}

TEST_P(cta_rdbms_StmtTest, insert_with_bindDouble) {
  using namespace cta::rdbms;

  const double insertValue = 1.234;

  // Insert a row into the test table
  {
    const char *const sql =
133
134
135
136
137
138
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  DOUBLE_COL)"          "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :DOUBLE_COL)";
139
140
141
142
143
144
145
146
    auto stmt = m_conn.createStmt(sql);
    stmt.bindDouble(":DOUBLE_COL", insertValue);
    stmt.executeNonQuery();
  }

  // Select the row back from the table
  {
    const char *const sql =
147
148
149
150
      "SELECT"                     "\n"
      "  DOUBLE_COL AS DOUBLE_COL" "\n"
      "FROM"                       "\n"
      "  STMT_TEST";
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalDouble("DOUBLE_COL");

    ASSERT_TRUE((bool)selectValue);

    const double diff = insertValue - selectValue.value();
    ASSERT_TRUE(0.000001 > diff);

    ASSERT_FALSE(rset.next());
  }
}

166
167
168
169
170
171
172
173
TEST_P(cta_rdbms_StmtTest, insert_with_bindUint64) {
  using namespace cta::rdbms;

  const uint64_t insertValue = 1234;

  // Insert a row into the test table
  {
    const char *const sql =
174
175
176
177
178
179
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  UINT64_COL) "         "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :UINT64_COL)";
180
181
182
183
184
185
186
187
    auto stmt = m_conn.createStmt(sql);
    stmt.bindDouble(":UINT64_COL", insertValue);
    stmt.executeNonQuery();
  }

  // Select the row back from the table
  {
    const char *const sql =
188
189
190
191
      "SELECT"                     "\n"
      "  UINT64_COL AS UINT64_COL" "\n"
      "FROM"                       "\n"
      "  STMT_TEST";
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalDouble("UINT64_COL");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }
}

206
TEST_P(cta_rdbms_StmtTest, insert_with_bindUint64_2_pow_64_minus_1) {
207
208
  using namespace cta::rdbms;

209
210
211
212
213
  const uint64_t insertValue = 18446744073709551615U;

  // Insert a row into the test table
  {
    const char *const sql =
214
215
216
217
218
219
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  UINT64_COL)"          "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :UINT64_COL)";
220
221
222
223
224
    auto stmt = m_conn.createStmt(sql);
    stmt.bindUint64(":UINT64_COL", insertValue);
    stmt.executeNonQuery();
  }

225
226
227
  // Select the row back from the table
  {
    const char *const sql =
228
229
230
231
      "SELECT"                     "\n"
      "  UINT64_COL AS UINT64_COL" "\n"
      "FROM"                       "\n"
      "  STMT_TEST";
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalUint64("UINT64_COL");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }
}

TEST_P(cta_rdbms_StmtTest, insert_with_bindUint64_2_pow_64_minus_2) {
  using namespace cta::rdbms;

  const uint64_t insertValue = 18446744073709551614U;

  // Insert a row into the test table
  {
    const char *const sql =
254
255
256
257
258
259
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  UINT64_COL)"          "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :UINT64_COL)";
260
261
262
263
264
    auto stmt = m_conn.createStmt(sql);
    stmt.bindUint64(":UINT64_COL", insertValue);
    stmt.executeNonQuery();
  }

265
266
267
  // Select the row back from the table
  {
    const char *const sql =
268
269
270
271
      "SELECT"                     "\n"
      "  UINT64_COL AS UINT64_COL" "\n"
      "FROM"                       "\n"
      "  STMT_TEST";
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalUint64("UINT64_COL");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }
}

286
287
288
289
290
291
292
293
TEST_P(cta_rdbms_StmtTest, insert_with_bindString) {
  using namespace cta::rdbms;

  const std::string insertValue = "value";

  // Insert a row into the test table
  {
    const char *const sql =
294
295
296
297
298
299
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  STRING_COL)"          "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :STRING_COL)";
300
301
302
303
304
305
306
307
    auto stmt = m_conn.createStmt(sql);
    stmt.bindString(":STRING_COL", insertValue);
    stmt.executeNonQuery();
  }

  // Select the row back from the table
  {
    const char *const sql =
308
309
310
311
      "SELECT"                     "\n"
      "  STRING_COL AS STRING_COL" "\n"
      "FROM"                       "\n"
      "  STMT_TEST";
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalString("STRING_COL");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }
}

326
327
328
329
330
331
332
333
TEST_P(cta_rdbms_StmtTest, insert_with_bindBool_true) {
  using namespace cta::rdbms;

  const bool insertValue = true;

  // Insert a row into the test table
  {
    const char *const sql =
334
335
336
337
338
339
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  BOOL_COL)"            "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :BOOL_COL)";
340
341
342
343
344
345
346
347
    auto stmt = m_conn.createStmt(sql);
    stmt.bindBool(":BOOL_COL", insertValue);
    stmt.executeNonQuery();
  }

  // Select the row back from the table
  {
    const char *const sql =
348
349
350
351
      "SELECT"                 "\n"
      "  BOOL_COL AS BOOL_COL" "\n"
      "FROM"                   "\n"
      "  STMT_TEST";
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalBool("BOOL_COL");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }
}

366
367
368
369
370
371
372
373
TEST_P(cta_rdbms_StmtTest, insert_with_bindBool_false) {
  using namespace cta::rdbms;

  const bool insertValue = false;

  // Insert a row into the test table
  {
    const char *const sql =
374
375
376
377
378
379
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
      "  BOOL_COL)"            "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
      "  :BOOL_COL)";
380
381
382
383
384
385
386
387
    auto stmt = m_conn.createStmt(sql);
    stmt.bindBool(":BOOL_COL", insertValue);
    stmt.executeNonQuery();
  }

  // Select the row back from the table
  {
    const char *const sql =
388
389
390
391
      "SELECT"                 "\n"
      "  BOOL_COL AS BOOL_COL" "\n"
      "FROM"                   "\n"
      "  STMT_TEST";
392
393
394
395
396
397
398
399
400
401
402
403
404
405
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalBool("BOOL_COL");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }
}

406
407
408
409
410
411
412
413
TEST_P(cta_rdbms_StmtTest, insert_with_bindString_invalid_bool_value) {
  using namespace cta::rdbms;

  const std::string insertValue = "2"; // null, "0" and "1" are valid values

  // Insert a row into the test table
  {
    const char *const sql =
414
415
416
417
418
      "INSERT INTO STMT_TEST(" "\n"
      "  ID,"                  "\n"
        "BOOL_COL)"            "\n"
      "VALUES("                "\n"
      "  1,"                   "\n"
419
420
421
        ":BOOL_COL)";
    auto stmt = m_conn.createStmt(sql);
    stmt.bindString(":BOOL_COL", insertValue);
422
423

    ASSERT_THROW(stmt.executeNonQuery(), cta::exception::DatabaseCheckConstraintError);
424
425
426
  }
}

427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
TEST_P(cta_rdbms_StmtTest, insert_same_primary_twice) {
  using namespace cta::rdbms;

  const uint64_t insertValue = 1234;

  // Insert an ID into the test table
  {
    const char *const sql =
      "INSERT INTO STMT_TEST(" "\n"
      "  ID)"                  "\n"
      "VALUES("                "\n"
      "  :ID)";
    auto stmt = m_conn.createStmt(sql);
    stmt.bindUint64(":ID", insertValue);
    stmt.executeNonQuery();
  }

  // Select the ID back from the table
  {
    const char *const sql =
      "SELECT"      "\n"
      "  ID AS ID"  "\n"
      "FROM"        "\n"
      "  STMT_TEST";
    auto stmt = m_conn.createStmt(sql);
    auto rset = stmt.executeQuery();
    ASSERT_TRUE(rset.next());

    const auto selectValue = rset.columnOptionalDouble("ID");

    ASSERT_TRUE((bool)selectValue);

    ASSERT_EQ(insertValue,selectValue.value());

    ASSERT_FALSE(rset.next());
  }

  // Attempt to insert the same ID again
  {
    const char *const sql =
      "INSERT INTO STMT_TEST(" "\n"
      "  ID) "                 "\n"
      "VALUES("                "\n"
      "  :ID)";
    auto stmt = m_conn.createStmt(sql);
    stmt.bindUint64(":ID", insertValue);
    switch(m_login.dbType) {
    case Login::DBTYPE_IN_MEMORY:
    case Login::DBTYPE_ORACLE:
    case Login::DBTYPE_POSTGRESQL:
      ASSERT_THROW(stmt.executeNonQuery(), cta::exception::DatabaseUniqueError);
      break;
    default:
      ASSERT_THROW(stmt.executeNonQuery(), cta::exception::DatabasePrimaryKeyError);
    }
  }
}

485
} // namespace unitTests