libdballe 9.6
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
27#define TRACE(...) do { } while (0)
29#define IFTRACE if (0)
30#endif
31
34namespace dballe {
35class Datetime;
36
37namespace sql {
38
40enum class ServerType
41{
42 MYSQL,
43 SQLITE,
44 ORACLE,
45 POSTGRES,
46};
47
50
51
52class Connection : public std::enable_shared_from_this<Connection>
53{
54private:
55 static void atfork_prepare_hook();
56 static void atfork_parent_hook();
57 static void atfork_child_hook();
58
59protected:
60 std::string url;
61
62 Connection();
63
66
67 // Optional pthread_atfork hooks
68 virtual void fork_prepare();
69 virtual void fork_parent();
70 virtual void fork_child();
71
72public:
80
81 virtual ~Connection();
82
83 const std::string& get_url() const { return url; }
84
91 virtual std::unique_ptr<Transaction> transaction(bool readonly=false) = 0;
92
94 virtual bool has_table(const std::string& name) = 0;
95
101 virtual std::string get_setting(const std::string& key) = 0;
102
108 virtual void set_setting(const std::string& key, const std::string& value) = 0;
109
111 virtual void drop_settings() = 0;
112
114 virtual void add_datetime(Querybuf& qb, const Datetime& dt) const;
115
117 virtual void execute(const std::string& query) = 0;
118
120 virtual void explain(const std::string& query, FILE* out) = 0;
121
123 static std::shared_ptr<Connection> create(const DBConnectOptions& options);
124};
125
136{
137public:
138 Transaction() {}
139 Transaction(const Transaction&) = delete;
140 Transaction& operator=(const Transaction&) = delete;
141 virtual ~Transaction();
142
144 virtual void commit() = 0;
145
147 virtual void rollback() = 0;
148
150 virtual void rollback_nothrow() noexcept = 0;
151
154 virtual void lock_table(const char* name) = 0;
155};
156
157}
158}
159
160#endif
Options controlling how to connect to a database.
Definition: db.h:18
Definition: sql.h:53
virtual void drop_settings()=0
Drop the settings table.
virtual void add_datetime(Querybuf &qb, const Datetime &dt) const
Format a datetime and add it to the querybuf.
virtual void set_setting(const std::string &key, const std::string &value)=0
Set a value in the settings table.
virtual void execute(const std::string &query)=0
Execute a query without reading its results.
static std::shared_ptr< Connection > create(const DBConnectOptions &options)
Create a new connection from a URL.
virtual bool has_table(const std::string &name)=0
Check if the database contains a table.
virtual std::unique_ptr< Transaction > transaction(bool readonly=false)=0
Begin a transaction.
void register_atfork()
Register this connection to be notified in case of fork()-ing.
ServerType server_type
Type of SQL server we are connected to.
Definition: sql.h:79
virtual std::string get_setting(const std::string &key)=0
Get a value from the settings table.
virtual void explain(const std::string &query, FILE *out)=0
Format and print the EXPLAIN output for the query to the given file.
A RAII transaction interface for SQL transactions.
Definition: sql.h:136
virtual void lock_table(const char *name)=0
Get an exclusive lock on the given table until the end of the transaction.
virtual void rollback_nothrow() noexcept=0
Roll back this transaction.
virtual void rollback()=0
Roll back this transaction.
virtual void commit()=0
Commit this transaction.
Forward declarations for public dballe/sql names.
const char * format_server_type(ServerType type)
Return a string description for a ServerType value.
ServerType
Supported SQL servers.
Definition: sql.h:41
Date and time.
Definition: types.h:165
String buffer for composing database queries.
Definition: querybuf.h:16