Move 3 utility functions from gnc-backend-sql to gnc-sql-column-table-entry.

gnc_sql_load_object, gnc_sql_load_guid, and gnc_sql_append_guids_to_sql.
This commit is contained in:
John Ralls 2016-11-13 14:52:03 -08:00
parent b838c1ebf3
commit 00950e9724
6 changed files with 111 additions and 116 deletions

View File

@ -474,79 +474,4 @@ gnc_sql_run_query (QofBackend* qof_be, gpointer pQuery)
}
#endif //if 0: query creation isn't used yet, code never tested.
/* ================================================================= */
void
_retrieve_guid_ (gpointer pObject, gpointer pValue)
{
GncGUID* pGuid = (GncGUID*)pObject;
GncGUID* guid = (GncGUID*)pValue;
g_return_if_fail (pObject != NULL);
g_return_if_fail (pValue != NULL);
memcpy (pGuid, guid, sizeof (GncGUID));
}
// Table to retrieve just the guid
static EntryVec guid_table
{
gnc_sql_make_table_entry<CT_GUID>("guid", 0, 0, nullptr, _retrieve_guid_)
};
const GncGUID*
gnc_sql_load_guid (const GncSqlBackend* sql_be, GncSqlRow& row)
{
static GncGUID guid;
g_return_val_if_fail (sql_be != NULL, NULL);
gnc_sql_load_object (sql_be, row, NULL, &guid, guid_table);
return &guid;
}
// Table to retrieve just the guid
static EntryVec tx_guid_table
{
gnc_sql_make_table_entry<CT_GUID>("tx_guid", 0, 0, nullptr, _retrieve_guid_)
};
void
gnc_sql_load_object (const GncSqlBackend* sql_be, GncSqlRow& row,
QofIdTypeConst obj_name, gpointer pObject,
const EntryVec& table)
{
QofSetterFunc setter;
g_return_if_fail (sql_be != NULL);
g_return_if_fail (pObject != NULL);
for (auto const& table_row : table)
{
table_row->load (sql_be, row, obj_name, pObject);
}
}
/* ================================================================= */
uint_t
gnc_sql_append_guids_to_sql (std::stringstream& sql, const InstanceVec& instances)
{
char guid_buf[GUID_ENCODING_LENGTH + 1];
for (auto inst : instances)
{
(void)guid_to_string_buff (qof_instance_get_guid (inst), guid_buf);
if (inst != *(instances.begin()))
{
sql << ",";
}
sql << "'" << guid_buf << "'";
}
return instances.size();
}
/* ========================== END OF FILE ===================== */

View File

@ -52,52 +52,12 @@ extern "C"
using StrVec = std::vector<std::string>;
using PairVec = std::vector<std::pair<std::string, std::string>>;
using uint_t = unsigned int;
class GncSqlRow;
#define GNC_SQL_BACKEND "gnc:sql:1"
#define GNC_SQL_BACKEND_VERSION 1
/**
* Loads a Gnucash object from the database.
*
* @param sql_be SQL backend struct
* @param row DB result row
* @param obj_name QOF object type name
* @param pObject Object to be loaded
* @param table DB table description
*/
void gnc_sql_load_object (const GncSqlBackend* sql_be, GncSqlRow& row,
QofIdTypeConst obj_name, gpointer pObject,
const EntryVec& table);
/**
* Loads the object guid from a database row. The table must have a column
* named "guid" with type CT_GUID.
*
* @param sql_be SQL backend struct
* @param row Database row
* @return GncGUID
*/
const GncGUID* gnc_sql_load_guid (const GncSqlBackend* sql_be, GncSqlRow& row);
/**
* Appends the ascii strings for a list of GUIDs to the end of an SQL string.
*
* @param str SQL string
* @param list List of GUIDs
* @param maxCount Max # of GUIDs to append
* @return Number of GUIDs appended
*/
uint_t gnc_sql_append_guids_to_sql (std::stringstream& sql,
const InstanceVec& instances);
void _retrieve_guid_ (gpointer pObject, gpointer pValue);
gpointer gnc_sql_compile_query (QofBackend* qof_be, QofQuery* pQuery);
void gnc_sql_free_query (QofBackend* qof_be, gpointer pQuery);
void gnc_sql_run_query (QofBackend* qof_be, gpointer pQuery);

View File

@ -151,6 +151,18 @@ static const EntryVec col_table
(QofSetterFunc)set_gdate_val),
};
static void
_retrieve_guid_ (gpointer pObject, gpointer pValue)
{
GncGUID* pGuid = (GncGUID*)pObject;
GncGUID* guid = (GncGUID*)pValue;
g_return_if_fail (pObject != NULL);
g_return_if_fail (pValue != NULL);
memcpy (pGuid, guid, sizeof (GncGUID));
}
/* Special column table because we need to be able to access the table by
a column other than the primary key */
static const EntryVec obj_guid_col_table

