Convert upgrade_table to member, remove some convenience functions.

To wit, gnc_sql_execute_select_sql, gnc_sql_execute_nonselect_sql,
gnc_sql_create_temp_table, and gnc_sql_create_select_statement.
This commit is contained in:
John Ralls 2016-07-24 15:16:15 -07:00
parent 54acef27c2
commit c2082bea99
22 changed files with 188 additions and 272 deletions

View File

@ -224,19 +224,17 @@ GncSqlAccountBackend::load_all (GncSqlBackend* be)
pBook = be->book(); pBook = be->book();
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
if (stmt == nullptr) sql << "SELECT * FROM " << TABLE_NAME;
{ auto stmt = be->create_statement_from_sql(sql.str());
LEAVE ("stmt == NULL");
return;
}
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
for (auto row : *result) for (auto row : *result)
load_single_account (be, row, &l_accounts_needing_parents); load_single_account (be, row, &l_accounts_needing_parents);
auto sql = g_strdup_printf ("SELECT DISTINCT guid FROM %s", TABLE_NAME); sql.str("");
gnc_sql_slots_load_for_sql_subquery (be, sql, (BookLookupFn)xaccAccountLookup); sql << "SELECT DISTINCT guid FROM " << TABLE_NAME;
g_free (sql); gnc_sql_slots_load_for_sql_subquery (be, sql.str().c_str(),
(BookLookupFn)xaccAccountLookup);
/* While there are items on the list of accounts needing parents, /* While there are items on the list of accounts needing parents,
try to see if the parent has now been loaded. Theory says that if try to see if the parent has now been loaded. Theory says that if

View File

@ -673,6 +673,50 @@ GncSqlBackend::set_table_version (const std::string& table_name,
return true; return true;
} }
void
GncSqlBackend::upgrade_table (const std::string& table_name,
const EntryVec& col_table) noexcept
{
DEBUG ("Upgrading %s table\n", table_name.c_str());
auto temp_table_name = table_name + "_new";
create_table (temp_table_name, col_table);
std::stringstream sql;
sql << "INSERT INTO " << temp_table_name << " SELECT * FROM " << table_name;
auto stmt = create_statement_from_sql(sql.str());
execute_nonselect_statement(stmt);
sql.str("");
sql << "DROP TABLE " << table_name;
stmt = create_statement_from_sql(sql.str());
execute_nonselect_statement(stmt);
sql.str("");
sql << "ALTER TABLE " << temp_table_name << " RENAME TO " << table_name;
stmt = create_statement_from_sql(sql.str());
execute_nonselect_statement(stmt);
}
/* This is required because we're passing be->timespace_format to
* g_strdup_printf.
*/
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
std::string
GncSqlBackend::time64_to_string (time64 t) const noexcept
{
auto tm = gnc_gmtime (&t);
auto year = tm->tm_year + 1900;
auto datebuf = g_strdup_printf (m_timespec_format,
year, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
gnc_tm_free (tm);
std::string date{datebuf};
g_free(datebuf);
return date;
}
#pragma GCC diagnostic warning "-Wformat-nonliteral"
void void
gnc_sql_sync_all (GncSqlBackend* be, QofBook* book) gnc_sql_sync_all (GncSqlBackend* be, QofBook* book)
@ -1657,30 +1701,6 @@ typedef void (*TimespecSetterFunc) (const gpointer, Timespec*);
#define TIMESPEC_STR_FORMAT "%04d%02d%02d%02d%02d%02d" #define TIMESPEC_STR_FORMAT "%04d%02d%02d%02d%02d%02d"
#define TIMESPEC_COL_SIZE (4+2+2+2+2+2) #define TIMESPEC_COL_SIZE (4+2+2+2+2+2)
/* This is required because we're passing be->timespace_format to
* g_strdup_printf.
*/
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
gchar*
gnc_sql_convert_timespec_to_string (const GncSqlBackend* be, Timespec ts)
{
time64 time;
struct tm* tm;
gint year;
gchar* datebuf;
time = timespecToTime64 (ts);
tm = gnc_gmtime (&time);
year = tm->tm_year + 1900;
datebuf = g_strdup_printf (be->timespec_format(),
year, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
gnc_tm_free (tm);
return datebuf;
}
#pragma GCC diagnostic warning "-Wformat-nonliteral"
template<> void template<> void
GncSqlColumnTableEntryImpl<CT_TIMESPEC>::load (const GncSqlBackend* be, GncSqlColumnTableEntryImpl<CT_TIMESPEC>::load (const GncSqlBackend* be,
GncSqlRow& row, GncSqlRow& row,
@ -1764,9 +1784,8 @@ GncSqlColumnTableEntryImpl<CT_TIMESPEC>::add_to_query(const GncSqlBackend* be,
if (ts.tv_sec != 0 || ts.tv_nsec != 0) if (ts.tv_sec != 0 || ts.tv_nsec != 0)
{ {
char* datebuf = gnc_sql_convert_timespec_to_string (be, ts); auto datebuf = be->time64_to_string (ts.tv_sec);
vec.emplace_back (std::make_pair (std::string{m_col_name}, vec.emplace_back (std::make_pair (std::string{m_col_name}, datebuf));
std::string{datebuf}));
return; return;
} }
} }
@ -2005,18 +2024,6 @@ gnc_sql_load_object (const GncSqlBackend* be, GncSqlRow& row,
} }
/* ================================================================= */ /* ================================================================= */
GncSqlStatementPtr
gnc_sql_create_select_statement (GncSqlBackend* be, const gchar* table_name)
{
g_return_val_if_fail (be != NULL, NULL);
g_return_val_if_fail (table_name != NULL, NULL);
auto sql = g_strdup_printf ("SELECT * FROM %s", table_name);
auto stmt = be->create_statement_from_sql(sql);
g_free (sql);
return stmt;
}
static GncSqlStatementPtr static GncSqlStatementPtr
create_single_col_select_statement (GncSqlBackend* be, create_single_col_select_statement (GncSqlBackend* be,
const gchar* table_name, const gchar* table_name,
@ -2031,35 +2038,6 @@ create_single_col_select_statement (GncSqlBackend* be,
/* ================================================================= */ /* ================================================================= */
GncSqlResultPtr
gnc_sql_execute_select_sql (GncSqlBackend* be, const gchar* sql)
{
g_return_val_if_fail (be != NULL, NULL);
g_return_val_if_fail (sql != NULL, NULL);
auto stmt = be->create_statement_from_sql(sql);
if (stmt == nullptr)
{
return nullptr;
}
return be->execute_select_statement (stmt);
}
gint
gnc_sql_execute_nonselect_sql (GncSqlBackend* be, const gchar* sql)
{
g_return_val_if_fail (be != NULL, 0);
g_return_val_if_fail (sql != NULL, 0);
auto stmt = be->create_statement_from_sql(sql);
if (stmt == NULL)
{
return -1;
}
return be->execute_nonselect_statement (stmt);
}
uint_t uint_t
gnc_sql_append_guids_to_sql (std::stringstream& sql, const InstanceVec& instances) gnc_sql_append_guids_to_sql (std::stringstream& sql, const InstanceVec& instances)
{ {
@ -2316,48 +2294,6 @@ GncSqlObjectBackend::create_tables (GncSqlBackend* be)
"Table creation aborted.", m_table_name.c_str(), m_version, version); "Table creation aborted.", m_table_name.c_str(), m_version, version);
} }
gboolean
gnc_sql_create_temp_table (const GncSqlBackend* be, const gchar* table_name,
const EntryVec& col_table)
{
g_return_val_if_fail (be != NULL, FALSE);
g_return_val_if_fail (table_name != NULL, FALSE);
return be->create_table (table_name, col_table);
}
/* Create a temporary table, copy the data from the old table, delete the
old table, then rename the new one. */
void
gnc_sql_upgrade_table (GncSqlBackend* be, const gchar* table_name,
const EntryVec& col_table)
{
gchar* sql;
gchar* temp_table_name;
g_return_if_fail (be != NULL);
g_return_if_fail (table_name != NULL);
DEBUG ("Upgrading %s table\n", table_name);
temp_table_name = g_strdup_printf ("%s_new", table_name);
(void)gnc_sql_create_temp_table (be, temp_table_name, col_table);
sql = g_strdup_printf ("INSERT INTO %s SELECT * FROM %s",
temp_table_name, table_name);
(void)gnc_sql_execute_nonselect_sql (be, sql);
g_free (sql);
sql = g_strdup_printf ("DROP TABLE %s", table_name);
(void)gnc_sql_execute_nonselect_sql (be, sql);
g_free (sql);
sql = g_strdup_printf ("ALTER TABLE %s RENAME TO %s", temp_table_name,
table_name);
(void)gnc_sql_execute_nonselect_sql (be, sql);
g_free (sql);
g_free (temp_table_name);
}
/* ================================================================= */ /* ================================================================= */

View File

@ -21,7 +21,7 @@
/** /**
* @defgroup SQLBE SQL Backend Core * @defgroup SQLBE SQL Backend Core
@{ @{
*/ */
/** @addtogroup Columns Columns /** @addtogroup Columns Columns
@ -112,13 +112,13 @@ public:
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr& stmt) const noexcept; GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr& stmt) const noexcept;
int execute_nonselect_statement(const GncSqlStatementPtr& stmt) const noexcept; int execute_nonselect_statement(const GncSqlStatementPtr& stmt) const noexcept;
std::string quote_string(const std::string&) const noexcept; std::string quote_string(const std::string&) const noexcept;
/** /**
* Creates a table in the database * Creates a table in the database
* *
* @param table_name Table name * @param table_name Table name
* @param col_table DB table description * @param col_table DB table description
* @return TRUE if successful, FALSE if unsuccessful * @return TRUE if successful, FALSE if unsuccessful
*/ */
bool create_table(const std::string& table_name, const EntryVec& col_table) const noexcept; bool create_table(const std::string& table_name, const EntryVec& col_table) const noexcept;
/** /**
* Creates a table in the database and sets its version * Creates a table in the database and sets its version
@ -150,8 +150,35 @@ public:
*/ */
bool add_columns_to_table(const std::string& table_name, bool add_columns_to_table(const std::string& table_name,
const EntryVec& col_table) const noexcept; const EntryVec& col_table) const noexcept;
/**
* Upgrades a table to a new structure.
*
* The upgrade is done by creating a new table with the new structure,
* SELECTing the old data into the new table, deleting the old table, then
* renaming the new table. Therefore, this will only work if the new table
* structure is similar enough to the old table that the SELECT will work.
*
* @param table_name SQL table name
* @param col_table Column table
*/
void upgrade_table (const std::string& table_name,
const EntryVec& col_table) noexcept;
/**
* Returns the version number for a DB table.
*
* @param table_name Table name
* @return Version number, or 0 if the table does not exist
*/
uint_t get_table_version(const std::string& table_name) const noexcept; uint_t get_table_version(const std::string& table_name) const noexcept;
bool set_table_version (const std::string& table_name, uint_t version) noexcept; bool set_table_version (const std::string& table_name, uint_t version) noexcept;
/**
* Converts a time64 value to a string value for the database.
*
* @param t time64 to be converted.
* @return String representation of the Timespec
*/
std::string time64_to_string (time64 t) const noexcept;
QofBook* book() const noexcept { return m_book; } QofBook* book() const noexcept { return m_book; }
bool pristine() const noexcept { return m_is_pristine_db; } bool pristine() const noexcept { return m_is_pristine_db; }
@ -390,8 +417,8 @@ class GncSqlObjectBackend
public: public:
GncSqlObjectBackend (int version, const std::string& type, GncSqlObjectBackend (int version, const std::string& type,
const std::string& table, const EntryVec& vec) : const std::string& table, const EntryVec& vec) :
m_table_name{table}, m_version{version}, m_type_name{type}, m_table_name{table}, m_version{version}, m_type_name{type},
m_col_table{vec} {} m_col_table{vec} {}
/** /**
* Load all objects of m_type in the database into memory. * Load all objects of m_type in the database into memory.
* @param be The GncSqlBackend containing the database connection. * @param be The GncSqlBackend containing the database connection.
@ -592,21 +619,21 @@ public:
QofIdTypeConst obj_name, QofIdTypeConst obj_name,
gpointer pObject, T get_ref) gpointer pObject, T get_ref)
const noexcept const noexcept
{
g_return_if_fail (pObject != NULL);
try
{ {
GncGUID guid; g_return_if_fail (pObject != NULL);
auto val = row.get_string_at_col (m_col_name);
(void)string_to_guid (val.c_str(), &guid); try
auto target = get_ref(&guid); {
if (target != nullptr) GncGUID guid;
set_parameter (pObject, target, get_setter(obj_name), auto val = row.get_string_at_col (m_col_name);
m_gobj_param_name); (void)string_to_guid (val.c_str(), &guid);
auto target = get_ref(&guid);
if (target != nullptr)
set_parameter (pObject, target, get_setter(obj_name),
m_gobj_param_name);
}
catch (std::invalid_argument) {}
} }
catch (std::invalid_argument) {}
}
protected: protected:
template <typename T> T template <typename T> T
@ -676,7 +703,7 @@ public:
gpointer pObject) const noexcept override; gpointer pObject) const noexcept override;
void add_to_table(const GncSqlBackend* be, ColVec& vec) const noexcept override; void add_to_table(const GncSqlBackend* be, ColVec& vec) const noexcept override;
void add_to_query(const GncSqlBackend* be, QofIdTypeConst obj_name, void add_to_query(const GncSqlBackend* be, QofIdTypeConst obj_name,
gpointer pObject, PairVec& vec) const noexcept override; gpointer pObject, PairVec& vec) const noexcept override;
}; };
template <GncSqlObjectType Type> template <GncSqlObjectType Type>
@ -845,25 +872,6 @@ gboolean gnc_sql_do_db_operation (GncSqlBackend* be,
gpointer pObject, gpointer pObject,
const EntryVec& table); const EntryVec& table);
/**
* Executes an SQL SELECT statement from an SQL char string and returns the
* result rows. If an error occurs, an entry is added to the log, an error
* status is returned to qof and NULL is returned.
*
* @param be SQL backend struct
* @param sql SQL SELECT string
* @return Results, or NULL if an error has occured
*/
GncSqlResultPtr gnc_sql_execute_select_sql (GncSqlBackend* be, const gchar* sql);
/**
* Executes an SQL non-SELECT statement from an SQL char string.
*
* @param be SQL backend struct
* @param sql SQL non-SELECT string
* @returns Number of rows affected, or -1 if an error has occured
*/
gint gnc_sql_execute_nonselect_sql (GncSqlBackend* be, const gchar* sql);
/** /**
* Loads a Gnucash object from the database. * Loads a Gnucash object from the database.
@ -893,20 +901,6 @@ gboolean gnc_sql_object_is_it_in_db (GncSqlBackend* be,
QofIdTypeConst obj_name, QofIdTypeConst obj_name,
const gpointer pObject, const gpointer pObject,
const EntryVec& table ); const EntryVec& table );
/**
* Creates a temporary table in the database. A temporary table does not
* have a version number added to the versions table.
*
* @param be SQL backend struct
* @param table_name Table name
* @param col_table DB table description
* @return TRUE if successful, FALSE if unsuccessful
*/
gboolean gnc_sql_create_temp_table (const GncSqlBackend* be,
const gchar* table_name,
const EntryVec& col_table);
/** /**
* Loads the object guid from a database row. The table must have a column * Loads the object guid from a database row. The table must have a column
* named "guid" with type CT_GUID. * named "guid" with type CT_GUID.
@ -919,16 +913,6 @@ gboolean gnc_sql_create_temp_table (const GncSqlBackend* be,
const GncGUID* gnc_sql_load_guid (const GncSqlBackend* be, GncSqlRow& row); const GncGUID* gnc_sql_load_guid (const GncSqlBackend* be, GncSqlRow& row);
/**
* Creates a basic SELECT statement for a table.
*
* @param be SQL backend struct
* @param table_name Table name
* @return Statement
*/
GncSqlStatementPtr gnc_sql_create_select_statement (GncSqlBackend* be,
const gchar* table_name);
/** /**
* Appends the ascii strings for a list of GUIDs to the end of an SQL string. * Appends the ascii strings for a list of GUIDs to the end of an SQL string.
* *
@ -940,30 +924,6 @@ GncSqlStatementPtr gnc_sql_create_select_statement (GncSqlBackend* be,
uint_t gnc_sql_append_guids_to_sql (std::stringstream& sql, uint_t gnc_sql_append_guids_to_sql (std::stringstream& sql,
const InstanceVec& instances); const InstanceVec& instances);
/**
* Converts a Timespec value to a string value for the database.
*
* @param be SQL backend
* @param ts Timespec to be converted
* @return String representation of the Timespec
*/
gchar* gnc_sql_convert_timespec_to_string (const GncSqlBackend* be,
Timespec ts);
/**
* Upgrades a table to a new structure. The upgrade is done by creating a new
* table with the new structure, SELECTing the old data into the new table,
* deleting the old table, then renaming the new table. Therefore, this will
* only work if the new table structure is similar enough to the old table that
* the SELECT will work.
*
* @param be SQL backend
* @param table_name SQL table name
* @param col_table Column table
*/
void gnc_sql_upgrade_table (GncSqlBackend* be, const gchar* table_name,
const EntryVec& col_table);
void _retrieve_guid_ (gpointer pObject, gpointer pValue); void _retrieve_guid_ (gpointer pObject, gpointer pValue);
gpointer gnc_sql_compile_query (QofBackend* pBEnd, QofQuery* pQuery); gpointer gnc_sql_compile_query (QofBackend* pBEnd, QofQuery* pQuery);
@ -1058,5 +1018,5 @@ GncSqlColumnTableEntry::add_value_to_vec(const GncSqlBackend* be,
#endif /* GNC_BACKEND_SQL_H */ #endif /* GNC_BACKEND_SQL_H */
/** /**
@} end of the SQL Backend Core doxygen group @} end of the SQL Backend Core doxygen group
*/ */

View File

@ -238,7 +238,9 @@ GncSqlBillTermBackend::load_all (GncSqlBackend* be)
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;
GList* l_billterms_needing_parents = NULL; GList* l_billterms_needing_parents = NULL;
@ -315,7 +317,7 @@ GncSqlBillTermBackend::create_tables (GncSqlBackend* be)
else if (version == 1) else if (version == 1)
{ {
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Billterms table upgraded from version 1 to version %d\n", PINFO ("Billterms table upgraded from version 1 to version %d\n",

View File

@ -171,7 +171,9 @@ GncSqlBookBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, BOOK_TABLE); std::stringstream sql;
sql << "SELECT * FROM " << BOOK_TABLE;
auto stmt = be->create_statement_from_sql(sql.str());
if (stmt != nullptr) if (stmt != nullptr)
{ {
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);

View File

@ -236,19 +236,19 @@ static gboolean
delete_budget_amounts (GncSqlBackend* be, GncBudget* budget) delete_budget_amounts (GncSqlBackend* be, GncBudget* budget)
{ {
gchar guid_buf[GUID_ENCODING_LENGTH + 1]; gchar guid_buf[GUID_ENCODING_LENGTH + 1];
gchar* sql;
g_return_val_if_fail (be != NULL, FALSE); g_return_val_if_fail (be != NULL, FALSE);
g_return_val_if_fail (budget != NULL, FALSE); g_return_val_if_fail (budget != NULL, FALSE);
(void)guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (budget)), (void)guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (budget)),
guid_buf); guid_buf);
sql = g_strdup_printf ("DELETE FROM %s WHERE budget_guid='%s'", AMOUNTS_TABLE, std::stringstream sql;
guid_buf); sql << "DELETE FROM " << AMOUNTS_TABLE << " WHERE budget_guid='"<<
(void)gnc_sql_execute_nonselect_sql (be, sql); guid_buf << "'";
g_free (sql); auto stmt = be->create_statement_from_sql(sql.str());
be->execute_nonselect_statement(stmt);
return TRUE; return true;
} }
/** /**
@ -335,10 +335,9 @@ GncSqlBudgetBackend::load_all (GncSqlBackend* be)
InstanceVec instances; InstanceVec instances;
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, BUDGET_TABLE); std::stringstream sql;
if (stmt == nullptr) sql << "SELECT * FROM " << BUDGET_TABLE;
return; auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
for (auto row : *result) for (auto row : *result)
{ {

View File

@ -146,8 +146,9 @@ GncSqlCommodityBackend::load_all (GncSqlBackend* be)
gnc_commodity_table* pTable; gnc_commodity_table* pTable;
pTable = gnc_commodity_table_get_table (be->book()); pTable = gnc_commodity_table_get_table (be->book());
auto stmt = gnc_sql_create_select_statement (be, COMMODITIES_TABLE); std::stringstream sql;
if (stmt == nullptr) return; sql << "SELECT * FROM " << COMMODITIES_TABLE;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
for (auto row : *result) for (auto row : *result)

View File

@ -126,7 +126,9 @@ GncSqlCustomerBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;
@ -157,7 +159,7 @@ GncSqlCustomerBackend::create_tables (GncSqlBackend* be)
else if (version == 1) else if (version == 1)
{ {
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Customers table upgraded from version 1 to version %d\n", PINFO ("Customers table upgraded from version 1 to version %d\n",

View File

@ -112,7 +112,9 @@ GncSqlEmployeeBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;
@ -144,7 +146,7 @@ GncSqlEmployeeBackend::create_tables (GncSqlBackend* be)
else if (version == 1) else if (version == 1)
{ {
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Employees table upgraded from version 1 to version %d\n", PINFO ("Employees table upgraded from version 1 to version %d\n",

View File

@ -194,7 +194,9 @@ GncSqlEntryBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;
@ -228,7 +230,7 @@ GncSqlEntryBackend::create_tables (GncSqlBackend* be)
1->2: 64 bit int handling 1->2: 64 bit int handling
2->3: "entered" -> "date_entered", and it can be NULL 2->3: "entered" -> "date_entered", and it can be NULL
*/ */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Entries table upgraded from version %d to version %d\n", version, PINFO ("Entries table upgraded from version %d to version %d\n", version,

View File

@ -133,7 +133,9 @@ GncSqlInvoiceBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;
@ -167,7 +169,7 @@ GncSqlInvoiceBackend::create_tables (GncSqlBackend* be)
1->2: 64 bit int handling 1->2: 64 bit int handling
2->3: invoice open date can be NULL 2->3: invoice open date can be NULL
*/ */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Invoices table upgraded from version %d to version %d\n", version, PINFO ("Invoices table upgraded from version %d to version %d\n", version,

View File

@ -105,7 +105,9 @@ GncSqlJobBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;

View File

@ -127,7 +127,9 @@ GncSqlLotsBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
if (stmt != nullptr) if (stmt != nullptr)
{ {
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
@ -165,7 +167,7 @@ GncSqlLotsBackend::create_tables (GncSqlBackend* be)
Create a temporary table, copy the data from the old table, delete the Create a temporary table, copy the data from the old table, delete the
old table, then rename the new one. */ old table, then rename the new one. */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Lots table upgraded from version 1 to version %d\n", TABLE_VERSION); PINFO ("Lots table upgraded from version 1 to version %d\n", TABLE_VERSION);

View File

@ -105,7 +105,9 @@ GncSqlOrderBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;

View File

@ -108,7 +108,9 @@ GncSqlPriceBackend::load_all (GncSqlBackend* be)
pBook = be->book(); pBook = be->book();
pPriceDB = gnc_pricedb_get_db (pBook); pPriceDB = gnc_pricedb_get_db (pBook);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
if (stmt != nullptr) if (stmt != nullptr)
{ {
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
@ -153,7 +155,7 @@ GncSqlPriceBackend::create_tables (GncSqlBackend* be)
else if (version == 1) else if (version == 1)
{ {
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
be->set_table_version (TABLE_NAME, TABLE_VERSION); be->set_table_version (TABLE_NAME, TABLE_VERSION);
PINFO ("Prices table upgraded from version 1 to version %d\n", TABLE_VERSION); PINFO ("Prices table upgraded from version 1 to version %d\n", TABLE_VERSION);

View File

@ -388,17 +388,17 @@ upgrade_recurrence_table_1_2 (GncSqlBackend* be)
/* Step 2: insert a default value in the newly created column */ /* Step 2: insert a default value in the newly created column */
{ {
gchar* weekend_adj_str = recurrenceWeekendAdjustToString (WEEKEND_ADJ_NONE); gchar* weekend_adj_str = recurrenceWeekendAdjustToString (WEEKEND_ADJ_NONE);
gchar* update_query = g_strdup_printf ("UPDATE %s SET %s = '%s';", std::stringstream sql;
TABLE_NAME, sql << "UPDATE " << TABLE_NAME << " SET " <<
weekend_adjust_col_table[0]->name(), weekend_adjust_col_table[0]->name() << "='" <<
weekend_adj_str); weekend_adj_str << "'";
(void)gnc_sql_execute_nonselect_sql (be, update_query); auto stmt = be->create_statement_from_sql(sql.str());
be->execute_nonselect_statement(stmt);
g_free (weekend_adj_str); g_free (weekend_adj_str);
g_free (update_query);
} }
/* Step 3: rewrite the table, requiring the weekend_adj column to be non-null */ /* Step 3: rewrite the table, requiring the weekend_adj column to be non-null */
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
} }

View File

@ -123,7 +123,9 @@ GncSqlSchedXactionBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, SCHEDXACTION_TABLE); std::stringstream sql;
sql << "SELECT * FROM " << SCHEDXACTION_TABLE;
auto stmt = be->create_statement_from_sql(sql.str());
if (stmt == NULL) return; if (stmt == NULL) return;
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
SchedXactions* sxes; SchedXactions* sxes;

View File

@ -1017,7 +1017,7 @@ GncSqlSlotsBackend::create_tables (GncSqlBackend* be)
*/ */
if (version == 1) if (version == 1)
{ {
gnc_sql_upgrade_table (be, TABLE_NAME, col_table); be->upgrade_table(TABLE_NAME, col_table);
ok = be->create_index ("slots_guid_index", TABLE_NAME, ok = be->create_index ("slots_guid_index", TABLE_NAME,
obj_guid_col_table); obj_guid_col_table);
if (!ok) if (!ok)

View File

@ -294,7 +294,9 @@ GncSqlTaxTableBackend::load_all (GncSqlBackend* be)
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
/* First time, create the query */ /* First time, create the query */
auto stmt = gnc_sql_create_select_statement (be, TT_TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TT_TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
GList* tt_needing_parents = NULL; GList* tt_needing_parents = NULL;
@ -341,7 +343,7 @@ GncSqlTaxTableBackend::create_tables (GncSqlBackend* be)
else if (version == 1) else if (version == 1)
{ {
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table (be, TT_TABLE_NAME, tt_col_table); be->upgrade_table(TT_TABLE_NAME, tt_col_table);
be->set_table_version (TT_TABLE_NAME, TT_TABLE_VERSION); be->set_table_version (TT_TABLE_NAME, TT_TABLE_VERSION);
PINFO ("Taxtables table upgraded from version 1 to version %d\n", PINFO ("Taxtables table upgraded from version 1 to version %d\n",
TT_TABLE_VERSION); TT_TABLE_VERSION);
@ -356,7 +358,7 @@ GncSqlTaxTableBackend::create_tables (GncSqlBackend* be)
else if (version == 1) else if (version == 1)
{ {
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table (be, TTENTRIES_TABLE_NAME, ttentries_col_table); be->upgrade_table(TTENTRIES_TABLE_NAME, ttentries_col_table);
be->set_table_version (TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION); be->set_table_version (TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION);
PINFO ("Taxtable entries table upgraded from version 1 to version %d\n", PINFO ("Taxtable entries table upgraded from version 1 to version %d\n",
TTENTRIES_TABLE_VERSION); TTENTRIES_TABLE_VERSION);

View File

@ -492,7 +492,7 @@ GncSqlTransBackend::create_tables (GncSqlBackend* be)
1->2: 64 bit int handling 1->2: 64 bit int handling
2->3: allow dates to be NULL 2->3: allow dates to be NULL
*/ */
gnc_sql_upgrade_table (be, m_table_name.c_str(), tx_col_table); be->upgrade_table(m_table_name.c_str(), tx_col_table);
be->set_table_version (m_table_name.c_str(), m_version); be->set_table_version (m_table_name.c_str(), m_version);
PINFO ("Transactions table upgraded from version %d to version %d\n", PINFO ("Transactions table upgraded from version %d to version %d\n",
version, m_version); version, m_version);
@ -522,7 +522,7 @@ GncSqlSplitBackend::create_tables (GncSqlBackend* be)
/* Upgrade: /* Upgrade:
1->2: 64 bit int handling 1->2: 64 bit int handling
3->4: Split reconcile date can be NULL */ 3->4: Split reconcile date can be NULL */
gnc_sql_upgrade_table (be, m_table_name.c_str(), split_col_table); be->upgrade_table(m_table_name.c_str(), split_col_table);
if (!be->create_index("splits_tx_guid_index", if (!be->create_index("splits_tx_guid_index",
m_table_name.c_str(), m_table_name.c_str(),
tx_guid_col_table)) tx_guid_col_table))
@ -969,9 +969,8 @@ convert_query_term_to_sql (const GncSqlBackend* be, const gchar* fieldName,
else if (g_strcmp0 (pPredData->type_name, QOF_TYPE_DATE) == 0) else if (g_strcmp0 (pPredData->type_name, QOF_TYPE_DATE) == 0)
{ {
query_date_t date_data = (query_date_t)pPredData; query_date_t date_data = (query_date_t)pPredData;
gchar* datebuf;
datebuf = gnc_sql_convert_timespec_to_string (be, date_data->date); auto datebuf = be->time64_to_string (date_data->date.tv_sec);
g_string_append_printf (sql, "'%s'", datebuf); g_string_append_printf (sql, "'%s'", datebuf);
} }

View File

@ -115,7 +115,9 @@ GncSqlVendorBackend::load_all (GncSqlBackend* be)
{ {
g_return_if_fail (be != NULL); g_return_if_fail (be != NULL);
auto stmt = gnc_sql_create_select_statement (be, TABLE_NAME); std::stringstream sql;
sql << "SELECT * FROM " << TABLE_NAME;
auto stmt = be->create_statement_from_sql(sql.str());
auto result = be->execute_select_statement(stmt); auto result = be->execute_select_statement(stmt);
InstanceVec instances; InstanceVec instances;

View File

@ -591,31 +591,29 @@ gnc_sql_add_objectref_guid_col_info_to_list (const GncSqlBackend* be,// 1
test_gnc_sql_add_objectref_guid_col_info_to_list (Fixture *fixture, gconstpointer pData) test_gnc_sql_add_objectref_guid_col_info_to_list (Fixture *fixture, gconstpointer pData)
{ {
}*/ }*/
/* gnc_sql_convert_timespec_to_string /* GncDbiBackend::time64_to_string
gchar* std::string
gnc_sql_convert_timespec_to_string (const GncSqlBackend* be, Timespec ts)// C: 1 */ GncDbiBackend::time64_to_string (time64 t)// C: 1 */
#define numtests 6 #define numtests 6
static void static void
test_gnc_sql_convert_timespec_to_string () test_time64_to_string ()
{ {
GncSqlBackend be {nullptr, nullptr, "%4d-%02d-%02d %02d:%02d:%02d"}; GncSqlBackend be {nullptr, nullptr, "%4d-%02d-%02d %02d:%02d:%02d"};
const char* date[numtests] = {"1995-03-11 19:17:26", const char* dates[numtests] = {"1995-03-11 19:17:26",
"2001-04-20 11:44:07", "2001-04-20 11:44:07",
"1964-02-29 09:15:23", "1964-02-29 09:15:23",
"1959-04-02 00:00:00", "1959-04-02 00:00:00",
"2043-11-22 05:32:45", "2043-11-22 05:32:45",
"2153-12-18 01:15:30" "2153-12-18 01:15:30"
}; };
int i;
for (i = 0; i < numtests; i++) for (auto date : dates)
{ {
Timespec ts = gnc_iso8601_to_timespec_gmt (date[i]); Timespec ts = gnc_iso8601_to_timespec_gmt (date);
gchar* datestr = gnc_sql_convert_timespec_to_string (&be, ts); auto datestr = be.time64_to_string (ts.tv_sec);
g_assert_cmpstr (date[i], == , datestr); g_assert_cmpstr (date, == , datestr.c_str());
g_free (datestr);
} }
} }
@ -937,8 +935,8 @@ test_suite_gnc_backend_sql (void)
// GNC_TEST_ADD (suitename, "add value guid to vec", Fixture, nullptr, test_add_value_guid_to_vec, teardown); // GNC_TEST_ADD (suitename, "add value guid to vec", Fixture, nullptr, test_add_value_guid_to_vec, teardown);
// GNC_TEST_ADD (suitename, "gnc sql add gvalue objectref guid to slist", Fixture, nullptr, test_gnc_sql_add_objectref_guid_to_vec, teardown); // GNC_TEST_ADD (suitename, "gnc sql add gvalue objectref guid to slist", Fixture, nullptr, test_gnc_sql_add_objectref_guid_to_vec, teardown);
// GNC_TEST_ADD (suitename, "gnc sql add objectref guid col info to list", Fixture, nullptr, test_gnc_sql_add_objectref_guid_col_info_to_list, teardown); // GNC_TEST_ADD (suitename, "gnc sql add objectref guid col info to list", Fixture, nullptr, test_gnc_sql_add_objectref_guid_col_info_to_list, teardown);
GNC_TEST_ADD_FUNC (suitename, "gnc sql convert timespec to string", GNC_TEST_ADD_FUNC (suitename, "GncDbiBackend time64 to string",
test_gnc_sql_convert_timespec_to_string); test_time64_to_string);
// GNC_TEST_ADD (suitename, "load timespec", Fixture, nullptr, test_load_timespec, teardown); // GNC_TEST_ADD (suitename, "load timespec", Fixture, nullptr, test_load_timespec, teardown);
// GNC_TEST_ADD (suitename, "add timespec col info to list", Fixture, nullptr, test_add_timespec_col_info_to_list, teardown); // GNC_TEST_ADD (suitename, "add timespec col info to list", Fixture, nullptr, test_add_timespec_col_info_to_list, teardown);
// GNC_TEST_ADD (suitename, "add value timespec to vec", Fixture, nullptr, test_add_value_timespec_to_vec, teardown); // GNC_TEST_ADD (suitename, "add value timespec to vec", Fixture, nullptr, test_add_value_timespec_to_vec, teardown);
@ -970,7 +968,6 @@ test_suite_gnc_backend_sql (void)
// GNC_TEST_ADD (suitename, "build update statement", Fixture, nullptr, test_build_update_statement, teardown); // GNC_TEST_ADD (suitename, "build update statement", Fixture, nullptr, test_build_update_statement, teardown);
// GNC_TEST_ADD (suitename, "build delete statement", Fixture, nullptr, test_build_delete_statement, teardown); // GNC_TEST_ADD (suitename, "build delete statement", Fixture, nullptr, test_build_delete_statement, teardown);
// GNC_TEST_ADD (suitename, "do create table", Fixture, nullptr, test_do_create_table, teardown); // GNC_TEST_ADD (suitename, "do create table", Fixture, nullptr, test_do_create_table, teardown);
// GNC_TEST_ADD (suitename, "gnc sql create temp table", Fixture, nullptr, test_gnc_sql_create_temp_table, teardown);
// GNC_TEST_ADD (suitename, "gnc sql create index", Fixture, nullptr, test_gnc_sql_create_index, teardown); // GNC_TEST_ADD (suitename, "gnc sql create index", Fixture, nullptr, test_gnc_sql_create_index, teardown);
// GNC_TEST_ADD (suitename, "gnc sql upgrade table", Fixture, nullptr, test_gnc_sql_upgrade_table, teardown); // GNC_TEST_ADD (suitename, "gnc sql upgrade table", Fixture, nullptr, test_gnc_sql_upgrade_table, teardown);
// GNC_TEST_ADD (suitename, "gnc sql add columns to table", Fixture, nullptr, test_gnc_sql_add_columns_to_table, teardown); // GNC_TEST_ADD (suitename, "gnc sql add columns to table", Fixture, nullptr, test_gnc_sql_add_columns_to_table, teardown);