1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include "odbc-selectcommand.h"
#include "column.h"
#include "command.h"
#include "error.h"
#include "odbc-column.h"
#include "odbc-command.h"
#include "odbc-error.h"
#include "selectcommand.h"
#include <algorithm>
#include <array>
#include <glibmm/ustring.h>
#include <numeric>
#include <sqlext.h>
#include <utility>
namespace ODBC {
class Connection;
}
ODBC::SelectCommand::SelectCommand(const Connection & c, const std::string & s) :
DB::Command(s), ODBC::Command(c, s), DB::SelectCommand(s)
{
}
ODBC::SelectCommand::~SelectCommand()
{
if (columnCount()) {
SQLCloseCursor(hStmt);
}
}
constexpr std::array<SQLCHAR, 6> truncated = {'0', '1', '0', '0', '4', '\0'};
bool
ODBC::SelectCommand::fetch()
{
if (!columnCount()) {
execute();
}
for (RETCODE rc = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); rc != SQL_NO_DATA;
rc = SQLFetchScroll(hStmt, SQL_FETCH_RELATIVE, 0)) {
if (!SQL_SUCCEEDED(rc)) {
throw Error(rc, SQL_HANDLE_STMT, hStmt);
}
if (rc != SQL_SUCCESS) {
std::array<SQLCHAR, 6> sqlstatus {};
RETCODE diagrc = SQLGetDiagRec(SQL_HANDLE_STMT, hStmt, 1, sqlstatus.data(), nullptr, nullptr, 0, nullptr);
if (SQL_SUCCEEDED(diagrc)) {
if (sqlstatus == truncated) {
for (const auto & c : largeColumns) {
c->resize();
}
continue;
}
}
}
if (std::accumulate(largeColumns.begin(), largeColumns.end(), false, [](auto && resized, auto && c) {
return resized |= c->resize();
})) {
continue;
}
return true;
}
return false;
}
void
ODBC::SelectCommand::execute()
{
SQLRETURN rc = SQLExecute(hStmt);
if (!SQL_SUCCEEDED(rc)) {
throw Error(rc, SQL_HANDLE_STMT, hStmt);
}
SQLSMALLINT colCount;
if (!SQL_SUCCEEDED(rc = SQLNumResultCols(hStmt, &colCount))) {
throw Error(rc, SQL_HANDLE_STMT, hStmt);
}
for (SQLUSMALLINT col = 0; col < colCount; col++) {
std::array<SQLCHAR, 300> _colName {};
SQLSMALLINT nameLen, dp, nullable, bindType;
SQLULEN bindSize;
SQLUSMALLINT sqlcol = col + 1;
// NOLINTNEXTLINE(hicpp-no-array-decay)
if (!SQL_SUCCEEDED(rc = SQLDescribeCol(hStmt, sqlcol, _colName.data(), _colName.size(), &nameLen, &bindType,
&bindSize, &dp, &nullable))) {
throw Error(rc, SQL_HANDLE_STMT, hStmt);
}
Glib::ustring colName(
reinterpret_cast<const char *>(_colName.data()), static_cast<Glib::ustring::size_type>(nameLen));
DB::Column * ncol;
switch (bindType) {
case SQL_DECIMAL:
case SQL_NUMERIC:
case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
ncol = insertColumn(std::make_unique<FloatingPointColumn>(this, colName, col)).get();
break;
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_TINYINT:
case SQL_BIGINT:
ncol = insertColumn(std::make_unique<SignedIntegerColumn>(this, colName, col)).get();
break;
case SQL_TYPE_TIME:
case SQL_INTERVAL_YEAR:
case SQL_INTERVAL_MONTH:
case SQL_INTERVAL_DAY:
case SQL_INTERVAL_HOUR:
case SQL_INTERVAL_MINUTE:
case SQL_INTERVAL_SECOND:
case SQL_INTERVAL_YEAR_TO_MONTH:
case SQL_INTERVAL_DAY_TO_HOUR:
case SQL_INTERVAL_DAY_TO_MINUTE:
case SQL_INTERVAL_DAY_TO_SECOND:
case SQL_INTERVAL_HOUR_TO_MINUTE:
case SQL_INTERVAL_HOUR_TO_SECOND:
case SQL_INTERVAL_MINUTE_TO_SECOND:
ncol = insertColumn(std::make_unique<IntervalColumn>(this, colName, col)).get();
break;
case SQL_TIMESTAMP:
case SQL_DATETIME:
case SQL_TYPE_DATE:
case SQL_TYPE_TIMESTAMP:
ncol = insertColumn(std::make_unique<TimeStampColumn>(this, colName, col)).get();
break;
case SQL_UNKNOWN_TYPE:
throw DB::ColumnTypeNotSupported();
default:
SQLLEN octetSize = 0;
if (!SQL_SUCCEEDED(rc
= SQLColAttribute(hStmt, sqlcol, SQL_DESC_OCTET_LENGTH, nullptr, 0, nullptr, &octetSize))) {
throw Error(rc, SQL_HANDLE_STMT, hStmt);
}
bindSize = static_cast<decltype(bindSize)>(octetSize);
auto lc = std::make_unique<CharArrayColumn>(this, colName, col, bindSize);
largeColumns.insert(lc.get());
ncol = insertColumn(std::move(lc)).get();
break;
};
dynamic_cast<Column *>(ncol)->bind();
}
}
|