29 #include <gnc-datetime.hpp> 30 #include "gnc-sql-backend.hpp" 31 #include "gnc-sql-object-backend.hpp" 32 #include "gnc-sql-column-table-entry.hpp" 33 #include "gnc-sql-result.hpp" 39 get_autoinc_id (
void*
object,
const QofParam* param)
46 set_autoinc_id (
void*
object,
void* item)
57 g_return_val_if_fail (obj_name != NULL, NULL);
59 if (m_flags & COL_AUTOINC)
61 getter = get_autoinc_id;
63 else if (m_qof_param_name != NULL)
79 if (m_flags & COL_AUTOINC)
81 setter = set_autoinc_id;
83 else if (m_qof_param_name !=
nullptr)
85 g_assert (obj_name != NULL);
98 PairVec& vec)
const noexcept
100 auto inst = get_row_value_from_object<QofInstance*>(obj_name, pObject);
101 if (inst ==
nullptr)
return;
103 if (guid !=
nullptr) {
105 vec.emplace_back (std::make_pair (std::string{m_col_name}, quote_string(guid_s)));
114 vec.emplace_back(std::move(info));
123 gpointer pObject)
const noexcept
125 g_return_if_fail (pObject != NULL);
126 g_return_if_fail (m_gobj_param_name != NULL || get_setter(obj_name) != NULL);
128 auto s = row.get_string_at_col (m_col_name);
130 set_parameter(pObject, s->c_str(), get_setter(obj_name), m_gobj_param_name);
137 vec.emplace_back(std::move(info));
145 const gpointer pObject,
146 PairVec& vec)
const noexcept
148 auto s = get_row_value_from_object<char*>(obj_name, pObject);
152 std::ostringstream stream;
154 vec.emplace_back (std::make_pair (std::string{m_col_name},
155 quote_string(stream.str())));
161 typedef gint (*IntAccessFunc) (
const gpointer);
162 typedef void (*IntSetterFunc) (
const gpointer, gint);
168 gpointer pObject)
const noexcept
171 g_return_if_fail (pObject != NULL);
172 g_return_if_fail (m_gobj_param_name != NULL || get_setter(obj_name) != NULL);
174 auto val = row.get_int_at_col(m_col_name);
176 set_parameter(pObject, *val,
177 reinterpret_cast<IntSetterFunc>(get_setter(obj_name)),
185 vec.emplace_back(std::move(info));
190 const gpointer pObject,
191 PairVec& vec)
const noexcept
193 add_value_to_vec<int>(obj_name, pObject, vec);
197 typedef gboolean (*BooleanAccessFunc) (
const gpointer);
198 typedef void (*BooleanSetterFunc) (
const gpointer, gboolean);
207 g_return_if_fail (pObject != NULL);
208 g_return_if_fail (m_gobj_param_name != NULL || get_setter(obj_name) != NULL);
210 auto val = row.get_int_at_col (m_col_name);
212 set_parameter(pObject, static_cast<int>(*val),
213 reinterpret_cast<BooleanSetterFunc>(get_setter(obj_name)),
221 vec.emplace_back(std::move(info));
226 const gpointer pObject,
227 PairVec& vec)
const noexcept
229 add_value_to_vec<int>(obj_name, pObject, vec);
233 typedef gint64 (*Int64AccessFunc) (
const gpointer);
234 typedef void (*Int64SetterFunc) (
const gpointer, gint64);
243 g_return_if_fail (m_gobj_param_name !=
nullptr || get_setter(obj_name) !=
nullptr);
245 auto val = row.get_int_at_col (m_col_name);
247 set_parameter(pObject, *val,
248 reinterpret_cast<Int64SetterFunc>(get_setter(obj_name)),
257 vec.emplace_back(std::move(info));
262 const gpointer pObject,
263 PairVec& vec)
const noexcept
265 add_value_to_vec<int64_t>(obj_name, pObject, vec);
276 g_return_if_fail (pObject != NULL);
277 g_return_if_fail (m_gobj_param_name !=
nullptr || get_setter(obj_name) !=
nullptr);
280 if (
auto int_val{row.get_int_at_col(m_col_name)})
281 val =
static_cast<decltype(val)
>(*int_val);
282 else if (
auto float_val{row.get_float_at_col(m_col_name)})
283 val =
static_cast<decltype(val)
>(*float_val);
284 else if (
auto double_val{row.get_double_at_col(m_col_name)})
287 set_parameter(pObject, val, get_setter(obj_name), m_gobj_param_name);
294 vec.emplace_back(std::move(info));
299 const gpointer pObject,
300 PairVec& vec)
const noexcept
302 add_value_to_vec<double*>(obj_name, pObject, vec);
317 g_return_if_fail (pObject != NULL);
318 g_return_if_fail (m_gobj_param_name !=
nullptr || get_setter(obj_name) !=
nullptr);
320 auto strval{row.get_string_at_col(m_col_name)};
322 set_parameter(pObject, &guid, get_setter(obj_name), m_gobj_param_name);
329 vec.emplace_back(std::move(info));
334 const gpointer pObject,
335 PairVec& vec)
const noexcept
337 auto s = get_row_value_from_object<GncGUID*>(obj_name, pObject);
342 vec.emplace_back (std::make_pair (std::string{m_col_name}, quote_string(guid_s)));
348 typedef time64 (*Time64AccessFunc) (
const gpointer);
349 typedef void (*Time64SetterFunc) (
const gpointer,
time64);
350 constexpr
int TIME_COL_SIZE = 4 + 3 + 3 + 3 + 3 + 3;
360 g_return_if_fail (m_gobj_param_name !=
nullptr || get_setter(obj_name) !=
nullptr);
361 auto strval = row.get_string_at_col(m_col_name);
364 if (!strval->empty())
368 t =
static_cast<time64>(time);
370 catch (
const std::invalid_argument& err)
372 PWARN(
"An invalid date %s was found in your database." 373 "It has been set to 1 January 1970.",
379 if (
auto time64val = row.get_time64_at_col (m_col_name))
383 if (m_gobj_param_name !=
nullptr)
386 set_parameter(pObject, &t64, m_gobj_param_name);
390 set_parameter(pObject, t,
391 reinterpret_cast<Time64SetterFunc>(get_setter(obj_name)),
401 vec.emplace_back(std::move(info));
406 const gpointer pObject,
407 PairVec& vec)
const noexcept
414 if (m_gobj_param_name !=
nullptr)
417 g_object_get (pObject, m_gobj_param_name, &t,
nullptr);
422 auto getter = (Time64AccessFunc)get_getter (obj_name);
423 g_return_if_fail(getter !=
nullptr);
424 t64 = (*getter)(pObject);
426 if (t64 > MINTIME && t64 < MAXTIME)
429 std::string timestr(
"'");
430 timestr += time.format_iso8601() +
"'";
431 vec.emplace_back (std::make_pair (std::string{m_col_name}, timestr));
435 vec.emplace_back (std::make_pair (std::string{m_col_name},
441 #define DATE_COL_SIZE 8 447 gpointer pObject)
const noexcept
449 g_return_if_fail (pObject != NULL);
450 g_return_if_fail (m_gobj_param_name !=
nullptr || get_setter(obj_name) !=
nullptr);
451 if (row.is_col_null(m_col_name))
454 g_date_clear (&date, 1);
456 auto strval{row.get_string_at_col(m_col_name)};
461 auto year =
static_cast<GDateYear
>(stoi (strval->substr (0,4)));
462 auto month =
static_cast<GDateMonth
>(stoi (strval->substr (4,2)));
463 auto day =
static_cast<GDateDay
>(stoi (strval->substr (6,2)));
465 if (year != 0 || month != 0 || day != (GDateDay)0)
466 g_date_set_dmy(&date, day, month, year);
470 auto timeval = row.get_time64_at_col(m_col_name);
476 auto time = *timeval;
478 g_date_set_dmy(&date, tm->tm_mday,
479 static_cast<GDateMonth>(tm->tm_mon + 1),
484 set_parameter(pObject, &date, get_setter(obj_name), m_gobj_param_name);
491 vec.emplace_back(std::move(info));
496 const gpointer pObject,
497 PairVec& vec)
const noexcept
499 GDate *date = get_row_value_from_object<GDate*>(obj_name, pObject);
501 if (date && g_date_valid (date))
503 std::ostringstream buf;
504 buf << std::setfill (
'0') << std::setw (4) << g_date_get_year (date) <<
505 std::setw (2) << g_date_get_month (date) <<
506 std::setw (2) <<
static_cast<int>(g_date_get_day (date));
507 vec.emplace_back (std::make_pair (std::string{m_col_name},
508 quote_string(buf.str())));
514 typedef gnc_numeric (*NumericGetterFunc) (
const gpointer);
515 typedef void (*NumericSetterFunc) (gpointer, gnc_numeric);
517 static const EntryVec numeric_col_table =
519 gnc_sql_make_table_entry<CT_INT64>(
"num", 0, COL_NNUL,
"guid"),
520 gnc_sql_make_table_entry<CT_INT64>(
"denom", 0, COL_NNUL,
"guid")
524 void set_parameter<gpointer, gnc_numeric>(gpointer object,
526 const char* property)
528 qof_instance_increase_editlevel(
object);
529 g_object_set(
object, property, &item,
nullptr);
530 qof_instance_decrease_editlevel(
object);
537 gpointer pObject)
const noexcept
541 g_return_if_fail (pObject != NULL);
542 g_return_if_fail (m_gobj_param_name !=
nullptr || get_setter(obj_name) !=
nullptr);
544 auto buf = g_strdup_printf (
"%s_num", m_col_name);
545 auto num = row.get_int_at_col (buf);
547 buf = g_strdup_printf (
"%s_denom", m_col_name);
548 auto denom = row.get_int_at_col (buf);
553 auto n = gnc_numeric_create (*num, *denom);
554 set_parameter(pObject, n,
555 reinterpret_cast<NumericSetterFunc>(get_setter(obj_name)),
564 for (
auto const& subtable_row : numeric_col_table)
566 gchar* buf = g_strdup_printf(
"%s_%s", m_col_name,
567 subtable_row->m_col_name);
569 m_flags & COL_PKEY, m_flags & COL_NNUL);
571 vec.emplace_back(std::move(info));
577 const gpointer pObject,
578 PairVec& vec)
const noexcept
581 NumericGetterFunc getter;
584 g_return_if_fail (obj_name != NULL);
585 g_return_if_fail (pObject != NULL);
587 if (m_gobj_param_name !=
nullptr)
590 g_object_get (pObject, m_gobj_param_name, &s, NULL);
595 getter =
reinterpret_cast<NumericGetterFunc
>(get_getter (obj_name));
598 n = (*getter) (pObject);
602 n = gnc_numeric_zero ();
606 std::ostringstream buf;
607 std::string num_col{m_col_name};
608 std::string denom_col{m_col_name};
610 denom_col +=
"_denom";
611 buf << gnc_numeric_num (n);
612 vec.emplace_back (std::make_pair (num_col, buf.str ()));
614 buf << gnc_numeric_denom (n);
615 vec.emplace_back (denom_col, buf.str ());
619 _retrieve_guid_ (gpointer pObject, gpointer pValue)
624 g_return_if_fail (pObject != NULL);
625 g_return_if_fail (pValue != NULL);
631 static EntryVec guid_table
633 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, 0,
nullptr, _retrieve_guid_)
641 g_return_val_if_fail (sql_be != NULL, NULL);
643 gnc_sql_load_object (sql_be, row, NULL, &guid, guid_table);
651 const EntryVec&
table)
653 g_return_if_fail (sql_be != NULL);
654 g_return_if_fail (pObject != NULL);
656 for (
auto const& table_row :
table)
658 table_row->load (sql_be, row, obj_name, pObject);
663 gnc_sql_append_guids_to_sql (std::stringstream& sql,
664 const InstanceVec& instances)
668 for (
auto inst : instances)
672 if (inst != *(instances.begin()))
676 sql <<
"'" << guid_buf <<
"'";
679 return instances.size();
689 GncSqlColumnTableEntry::get_row_value_from_object<int>(
QofIdTypeConst obj_name,
691 std::false_type)
const 693 g_return_val_if_fail(obj_name !=
nullptr && pObject !=
nullptr, 0);
695 if (m_gobj_param_name !=
nullptr)
696 g_object_get(const_cast<void*>(pObject), m_gobj_param_name, &result,
701 if (getter !=
nullptr)
703 auto value = ((getter)(const_cast<void*>(pObject),
nullptr));
704 result =
reinterpret_cast<uint64_t
>(value) &
705 UINT64_C(0x00000000FFFFFFFF);
information required to create a column in a table.
QofSetterFunc get_setter(QofIdTypeConst obj_name) const noexcept
Retrieve the setter function depending on whether it's an auto-increment field, a QofClass getter...
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
void add_objectref_guid_to_query(QofIdTypeConst obj_name, const void *pObject, PairVec &vec) const noexcept
Adds a name/guid std::pair to a PairVec for creating a query.
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Given a string, replace the given guid with the parsed one unless the given value is null...
void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept override
Add a pair of the table column heading and object's value's string representation to a PairVec; used ...
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
QofAccessFunc qof_class_get_parameter_getter(QofIdTypeConst obj_name, const char *parameter)
Return the object's parameter getter function.
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
#define PWARN(format, args...)
Log a warning.
void load(const GncSqlBackend *sql_be, GncSqlRow &row, QofIdTypeConst obj_name, void *pObject) const noexcept override
Load a value into an object from the database row.
Row of SQL Query results.
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
gchar * guid_to_string(const GncGUID *guid)
The guid_to_string() routine returns a null-terminated string encoding of the id. ...
struct tm * gnc_gmtime(const time64 *secs)
fill out a time struct from a 64-bit time value
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
void add_objectref_guid_to_table(ColVec &vec) const noexcept
Adds a column info structure for an object reference GncGUID to a ColVec.
QofSetterFunc qof_class_get_parameter_setter(QofIdTypeConst obj_name, const char *parameter)
Return the object's parameter setter function.
The type used to store guids in C.
Main SQL backend structure.
QofAccessFunc get_getter(QofIdTypeConst obj_name) const noexcept
Retrieve the getter function depending on whether it's an auto-increment field, a QofClass getter...