2010-12-17 17:50:25 -06:00
|
|
|
/********************************************************************
|
2016-07-16 14:12:03 -05:00
|
|
|
* gnc-backend-dbi.hpp: load and save data to SQL via libdbi *
|
2010-12-17 17:50:25 -06:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
\********************************************************************/
|
2010-12-20 11:53:15 -06:00
|
|
|
|
2010-12-17 17:50:25 -06:00
|
|
|
/* Private structures and variables for gnc-backend-dbi.c and its unit tests */
|
2016-07-16 14:12:03 -05:00
|
|
|
#ifndef GNC_BACKEND_DBI_HPP
|
|
|
|
#define GNC_BACKEND_DBI_HPP
|
2015-11-28 15:40:55 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2010-12-17 17:50:25 -06:00
|
|
|
#include <dbi/dbi.h>
|
2016-07-21 18:21:59 -05:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#define GETPID() GetCurrentProcessId()
|
|
|
|
#else
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#define GETPID() getpid()
|
|
|
|
#endif
|
2015-11-28 15:40:55 -06:00
|
|
|
}
|
2016-11-01 13:58:21 -05:00
|
|
|
#include <gnc-sql-backend.hpp>
|
|
|
|
#include <gnc-sql-connection.hpp>
|
|
|
|
|
|
|
|
class GncSqlRow;
|
2016-07-25 13:29:30 -05:00
|
|
|
|
2016-07-21 18:21:59 -05:00
|
|
|
#define GNC_HOST_NAME_MAX 255
|
2010-12-17 17:50:25 -06:00
|
|
|
|
2011-01-17 18:36:08 -06:00
|
|
|
/**
|
|
|
|
* Options to conn_table_operation
|
|
|
|
* @var drop Drop (remove without recourse) the table from the database
|
|
|
|
* @var empty Delete all of the records from the table
|
|
|
|
* @var backup Rename the table "name" to "name_back"
|
|
|
|
* @var rollback drop the name table if it exists and rename name_back to name
|
|
|
|
* @var drop_backup Drop the backup table
|
|
|
|
*/
|
2016-11-28 12:24:35 -06:00
|
|
|
enum TableOpType
|
2011-01-17 18:36:08 -06:00
|
|
|
{
|
|
|
|
drop = 0,
|
|
|
|
empty,
|
|
|
|
backup,
|
|
|
|
rollback,
|
|
|
|
drop_backup
|
2016-11-28 12:24:35 -06:00
|
|
|
};
|
2011-01-17 18:36:08 -06:00
|
|
|
|
2011-09-18 12:57:28 -05:00
|
|
|
/**
|
|
|
|
* Return values from conn_test_dbi_library
|
|
|
|
* @var GNC_DBI_PASS Did not find the large numbers bug
|
|
|
|
* @var GNC_DBI_FAIL_SETUP Could not completed the test
|
|
|
|
* @var GNC_DBI_FAIL_TEST Found the large numbers bug
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GNC_DBI_PASS = 0,
|
|
|
|
GNC_DBI_FAIL_SETUP,
|
|
|
|
GNC_DBI_FAIL_TEST
|
|
|
|
} GncDbiTestResult;
|
|
|
|
|
2016-07-26 19:38:11 -05:00
|
|
|
/**
|
|
|
|
* Supported Dbi Backends.
|
|
|
|
*/
|
|
|
|
enum class DbType
|
|
|
|
{
|
|
|
|
DBI_SQLITE, /**< Sqlite3 */
|
|
|
|
DBI_MYSQL, /**< MySQL and probably MariaDB */
|
|
|
|
DBI_PGSQL /**< Postgresql */
|
|
|
|
};
|
2010-12-17 17:50:25 -06:00
|
|
|
|
2016-06-18 14:40:28 -05:00
|
|
|
/**
|
|
|
|
* Implementations of GncSqlBackend.
|
|
|
|
*/
|
2016-11-28 12:27:09 -06:00
|
|
|
struct UriStrings;
|
|
|
|
|
|
|
|
template <DbType Type>
|
2016-07-21 18:21:59 -05:00
|
|
|
class GncDbiBackend : public GncSqlBackend
|
2010-12-17 17:50:25 -06:00
|
|
|
{
|
2016-07-21 18:21:59 -05:00
|
|
|
public:
|
2016-11-06 11:50:15 -06:00
|
|
|
GncDbiBackend(GncSqlConnection *conn, QofBook* book) :
|
|
|
|
GncSqlBackend(conn, book), m_exists{false} {}
|
2016-11-28 12:27:09 -06:00
|
|
|
~GncDbiBackend();
|
|
|
|
void session_begin(QofSession*, const char*, bool, bool, bool) override;
|
|
|
|
void session_end() override;
|
|
|
|
void load(QofBook*, QofBackendLoadType) override;
|
|
|
|
void safe_sync(QofBook*) override;
|
2016-07-21 18:21:59 -05:00
|
|
|
bool connected() const noexcept { return m_conn != nullptr; }
|
|
|
|
/** FIXME: Just a pass-through to m_conn: */
|
2016-11-28 12:27:09 -06:00
|
|
|
void set_dbi_error(int error, unsigned int repeat, bool retry) noexcept
|
2016-07-21 18:21:59 -05:00
|
|
|
{
|
|
|
|
m_conn->set_error(error, repeat, retry);
|
|
|
|
}
|
|
|
|
void retry_connection(const char* msg) const noexcept
|
|
|
|
{
|
|
|
|
m_conn->retry_connection(msg);
|
|
|
|
}
|
|
|
|
/*-----*/
|
|
|
|
bool exists() { return m_exists; }
|
|
|
|
void set_exists(bool exists) { m_exists = exists; }
|
|
|
|
private:
|
2016-11-28 12:27:09 -06:00
|
|
|
dbi_conn conn_setup(PairVec& options, UriStrings& uri);
|
|
|
|
bool conn_test_dbi_library(dbi_conn conn);
|
|
|
|
bool set_standard_connection_options(dbi_conn conn, const UriStrings& uri);
|
|
|
|
bool create_database(dbi_conn conn, const char* db);
|
2016-07-21 18:21:59 -05:00
|
|
|
bool m_exists; // Does the database exist?
|
2010-12-17 17:50:25 -06:00
|
|
|
};
|
|
|
|
|
2016-05-19 16:23:23 -05:00
|
|
|
/* external access required for tests */
|
2016-05-20 12:09:47 -05:00
|
|
|
std::string adjust_sql_options_string(const std::string&);
|
2010-12-17 17:50:25 -06:00
|
|
|
|
2016-03-28 01:48:02 -05:00
|
|
|
|
2016-04-09 17:31:36 -05:00
|
|
|
|
2016-07-16 14:12:03 -05:00
|
|
|
#endif //GNC_BACKEND_DBI_HPP
|