forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreparator.cpp
More file actions
executable file
·192 lines (150 loc) · 3.8 KB
/
Preparator.cpp
File metadata and controls
executable file
·192 lines (150 loc) · 3.8 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// Preparator.cpp
//
// $Id: //poco/Main/Data/ODBC/src/Preparator.cpp#5 $
//
// Library: Data
// Package: DataCore
// Module: Preparator
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/ODBC/Preparator.h"
#include "Poco/Data/ODBC/ODBCMetaColumn.h"
#include "Poco/Exception.h"
using Poco::InvalidArgumentException;
namespace Poco {
namespace Data {
namespace ODBC {
Preparator::Preparator(const StatementHandle& rStmt,
const std::string& statement,
std::size_t maxFieldSize,
DataExtraction dataExtraction):
_rStmt(rStmt),
_maxFieldSize(maxFieldSize),
_dataExtraction(dataExtraction)
{
SQLCHAR* pStr = (SQLCHAR*) statement.c_str();
if (Utility::isError(Poco::Data::ODBC::SQLPrepare(_rStmt, pStr, (SQLINTEGER) statement.length())))
throw StatementException(_rStmt);
}
Preparator::Preparator(const Preparator& other):
_rStmt(other._rStmt),
_maxFieldSize(other._maxFieldSize),
_dataExtraction(other._dataExtraction)
{
resize();
}
Preparator::~Preparator()
{
freeMemory();
}
void Preparator::freeMemory() const
{
IndexMap::iterator it = _varLengthArrays.begin();
IndexMap::iterator end = _varLengthArrays.end();
for (; it != end; ++it)
{
switch (it->second)
{
case DT_BOOL:
deleteCachedArray<bool>(it->first);
break;
case DT_CHAR:
deleteCachedArray<char>(it->first);
break;
case DT_UCHAR:
deleteCachedArray<unsigned char>(it->first);
break;
case DT_CHAR_ARRAY:
{
char** pc = AnyCast<char*>(&_values[it->first]);
if (pc) std::free(*pc);
break;
}
case DT_UCHAR_ARRAY:
{
unsigned char** pc = AnyCast<unsigned char*>(&_values[it->first]);
if (pc) std::free(*pc);
break;
}
case DT_BOOL_ARRAY:
{
bool** pb = AnyCast<bool*>(&_values[it->first]);
if (pb) std::free(*pb);
break;
}
default:
throw InvalidArgumentException("Unknown data type.");
}
}
}
std::size_t Preparator::columns() const
{
if (_values.empty()) resize();
return _values.size();
}
void Preparator::resize() const
{
SQLSMALLINT nCol = 0;
if (!Utility::isError(SQLNumResultCols(_rStmt, &nCol)) && 0 != nCol)
{
_values.resize(nCol, 0);
_lengths.resize(nCol, 0);
_lenLengths.resize(nCol);
if(_varLengthArrays.size())
{
freeMemory();
_varLengthArrays.clear();
}
}
}
std::size_t Preparator::maxDataSize(std::size_t pos) const
{
poco_assert_dbg (pos < _values.size());
std::size_t sz = 0;
std::size_t maxsz = getMaxFieldSize();
try
{
ODBCMetaColumn mc(_rStmt, pos);
sz = mc.length();
// accomodate for terminating zero (non-bulk only!)
if (!isBulk() && ODBCMetaColumn::FDT_STRING == mc.type()) ++sz;
}
catch (StatementException&) { }
if (!sz || sz > maxsz) sz = maxsz;
return sz;
}
std::size_t Preparator::actualDataSize(std::size_t col, std::size_t row) const
{
SQLLEN size = (POCO_DATA_INVALID_ROW == row) ? _lengths.at(col) :
_lenLengths.at(col).at(row);
// workaround for drivers returning negative length
if (size < 0 && SQL_NULL_DATA != size) size *= -1;
return size;
}
void Preparator::prepareBoolArray(std::size_t pos, SQLSMALLINT valueType, std::size_t length)
{
poco_assert_dbg (DE_BOUND == _dataExtraction);
poco_assert_dbg (pos < _values.size());
poco_assert_dbg (pos < _lengths.size());
poco_assert_dbg (pos < _lenLengths.size());
bool* pArray = (bool*) std::calloc(length, sizeof(bool));
_values[pos] = Any(pArray);
_lengths[pos] = 0;
_lenLengths[pos].resize(length);
_varLengthArrays.insert(IndexMap::value_type(pos, DT_BOOL_ARRAY));
if (Utility::isError(SQLBindCol(_rStmt,
(SQLUSMALLINT) pos + 1,
valueType,
(SQLPOINTER) pArray,
(SQLINTEGER) sizeof(bool),
&_lenLengths[pos][0])))
{
throw StatementException(_rStmt, "SQLBindCol()");
}
}
} } } // namespace Poco::Data::ODBC