libdballe  8.3
sql.h
Go to the documentation of this file.
1 
4 #ifndef DBALLE_SQL_H
5 #define DBALLE_SQL_H
6 
7 #include <dballe/core/error.h>
8 #include <dballe/fwd.h>
9 #include <dballe/sql/fwd.h>
10 #include <string>
11 #include <memory>
12 
14 #undef USE_REF_INT
15 
20 // #define TRACE_DB
21 
22 #ifdef TRACE_DB
23 #define TRACE(...) fprintf(stderr, __VA_ARGS__)
24 #define IFTRACE if (1)
25 #else
26 
27 #define TRACE(...) do { } while (0)
28 
29 #define IFTRACE if (0)
30 #endif
31 
34 namespace dballe {
35 class Datetime;
36 
37 namespace sql {
38 
44 enum class ServerType
45 {
46  MYSQL,
47  SQLITE,
48  ORACLE,
49  POSTGRES,
50 };
52 
56 const char* format_server_type(ServerType type);
57 
58 
60 {
61 protected:
62  std::string url;
63 
64 public:
71  ServerType server_type;
72 
73  Connection();
74  virtual ~Connection();
75 
76  const std::string& get_url() const { return url; }
77 
84  virtual std::unique_ptr<Transaction> transaction(bool readonly=false) = 0;
85 
87  virtual bool has_table(const std::string& name) = 0;
88 
94  virtual std::string get_setting(const std::string& key) = 0;
95 
101  virtual void set_setting(const std::string& key, const std::string& value) = 0;
102 
104  virtual void drop_settings() = 0;
105 
107  virtual void add_datetime(Querybuf& qb, const Datetime& dt) const;
108 
110  virtual void execute(const std::string& query) = 0;
111 
113  virtual void explain(const std::string& query, FILE* out) = 0;
114 
116  static std::unique_ptr<Connection> create(const DBConnectOptions& options);
117 };
118 
129 {
130 public:
131  Transaction() {}
132  Transaction(const Transaction&) = delete;
133  Transaction& operator=(const Transaction&) = delete;
134  virtual ~Transaction();
135 
137  virtual void commit() = 0;
138 
140  virtual void rollback() = 0;
141 
143  virtual void rollback_nothrow() noexcept = 0;
144 
147  virtual void lock_table(const char* name) = 0;
148 };
149 
150 }
151 }
152 
153 #endif
Forward declarations for public dballe/sql names.
Options controlling how to connect to a database.
Definition: db.h:17
ServerType server_type
Type of SQL server we are connected to.
Definition: sql.h:71
Definition: cmdline.h:18
Definition: sql.h:59
A RAII transaction interface for SQL transactions.
Definition: sql.h:128
Date and time.
Definition: types.h:164
String buffer for composing database queries.
Definition: querybuf.h:15