mysql_ps_resultset_metadata.cpp
13.7 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/C++ is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
MySQL Connectors. There are special exceptions to the terms and
conditions of the GPLv2 as it is applied to this software, see the
FLOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <cppconn/exception.h>
#include "mysql_util.h"
#include "mysql_ps_resultset.h"
#include "mysql_ps_resultset_metadata.h"
#include "nativeapi/native_statement_wrapper.h"
#include "nativeapi/native_resultset_wrapper.h"
#include "mysql_debug.h"
#define MAX_LEN_PER_CHAR 4
namespace sql
{
namespace mysql
{
/* {{{ MySQL_PreparedResultSetMetaData::MySQL_PreparedResultSetMetaData -I- */
MySQL_PreparedResultSetMetaData::MySQL_PreparedResultSetMetaData(boost::shared_ptr< NativeAPI::NativeStatementWrapper > & _proxy,
boost::shared_ptr< MySQL_DebugLogger> & l)
: proxy(_proxy), logger(l), result_meta(_proxy->result_metadata()),
num_fields(_proxy->field_count())
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::MySQL_PreparedResultSetMetaData");
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::~MySQL_PreparedResultSetMetaData -I- */
MySQL_PreparedResultSetMetaData::~MySQL_PreparedResultSetMetaData()
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::~MySQL_PreparedResultSetMetaData");
CPP_INFO_FMT("this=%p", this);
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::checkColumnIndex -I- */
void
MySQL_PreparedResultSetMetaData::checkColumnIndex(unsigned int columnIndex) const
{
if (columnIndex == 0 || columnIndex > num_fields) {
throw sql::InvalidArgumentException("Invalid value for columnIndex");
}
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getCatalogName -I- */
SQLString
MySQL_PreparedResultSetMetaData::getCatalogName(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getCatalogName");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return getFieldMeta(columnIndex)->catalog;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getColumnCount -I- */
unsigned int
MySQL_PreparedResultSetMetaData::getColumnCount()
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getColumnCount");
CPP_INFO_FMT("this=%p", this);
return num_fields;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getColumnDisplaySize -I- */
unsigned int
MySQL_PreparedResultSetMetaData::getColumnDisplaySize(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getColumnDisplaySize");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
const MYSQL_FIELD* const field = getFieldMeta(columnIndex);
const sql::mysql::util::OUR_CHARSET* const cs = sql::mysql::util::find_charset(field->charsetnr);
if (!cs) {
std::ostringstream msg("Server sent uknown charsetnr (");
msg << field->charsetnr << ") . Please report";
throw SQLException(msg.str());
}
int ret = field->length / cs->char_maxlen;
CPP_INFO_FMT("column=%u display_size=%d", columnIndex, ret);
return ret;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getColumnLabel -I- */
SQLString
MySQL_PreparedResultSetMetaData::getColumnLabel(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getColumnLabel");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return getFieldMeta(columnIndex)->name;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getColumnName -I- */
SQLString
MySQL_PreparedResultSetMetaData::getColumnName(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getColumnName");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return getFieldMeta(columnIndex)->name;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getColumnType -I- */
int
MySQL_PreparedResultSetMetaData::getColumnType(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getColumnType");
CPP_INFO_FMT("this=%p", this);
CPP_INFO_FMT("column=%u", columnIndex);
checkColumnIndex(columnIndex);
int mysql_type = getFieldMeta(columnIndex)->type;
CPP_INFO_FMT("type=%d", mysql_type);
int ret = sql::mysql::util::mysql_type_to_datatype(
getFieldMeta(columnIndex)
);
CPP_INFO_FMT("our type is %d", ret);
return ret;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getColumnTypeName -I- */
SQLString
MySQL_PreparedResultSetMetaData::getColumnTypeName(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getColumnTypeName");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return sql::mysql::util::mysql_type_to_string(getFieldMeta(columnIndex), this->logger);
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getFieldMeta -I- */
MYSQL_FIELD* MySQL_PreparedResultSetMetaData::getFieldMeta(unsigned int columnIndex) const
{
return result_meta->fetch_field_direct(columnIndex - 1);
}
/* }}} */
// Precision - total number of digits
/* {{{ MySQL_PreparedResultSetMetaData::getPrecision -I- */
unsigned int
MySQL_PreparedResultSetMetaData::getPrecision(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getPrecision");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
unsigned int ret = getFieldMeta(columnIndex)->max_length - getScale(columnIndex);
CPP_INFO_FMT("column=%u precision=%d", columnIndex, ret);
return ret;
}
/* }}} */
// Scale - Number of digits right of the decimal point
/* {{{ MySQL_PreparedResultSetMetaData::getScale -I- */
unsigned int
MySQL_PreparedResultSetMetaData::getScale(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getScale");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
unsigned int ret = getFieldMeta(columnIndex)->decimals;
CPP_INFO_FMT("column=%u scale=%d", columnIndex, ret);
return ret;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getSchemaName -I- */
SQLString
MySQL_PreparedResultSetMetaData::getSchemaName(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getSchemaName");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
const char* const db = getFieldMeta(columnIndex)->db;
return db ? db : "";
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::getTableName -I- */
SQLString
MySQL_PreparedResultSetMetaData::getTableName(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::getTableName");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return getFieldMeta(columnIndex)->org_table;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isAutoIncrement -I- */
bool
MySQL_PreparedResultSetMetaData::isAutoIncrement(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isAutoIncrement");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return (getFieldMeta(columnIndex)->flags & AUTO_INCREMENT_FLAG) != 0;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isCaseSensitive -I- */
bool
MySQL_PreparedResultSetMetaData::isCaseSensitive(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isCaseSensitive");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
const MYSQL_FIELD* const field = getFieldMeta(columnIndex);
if (field->flags& NUM_FLAG || field->type == MYSQL_TYPE_NEWDECIMAL || field->type == MYSQL_TYPE_DECIMAL) {
return false;
}
const sql::mysql::util::OUR_CHARSET* const cs = sql::mysql::util::find_charset(field->charsetnr);
if (!cs) {
std::ostringstream msg("Server sent uknown charsetnr (");
msg << field->charsetnr << ") . Please report";
throw SQLException(msg.str());
}
return NULL == strstr(cs->collation, "_ci");
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isCurrency -I- */
bool
MySQL_PreparedResultSetMetaData::isCurrency(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isCurrency");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return false;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isDefinitelyWritable -I- */
bool
MySQL_PreparedResultSetMetaData::isDefinitelyWritable(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isDefinitelyWritable");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return isWritable(columnIndex);
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isNullable -I- */
int
MySQL_PreparedResultSetMetaData::isNullable(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isNullable");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return getFieldMeta(columnIndex)->flags & NOT_NULL_FLAG ? columnNoNulls : columnNullable;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isReadOnly -I- */
bool
MySQL_PreparedResultSetMetaData::isReadOnly(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isReadOnly");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
/* Seems for Views, where the value is generated DB is empty everything else is set */
const char* const db = getFieldMeta(columnIndex)->db;
return !(db && strlen(db));
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isSearchable -I- */
bool
MySQL_PreparedResultSetMetaData::isSearchable(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isSearchable");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return true;
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isSigned -I- */
bool
MySQL_PreparedResultSetMetaData::isSigned(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isSigned");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
if (getFieldMeta(columnIndex)->type == FIELD_TYPE_YEAR) {
return false;
}
return !(getFieldMeta(columnIndex)->flags & UNSIGNED_FLAG);
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isWritable -I- */
bool
MySQL_PreparedResultSetMetaData::isWritable(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isWritable");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return !isReadOnly(columnIndex);
}
/* }}} */
/* {{{ MySQL_PreparedResultSetMetaData::isZerofill -I- */
bool
MySQL_PreparedResultSetMetaData::isZerofill(unsigned int columnIndex)
{
CPP_ENTER("MySQL_PreparedResultSetMetaData::isZerofill");
CPP_INFO_FMT("this=%p", this);
checkColumnIndex(columnIndex);
return (getFieldMeta(columnIndex)->flags & ZEROFILL_FLAG) != 0;
}
/* }}} */
} /* namespace mysql */
} /* namespace sql */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/