forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cpp
More file actions
161 lines (132 loc) · 3.22 KB
/
Copy pathUtility.cpp
File metadata and controls
161 lines (132 loc) · 3.22 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
//
// Utility.cpp
//
// $Id: //poco/Main/Data/ODBC/src/Utility.cpp#3 $
//
// Library: ODBC
// Package: ODBC
// Module: Utility
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/ODBC/Utility.h"
#include "Poco/Data/ODBC/Handle.h"
#include "Poco/Data/ODBC/ODBCException.h"
#include "Poco/NumberFormatter.h"
#include "Poco/DateTime.h"
#include <cmath>
namespace Poco {
namespace Data {
namespace ODBC {
const TypeInfo Utility::_dataTypes;
Utility::DriverMap& Utility::drivers(Utility::DriverMap& driverMap)
{
static const EnvironmentHandle henv;
const int length = sizeof(SQLCHAR) * 512;
SQLCHAR desc[length];
std::memset(desc, 0, length);
SQLSMALLINT len1 = length;
SQLCHAR attr[length];
std::memset(attr, 0, length);
SQLSMALLINT len2 = length;
RETCODE rc = 0;
if (!Utility::isError(rc = SQLDrivers(henv,
SQL_FETCH_FIRST,
desc,
length,
&len1,
attr,
len2,
&len2)))
{
do
{
driverMap.insert(DSNMap::value_type(std::string((char *) desc),
std::string((char *) attr)));
std::memset(desc, 0, length);
std::memset(attr, 0, length);
len2 = length;
}while (!Utility::isError(rc = SQLDrivers(henv,
SQL_FETCH_NEXT,
desc,
length,
&len1,
attr,
len2,
&len2)));
}
if (SQL_NO_DATA != rc)
throw EnvironmentException(henv);
return driverMap;
}
Utility::DSNMap& Utility::dataSources(Utility::DSNMap& dsnMap)
{
static const EnvironmentHandle henv;
const int length = sizeof(SQLCHAR) * 512;
const int dsnLength = sizeof(SQLCHAR) * (SQL_MAX_DSN_LENGTH + 1);
SQLCHAR dsn[dsnLength];
std::memset(dsn, 0, dsnLength);
SQLSMALLINT len1 = sizeof(SQLCHAR) * SQL_MAX_DSN_LENGTH;
SQLCHAR desc[length];
std::memset(desc, 0, length);
SQLSMALLINT len2 = length;
RETCODE rc = 0;
while (!Utility::isError(rc = Poco::Data::ODBC::SQLDataSources(henv,
SQL_FETCH_NEXT,
dsn,
SQL_MAX_DSN_LENGTH,
&len1,
desc,
len2,
&len2)))
{
dsnMap.insert(DSNMap::value_type(std::string((char *) dsn), std::string((char *) desc)));
std::memset(dsn, 0, dsnLength);
std::memset(desc, 0, length);
len2 = length;
}
if (SQL_NO_DATA != rc)
throw EnvironmentException(henv);
return dsnMap;
}
void Utility::dateTimeSync(Poco::DateTime& dt, const SQL_TIMESTAMP_STRUCT& ts)
{
double msec = ts.fraction/1000000;
double usec = 1000 * (msec - std::floor(msec));
dt.assign(ts.year,
ts.month,
ts.day,
ts.hour,
ts.minute,
ts.second,
(int) std::floor(msec),
(int) std::floor(usec));
}
void Utility::dateSync(SQL_DATE_STRUCT& ds, const Date& d)
{
ds.year = d.year();
ds.month = d.month();
ds.day = d.day();
}
void Utility::timeSync(SQL_TIME_STRUCT& ts, const Time& t)
{
ts.hour = t.hour();
ts.minute = t.minute();
ts.second = t.second();
}
void Utility::dateTimeSync(SQL_TIMESTAMP_STRUCT& ts, const Poco::DateTime& dt)
{
ts.year = dt.year();
ts.month = dt.month();
ts.day = dt.day();
ts.hour = dt.hour();
ts.minute = dt.minute();
ts.second = dt.second();
// Fraction support is limited to milliseconds due to MS SQL Server limitation
// see http://support.microsoft.com/kb/263872
ts.fraction = (dt.millisecond() * 1000000);// + (dt.microsecond() * 1000);
}
} } } // namespace Poco::Data::ODBC