Line data Source code
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 :
43 : /**
44 : * Options to conn_table_operation
45 : * @var drop Drop (remove without recourse) the table from the database
46 : * @var empty Delete all of the records from the table
47 : * @var backup Rename the table "name" to "name_back"
48 : * @var rollback drop the name table if it exists and rename name_back to name
49 : * @var drop_backup Drop the backup table
50 : */
51 : enum TableOpType
52 : {
53 : backup = 0,
54 : rollback,
55 : drop_backup,
56 : recover
57 : };
58 :
59 : /**
60 : * Return values from conn_test_dbi_library
61 : * @var GNC_DBI_PASS Did not find the large numbers bug
62 : * @var GNC_DBI_FAIL_SETUP Could not completed the test
63 : * @var GNC_DBI_FAIL_TEST Found the large numbers bug
64 : */
65 : typedef enum
66 : {
67 : GNC_DBI_PASS = 0,
68 : GNC_DBI_FAIL_SETUP,
69 : GNC_DBI_FAIL_TEST
70 : } GncDbiTestResult;
71 :
72 : /**
73 : * Supported Dbi Backends.
74 : */
75 : enum class DbType
76 : {
77 : DBI_SQLITE, /**< Sqlite3 */
78 : DBI_MYSQL, /**< MySQL and probably MariaDB */
79 : DBI_PGSQL /**< Postgresql */
80 : };
81 :
82 : /**
83 : * Implementations of GncSqlBackend.
84 : */
85 : struct UriStrings;
86 :
87 : template <DbType Type>
88 : class GncDbiBackend : public GncSqlBackend
89 : {
90 : public:
91 0 : GncDbiBackend(GncSqlConnection *conn, QofBook* book) :
92 0 : 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 0 : bool connected() const noexcept { return m_conn != nullptr; }
99 : /** FIXME: Just a pass-through to m_conn: */
100 0 : void set_dbi_error(QofBackendError error, unsigned int repeat,
101 : bool retry) noexcept
102 : {
103 0 : m_conn->set_error(error, repeat, retry);
104 0 : }
105 0 : void retry_connection(const char* msg) const noexcept
106 : {
107 0 : m_conn->retry_connection(msg);
108 0 : }
109 : /*-----*/
110 : bool exists() { return m_exists; }
111 0 : 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 0 : gnc_push_locale(const int category, const std::string locale)
123 : {
124 0 : std::string retval(setlocale(category, nullptr));
125 0 : setlocale(category, locale.c_str());
126 0 : return retval;
127 : }
128 :
129 : inline void
130 0 : gnc_pop_locale(const int category, std::string locale)
131 : {
132 0 : setlocale(category, locale.c_str());
133 0 : }
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
|