GnuCash de0bfde+
Loading...
Searching...
No Matches
gnc-dbisqlresult.cpp
1/********************************************************************
2 * gnc-dbisqlresult.cpp: Encapsulate libdbi dbi_result *
3 * *
4 * Copyright 2016 John Ralls <jralls@ceridwen.us> *
5 * *
6 * This program is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU General Public License as *
8 * published by the Free Software Foundation; either version 2 of *
9 * the License, or (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License*
17 * along with this program; if not, contact: *
18 * *
19 * Free Software Foundation Voice: +1-617-542-5942 *
20 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 * Boston, MA 02110-1301, USA gnu@gnu.org *
22\********************************************************************/
23
24#include <guid.hpp>
25#include <config.h>
26#include <gnc-locale-utils.h>
27#include <dbi/dbi.h>
28/* For direct access to dbi data structs, sadly needed for datetime */
29#include <dbi/dbi-dev.h>
30#include <cmath>
31#include <gnc-datetime.hpp>
32#include "gnc-dbisqlresult.hpp"
33#include "gnc-dbisqlconnection.hpp"
34
35static QofLogModule log_module = G_LOG_DOMAIN;
36
37#if LIBDBI_VERSION >= 900
38#define HAVE_LIBDBI_TO_LONGLONG 1
39#else
40#define HAVE_LIBDBI_TO_LONGLONG 0
41#endif
42
43GncDbiSqlResult::~GncDbiSqlResult()
44{
45 /* A failed query has no result to free; its error is already set. */
46 if (!m_dbi_result)
47 return;
48
49 int status = dbi_result_free (m_dbi_result);
50
51 if (status == 0)
52 return;
53
54 PERR ("Error %d in dbi_result_free() result.", m_conn->dberror() );
56}
57
58int
59GncDbiSqlResult::dberror() const noexcept
60{
61 return m_conn->dberror();
62}
63
65GncDbiSqlResult::begin()
66{
67
68 if (m_dbi_result == nullptr ||
69 dbi_result_get_numrows(m_dbi_result) == 0)
70 return m_sentinel;
71 int status = dbi_result_first_row(m_dbi_result);
72 if (status)
73 return m_row;
74 int error = dberror(); //
75
76 if (error != DBI_ERROR_BADIDX) //otherwise just an empty result set
77 {
78 PERR ("Error %d in dbi_result_first_row()", dberror());
80 }
81 return m_sentinel;
82}
83
84uint64_t
85GncDbiSqlResult::size() const noexcept
86{
87 return dbi_result_get_numrows(m_dbi_result);
88}
89/* --------------------------------------------------------- */
90
92GncDbiSqlResult::IteratorImpl::operator++()
93{
94 int status = dbi_result_next_row (m_inst->m_dbi_result);
95 if (status)
96 return m_inst->m_row;
97 int error = m_inst->dberror();
98 if (error == DBI_ERROR_BADIDX || error == 0) //ran off the end of the results
99 return m_inst->m_sentinel;
100 PERR("Error %d incrementing results iterator.", error);
101 qof_backend_set_error (m_inst->m_conn->qbe(), ERR_BACKEND_SERVER_ERR);
102 return m_inst->m_sentinel;
103}
104
105std::optional<int64_t>
106GncDbiSqlResult::IteratorImpl::get_int_at_col(const char* col) const
107{
108 auto type = dbi_result_get_field_type (m_inst->m_dbi_result, col);
109 if(type != DBI_TYPE_INTEGER)
110 return std::nullopt;
111 return std::optional<int64_t>{dbi_result_get_longlong (m_inst->m_dbi_result, col)};
112}
113
114std::optional<double>
115GncDbiSqlResult::IteratorImpl::get_float_at_col(const char* col) const
116{
117 constexpr double float_precision = 1000000.0;
118 auto type = dbi_result_get_field_type (m_inst->m_dbi_result, col);
119 auto attrs = dbi_result_get_field_attribs (m_inst->m_dbi_result, col);
120 if(type != DBI_TYPE_DECIMAL ||
121 (attrs & DBI_DECIMAL_SIZEMASK) != DBI_DECIMAL_SIZE4)
122 return std::nullopt;
123 auto locale = gnc_push_locale (LC_NUMERIC, "C");
124 auto interim = dbi_result_get_float(m_inst->m_dbi_result, col);
125 gnc_pop_locale (LC_NUMERIC, locale);
126 double retval = static_cast<double>(round(interim * float_precision)) / float_precision;
127 return std::optional<double>{retval};
128}
129
130std::optional<double>
131GncDbiSqlResult::IteratorImpl::get_double_at_col(const char* col) const
132{
133 auto type = dbi_result_get_field_type (m_inst->m_dbi_result, col);
134 auto attrs = dbi_result_get_field_attribs (m_inst->m_dbi_result, col);
135 if(type != DBI_TYPE_DECIMAL ||
136 (attrs & DBI_DECIMAL_SIZEMASK) != DBI_DECIMAL_SIZE8)
137 return std::nullopt;
138 auto locale = gnc_push_locale (LC_NUMERIC, "C");
139 auto retval = dbi_result_get_double(m_inst->m_dbi_result, col);
140 gnc_pop_locale (LC_NUMERIC, locale);
141 return std::optional<double>{retval};
142}
143
144std::optional<std::string>
145GncDbiSqlResult::IteratorImpl::get_string_at_col(const char* col) const
146{
147 auto type = dbi_result_get_field_type (m_inst->m_dbi_result, col);
148 dbi_result_get_field_attribs (m_inst->m_dbi_result, col);
149 if(type != DBI_TYPE_STRING)
150 return std::nullopt;
151 auto strval = dbi_result_get_string(m_inst->m_dbi_result, col);
152 return std::optional<std::string>{strval ? strval : ""};
153}
154
155std::optional<time64>
156GncDbiSqlResult::IteratorImpl::get_time64_at_col (const char* col) const
157{
158 auto result = (dbi_result_t*) (m_inst->m_dbi_result);
159 auto type = dbi_result_get_field_type (result, col);
160 dbi_result_get_field_attribs (result, col);
161 if (type != DBI_TYPE_DATETIME)
162 return std::nullopt;
163#if HAVE_LIBDBI_TO_LONGLONG
164 /* A less evil hack than the one required by libdbi-0.8, but
165 * still necessary to work around the same bug.
166 */
167 auto timeval = dbi_result_get_as_longlong(result, col);
168#else
169 /* A seriously evil hack to work around libdbi bug #15
170 * https://sourceforge.net/p/libdbi/bugs/15/. When libdbi
171 * v0.9 is widely available this can be replaced with
172 * dbi_result_get_as_longlong.
173 * Note: 0.9 is available in Debian Jessie and Fedora 21.
174 */
175 auto row = dbi_result_get_currow (result);
176 auto idx = dbi_result_get_field_idx (result, col) - 1;
177 time64 timeval = result->rows[row]->field_values[idx].d_datetime;
178#endif //HAVE_LIBDBI_TO_LONGLONG
179 if (timeval < MINTIME || timeval > MAXTIME)
180 timeval = 0;
181 return std::optional<time64>(timeval);
182}
183
184
185/* --------------------------------------------------------- */
186
Row of SQL Query results.
void qof_backend_set_error(QofBackend *qof_be, QofBackendError err)
Set the error on the specified QofBackend.
@ ERR_BACKEND_SERVER_ERR
error in response from server
Definition qofbackend.h:71
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244