libdballe 9.13
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 <memory>
11#include <string>
12
14#undef USE_REF_INT
15
19
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(...) \
28 do \
29 { \
30 } while (0)
31
32#define IFTRACE if (0)
33#endif
34
36
37namespace dballe {
38class Datetime;
39
40namespace sql {
41
43enum class ServerType {
44 MYSQL,
45 SQLITE,
46 ORACLE,
47 POSTGRES,
48};
49
52
53class Connection : public std::enable_shared_from_this<Connection>
54{
55private:
56 static void atfork_prepare_hook();
57 static void atfork_parent_hook();
58 static void atfork_child_hook();
59
60protected:
61 std::string url;
62
63 Connection();
64
67
68 // Optional pthread_atfork hooks
69 virtual void fork_prepare();
70 virtual void fork_parent();
71 virtual void fork_child();
72
73public:
81
82 virtual ~Connection();
83
84 const std::string& get_url() const { return url; }
85
92 virtual std::unique_ptr<Transaction> transaction(bool readonly = false) = 0;
93
95 virtual bool has_table(const std::string& name) = 0;
96
102 virtual std::string get_setting(const std::string& key) = 0;
103
109 virtual void set_setting(const std::string& key,
110 const std::string& value) = 0;
111
113 virtual void drop_settings() = 0;
114
116 virtual void add_datetime(Querybuf& qb, const Datetime& dt) const;
117
119 virtual void execute(const std::string& query) = 0;
120
122 virtual void explain(const std::string& query, FILE* out) = 0;
123
125 static std::shared_ptr<Connection> create(const DBConnectOptions& options);
126};
127
137class Transaction
138{
139public:
140 Transaction() {}
141 Transaction(const Transaction&) = delete;
142 Transaction& operator=(const Transaction&) = delete;
143 virtual ~Transaction();
144
146 virtual void commit() = 0;
147
149 virtual void rollback() = 0;
150
152 virtual void rollback_nothrow() noexcept = 0;
153
156 virtual void lock_table(const char* name) = 0;
157};
158
159} // namespace sql
160} // namespace dballe
161
162#endif
Options controlling how to connect to a database.
Definition db.h:18
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:80
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.
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:43
Date and time.
Definition types.h:164
String buffer for composing database queries.
Definition querybuf.h:16