GnuCash  5.6-150-g038405b370+
gnc-backend-dbi.hpp
1 /********************************************************************
2  * gnc-backend-dbi.hpp: load and save data to SQL via libdbi *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20 \********************************************************************/
21 
22 /* Private structures and variables for gnc-backend-dbi.c and its unit tests */
23 #ifndef GNC_BACKEND_DBI_HPP
24 #define GNC_BACKEND_DBI_HPP
25 
26 #include <dbi/dbi.h>
27 #ifdef G_OS_WIN32
28 #include <winsock2.h>
29 #define GETPID() GetCurrentProcessId()
30 #else
31 #include <limits.h>
32 #include <unistd.h>
33 #define GETPID() getpid()
34 #endif
35 
36 #include <gnc-sql-backend.hpp>
37 #include <gnc-sql-connection.hpp>
38 
39 class GncSqlRow;
40 
41 #define GNC_HOST_NAME_MAX 255
42 
51 enum TableOpType
52 {
53  backup = 0,
54  rollback,
55  drop_backup,
56  recover
57 };
58 
65 typedef enum
66 {
67  GNC_DBI_PASS = 0,
68  GNC_DBI_FAIL_SETUP,
69  GNC_DBI_FAIL_TEST
70 } GncDbiTestResult;
71 
75 enum class DbType
76 {
77  DBI_SQLITE,
78  DBI_MYSQL,
79  DBI_PGSQL
80 };
81 
85 struct UriStrings;
86 
87 template <DbType Type>
89 {
90 public:
91  GncDbiBackend(GncSqlConnection *conn, QofBook* book) :
92  GncSqlBackend(conn, book), m_exists{false} {}
93  ~GncDbiBackend();
94  void session_begin(QofSession*, const char*, SessionOpenMode) override;
95  void session_end() override;
96  void load(QofBook*, QofBackendLoadType) override;
97  void safe_sync(QofBook*) override;
98  bool connected() const noexcept { return m_conn != nullptr; }
100  void set_dbi_error(QofBackendError error, unsigned int repeat,
101  bool retry) noexcept
102  {
103  m_conn->set_error(error, repeat, retry);
104  }
105  void retry_connection(const char* msg) const noexcept
106  {
107  m_conn->retry_connection(msg);
108  }
109  /*-----*/
110  bool exists() { return m_exists; }
111  void set_exists(bool exists) { m_exists = exists; }
112 private:
113  dbi_conn conn_setup(PairVec& options, UriStrings& uri);
114  bool conn_test_dbi_library(dbi_conn conn);
115  bool set_standard_connection_options(dbi_conn conn, const UriStrings& uri);
116  bool create_database(dbi_conn conn, const char* db);
117  bool m_exists; // Does the database exist?
118 };
119 
120 /* locale-stack */
121 inline std::string
122 gnc_push_locale(const int category, const std::string locale)
123 {
124  std::string retval(setlocale(category, nullptr));
125  setlocale(category, locale.c_str());
126  return retval;
127 }
128 
129 inline void
130 gnc_pop_locale(const int category, std::string locale)
131 {
132  setlocale(category, locale.c_str());
133 }
134 
135 /* external access required for tests */
136 std::string adjust_sql_options_string(const std::string&);
137 
138 
139 
140 #endif //GNC_BACKEND_DBI_HPP
void set_dbi_error(QofBackendError error, unsigned int repeat, bool retry) noexcept
FIXME: Just a pass-through to m_conn:
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:57
GncSqlConnection * m_conn
SQL connection.
void safe_sync(QofBook *) override
Safely resave a database by renaming all of its tables, recreating everything, and then dropping the ...
Row of SQL Query results.
void load(QofBook *, QofBackendLoadType) override
Load the minimal set of application data needed for the application to be operable at initial startup...
SessionOpenMode
Mode for opening sessions.
Definition: qofsession.h:120
Encapsulate the connection to the database.
void session_begin(QofSession *, const char *, SessionOpenMode) override
Open the file or connect to the server.
Main SQL backend structure.