Branch data Line data Source code
1 : : /********************************************************************
2 : : * gnc-dbisqlconnection.hpp: Encapsulate libdbi dbi_conn *
3 : : * *
4 : : * Copyright 2016 John Ralls <jralls@ceridwen.us> *
5 : : * *
6 : : * This program is free software; you can redistribute it and/or *
7 : : * modify it under the terms of the GNU General Public License as *
8 : : * published by the Free Software Foundation; either version 2 of *
9 : : * the License, or (at your option) any later version. *
10 : : * *
11 : : * This program is distributed in the hope that it will be useful, *
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : : * GNU General Public License for more details. *
15 : : * *
16 : : * You should have received a copy of the GNU General Public License*
17 : : * along with this program; if not, contact: *
18 : : * *
19 : : * Free Software Foundation Voice: +1-617-542-5942 *
20 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : : \********************************************************************/
23 : : #ifndef _GNC_DBISQLCONNECTION_HPP_
24 : : #define _GNC_DBISQLCONNECTION_HPP_
25 : :
26 : : #include <string>
27 : : #include <vector>
28 : :
29 : : #include <gnc-sql-connection.hpp>
30 : : #include "gnc-backend-dbi.hpp"
31 : : #include "gnc-dbisqlresult.hpp"
32 : : #include "gnc-dbiprovider.hpp"
33 : : #include "gnc-backend-dbi.h"
34 : :
35 : : using StrVec = std::vector<std::string>;
36 : : class GncDbiProvider;
37 : :
38 : : /**
39 : : * Encapsulate a libdbi dbi_conn connection.
40 : : */
41 : : class GncDbiSqlConnection : public GncSqlConnection
42 : : {
43 : : public:
44 : : GncDbiSqlConnection (DbType type, QofBackend* qbe, dbi_conn conn,
45 : : SessionOpenMode mode);
46 : : ~GncDbiSqlConnection() override;
47 : : GncSqlResultPtr execute_select_statement (const GncSqlStatementPtr&)
48 : : noexcept override;
49 : : int execute_nonselect_statement (const GncSqlStatementPtr&)
50 : : noexcept override;
51 : : GncSqlStatementPtr create_statement_from_sql (const std::string&)
52 : : const noexcept override;
53 : : bool does_table_exist (const std::string&) const noexcept override;
54 : : bool begin_transaction () noexcept override;
55 : : bool rollback_transaction () noexcept override;
56 : : bool commit_transaction () noexcept override;
57 : : bool create_table (const std::string&, const ColVec&) const noexcept override;
58 : : bool create_index (const std::string&, const std::string&, const EntryVec&)
59 : : const noexcept override;
60 : : bool add_columns_to_table (const std::string&, const ColVec&)
61 : : const noexcept override;
62 : : std::string quote_string (const std::string&) const noexcept override;
63 : 0 : int dberror() const noexcept override {
64 : 0 : return dbi_conn_error(m_conn, nullptr); }
65 : 0 : QofBackend* qbe () const noexcept { return m_qbe; }
66 : : dbi_conn conn() const noexcept { return m_conn; }
67 : 0 : inline void set_error(QofBackendError error, unsigned int repeat,
68 : : bool retry) noexcept override
69 : : {
70 : 0 : m_last_error = error;
71 : 0 : m_error_repeat = repeat;
72 : 0 : m_retry = retry;
73 : 0 : }
74 : 0 : inline void init_error() noexcept
75 : : {
76 : 0 : set_error(ERR_BACKEND_NO_ERR, 0, false);
77 : 0 : }
78 : : /** Check if the dbi connection is valid. If not attempt to re-establish it
79 : : * Returns TRUE if there is a valid connection in the end or FALSE otherwise
80 : : */
81 : : bool verify() noexcept override;
82 : : bool retry_connection(const char* msg) noexcept override;
83 : :
84 : : bool table_operation (TableOpType op) noexcept;
85 : : std::string add_columns_ddl(const std::string& table_name,
86 : : const ColVec& info_vec) const noexcept;
87 : : bool drop_indexes() noexcept;
88 : : private:
89 : : QofBackend* m_qbe = nullptr;
90 : : dbi_conn m_conn;
91 : : std::unique_ptr<GncDbiProvider> m_provider;
92 : : /** Used by the error handler routines to flag if the connection is ok to
93 : : * use
94 : : */
95 : : bool m_conn_ok;
96 : : /** Code of the last error that occurred. This is set in the error callback
97 : : * function.
98 : : */
99 : : QofBackendError m_last_error;
100 : : /** Used in case of transient errors. After such error, another attempt at
101 : : * the original call is allowed. error_repeat tracks the number of attempts
102 : : * and can be used to prevent infinite loops.
103 : : */
104 : : unsigned int m_error_repeat;
105 : : /** Signals the calling function that it should retry (the error handler
106 : : * detected a transient error and managed to resolve it, but it can't run
107 : : * the original query)
108 : : */
109 : : bool m_retry;
110 : : unsigned int m_sql_savepoint;
111 : : bool m_readonly;
112 : : bool lock_database(bool break_lock);
113 : : void unlock_database();
114 : : bool rename_table(const std::string& old_name, const std::string& new_name);
115 : : bool drop_table(const std::string& table);
116 : : bool merge_tables(const std::string& table, const std::string& other);
117 : : bool check_and_rollback_failed_save();
118 : : };
119 : :
120 : : #endif //_GNC_DBISQLCONNECTION_HPP_
|