libdballe  7.7
sql.h
1 /*
2  * db/sql - Generic infrastructure for talking with SQL databases
3  *
4  * Copyright (C) 2005--2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBALLE_DB_INTERNALS_H
23 #define DBALLE_DB_INTERNALS_H
24 
25 #include <wreport/error.h>
26 #include <string>
27 #include <memory>
28 
30 #undef USE_REF_INT
31 
36 // #define TRACE_DB
37 
38 #ifdef TRACE_DB
39 #define TRACE(...) fprintf(stderr, __VA_ARGS__)
40 #define IFTRACE if (1)
41 #else
42 
43 #define TRACE(...) do { } while (0)
44 
45 #define IFTRACE if (0)
46 #endif
47 
50 namespace dballe {
51 class Datetime;
52 class Querybuf;
53 
54 namespace db {
55 class Transaction;
56 class Statement;
57 
61 enum class ServerType
62 {
63  MYSQL,
64  SQLITE,
65  ORACLE,
66  POSTGRES,
67 };
68 
70 {
71 protected:
72  std::string url;
73 
74 public:
81  ServerType server_type;
82 
83  virtual ~Connection();
84 
85  const std::string& get_url() const { return url; }
86 
93  virtual std::unique_ptr<Transaction> transaction() = 0;
94 
96  virtual bool has_table(const std::string& name) = 0;
97 
103  virtual std::string get_setting(const std::string& key) = 0;
104 
110  virtual void set_setting(const std::string& key, 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  static std::unique_ptr<Connection> create_from_url(const char* url);
120 
122  static std::unique_ptr<Connection> create_from_url(const std::string& url);
123 };
124 
135 {
136 public:
137  virtual ~Transaction() {}
138 
140  virtual void commit() = 0;
141 
143  virtual void rollback() = 0;
144 
147  virtual void lock_table(const char* name) = 0;
148 };
149 
150 }
151 }
152 
153 #endif
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
String buffer for composing database queries.
Definition: querybuf.h:37
ServerType server_type
Type of SQL server we are connected to.
Definition: sql.h:81
A RAII transaction interface.
Definition: sql.h:134
Date and time.
Definition: types.h:147
Definition: sql.h:69