create_table.h
Go to the documentation of this file.
1 #ifndef cetlib_sqlite_create_table_h
2 #define cetlib_sqlite_create_table_h
3 
4 // =======================================================
5 //
6 // create_table
7 //
8 // The create_table(_as) interface allows users to create tables for a
9 // given SQLite database. Two free functions exist:
10 //
11 // (1) 'create_table' which takes a database handle object (of type
12 // sqlite3*) or a cet::sqlite::Connection object, a string
13 // corresponding to the desired table name, and a pack of
14 // column<> arguments that correspond to the desired SQLite
15 // columns.
16 //
17 // (2) 'create_table_as' which takes a string corresponding to the
18 // desired table name, and a const reference to a SelectStmt
19 // object. This signature does not receive a database handle
20 // object, since the handle was already supplied when
21 // constructing the SelectStmt object.
22 //
23 // See below for examples of usage. Note that calling
24 // 'create_table(_as)' does NOT create an explicit SQLite transaction.
25 // If a transaction is desired, the create_table* calls should be
26 // surrounded by the appropriate transaction calls.
27 //
28 // Example of usage:
29 //
30 // using namespace cet::sqlite;
31 //
32 // Connection c {"a.db"};
33 // create_table(c, "workers", column<string>{"name"}, column<int>{"id"});
34 //
35 // // insert entries into table...
36 //
37 // Transaction txn {c}; // Can work with transactions.
38 // create_table_as("workersNamedDavid",
39 // select("*").from(c,"workers").where("name='David'"));
40 // txn.commit();
41 //
42 // =======================================================
43 
44 #include "cetlib/sqlite/column.h"
45 #include "cetlib/sqlite/exec.h"
46 #include "cetlib/sqlite/select.h"
47 
48 #include "sqlite3.h"
49 
50 #include <sstream>
51 #include <string>
52 #include <tuple>
53 #include <type_traits>
54 
55 using namespace std::string_literals;
56 
57 namespace cet::sqlite {
58  template <typename... Cols>
59  void create_table(sqlite3* const db,
60  std::string const& tablename,
61  Cols const&... cols);
62 
63  void create_table_as(std::string const& tablename, SelectStmt const& stmt);
64 }
65 
66 // ========================================================
67 // Implementation below
68 
69 namespace cet::sqlite {
70  namespace detail {
71 
72  template <typename T, typename... Constraints>
75  {
76  std::string info{col.name() + col.sqlite_type()};
77  info += (Constraints::snippet() + ... + ""s);
78  return info;
79  }
80 
81  template <typename H, typename... T>
82  inline std::string
83  columns(H const& h, T const&... t)
84  {
85  return (column_info(h) + ... + (", " + column_info(t)));
86  }
87 
88  inline std::string
89  create_table(std::string const& tablename)
90  {
91  return "CREATE TABLE "s + tablename;
92  }
93 
94  template <typename... Cols>
96  create_table_ddl(std::string const& tablename, Cols const&... cols)
97  {
98  std::string ddl{create_table(tablename)};
99  ddl += "( ";
100  ddl += columns(cols...);
101  ddl += " )";
102  return ddl;
103  }
104 
105  inline std::string
106  create_table_as_ddl(std::string const& tablename, SelectStmt const& stmt)
107  {
108  std::string ddl{create_table(tablename)};
109  ddl += " AS ";
110  ddl += stmt.ddl_;
111  return ddl;
112  }
113 
114  } // detail
115 
116  template <typename... Cols>
117  void
119  std::string const& tablename,
120  Cols const&... cols)
121  {
122  auto const& ddl = detail::create_table_ddl(tablename, cols...);
123  sqlite::exec(db, ddl);
124  }
125 
126  inline void
127  create_table_as(std::string const& tablename, SelectStmt const& stmt)
128  {
129  std::string ddl{detail::create_table_as_ddl(tablename, stmt)};
130  sqlite::exec(stmt.db_, ddl);
131  }
132 
133 } // cet::sqlite
134 
135 #endif /* cetlib_sqlite_create_table_h */
136 
137 // Local Variables:
138 // mode: c++
139 // End:
std::string column_info(column< T, Constraints... > const &col)
Definition: create_table.h:74
std::string string
Definition: nybbler.cc:12
std::string create_table(std::string const &tablename)
Definition: create_table.h:89
void create_table_as(std::string const &tablename, SelectStmt const &stmt)
Definition: create_table.h:127
std::string columns(H const &h, T const &...t)
Definition: create_table.h:83
std::string ddl_
Definition: select.h:93
std::string create_table_ddl(std::string const &tablename, Cols const &...cols)
Definition: create_table.h:96
struct sqlite3 sqlite3
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
static QCString * s
Definition: config.cpp:1042
std::string create_table_as_ddl(std::string const &tablename, SelectStmt const &stmt)
Definition: create_table.h:106