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
|
//
// Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
#include "soci/soci.h"
#include "soci/odbc/soci-odbc.h"
#include "test-context.h"
#include <iostream>
#include <string>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <catch.hpp>
using namespace soci;
using namespace soci::tests;
// A generic version class: we might want to factor it out later if it is
// needed elsewhere (it would probably also need to be renamed to something
// less generic then).
class odbc_version
{
public:
odbc_version()
{
initialized_ = false;
}
odbc_version(unsigned major, unsigned minor, unsigned release)
: major_(major), minor_(minor), release_(release)
{
initialized_ = true;
}
bool init_from_string(char const* s)
{
initialized_ = std::sscanf(s, "%u.%u.%u",
&major_, &minor_, &release_) == 3;
return initialized_;
}
bool is_initialized() const { return initialized_; }
std::string as_string() const
{
if (initialized_)
{
char buf[128];
// This uses the ODBC convention of padding the minor and release
// versions with 0 and might be not appropriate in general.
snprintf(buf, sizeof(buf), "%u.%02u.%04u", major_, minor_, release_);
return buf;
}
else
{
return "(uninitialized)";
}
}
// Compare versions using the lexicographical sort order, with
// uninitialized version considered less than any initialized one.
bool operator<(odbc_version const& v) const
{
if (!initialized_)
return v.initialized_;
return major_ < v.major_ ||
(major_ == v.major_ && (minor_ < v.minor_ ||
(minor_ == v.minor_ && release_ < v.release_)));
}
private:
unsigned major_, minor_, release_;
bool initialized_;
};
std::ostream& operator<<(std::ostream& os, odbc_version const& v)
{
os << v.as_string();
return os;
}
std::string connectString;
backend_factory const &backEnd = *soci::factory_odbc();
// DDL Creation objects for common tests
struct table_creator_one : public table_creator_base
{
table_creator_one(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, val integer, c char, "
"str varchar(20), sh int2, ll bigint, ul numeric(20), "
"d float8, num76 numeric(7,6), "
"tm timestamp, i1 integer, i2 integer, i3 integer, "
"name varchar(20))";
}
};
struct table_creator_two : public table_creator_base
{
table_creator_two(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(num_float float8, num_int integer,"
" name varchar(20), sometime timestamp, chr char)";
}
};
struct table_creator_three : public table_creator_base
{
table_creator_three(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(name varchar(100) not null, "
"phone varchar(15))";
}
};
struct table_creator_for_get_affected_rows : table_creator_base
{
table_creator_for_get_affected_rows(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val integer)";
}
};
struct table_creator_for_xml : table_creator_base
{
table_creator_for_xml(soci::session& sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, x xml)";
}
};
struct table_creator_for_clob : table_creator_base
{
table_creator_for_clob(soci::session& sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, s text)";
}
};
//
// Support for SOCI Common Tests
//
class test_context : public test_context_common
{
public:
test_context() = default;
bool initialize_connect_string(std::string argFromCommandLine) override
{
if (!test_context_base::initialize_connect_string(argFromCommandLine))
return false;
m_verDriver = get_driver_version();
std::cout << "Using ODBC driver version " << m_verDriver << "\n";
return true;
}
std::string get_example_connection_string() const override
{
return "FILEDSN=./test-postgresql.dsn";
}
std::string get_backend_name() const override
{
return "odbc";
}
table_creator_base * table_creator_1(soci::session& s) const override
{
return new table_creator_one(s);
}
table_creator_base * table_creator_2(soci::session& s) const override
{
return new table_creator_two(s);
}
table_creator_base * table_creator_3(soci::session& s) const override
{
return new table_creator_three(s);
}
table_creator_base * table_creator_4(soci::session& s) const override
{
return new table_creator_for_get_affected_rows(s);
}
table_creator_base* table_creator_xml(soci::session& s) const override
{
return new table_creator_for_xml(s);
}
table_creator_base* table_creator_clob(soci::session& s) const override
{
return new table_creator_for_clob(s);
}
bool has_real_xml_support() const override
{
return true;
}
std::string to_date_time(std::string const &datdt_string) const override
{
return "timestamptz(\'" + datdt_string + "\')";
}
bool has_fp_bug() const override
{
// The bug with using insufficiently many digits for double values was
// only fixed in 9.03.0400 version of the ODBC driver (see commit
// a5fed2338b59ae16a2d3a8d2744b084949684775 in its repository), so we
// need to check for its version here.
//
// Be pessimistic if we failed to retrieve the version at all.
return !m_verDriver.is_initialized() || m_verDriver < odbc_version(9, 3, 400);
}
bool has_partial_update_bug() const override
{
// ODBC driver has version-dependent bugs related to handling array
// parameters: after v13.02, it fails to insert anything, see
// https://github.com/postgresql-interfaces/psqlodbc/issues/89, and
// with the previous versions (e.g. v10.03) it's even worse, as it does
// insert the row with valid values but still returns fatal error at
// ODBC level.
//
// So far there is no known version where it works correctly, but if
// the issue above is fixed, we should check for the version including
// this fix here.
return true;
}
std::string fix_crlf_if_necessary(std::string const& s) const override
{
// Version 9.03.0300 (ancient, but still used on AppVeyor CI) is known
// to have a bug which replaces new lines, i.e. LF characters, with CR
// LF when reading CLOBs. Assume it was also fixed in later versions.
if ( m_verDriver.is_initialized() && odbc_version(9, 3, 300) < m_verDriver )
return s;
std::string s2;
s2.reserve(s.size());
for (std::size_t i = 0; i < s.size(); ++i)
{
if (s[i] == '\r')
continue;
s2 += s[i];
}
return s2;
}
std::string sql_length(std::string const& s) const override
{
return "char_length(" + s + ")";
}
private:
odbc_version get_driver_version() const
{
try
{
soci::session sql(get_backend_factory(), get_connect_string());
odbc_session_backend* const
odbc_session = static_cast<odbc_session_backend*>(sql.get_backend());
if (!odbc_session)
{
std::cerr << "Failed to get odbc_session_backend?\n";
return odbc_version();
}
char driver_ver[1024];
SQLSMALLINT len = sizeof(driver_ver);
SQLRETURN rc = SQLGetInfo(odbc_session->hdbc_, SQL_DRIVER_VER,
driver_ver, len, &len);
if (soci::is_odbc_error(rc))
{
std::cerr << "Retrieving ODBC driver version failed: "
<< rc << "\n";
return odbc_version();
}
odbc_version v;
if (!v.init_from_string(driver_ver))
{
std::cerr << "Unknown ODBC driver version format: \""
<< driver_ver << "\"\n";
}
return v;
}
catch ( ... )
{
// Failure getting the version is not fatal.
return odbc_version();
}
}
odbc_version m_verDriver;
};
test_context tc_odbc_postgresql;
|