24 #ifndef __GNC_SQL_COLUMN_TABLE_ENTRY_HPP__ 25 #define __GNC_SQL_COLUMN_TABLE_ENTRY_HPP__ 33 #include "gnc-sql-result.hpp" 36 using ColVec = std::vector<GncSqlColumnInfo>;
37 using PairVec = std::vector<std::pair<std::string, std::string>>;
38 using InstanceVec = std::vector<QofInstance*>;
39 using uint_t =
unsigned int;
53 } GncSqlBasicColumnType;
55 enum ColumnFlags :
int 89 static inline std::string
90 quote_string(
const std::string& str)
92 if (str ==
"NULL" || str ==
"null")
return "NULL";
96 if (str.empty())
return "''";
98 retval.reserve(str.length() + 2);
99 retval.insert(0, 1,
'\'');
100 for (
auto c = str.begin(); c != str.end(); ++c)
131 int f,
const char* gobj_name =
nullptr,
132 const char* qof_name =
nullptr,
135 m_col_name{
name}, m_col_type{type}, m_size{s},
136 m_flags{
static_cast<ColumnFlags
>(f)},
137 m_gobj_param_name{gobj_name}, m_qof_param_name{qof_name}, m_getter{
get},
150 virtual void add_to_table(ColVec& vec)
const noexcept = 0;
157 void* pObject, PairVec& vec)
const noexcept = 0;
172 const char*
name() const noexcept {
return m_col_name; }
176 bool is_autoincr() const noexcept {
return m_flags & COL_AUTOINC; }
182 template<
typename T>
void load_from_guid_ref(
GncSqlRow& row,
184 void* pObject, T get_ref)
188 g_return_if_fail (pObject != NULL);
191 auto val = row.get_string_at_col (m_col_name);
194 DEBUG(
"set parameter: No string in column %s.", m_col_name);
200 auto target = get_ref(&guid);
201 if (target !=
nullptr)
202 set_parameter (pObject, target,
get_setter(obj_name),
205 DEBUG(
"GUID %s returned null %s reference.",
206 val->c_str(), m_gobj_param_name);
211 DEBUG(
"Can't load empty guid string for column %s", m_col_name);
213 DEBUG(
"Invalid GUID %s for column %s", val->c_str(), m_col_name);
219 template <
typename T> T
220 get_row_value_from_object(
QofIdTypeConst obj_name,
const void* pObject)
const;
221 template <
typename T>
void 223 const void* pObject, PairVec& vec)
const;
234 PairVec& vec)
const noexcept;
243 const char* m_col_name =
nullptr;
244 const GncSqlObjectType m_col_type;
247 const char* m_gobj_param_name =
nullptr;
248 const char* m_qof_param_name =
nullptr;
251 template <
typename T> T get_row_value_from_object(
QofIdTypeConst obj_name,
253 std::true_type)
const;
254 template <
typename T> T get_row_value_from_object(
QofIdTypeConst obj_name,
256 std::false_type)
const;
257 template <
typename T>
void add_value_to_vec(
QofIdTypeConst obj_name,
259 PairVec& vec, std::true_type)
const;
260 template <
typename T>
void add_value_to_vec(
QofIdTypeConst obj_name,
262 PairVec& vec, std::false_type)
const;
266 template <GncSqlObjectType Type>
272 int f,
const char* gobj_name =
nullptr,
273 const char* qof_name =
nullptr,
280 void* pObject)
const noexcept
override;
283 const noexcept
override;
286 using GncSqlColumnTableEntryPtr = std::shared_ptr<GncSqlColumnTableEntry>;
287 using EntryVec = std::vector<GncSqlColumnTableEntryPtr>;
289 template <GncSqlObjectType Type>
290 std::shared_ptr<GncSqlColumnTableEntryImpl<Type>>
291 gnc_sql_make_table_entry(
const char* name,
unsigned int s,
int f)
293 return std::make_shared<GncSqlColumnTableEntryImpl<Type>>(name, Type, s, f);
296 template <GncSqlObjectType Type>
297 std::shared_ptr<GncSqlColumnTableEntryImpl<Type>>
298 gnc_sql_make_table_entry(
const char* name,
unsigned int s,
int f,
301 return std::make_shared<GncSqlColumnTableEntryImpl<Type>>(name, Type, s,
307 template <GncSqlObjectType Type>
308 std::shared_ptr<GncSqlColumnTableEntryImpl<Type>>
309 gnc_sql_make_table_entry(
const char* name,
unsigned int s,
int f,
310 const char* param,
bool qofp)
312 return std::make_shared<GncSqlColumnTableEntryImpl<Type>>(name, Type, s,
317 template <GncSqlObjectType Type>
318 std::shared_ptr<GncSqlColumnTableEntryImpl<Type>>
319 gnc_sql_make_table_entry(
const char* name,
unsigned int s,
int f,
322 return std::make_shared<GncSqlColumnTableEntryImpl<Type>>(
323 name, Type, s, f,
nullptr,
nullptr,
get,
set);
327 template <
typename T> T
328 GncSqlColumnTableEntry::get_row_value_from_object(
QofIdTypeConst obj_name,
329 const void* pObject)
const 331 return get_row_value_from_object<T>(obj_name, pObject,
332 std::is_pointer<T>());
335 template <
typename T> T
336 GncSqlColumnTableEntry::get_row_value_from_object(
QofIdTypeConst obj_name,
338 std::true_type)
const 340 g_return_val_if_fail(obj_name !=
nullptr && pObject !=
nullptr,
nullptr);
342 if (m_gobj_param_name !=
nullptr)
343 g_object_get(const_cast<void*>(pObject), m_gobj_param_name,
348 if (getter !=
nullptr)
349 result =
reinterpret_cast<T
>((getter)(const_cast<void*>(pObject),
355 template <
typename T> T
356 GncSqlColumnTableEntry::get_row_value_from_object(
QofIdTypeConst obj_name,
358 std::false_type)
const 360 g_return_val_if_fail(obj_name !=
nullptr && pObject !=
nullptr,
362 T result =
static_cast<T
>(0);
363 if (m_gobj_param_name !=
nullptr)
364 g_object_get(const_cast<void*>(pObject), m_gobj_param_name,
369 if (getter !=
nullptr)
370 result =
reinterpret_cast<T
>((getter)(const_cast<void*>(pObject),
376 template <
typename T>
void 381 add_value_to_vec<T>(obj_name, pObject, vec, std::is_pointer<T>());
384 template <
typename T>
void 387 PairVec& vec, std::true_type)
const 389 T s = get_row_value_from_object<T>(obj_name, pObject);
393 std::ostringstream stream;
395 vec.emplace_back(std::make_pair(std::string{m_col_name}, stream.str()));
400 template <>
inline void 401 GncSqlColumnTableEntry::add_value_to_vec<double*>(
QofIdTypeConst obj_name,
403 PairVec& vec, std::true_type)
const 405 double* s = get_row_value_from_object<double*>(obj_name, pObject);
409 std::ostringstream stream;
410 stream << std::setprecision(12) << std::fixed << *s;
411 vec.emplace_back(std::make_pair(std::string{m_col_name}, stream.str()));
416 template <
typename T>
void 419 PairVec& vec, std::false_type)
const 421 T s = get_row_value_from_object<T>(obj_name, pObject);
423 std::ostringstream stream;
425 vec.emplace_back(std::make_pair(std::string{m_col_name}, stream.str()));
429 template <>
inline void 430 GncSqlColumnTableEntry::add_value_to_vec<double>(
QofIdTypeConst obj_name,
432 PairVec& vec, std::false_type)
const 434 double s = *get_row_value_from_object<double*>(obj_name, pObject);
436 std::ostringstream stream;
437 stream << std::setprecision(12) << std::fixed << s;
438 vec.emplace_back(std::make_pair(std::string{m_col_name}, stream.str()));
453 const EntryVec&
table);
470 uint_t gnc_sql_append_guids_to_sql (std::stringstream& sql,
471 const InstanceVec& instances);
479 unsigned int size = 0,
bool unicode =
false,
480 bool autoinc =
false,
bool primary =
false,
481 bool not_null =
false) :
486 unsigned int size = 0,
bool unicode =
true) :
521 template <
typename T,
typename P,
typename F>
522 void set_parameter(T
object, P item, F& setter)
524 (*setter)(object, item);
527 template <
typename T,
typename P>
528 void set_parameter(T
object, P item,
QofSetterFunc setter, std::true_type)
530 (*setter)(object, (
void*)item);
533 template <
typename T,
typename P>
534 void set_parameter(T
object, P item,
QofSetterFunc setter, std::false_type)
536 (*setter)(object, (
void*)(&item));
539 template <
typename T,
typename P>
542 set_parameter(
object, item, setter, std::is_pointer<P>());
553 template <
typename T,
typename P>
554 void set_parameter(T
object, P item,
const char* property)
562 g_object_set(
object, property, item,
nullptr);
577 template <
typename T,
typename P,
typename F>
578 void set_parameter(T
object, P item, F setter,
const char* property)
581 set_parameter(
object, item, property);
583 set_parameter(
object, item, setter);
586 #endif //__GNC_SQL_COLUMN_TABLE_ENTRY_HPP__ 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...
bool m_not_null
Column forbids NULL values.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
virtual void add_to_table(ColVec &vec) const noexcept=0
Add a GncSqlColumnInfo structure for the column type to a ColVec.
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.
bool m_autoinc
Column is autoinc (int type)
#define DEBUG(format, args...)
Print a debugging message.
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...
virtual void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept=0
Add a pair of the table column heading and object's value's string representation to a PairVec; used ...
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 ...
GncSqlBasicColumnType m_type
Column basic type.
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
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.
gboolean qof_begin_edit(QofInstance *inst)
begin_edit
const char * name() const noexcept
Retrieve the field name so that we don't need to make create_single_col_select_statement and friend...
bool m_unicode
Column is unicode (string types)
Row of SQL Query results.
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
part2 – deal with the backend
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
bool m_primary_key
Column is the primary key.
bool is_autoincr() const noexcept
Report if the entry is an auto-increment field.
unsigned int m_size
Column size (string types)
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
void add_objectref_guid_to_table(ColVec &vec) const noexcept
Adds a column info structure for an object reference GncGUID to a ColVec.
std::string m_name
Column name.
Contains all of the information required to copy information between an object and the database for a...
The type used to store guids in C.
virtual void load(const GncSqlBackend *sql_be, GncSqlRow &row, QofIdTypeConst obj_name, void *pObject) const noexcept=0
Load a value into an object from the database row.
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...