Functions
transaction_t.cc File Reference
#include "cetlib/container_algorithms.h"
#include "cetlib/sqlite/Connection.h"
#include "cetlib/sqlite/ConnectionFactory.h"
#include "cetlib/sqlite/Transaction.h"
#include "cetlib/sqlite/column.h"
#include "cetlib/sqlite/create_table.h"
#include "cetlib/sqlite/insert.h"
#include "cetlib/sqlite/query_result.h"
#include "cetlib/sqlite/select.h"
#include "cetlib_except/exception.h"
#include <array>
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

int main ( void  )

Definition at line 44 of file transaction_t.cc.

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 }
auto insert_into(sqlite3 *const db, std::string const &tablename)
Definition: insert.h:110
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
static std::vector< std::string > const names
Definition: FragmentType.hh:8
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33