Branch data Line data Source code
1 : : /***********************************************************************\
2 : : * gnc-sql-connection.hpp: Encapsulate a SQL database connection. *
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 : :
24 : : #ifndef __GNC_SQL_CONNECTION_HPP__
25 : : #define __GNC_SQL_CONNECTION_HPP__
26 : :
27 : : #include <qof.h>
28 : : #include <memory>
29 : : #include <string>
30 : : #include <vector>
31 : :
32 : : class GncSqlResult;
33 : : using GncSqlResultPtr = GncSqlResult*;
34 : : class GncSqlColumnTableEntry;
35 : : using GncSqlColumnTableEntryPtr = std::shared_ptr<GncSqlColumnTableEntry>;
36 : : using EntryVec = std::vector<GncSqlColumnTableEntryPtr>;
37 : : using PairVec = std::vector<std::pair<std::string, std::string>>;
38 : : struct GncSqlColumnInfo;
39 : : using ColVec = std::vector<GncSqlColumnInfo>;
40 : :
41 : : /**
42 : : * SQL statement provider.
43 : : */
44 : : class GncSqlStatement
45 : : {
46 : : public:
47 : 0 : virtual ~GncSqlStatement() {}
48 : : virtual const char* to_sql() const = 0;
49 : : virtual void add_where_cond (QofIdTypeConst, const PairVec&) = 0;
50 : : };
51 : :
52 : : using GncSqlStatementPtr = std::unique_ptr<GncSqlStatement>;
53 : :
54 : : /**
55 : : * Encapsulate the connection to the database. This is an abstract class; the
56 : : * implementation is database-specific.
57 : : */
58 : : class GncSqlConnection
59 : : {
60 : : public:
61 : : /** Returns NULL if error */
62 : 0 : virtual ~GncSqlConnection() = default;
63 : : virtual GncSqlResultPtr execute_select_statement (const GncSqlStatementPtr&)
64 : : noexcept = 0;
65 : : /** Returns false if error */
66 : : virtual int execute_nonselect_statement (const GncSqlStatementPtr&)
67 : : noexcept = 0;
68 : : virtual GncSqlStatementPtr create_statement_from_sql (const std::string&)
69 : : const noexcept = 0;
70 : : /** Returns true if successful */
71 : : virtual bool does_table_exist (const std::string&) const noexcept = 0;
72 : : /** Returns TRUE if successful, false if error */
73 : : virtual bool begin_transaction () noexcept = 0;
74 : : /** Returns TRUE if successful, FALSE if error */
75 : : virtual bool rollback_transaction () noexcept = 0;
76 : : /** Returns TRUE if successful, FALSE if error */
77 : : virtual bool commit_transaction () noexcept = 0;
78 : : /** Returns TRUE if successful, FALSE if error */
79 : : virtual bool create_table (const std::string&, const ColVec&)
80 : : const noexcept = 0;
81 : : /** Returns TRUE if successful, FALSE if error */
82 : : virtual bool create_index (const std::string&, const std::string&,
83 : : const EntryVec&) const noexcept = 0;
84 : : /** Returns TRUE if successful, FALSE if error */
85 : : virtual bool add_columns_to_table (const std::string&, const ColVec&)
86 : : const noexcept = 0;
87 : : virtual std::string quote_string (const std::string&)
88 : : const noexcept = 0;
89 : : /** Get the connection error value.
90 : : * If not 0 will normally be meaningless outside of implementation code.
91 : : */
92 : : virtual int dberror() const noexcept = 0;
93 : : virtual void set_error(QofBackendError error, unsigned int repeat,
94 : : bool retry) noexcept = 0;
95 : : virtual bool verify() noexcept = 0;
96 : : virtual bool retry_connection(const char* msg) noexcept = 0;
97 : :
98 : : };
99 : :
100 : :
101 : : #endif //__GNC_SQL_CONNECTION_HPP__
|