Skip to content

Commit 2a81e59

Browse files
committed
MySQL DateTime support, samples Linux compile
1 parent 245dc64 commit 2a81e59

19 files changed

Lines changed: 6905 additions & 14 deletions

Data/MySQL/src/ResultMetadata.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ namespace
130130
case MYSQL_TYPE_LONGLONG:
131131
if (unsig) return Poco::Data::MetaColumn::FDT_UINT64;
132132
return Poco::Data::MetaColumn::FDT_INT64;
133-
133+
case MYSQL_TYPE_DATE:
134+
case MYSQL_TYPE_TIME:
135+
case MYSQL_TYPE_DATETIME:
136+
return Poco::Data::MetaColumn::FDT_TIMESTAMP;
137+
134138
case MYSQL_TYPE_STRING:
135139
case MYSQL_TYPE_VAR_STRING:
136140
return Poco::Data::MetaColumn::FDT_STRING;

Data/MySQL/src/ResultMetadata.cpp~

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
//
2+
// MySQLException.cpp
3+
//
4+
// $Id: //poco/1.4/Data/MySQL/src/ResultMetadata.cpp#1 $
5+
//
6+
// Library: Data
7+
// Package: MySQL
8+
// Module: ResultMetadata
9+
//
10+
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
11+
// and Contributors.
12+
//
13+
// Permission is hereby granted, free of charge, to any person or organization
14+
// obtaining a copy of the software and accompanying documentation covered by
15+
// this license (the "Software") to use, reproduce, display, distribute,
16+
// execute, and transmit the Software, and to prepare derivative works of the
17+
// Software, and to permit third-parties to whom the Software is furnished to
18+
// do so, all subject to the following:
19+
//
20+
// The copyright notices in the Software and this entire statement, including
21+
// the above license grant, this restriction and the following disclaimer,
22+
// must be included in all copies of the Software, in whole or in part, and
23+
// all derivative works of the Software, unless such copies or derivative
24+
// works are solely in the form of machine-executable object code generated by
25+
// a source language processor.
26+
//
27+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33+
// DEALINGS IN THE SOFTWARE.
34+
//
35+
36+
37+
#include "Poco/Data/MySQL/ResultMetadata.h"
38+
#include "Poco/Data/MySQL/MySQLException.h"
39+
#include <cstring>
40+
41+
namespace
42+
{
43+
class ResultMetadataHandle
44+
/// Simple exception-safe wrapper
45+
{
46+
public:
47+
48+
explicit ResultMetadataHandle(MYSQL_STMT* stmt)
49+
{
50+
h = mysql_stmt_result_metadata(stmt);
51+
}
52+
53+
~ResultMetadataHandle()
54+
{
55+
if (h)
56+
{
57+
mysql_free_result(h);
58+
}
59+
}
60+
61+
operator MYSQL_RES* ()
62+
{
63+
return h;
64+
}
65+
66+
private:
67+
68+
MYSQL_RES* h;
69+
};
70+
71+
std::size_t fieldSize(const MYSQL_FIELD& field)
72+
/// Convert field MySQL-type and field MySQL-length to actual field length
73+
{
74+
switch (field.type)
75+
{
76+
case MYSQL_TYPE_TINY: return sizeof(char);
77+
case MYSQL_TYPE_SHORT: return sizeof(short);
78+
case MYSQL_TYPE_INT24:
79+
case MYSQL_TYPE_LONG: return sizeof(Poco::Int32);
80+
case MYSQL_TYPE_FLOAT: return sizeof(float);
81+
case MYSQL_TYPE_DOUBLE: return sizeof(double);
82+
case MYSQL_TYPE_LONGLONG: return sizeof(Poco::Int64);
83+
84+
case MYSQL_TYPE_DATE:
85+
case MYSQL_TYPE_TIME:
86+
case MYSQL_TYPE_DATETIME:
87+
return sizeof(MYSQL_TIME);
88+
89+
case MYSQL_TYPE_DECIMAL:
90+
case MYSQL_TYPE_NEWDECIMAL:
91+
case MYSQL_TYPE_STRING:
92+
case MYSQL_TYPE_VAR_STRING:
93+
case MYSQL_TYPE_TINY_BLOB:
94+
case MYSQL_TYPE_MEDIUM_BLOB:
95+
case MYSQL_TYPE_LONG_BLOB:
96+
case MYSQL_TYPE_BLOB:
97+
return field.length;
98+
99+
default:
100+
throw Poco::Data::MySQL::StatementException("unknown field type");
101+
}
102+
}
103+
104+
Poco::Data::MetaColumn::ColumnDataType fieldType(const MYSQL_FIELD& field)
105+
/// Convert field MySQL-type to Poco-type
106+
{
107+
bool unsig = ((field.flags & UNSIGNED_FLAG) == UNSIGNED_FLAG);
108+
109+
switch (field.type)
110+
{
111+
case MYSQL_TYPE_TINY:
112+
if (unsig) return Poco::Data::MetaColumn::FDT_UINT8;
113+
return Poco::Data::MetaColumn::FDT_INT8;
114+
115+
case MYSQL_TYPE_SHORT:
116+
if (unsig) return Poco::Data::MetaColumn::FDT_UINT16;
117+
return Poco::Data::MetaColumn::FDT_INT16;
118+
119+
case MYSQL_TYPE_INT24:
120+
case MYSQL_TYPE_LONG:
121+
if (unsig) return Poco::Data::MetaColumn::FDT_UINT32;
122+
return Poco::Data::MetaColumn::FDT_INT32;
123+
124+
case MYSQL_TYPE_FLOAT:
125+
return Poco::Data::MetaColumn::FDT_FLOAT;
126+
127+
case MYSQL_TYPE_DOUBLE:
128+
return Poco::Data::MetaColumn::FDT_DOUBLE;
129+
130+
case MYSQL_TYPE_LONGLONG:
131+
if (unsig) return Poco::Data::MetaColumn::FDT_UINT64;
132+
return Poco::Data::MetaColumn::FDT_INT64;
133+
134+
case MYSQL_TYPE_STRING:
135+
case MYSQL_TYPE_VAR_STRING:
136+
return Poco::Data::MetaColumn::FDT_STRING;
137+
138+
case MYSQL_TYPE_TINY_BLOB:
139+
case MYSQL_TYPE_MEDIUM_BLOB:
140+
case MYSQL_TYPE_LONG_BLOB:
141+
case MYSQL_TYPE_BLOB:
142+
return Poco::Data::MetaColumn::FDT_BLOB;
143+
default:
144+
return Poco::Data::MetaColumn::FDT_UNKNOWN;
145+
}
146+
}
147+
} // namespace
148+
149+
150+
namespace Poco {
151+
namespace Data {
152+
namespace MySQL {
153+
154+
void ResultMetadata::reset()
155+
{
156+
_columns.resize(0);
157+
_row.resize(0);
158+
_buffer.resize(0);
159+
_lengths.resize(0);
160+
_isNull.resize(0);
161+
}
162+
163+
void ResultMetadata::init(MYSQL_STMT* stmt)
164+
{
165+
ResultMetadataHandle h(stmt);
166+
167+
if (!h)
168+
{
169+
// all right, it is normal
170+
// querys such an "INSERT INTO" just does not have result at all
171+
reset();
172+
return;
173+
}
174+
175+
std::size_t count = mysql_num_fields(h);
176+
MYSQL_FIELD* fields = mysql_fetch_fields(h);
177+
178+
std::size_t commonSize = 0;
179+
_columns.reserve(count);
180+
181+
{for (std::size_t i = 0; i < count; i++)
182+
{
183+
_columns.push_back(MetaColumn(
184+
i, // position
185+
fields[i].name, // name
186+
fieldType(fields[i]), // type
187+
fieldSize(fields[i]), // length
188+
0, // TODO: precision
189+
!IS_NOT_NULL(fields[i].flags) // nullable
190+
));
191+
192+
commonSize += _columns[i].length();
193+
}}
194+
195+
_buffer.resize(commonSize);
196+
_row.resize(count);
197+
_lengths.resize(count);
198+
_isNull.resize(count);
199+
200+
std::size_t offset = 0;
201+
202+
{for (std::size_t i = 0; i < count; i++)
203+
{
204+
std::memset(&_row[i], 0, sizeof(MYSQL_BIND));
205+
206+
_row[i].buffer_type = fields[i].type;
207+
_row[i].buffer_length = static_cast<unsigned int>(_columns[i].length());
208+
_row[i].buffer = &_buffer[0] + offset;
209+
_row[i].length = &_lengths[i];
210+
_row[i].is_null = &_isNull[i];
211+
212+
offset += _row[i].buffer_length;
213+
}}
214+
}
215+
216+
std::size_t ResultMetadata::columnsReturned() const
217+
{
218+
return static_cast<std::size_t>(_columns.size());
219+
}
220+
221+
const MetaColumn& ResultMetadata::metaColumn(std::size_t pos) const
222+
{
223+
return _columns[pos];
224+
}
225+
226+
MYSQL_BIND* ResultMetadata::row()
227+
{
228+
return &_row[0];
229+
}
230+
231+
std::size_t ResultMetadata::length(std::size_t pos) const
232+
{
233+
return _lengths[pos];
234+
}
235+
236+
const unsigned char* ResultMetadata::rawData(std::size_t pos) const
237+
{
238+
return reinterpret_cast<const unsigned char*>(_row[pos].buffer);
239+
}
240+
241+
bool ResultMetadata::isNull(std::size_t pos) const
242+
{
243+
return (_isNull[pos] != 0);
244+
}
245+
246+
}}} // namespace Poco::Data::MySQL

Data/MySQL/src/SessionImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// MySQLException.cpp
2+
// SessionImpl.cpp
33
//
44
// $Id: //poco/1.4/Data/MySQL/src/SessionImpl.cpp#1 $
55
//
@@ -67,7 +67,7 @@ const std::string SessionImpl::MYSQL_SERIALIZABLE = "SERIALIZABLE";
6767

6868

6969
SessionImpl::SessionImpl(const std::string& connectionString, std::size_t loginTimeout) :
70-
Poco::Data::AbstractSessionImpl<SessionImpl>(toLower(connectionString), loginTimeout),
70+
Poco::Data::AbstractSessionImpl<SessionImpl>(connectionString, loginTimeout),
7171
_handle(0),
7272
_connected(false),
7373
_inTransaction(false)

0 commit comments

Comments
 (0)