View File

@ -617,6 +617,71 @@ GncSqlColumnTableEntryImpl<CT_NUMERIC>::add_to_query(QofIdTypeConst obj_name,
vec.emplace_back (denom_col, buf.str ());
}
static void
_retrieve_guid_ (gpointer pObject, gpointer pValue)
{
GncGUID* pGuid = (GncGUID*)pObject;
GncGUID* guid = (GncGUID*)pValue;
g_return_if_fail (pObject != NULL);
g_return_if_fail (pValue != NULL);
memcpy (pGuid, guid, sizeof (GncGUID));
}
// Table to retrieve just the guid
static EntryVec guid_table
{
gnc_sql_make_table_entry<CT_GUID>("guid", 0, 0, nullptr, _retrieve_guid_)
};
const GncGUID*
gnc_sql_load_guid (const GncSqlBackend* sql_be, GncSqlRow& row)
{
static GncGUID guid;
g_return_val_if_fail (sql_be != NULL, NULL);
gnc_sql_load_object (sql_be, row, NULL, &guid, guid_table);
return &guid;
}
void
gnc_sql_load_object (const GncSqlBackend* sql_be, GncSqlRow& row,
QofIdTypeConst obj_name, gpointer pObject,
const EntryVec& table)
{
QofSetterFunc setter;
g_return_if_fail (sql_be != NULL);
g_return_if_fail (pObject != NULL);
for (auto const& table_row : table)
{
table_row->load (sql_be, row, obj_name, pObject);
}
}
uint_t
gnc_sql_append_guids_to_sql (std::stringstream& sql,
const InstanceVec& instances)
{
char guid_buf[GUID_ENCODING_LENGTH + 1];
for (auto inst : instances)
{
(void)guid_to_string_buff (qof_instance_get_guid (inst), guid_buf);
if (inst != *(instances.begin()))
{
sql << ",";
}
sql << "'" << guid_buf << "'";
}
return instances.size();
}
/* This is necessary for 64-bit builds because g++ complains
* that reinterpret_casting a void* (64 bits) to an int (32 bits)

View File

@ -36,6 +36,8 @@ extern "C"
struct GncSqlColumnInfo;
using ColVec = std::vector<GncSqlColumnInfo>;
using PairVec = std::vector<std::pair<std::string, std::string>>;
using InstanceVec = std::vector<QofInstance*>;
using uint_t = unsigned int;
class GncSqlBackend;
/**
@ -176,6 +178,7 @@ public:
catch (std::invalid_argument) {}
}
protected:
template <typename T> T
get_row_value_from_object(QofIdTypeConst obj_name, const void* pObject) const;
@ -370,6 +373,37 @@ GncSqlColumnTableEntry::add_value_to_vec(QofIdTypeConst obj_name,
return;
}
/**
* Load an arbitrary object from a result row.
*
* @param sql_be: GncSqlBackend*, pass-through to the implementation loader.
* @param row: The GncSqlResult
* @param obj_name: The object-name with which to retrieve the setter func.
* @param pObject: The target object being loaded.
* @param table: The table description to interpret the row.
*/
void gnc_sql_load_object (const GncSqlBackend* sql_be, GncSqlRow& row,
QofIdTypeConst obj_name, gpointer pObject,
const EntryVec& table);
/**
* Create a GncGUID from a guid stored in a row.
*
* @param sql_be: The active GncSqlBackend. Pass-throug to gnc_sql_load_object.
* @param row: The GncSqlResult row.
*/
const GncGUID*
gnc_sql_load_guid (const GncSqlBackend* sql_be, GncSqlRow& row);
/**
* Append the GUIDs of QofInstances to a SQL query.
*
* @param sql: The SQL Query in progress to which the GncGUIDS should be appended.
* @param instances: The QofInstances
* @return The number of instances
*/
uint_t gnc_sql_append_guids_to_sql (std::stringstream& sql,
const InstanceVec& instances);
/**
* information required to create a column in a table.
*/

View File

@ -36,7 +36,6 @@ class GncSqlBackend;
class GncSqlColumnTableEntry;
using GncSqlColumnTableEntryPtr = std::shared_ptr<GncSqlColumnTableEntry>;
using EntryVec = std::vector<GncSqlColumnTableEntryPtr>;
using InstanceVec = std::vector<QofInstance*>;
/**
* Encapsulates per-class table schema with functions to load, create a table,