GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-dbisqlconnection.hpp
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
35using StrVec = std::vector<std::string>;
36class GncDbiProvider;
37
42{
43public:
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 int dberror() const noexcept override {
64 return dbi_conn_error(m_conn, nullptr); }
65 QofBackend* qbe () const noexcept { return m_qbe; }
66 dbi_conn conn() const noexcept { return m_conn; }
67 inline void set_error(QofBackendError error, unsigned int repeat,
68 bool retry) noexcept override
69 {
70 m_last_error = error;
71 m_error_repeat = repeat;
72 m_retry = retry;
73 }
74 inline void init_error() noexcept
75 {
76 set_error(ERR_BACKEND_NO_ERR, 0, false);
77 }
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;
88private:
89 QofBackend* m_qbe = nullptr;
90 dbi_conn m_conn;
91 std::unique_ptr<GncDbiProvider> m_provider;
95 bool m_conn_ok;
99 QofBackendError m_last_error;
104 unsigned int m_error_repeat;
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_
Encapsulate a libdbi dbi_conn connection.
bool table_operation(TableOpType op) noexcept
Perform a specified SQL operation on every table in a database.
bool commit_transaction() noexcept override
Returns TRUE if successful, FALSE if error.
bool add_columns_to_table(const std::string &, const ColVec &) const noexcept override
Returns TRUE if successful, FALSE if error.
int execute_nonselect_statement(const GncSqlStatementPtr &) noexcept override
Returns false if error.
bool create_index(const std::string &, const std::string &, const EntryVec &) const noexcept override
Returns TRUE if successful, FALSE if error.
bool does_table_exist(const std::string &) const noexcept override
Returns true if successful.
bool begin_transaction() noexcept override
Returns TRUE if successful, false if error.
bool verify() noexcept override
Check if the dbi connection is valid.
int dberror() const noexcept override
Get the connection error value.
bool rollback_transaction() noexcept override
Returns TRUE if successful, FALSE if error.
bool create_table(const std::string &, const ColVec &) const noexcept override
Returns TRUE if successful, FALSE if error.
Encapsulate the connection to the database.
Pure virtual class to iterate over a query result set.
load and save data to SQL via libdbi
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition qofbackend.h:58
SessionOpenMode
Mode for opening sessions.
Definition qofsession.h:121