transaction_t.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
6 #include "cetlib/sqlite/column.h"
8 #include "cetlib/sqlite/insert.h"
10 #include "cetlib/sqlite/select.h"
11 #include "cetlib_except/exception.h"
12 
13 #include <array>
14 #include <cassert>
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 using namespace cet::sqlite;
21 using namespace std;
22 
23 namespace {
24 
25  vector<array<string, 2u>> const names{{{"Cindy", "Miller"}},
26  {{"Billy", "Baker"}}};
27 
28  void
29  check_table_contents(Connection& c, string const& table_name)
30  {
31  auto test_results = [](size_t const i, auto const& worker) {
32  auto const& [first_name, last_name] = worker;
33  assert(first_name == names[i][0]);
34  assert(last_name == names[i][1]);
35  };
37  workers << select("*").from(c, table_name);
38  cet::for_all_with_index(workers, test_results);
39  }
40 
41 } // unnamed namespace
42 
43 int
45 {
46  try {
47  ConnectionFactory factory;
48  unique_ptr<Connection> c{factory.make_connection(":memory:")};
49  // Test normal transaction
50  string const table_name{"Workers"};
52  *c, table_name, column<string>{"FirstName"}, column<string>{"LastName"});
53  {
54  Transaction txn{*c};
55  for (auto const& worker : names) {
56  insert_into(*c, table_name).values(worker[0], worker[1]);
57  }
58  txn.commit();
59  check_table_contents(*c, table_name);
60  }
61  string const table_name2{"Workers2"};
63  *c, table_name2, column<string>{"FirstName"}, column<string>{"LastName"});
64  // Test select before commit is called
65  {
66  Transaction txn{*c};
67  for (auto const& worker : names) {
68  insert_into(*c, table_name2).values(worker[0], worker[1]);
69  }
70  // Calling a SELECT before the transaction is committed implicitly ends
71  // the transaciton.
72  check_table_contents(*c, table_name2);
73  txn.commit(); // Call commit to avoid the error emitted from the
74  // ~Transaction().
75  }
76  // Fail to commit before transaction destroyed (due to exception
77  // throw)
78  {
79  try {
80  Transaction txn{*c};
81  insert_into(*c, table_name2).values("Johnny", "Davis");
82  throw 1.0;
83  }
84  catch (double) {
85  }
86  check_table_contents(*c, table_name2);
87  }
88  }
89  catch (cet::exception const& e) {
90  cerr << e.what() << '\n';
91  return 1;
92  }
93  catch (...) {
94  cerr << "Unknown exception caught.\n";
95  return 2;
96  }
97 }
int main()
auto insert_into(sqlite3 *const db, std::string const &tablename)
Definition: insert.h:110
void for_all_with_index(FwdCont &, Func)
STL namespace.
auto make_connection(std::string const &file_name, PolicyArgs &&...) -> Connection *
const double e
void create_table(sqlite3 *const db, std::string const &tablename, Cols const &...cols)
Definition: create_table.h:118
auto select(T const &...t)
Definition: select.h:146
static std::vector< std::string > const names
Definition: FragmentType.hh:8
workers
Definition: train.py:479
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33