GnuCash  5.6-150-g038405b370+
gnc-address-sql.cpp
1 /********************************************************************\
2  * gnc-address-sql.c -- address sql backend implementation *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20  * *
21 \********************************************************************/
22 
30 #include <glib.h>
31 
32 #include <config.h>
33 
34 #include "gnc-engine.h"
35 
36 #include "gncAddress.h"
37 #include <cstdlib>
38 #include <cstring>
39 #include <sstream>
40 #include "gnc-sql-backend.hpp"
41 #include "gnc-sql-column-table-entry.hpp"
42 
43 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
44 
45 #define ADDRESS_MAX_NAME_LEN 1024
46 #define ADDRESS_MAX_ADDRESS_LINE_LEN 1024
47 #define ADDRESS_MAX_PHONE_LEN 128
48 #define ADDRESS_MAX_FAX_LEN 128
49 #define ADDRESS_MAX_EMAIL_LEN 256
50 
51 static EntryVec col_table
52 ({
53  std::make_shared<GncSqlColumnTableEntryImpl<CT_STRING>>(
54  "name", CT_STRING, ADDRESS_MAX_NAME_LEN, COL_NNUL, "name"),
55  gnc_sql_make_table_entry<CT_STRING>(
56  "addr1", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr1"),
57  gnc_sql_make_table_entry<CT_STRING>(
58  "addr2", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr2"),
59  gnc_sql_make_table_entry<CT_STRING>(
60  "addr3", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr3"),
61  gnc_sql_make_table_entry<CT_STRING>(
62  "addr4", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr4"),
63  gnc_sql_make_table_entry<CT_STRING>(
64  "phone", ADDRESS_MAX_PHONE_LEN, COL_NNUL, "phone"),
65  gnc_sql_make_table_entry<CT_STRING>(
66  "fax", ADDRESS_MAX_FAX_LEN, COL_NNUL, "fax" ),
67  gnc_sql_make_table_entry<CT_STRING>(
68  "email", ADDRESS_MAX_EMAIL_LEN, COL_NNUL, "email"),
69 });
70 
71 typedef void (*AddressSetterFunc) (gpointer, GncAddress*);
72 typedef GncAddress* (*AddressGetterFunc) (const gpointer);
73 
74 template<> void
76  GncSqlRow& row,
77  QofIdTypeConst obj_name,
78  gpointer pObject) const noexcept
79 {
80  g_return_if_fail (sql_be != NULL);
81  g_return_if_fail (pObject != NULL);
82 
83  auto addr = gncAddressCreate (sql_be->book(), QOF_INSTANCE(pObject));
84 
85  for (auto const& subtable_row : col_table)
86  {
87  auto buf = std::string{m_col_name} + "_" + subtable_row->m_col_name;
88  auto val = row.get_string_at_col (buf.c_str());
89  auto sub_setter = subtable_row->get_setter(GNC_ID_ADDRESS);
90  if (val)
91  set_parameter (addr, val->c_str(), sub_setter,
92  subtable_row->m_gobj_param_name);
93  }
94 
95  set_parameter (pObject, addr,
96  reinterpret_cast<AddressSetterFunc>(get_setter(obj_name)),
97  m_gobj_param_name);
98 }
99 
100 template<> void
102 {
103  for (auto const& subtable_row : col_table)
104  {
105  auto buf = std::string{m_col_name} + "_" + subtable_row->m_col_name;
106  GncSqlColumnInfo info(buf.c_str(), BCT_STRING, subtable_row->m_size,
107  true, false, m_flags & COL_PKEY, m_flags & COL_NNUL);
108  vec.emplace_back(std::move(info));
109  }
110 }
111 
112 /* char is unusual in that we get a pointer but don't deref it to pass
113  * it to operator<<().
114  */
115 template<> void
117  const gpointer pObject,
118  PairVec& vec) const noexcept
119 {
120  auto addr(get_row_value_from_object<char*>(obj_name, pObject));
121  if (addr == nullptr) return;
122 
123  for (auto const& subtable_row : col_table)
124  {
125  auto s = subtable_row->get_row_value_from_object<char*>(GNC_ID_ADDRESS,
126  addr);
127  if (s == nullptr)
128  continue;
129  auto buf = std::string{m_col_name} + "_" + subtable_row->m_col_name;
130  vec.emplace_back(make_pair(buf, quote_string(s)));
131  }
132 }
133 /* ========================== END OF FILE ===================== */
information required to create a column in a table.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition: qofid.h:82
void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept override
Add a pair of the table column heading and object&#39;s value&#39;s string representation to a PairVec; used ...
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.
All type declarations for the whole Gnucash engine.
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
an Address object
Main SQL backend structure.