26#include <gnc-locale-utils.h>
29#include <dbi/dbi-dev.h>
31#include <gnc-datetime.hpp>
32#include "gnc-dbisqlresult.hpp"
33#include "gnc-dbisqlconnection.hpp"
35static QofLogModule log_module = G_LOG_DOMAIN;
37#if LIBDBI_VERSION >= 900
38#define HAVE_LIBDBI_TO_LONGLONG 1
40#define HAVE_LIBDBI_TO_LONGLONG 0
43GncDbiSqlResult::~GncDbiSqlResult()
49 int status = dbi_result_free (m_dbi_result);
54 PERR (
"Error %d in dbi_result_free() result.", m_conn->dberror() );
59GncDbiSqlResult::dberror() const noexcept
61 return m_conn->dberror();
65GncDbiSqlResult::begin()
68 if (m_dbi_result ==
nullptr ||
69 dbi_result_get_numrows(m_dbi_result) == 0)
71 int status = dbi_result_first_row(m_dbi_result);
74 int error = dberror();
76 if (error != DBI_ERROR_BADIDX)
78 PERR (
"Error %d in dbi_result_first_row()", dberror());
85GncDbiSqlResult::size() const noexcept
87 return dbi_result_get_numrows(m_dbi_result);
92GncDbiSqlResult::IteratorImpl::operator++()
94 int status = dbi_result_next_row (m_inst->m_dbi_result);
97 int error = m_inst->dberror();
98 if (error == DBI_ERROR_BADIDX || error == 0)
99 return m_inst->m_sentinel;
100 PERR(
"Error %d incrementing results iterator.", error);
102 return m_inst->m_sentinel;
105std::optional<int64_t>
106GncDbiSqlResult::IteratorImpl::get_int_at_col(
const char* col)
const
108 auto type = dbi_result_get_field_type (m_inst->m_dbi_result, col);
109 if(type != DBI_TYPE_INTEGER)
111 return std::optional<int64_t>{dbi_result_get_longlong (m_inst->m_dbi_result, col)};
115GncDbiSqlResult::IteratorImpl::get_float_at_col(
const char* col)
const
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)
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};
131GncDbiSqlResult::IteratorImpl::get_double_at_col(
const char* col)
const
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)
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};
144std::optional<std::string>
145GncDbiSqlResult::IteratorImpl::get_string_at_col(
const char* col)
const
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)
151 auto strval = dbi_result_get_string(m_inst->m_dbi_result, col);
152 return std::optional<std::string>{strval ? strval :
""};
156GncDbiSqlResult::IteratorImpl::get_time64_at_col (
const char* col)
const
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)
163#if HAVE_LIBDBI_TO_LONGLONG
167 auto timeval = dbi_result_get_as_longlong(result, col);
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;
179 if (timeval < MINTIME || timeval > MAXTIME)
181 return std::optional<time64>(timeval);
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
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
#define PERR(format, args...)
Log a serious error.