libdballe  7.29
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/transaction.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  bool profile = false;
64  unsigned profile_query_count = 0;
65 
66 public:
73  ServerType server_type;
74 
75  Connection();
76  virtual ~Connection();
77 
78  const std::string& get_url() const { return url; }
79 
86  virtual std::unique_ptr<Transaction> transaction() = 0;
87 
89  virtual bool has_table(const std::string& name) = 0;
90 
96  virtual std::string get_setting(const std::string& key) = 0;
97 
103  virtual void set_setting(const std::string& key, const std::string& value) = 0;
104 
106  virtual void drop_settings() = 0;
107 
109  virtual void add_datetime(Querybuf& qb, const Datetime& dt) const;
110 
112  virtual void execute(const std::string& query) = 0;
113 
115  virtual void explain(const std::string& query, FILE* out) = 0;
116 
118  static std::unique_ptr<Connection> create_from_url(const char* url);
119 
121  static std::unique_ptr<Connection> create_from_url(const std::string& url);
122 };
123 
134 {
135 public:
136  Transaction() {}
137  Transaction(const Transaction&) = delete;
138  Transaction& operator=(const Transaction&) = delete;
139 
142  virtual void lock_table(const char* name) = 0;
143 };
144 
145 }
146 }
147 
148 #endif
A RAII transaction interface.
Definition: transaction.h:15
ServerType server_type
Type of SQL server we are connected to.
Definition: sql.h:73
Forward declarations for public dballe/sql names.
virtual void set_setting(const std::string &key, const std::string &value)=0
Set a value in the settings table.
Definition: sql.h:59
virtual void drop_settings()=0
Drop the settings table.
virtual std::unique_ptr< Transaction > transaction()=0
Begin a transaction.
virtual void add_datetime(Querybuf &qb, const Datetime &dt) const
Format a datetime and add it to the querybuf.
A RAII transaction interface.
Definition: sql.h:133
virtual std::string get_setting(const std::string &key)=0
Get a value from the settings table.
Date and time.
Definition: types.h:158
virtual bool has_table(const std::string &name)=0
Check if the database contains a table.
virtual void execute(const std::string &query)=0
Execute a query without reading its results.
virtual void explain(const std::string &query, FILE *out)=0
Format and print the EXPLAIN output for the query to the given file.
virtual void lock_table(const char *name)=0
Get an exclusive lock on the given table until the end of the transaction.
String buffer for composing database queries.
Definition: querybuf.h:15
static std::unique_ptr< Connection > create_from_url(const char *url)
Create a new connection from a URL.