create_table_ddl_t.cc
Go to the documentation of this file.
1 // Tests for assembling the create-table SQLite expression. These
2 // tests DO NOT actually create the table.
3 
5 #include "cetlib/sqlite/column.h"
8 
9 #include <iostream>
10 #include <string>
11 
12 using namespace cet::sqlite;
13 using namespace std;
14 
15 namespace {
16  void
17  compare_statements(std::string test, std::string ref)
18  {
21  if (test != ref) {
22  throw Exception{errors::OtherError} << "Statements do not match:\n"
23  << " Test statement: " << test << '\n'
24  << " Ref. statement: " << ref << '\n';
25  }
26  }
27 }
28 
29 int
30 main() try {
31  // Test simple create_table() so we can use it later
32  {
33  auto const& test = detail::create_table("employees");
34  std::string const ref{"CREATE TABLE employees"};
35  compare_statements(test, ref);
36  }
37 
38  // One column
39  {
40  auto const& test =
41  detail::create_table_ddl("employees", column<string>{"name"});
42  std::string const ref{detail::create_table("employees") + "(name text)"};
43  compare_statements(test, ref);
44  }
45 
46  // Two columns
47  {
48  auto const& test = detail::create_table_ddl(
49  "employees", column<string>{"name"}, column<int>{"id"});
50  std::string const ref{detail::create_table("employees") +
51  "(name text, id integer)"};
52  compare_statements(test, ref);
53  }
54 
55  // Two columns, the first with the PRIMARY KEY constraint
56  {
57  auto const& test = detail::create_table_ddl(
58  "employees", column<int, primary_key>{"id"}, column<string>{"name"});
59  std::string const ref{detail::create_table("employees") +
60  "(id integer PRIMARY KEY, name text)"};
61  compare_statements(test, ref);
62  }
63 }
64 catch (std::exception const& e) {
65  std::cerr << e.what() << '\n';
66  return 1;
67 }
68 catch (...) {
69  std::cerr << "Caught unknown exception.\n";
70  return 2;
71 }
std::string string
Definition: nybbler.cc:12
void normalize_statement(std::string &to_replace)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:27
STL namespace.
int main()
std::string create_table(std::string const &tablename)
Definition: create_table.h:89
const double e
std::string create_table_ddl(std::string const &tablename, Cols const &...cols)
Definition: create_table.h:96
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33