29#include <glib/gstdio.h>
42#include "qofquery-p.h"
43#include "qofquerycore-p.h"
48#include "Recurrence.h"
53#include "gnc-locale-utils.h"
58#include "splint-defs.h"
61#include <boost/regex.hpp>
65#include <qofsession.hpp>
66#include <gnc-backend-prov.hpp>
68#include "gnc-backend-dbi.hpp"
70#include <gnc-sql-object-backend.hpp>
71#include "gnc-dbisqlresult.hpp"
72#include "gnc-dbisqlconnection.hpp"
74#if LIBDBI_VERSION >= 900
75#define HAVE_LIBDBI_R 1
76static dbi_inst dbi_instance =
nullptr;
78#define HAVE_LIBDBI_R 0
79#define HAVE_LIBDBI_TO_LONGLONG 0
82#define TRANSACTION_NAME "trans"
84static QofLogModule log_module = G_LOG_DOMAIN;
86#define FILE_URI_TYPE "file"
87#define FILE_URI_PREFIX (FILE_URI_TYPE "://")
88#define SQLITE3_URI_TYPE "sqlite3"
89#define SQLITE3_URI_PREFIX (SQLITE3_URI_TYPE "://")
90#define PGSQL_DEFAULT_PORT 5432
92static void adjust_sql_options (dbi_conn connection);
93template<DbType Type>
bool save_may_clobber_data (dbi_conn conn,
94 const std::string& dbname);
97class QofDbiBackendProvider :
public QofBackendProvider
100 QofDbiBackendProvider (
const char* name,
const char* type) :
101 QofBackendProvider {name, type} {}
102 QofDbiBackendProvider(QofDbiBackendProvider&) =
delete;
103 QofDbiBackendProvider operator=(QofDbiBackendProvider&) =
delete;
104 QofDbiBackendProvider(QofDbiBackendProvider&&) =
delete;
105 QofDbiBackendProvider operator=(QofDbiBackendProvider&&) =
delete;
106 ~QofDbiBackendProvider () =
default;
118 UriStrings() =
default;
119 UriStrings(
const std::string& uri);
120 ~UriStrings() =
default;
121 std::string basename()
const noexcept;
122 const char* dbname()
const noexcept;
123 std::string quote_dbname(DbType t)
const noexcept;
124 std::string m_protocol;
126 std::string m_dbname;
127 std::string m_username;
128 std::string m_password;
129 std::string m_basename;
133UriStrings::UriStrings(
const std::string& uri)
135 gchar *scheme, *host, *username, *password, *dbname;
139 m_protocol = std::string{scheme};
140 m_host = std::string{host};
142 m_dbname = std::string{dbname};
144 m_username = std::string{username};
146 m_password = std::string{password};
156UriStrings::basename() const noexcept
158 return m_protocol +
"_" + m_host +
"_" + m_username +
"_" + m_dbname;
162UriStrings::dbname() const noexcept
164 return m_dbname.c_str();
168UriStrings::quote_dbname(DbType t)
const noexcept
170 if (m_dbname.empty())
172 const char quote = (t == DbType::DBI_MYSQL ?
'`' :
'"');
173 std::string retval(1, quote);
174 retval += m_dbname + quote;
179set_options(dbi_conn conn,
const PairVec& options)
181 for (
const auto& option : options)
183 auto opt = option.first.c_str();
184 auto val = option.second.c_str();
185 auto result = dbi_conn_set_option(conn, opt, val);
188 const char *msg =
nullptr;
189 dbi_conn_error(conn, &msg);
190 PERR(
"Error setting %s option to %s: %s", opt, val, msg);
191 throw std::runtime_error(msg);
203template <DbType Type>
bool
204GncDbiBackend<Type>::set_standard_connection_options (dbi_conn conn,
209 options.push_back(std::make_pair(
"host", uri.m_host));
210 options.push_back(std::make_pair(
"dbname", uri.m_dbname));
211 options.push_back(std::make_pair(
"username", uri.m_username));
212 options.push_back(std::make_pair(
"password", uri.m_password));
213 options.push_back(std::make_pair(
"encoding",
"UTF-8"));
216 set_options(conn, options);
217 auto result = dbi_conn_set_option_numeric(conn,
"port", uri.m_portnum);
220 const char *msg =
nullptr;
221 auto err = dbi_conn_error(conn, &msg);
222 PERR(
"Error (%d) setting port option to %d: %s", err, uri.m_portnum, msg);
223 throw std::runtime_error(msg);
226 catch (std::runtime_error& err)
235template <DbType Type>
void error_handler(dbi_conn conn,
void* data);
236void error_handler(dbi_conn conn,
void* data);
238template <DbType Type> dbi_conn
239GncDbiBackend<Type>::conn_setup (PairVec& options,
UriStrings& uri)
241 const char* dbstr = (Type == DbType::DBI_SQLITE ?
"sqlite3" :
242 Type == DbType::DBI_MYSQL ?
"mysql" :
"pgsql");
244 dbi_conn conn =
nullptr;
246 conn = dbi_conn_new_r (dbstr, dbi_instance);
248 PERR (
"Attempt to connect with an uninitialized dbi_instance");
250 auto conn = dbi_conn_new (dbstr);
255 PERR (
"Unable to create %s dbi connection", dbstr);
260 dbi_conn_error_handler (conn, error_handler<Type>,
this);
261 if (!uri.m_dbname.empty() &&
262 !set_standard_connection_options(conn, uri))
264 dbi_conn_close(conn);
270 set_options(conn, options);
272 catch (std::runtime_error& err)
274 dbi_conn_close(conn);
283template <DbType Type>
bool
284GncDbiBackend<Type>::create_database(dbi_conn conn,
const char* db)
287 const char *dbcreate;
288 if (Type == DbType::DBI_MYSQL)
291 dbcreate =
"CREATE DATABASE %s CHARACTER SET utf8";
296 dbcreate =
"CREATE DATABASE %s WITH TEMPLATE template0 ENCODING 'UTF8'";
299 options.push_back(std::make_pair(
"dbname", dbname));
302 set_options(conn, options);
304 catch (std::runtime_error& err)
310 auto result = dbi_conn_connect (conn);
313 PERR (
"Unable to connect to %s database", dbname);
317 if (Type == DbType::DBI_MYSQL)
318 adjust_sql_options(conn);
319 auto dresult = dbi_conn_queryf (conn, dbcreate, db);
320 if (dresult ==
nullptr)
322 PERR (
"Unable to create database '%s'\n", db);
326 dbi_result_free (dresult);
327 if (Type == DbType::DBI_PGSQL)
329 const char *alterdb =
"ALTER DATABASE %s SET "
330 "standard_conforming_strings TO on";
331 dbi_result_free (dbi_conn_queryf (conn, alterdb, db));
333 dbi_conn_close(conn);
339error_handler<DbType::DBI_SQLITE> (dbi_conn conn,
void* user_data)
343 static_cast<decltype(dbi_be)
>(user_data);
344 int err_num = dbi_conn_error (conn, &msg);
350 if (err_num == DBI_ERROR_BADIDX)
return;
351 PERR (
"DBI error: %s\n", msg);
352 if (dbi_be->connected())
361 gboolean file_exists;
364 g_return_if_fail (session !=
nullptr);
365 g_return_if_fail (new_uri !=
nullptr);
371 std::string filepath{path};
373 GFileTest ftest =
static_cast<decltype (ftest)
> (
374 G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS) ;
375 file_exists = g_file_test (filepath.c_str(), ftest);
377 if (!create && !file_exists)
380 std::string msg{
"Sqlite3 file "};
382 PWARN (
"Sqlite3 file %s not found", filepath.c_str());
387 if (create && file_exists)
390 g_unlink (filepath.c_str());
394 auto msg =
"Might clobber, mode not SESSION_NEW_OVERWRITE";
403 options.push_back(std::make_pair(
"host",
"localhost"));
404 auto dirname = g_path_get_dirname (filepath.c_str());
405 auto basename = g_path_get_basename (filepath.c_str());
406 options.push_back(std::make_pair(
"dbname", basename));
407 options.push_back(std::make_pair(
"sqlite3_dbdir", dirname));
408 if (basename !=
nullptr) g_free (basename);
409 if (dirname !=
nullptr) g_free (dirname);
411 auto conn = conn_setup(options, uri);
418 auto result = dbi_conn_connect (conn);
422 dbi_conn_close(conn);
423 PERR (
"Unable to connect to %s: %d\n", new_uri, result);
431 if (create && !file_exists)
436 dbi_conn_close (conn);
438 g_unlink (filepath.c_str());
440 dbi_conn_close(conn);
441 LEAVE(
"Bad DBI Library");
450 catch (std::runtime_error& err)
458 PINFO (
"logpath=%s", filepath.c_str() ? filepath.c_str() :
"(null)");
464error_handler<DbType::DBI_MYSQL> (dbi_conn conn,
void* user_data)
467 static_cast<decltype(dbi_be)
>(user_data);
470 auto err_num = dbi_conn_error (conn, &msg);
476 if (err_num == DBI_ERROR_BADIDX)
return;
487 PINFO (
"DBI error: %s\n", msg);
488 dbi_be->set_exists(
false);
496 if (!dbi_be->connected())
498 PINFO (
"DBI error: %s\n", msg);
499 PINFO (
"Note: GncDbiSqlConnection not yet initialized. Skipping further error processing.");
506 PINFO (
"DBI error: %s - Reconnecting...\n", msg);
508 dbi_be->retry_connection(msg);
510 else if (err_num == 2003)
513 dbi_be->retry_connection (msg);
515 else if (err_num == 1007)
517 dbi_be->set_exists(
true);
523 PERR (
"DBI error: %s\n", msg);
528#define SQL_OPTION_TO_REMOVE "NO_ZERO_DATE"
535adjust_sql_options_string(
const std::string& str)
540 static const boost::regex reg{
"(?:," SQL_OPTION_TO_REMOVE
"$|\\b"
541 SQL_OPTION_TO_REMOVE
"\\b,?)"};
542 return regex_replace(str, reg, std::string{
""});
547adjust_sql_options (dbi_conn connection)
549 dbi_result result = dbi_conn_query( connection,
"SELECT @@sql_mode");
550 if (result ==
nullptr)
553 int err = dbi_conn_error(connection, &errmsg);
554 PERR(
"Unable to read sql_mode %d : %s", err, errmsg);
557 dbi_result_first_row(result);
558 std::string str{dbi_result_get_string_idx(result, 1)};
559 dbi_result_free(result);
563 int err = dbi_conn_error(connection, &errmsg);
565 PERR(
"Unable to get sql_mode %d : %s", err, errmsg);
567 PINFO(
"Sql_mode isn't set.");
570 PINFO(
"Initial sql_mode: %s", str.c_str());
571 if(str.find(SQL_OPTION_TO_REMOVE) != std::string::npos)
572 str = adjust_sql_options_string(str);
575 const char* backslash_option{
"NO_BACKSLASH_ESCAPES"};
577 if (str.find(backslash_option) == std::string::npos)
581 str.append(backslash_option);
584 PINFO(
"Setting sql_mode to %s", str.c_str());
585 std::string set_str{
"SET sql_mode='" + std::move(str) +
"'"};
586 dbi_result set_result = dbi_conn_query(connection,
590 dbi_result_free(set_result);
595 int err = dbi_conn_error(connection, &errmsg);
596 PERR(
"Unable to set sql_mode %d : %s", err, errmsg);
600template <DbType Type>
bool
601drop_database(dbi_conn conn,
const UriStrings& uri)
604 if (Type == DbType::DBI_PGSQL)
606 root_db =
"template1";
608 else if (Type == DbType::DBI_MYSQL)
614 PERR (
"Unknown database type, can't proceed.");
618 if (dbi_conn_select_db (conn, root_db) == -1)
620 PERR (
"Failed to switch out of %s, drop will fail.",
621 uri.quote_dbname(Type).c_str());
625 auto result = dbi_conn_queryf (conn,
"DROP DATABASE %s",
626 uri.quote_dbname(Type).c_str());
629 PERR (
"Failed to drop database %s prior to recreating it."
630 "Proceeding would combine old and new data.",
631 uri.quote_dbname(Type).c_str());
635 dbi_result_free (result);
639template <DbType Type>
void
645 g_return_if_fail (session !=
nullptr);
646 g_return_if_fail (new_uri !=
nullptr);
655 if (Type == DbType::DBI_PGSQL)
657 if (uri.m_portnum == 0)
658 uri.m_portnum = PGSQL_DEFAULT_PORT;
663 auto lcname = g_utf8_strdown (uri.dbname(), -1);
664 uri.m_dbname = std::string{lcname};
670 auto conn = conn_setup(options, uri);
678 auto result = dbi_conn_connect (conn);
681 if (Type == DbType::DBI_MYSQL)
682 adjust_sql_options (conn);
685 dbi_conn_close(conn);
691 if (create && save_may_clobber_data<Type>(conn, uri.quote_dbname(Type)))
695 if (!drop_database<Type>(conn, uri))
701 PWARN (
"Database already exists, Might clobber it.");
702 dbi_conn_close(conn);
713 PERR (
"Unable to connect to database '%s'\n", uri.dbname());
715 dbi_conn_close(conn);
721 PERR (
"Database '%s' does not exist\n", uri.dbname());
723 std::string msg{
"Database "};
732 !create_database(conn, uri.quote_dbname(Type).c_str()))
734 dbi_conn_close(conn);
738 conn = conn_setup(options, uri);
739 result = dbi_conn_connect (conn);
742 PERR (
"Unable to create database '%s'\n", uri.dbname());
744 dbi_conn_close(conn);
748 if (Type == DbType::DBI_MYSQL)
749 adjust_sql_options (conn);
750 if (!conn_test_dbi_library(conn))
752 if (Type == DbType::DBI_PGSQL)
753 dbi_conn_select_db (conn,
"template1");
754 dbi_result_free (dbi_conn_queryf (conn,
"DROP DATABASE %s",
755 uri.quote_dbname(Type).c_str()));
756 dbi_conn_close(conn);
766 catch (std::runtime_error& err)
772 auto translog_path = gnc_build_translog_path (uri.basename().c_str());
774 PINFO (
"logpath=%s", translog_path ? translog_path :
"(null)");
775 g_free (translog_path);
781error_handler<DbType::DBI_PGSQL> (dbi_conn conn,
void* user_data)
784 static_cast<decltype(dbi_be)
>(user_data);
787 auto err_num = dbi_conn_error (conn, &msg);
793 if (err_num == DBI_ERROR_BADIDX)
return;
794 if (g_str_has_prefix (msg,
"FATAL: database") &&
795 g_str_has_suffix (msg,
"does not exist\n"))
797 PINFO (
"DBI error: %s\n", msg);
798 dbi_be->set_exists(
false);
800 else if (g_strrstr (msg,
801 "server closed the connection unexpectedly"))
803 if (!dbi_be->connected())
805 PWARN (
"DBI Error: Connection lost, connection pointer invalid");
808 PINFO (
"DBI error: %s - Reconnecting...\n", msg);
810 dbi_be->retry_connection(msg);
812 else if (g_str_has_prefix (msg,
"connection pointer is NULL") ||
813 g_str_has_prefix (msg,
"could not connect to server"))
816 if (!dbi_be->connected())
822 dbi_be->retry_connection (msg);
827 PERR (
"DBI error: %s\n", msg);
828 if (dbi_be->connected())
835template <DbType Type>
void
836GncDbiBackend<Type>::session_end ()
840 finalize_version_info ();
846template <DbType Type>
847GncDbiBackend<Type>::~GncDbiBackend()
864template <DbType Type>
void
867 g_return_if_fail (book !=
nullptr);
869 ENTER (
"dbi_be=%p, book=%p",
this, book);
871 if (loadType == LOAD_TYPE_INITIAL_LOAD)
876 assert (
m_book ==
nullptr);
882 if (Type == DbType::DBI_SQLITE)
907template <DbType T>
bool
908save_may_clobber_data (dbi_conn conn,
const std::string& dbname)
912 auto result = dbi_conn_get_table_list (conn, dbname.c_str(),
nullptr);
916 retval = dbi_result_get_numrows (result) > 0;
917 dbi_result_free (result);
923save_may_clobber_data <DbType::DBI_PGSQL>(dbi_conn conn,
924 const std::string& dbname)
928 const char* query =
"SELECT relname FROM pg_class WHERE relname !~ '^(pg|sql)_' AND relkind = 'r' ORDER BY relname";
929 auto result = dbi_conn_query (conn, query);
933 retval = dbi_result_get_numrows (result) > 0;
934 dbi_result_free (result);
948template <DbType Type>
void
953 g_return_if_fail (conn !=
nullptr);
954 g_return_if_fail (book !=
nullptr);
957 if (!conn->begin_transaction())
959 LEAVE(
"Failed to obtain a transaction.");
962 if (!conn->table_operation (TableOpType::backup))
964 conn->rollback_transaction();
965 LEAVE (
"Failed to rename tables");
968 if (!conn->drop_indexes())
970 conn->rollback_transaction();
971 LEAVE (
"Failed to drop indexes");
978 conn->rollback_transaction();
979 LEAVE (
"Failed to create new database tables");
982 conn->commit_transaction();
983 conn->table_operation (TableOpType::drop_backup);
996 g_return_if_fail (conn !=
nullptr);
997 g_return_if_fail (book !=
nullptr);
1000 if (!conn->table_operation (TableOpType::backup))
1003 conn->table_operation (TableOpType::rollback);
1004 LEAVE (
"Failed to rename tables");
1007 if (!conn->drop_indexes())
1009 conn->table_operation (TableOpType::rollback);
1012 LEAVE (
"Failed to drop indexes");
1019 conn->table_operation (TableOpType::rollback);
1020 LEAVE (
"Failed to create new database tables");
1023 conn->table_operation (TableOpType::drop_backup);
1037 G_GNUC_UNUSED
size_t chars_read;
1042 g_return_val_if_fail (uri !=
nullptr, FALSE);
1045 f = g_fopen (filename,
"r");
1051 PINFO (
"doesn't exist (errno=%d) -> DBI", errno);
1056 chars_read = fread (buf,
sizeof (buf) - 1, 1, f);
1057 status = fclose (f);
1060 PERR (
"Error in fclose(): %d\n", errno);
1062 if (g_str_has_prefix (buf,
"SQLite format 3"))
1064 PINFO (
"has SQLite format string -> DBI");
1067 PINFO (
"exists, does not have SQLite format string -> not DBI");
1076 const char* driver_dir;
1078 gboolean have_sqlite3_driver = FALSE;
1079 gboolean have_mysql_driver = FALSE;
1080 gboolean have_pgsql_driver = FALSE;
1084 driver_dir = g_getenv (
"GNC_DBD_DIR");
1085 if (driver_dir ==
nullptr)
1087 PINFO (
"GNC_DBD_DIR not set: using libdbi built-in default\n");
1094 num_drivers = dbi_initialize_r (driver_dir, &dbi_instance);
1096 num_drivers = dbi_initialize (driver_dir);
1098 if (num_drivers <= 0)
1103 dbi_shutdown_r (dbi_instance);
1104 dbi_instance =
nullptr;
1107 gchar *libdir = gnc_path_get_libdir ();
1108 gchar *dir = g_build_filename (libdir,
"dbd",
nullptr);
1111 num_drivers = dbi_initialize_r (dir, &dbi_instance);
1113 num_drivers = dbi_initialize (dir);
1117 if (num_drivers <= 0)
1119 PWARN (
"No DBD drivers found\n");
1123 dbi_driver driver =
nullptr;
1124 PINFO (
"%d DBD drivers found\n", num_drivers);
1129 driver = dbi_driver_list_r (driver, dbi_instance);
1131 driver = dbi_driver_list (driver);
1134 if (driver !=
nullptr)
1136 const gchar* name = dbi_driver_get_name (driver);
1138 PINFO (
"Driver: %s\n", name);
1139 if (strcmp (name,
"sqlite3") == 0)
1141 have_sqlite3_driver = TRUE;
1143 else if (strcmp (name,
"mysql") == 0)
1145 have_mysql_driver = TRUE;
1147 else if (strcmp (name,
"pgsql") == 0)
1149 have_pgsql_driver = TRUE;
1153 while (driver !=
nullptr);
1156 if (have_sqlite3_driver)
1158 const char* name =
"GnuCash Libdbi (SQLITE3) Backend";
1160 qof_backend_register_provider(std::move(prov));
1162 qof_backend_register_provider(std::move(prov));
1165 if (have_mysql_driver)
1167 const char *name =
"GnuCash Libdbi (MYSQL) Backend";
1169 qof_backend_register_provider(std::move(prov));
1172 if (have_pgsql_driver)
1174 const char* name =
"GnuCash Libdbi (POSTGRESQL) Backend";
1176 qof_backend_register_provider(std::move(prov));
1184#ifndef GNC_NO_LOADABLE_MODULES
1192qof_backend_module_finalize (
void)
1204 dbi_shutdown_r (dbi_instance);
1205 dbi_instance =
nullptr;
1215log_failed_field(dbi_result result,
const char* fieldname)
1217 auto idx = dbi_result_get_field_idx(result, fieldname);
1218 if (dbi_result_field_is_null_idx(result, idx))
1219 PERR(
"Result field %s is NULL", fieldname);
1222 auto type = dbi_result_get_field_type_idx(result, idx);
1223 auto attribs = dbi_result_get_field_attribs_idx(result, idx);
1224 PERR(
"Result field %s has type %d and attribs %d",
1225 fieldname, type, attribs);
1238static GncDbiTestResult
1239dbi_library_test (dbi_conn conn)
1241 int64_t testlonglong = -9223372036854775807LL, resultlonglong = 0;
1242 uint64_t testulonglong = 9223372036854775807LLU, resultulonglong = 0;
1243 double testdouble = 1.7976921348623157E+307, resultdouble = 0.0;
1245 GncDbiTestResult retval = GNC_DBI_PASS;
1247 result = dbi_conn_query (conn,
"CREATE TEMPORARY TABLE numtest "
1248 "( test_int BIGINT, test_unsigned BIGINT,"
1249 " test_double FLOAT8 )");
1250 if (result ==
nullptr)
1252 PWARN (
"Test_DBI_Library: Create table failed");
1253 return GNC_DBI_FAIL_SETUP;
1255 dbi_result_free (result);
1256 std::stringstream querystr;
1257 querystr <<
"INSERT INTO numtest VALUES (" << testlonglong <<
1258 ", " << testulonglong <<
", " << std::setprecision(12) <<
1260 auto query = querystr.str();
1261 result = dbi_conn_query (conn, query.c_str());
1262 if (result ==
nullptr)
1264 PWARN (
"Test_DBI_Library: Failed to insert test row into table");
1265 return GNC_DBI_FAIL_SETUP;
1267 dbi_result_free (result);
1268 auto locale = gnc_push_locale (LC_NUMERIC,
"C");
1269 result = dbi_conn_query (conn,
"SELECT * FROM numtest");
1270 if (result ==
nullptr || !dbi_result_get_numrows(result))
1273 dbi_conn_error (conn, &errmsg);
1274 PWARN (
"Test_DBI_Library: Failed to retrieve test row into table: %s",
1276 dbi_result_free (result);
1277 dbi_result_free (dbi_conn_query (conn,
"DROP TABLE numtest"));
1278 gnc_pop_locale (LC_NUMERIC, locale);
1279 return GNC_DBI_FAIL_SETUP;
1281 while (dbi_result_next_row (result))
1283 resultlonglong = dbi_result_get_longlong (result,
"test_int");
1284 if (!resultlonglong)
1285 log_failed_field(result,
"test_int");
1286 resultulonglong = dbi_result_get_ulonglong (result,
"test_unsigned");
1287 if (!resultulonglong)
1288 log_failed_field(result,
"test_unsigned");
1289 resultdouble = dbi_result_get_double (result,
"test_double");
1291 log_failed_field(result,
"test_double");
1293 dbi_result_free (result);
1294 dbi_result_free (dbi_conn_query (conn,
"DROP TABLE numtest"));
1295 gnc_pop_locale (LC_NUMERIC, locale);
1296 if (testlonglong != resultlonglong)
1298 PWARN (
"Test_DBI_Library: LongLong Failed %" PRId64
" != % " PRId64,
1299 testlonglong, resultlonglong);
1300 retval = GNC_DBI_FAIL_TEST;
1302 if (testulonglong != resultulonglong)
1304 PWARN (
"Test_DBI_Library: Unsigned longlong Failed %" PRIu64
" != %"
1305 PRIu64, testulonglong, resultulonglong);
1306 retval = GNC_DBI_FAIL_TEST;
1309 if (testdouble >= resultdouble + 0.000001e307 ||
1310 testdouble <= resultdouble - 0.000001e307)
1312 PWARN (
"Test_DBI_Library: Double Failed %17e != %17e",
1313 testdouble, resultdouble);
1314 retval = GNC_DBI_FAIL_TEST;
1319template <DbType Type>
bool
1320GncDbiBackend<Type>::conn_test_dbi_library(dbi_conn conn)
1322 auto result = dbi_library_test (conn);
1328 case GNC_DBI_FAIL_SETUP:
1330 set_message (
"DBI library large number test incomplete");
1333 case GNC_DBI_FAIL_TEST:
1335 set_message (
"DBI library fails large number test");
1338 return result == GNC_DBI_PASS;
Account handling public routines.
Anchor Scheduled Transaction info in a book.
API for the transaction logger.
void load(QofBook *, QofBackendLoadType) override
Load the minimal set of application data needed for the application to be operable at initial startup...
void safe_sync(QofBook *) override
Safely resave a database by renaming all of its tables, recreating everything, and then dropping the ...
void set_dbi_error(QofBackendError error, unsigned int repeat, bool retry) noexcept
FIXME: Just a pass-through to m_conn:
void session_begin(QofSession *, const char *, SessionOpenMode) override
Open the file or connect to the server.
Encapsulate a libdbi dbi_conn connection.
void load(QofBook *, QofBackendLoadType) override
Load the contents of an SQL database into a book.
QofBook * m_book
The primary, main open book.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
GncSqlConnection * m_conn
SQL connection.
void create_tables() noexcept
Create/update all tables in the database.
void connect(GncSqlConnection *conn) noexcept
Connect the backend to a GncSqlConnection.
void sync(QofBook *) override
Save the contents of a book to an SQL database.
void init_version_info() noexcept
Initializes DB table version information.
bool type_check(const char *type)
Distinguish two providers with same access method.
QofBackend * create_backend(void)
Return a new, fully initialized backend.
load and save data to SQL via libdbi
G_MODULE_EXPORT void qof_backend_module_init(void)
This is the standardized initialization function of a qof_backend GModule, but compiling this can be ...
void gnc_module_init_backend_dbi(void)
Initialization function which can be used when this module is statically linked into the application.
void gnc_module_finalize_backend_dbi(void)
Shutdown function which can be used when this module is statically linked into the application.
All type declarations for the whole Gnucash engine.
Utility functions for file access.
File path resolution utility functions.
Generic api to store and retrieve preferences.
Utility functions for convert uri in separate components and back.
void qof_backend_set_error(QofBackend *qof_be, QofBackendError err)
Set the error on the specified QofBackend.
SessionOpenMode
Mode for opening sessions.
@ ERR_BACKEND_NO_SUCH_DB
the named database doesn't exist
@ ERR_BACKEND_BAD_URL
Can't parse url.
@ ERR_BACKEND_STORE_EXISTS
File exists, data would be destroyed.
@ ERR_BACKEND_SERVER_ERR
error in response from server
@ ERR_SQL_DB_TOO_OLD
database is old and needs upgrading
@ ERR_SQL_DB_TOO_NEW
database is newer, we can't write to it
@ ERR_BACKEND_CANT_CONNECT
bad dbname/login/passwd or network failure
@ ERR_SQL_DBI_UNTESTABLE
could not complete test for LibDBI bug
@ ERR_BACKEND_MISC
undetermined error
@ ERR_FILEIO_FILE_NOT_FOUND
not found / no such file
@ ERR_BACKEND_CONN_LOST
Lost connection to server.
@ ERR_SQL_BAD_DBI
LibDBI has numeric errors.
@ SESSION_READ_ONLY
Create a new store at the URI even if a store already exists there.
@ SESSION_NEW_STORE
Open will fail if the URI doesn't exist or is locked.
@ SESSION_NEW_OVERWRITE
Create a new store at the URI.
#define PINFO(format, args...)
Print an informational note.
#define LEAVE(format, args...)
Print a function exit debugging message.
#define PERR(format, args...)
Log a serious error.
#define PWARN(format, args...)
Log a warning.
#define ENTER(format, args...)
Print a function entry debugging message.
void set_message(std::string &&)
Set a descriptive message that can be displayed to the user when there's an error.
bool check_error()
Report if there is an error.
void set_error(QofBackendError err)
Set the error value only if there isn't already an error already.
void xaccLogSetBaseName(const char *basepath)
The xaccLogSetBaseName() method sets the base filepath and the root part of the journal file name.
void gnc_features_set_used(QofBook *book, const gchar *feature)
Indicate that the current book uses the given feature.
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
void gnc_uri_get_components(const gchar *uri, gchar **scheme, gchar **hostname, gint32 *port, gchar **username, gchar **password, gchar **path)
Converts a uri in separate components.