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
|
//
// 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 <ctime>
#include <cmath>
#include <catch.hpp>
using namespace soci;
using namespace soci::tests;
std::string connectString;
backend_factory const &backEnd = *soci::factory_odbc();
// MS SQL-specific tests
TEST_CASE("MS SQL long string", "[odbc][mssql][long]")
{
soci::session sql(backEnd, connectString);
struct long_text_table_creator : public table_creator_base
{
explicit long_text_table_creator(soci::session& sql)
: table_creator_base(sql)
{
// Notice that 4000 is the maximal length of an nvarchar() column,
// at least when using FreeTDS ODBC driver.
sql << "create table soci_test ("
"long_text nvarchar(max) null, "
"fixed_text nvarchar(4000) null"
")";
}
} long_text_table_creator(sql);
// Build a string at least 8000 characters long to test that it survives
// the round trip unscathed.
std::ostringstream os;
for ( int n = 0; n < 1000; ++n )
{
os << "Line #" << n << "\n";
}
std::string const str_in = os.str();
CHECK_NOTHROW((
sql << "insert into soci_test(long_text) values(:str)", use(str_in)
));
std::string str_out;
sql << "select long_text from soci_test", into(str_out);
// Don't just compare the strings because the error message in case they
// differ is completely unreadable due to their size, so give a better
// error in the common failure case.
if (str_out.length() != str_in.length())
{
FAIL("Read back string of length " << str_out.length() <<
" instead of expected " << str_in.length());
}
else
{
CHECK(str_out == str_in);
}
// The long string should be truncated when inserting it into a fixed size
// column.
CHECK_THROWS_AS(
(sql << "insert into soci_test(fixed_text) values(:str)", use(str_in)),
soci_error
);
}
struct wide_text_table_creator : public table_creator_base
{
explicit wide_text_table_creator(soci::session &sql)
: table_creator_base(sql)
{
sql << "create table soci_test ("
"wide_text nvarchar(40) null"
")";
}
};
TEST_CASE("MS SQL wide string", "[odbc][mssql][wstring]")
{
soci::session sql(backEnd, connectString);
wide_text_table_creator create_wide_text_table(sql);
std::wstring const str_in = L"Привет, SOCI!";
sql << "insert into soci_test(wide_text) values(:str)", use(str_in);
std::wstring str_out;
sql << "select wide_text from soci_test", into(str_out);
CHECK(str_out == str_in);
}
TEST_CASE("MS SQL wide string vector", "[odbc][mssql][vector][wstring]")
{
soci::session sql(backEnd, connectString);
wide_text_table_creator create_wide_text_table(sql);
std::vector<std::wstring> const str_in = {
L"Привет, SOCI!",
L"Привет, World!",
L"Привет, Universe!",
L"Привет, Galaxy!"};
sql << "insert into soci_test(wide_text) values(:str)", use(str_in);
std::vector<std::wstring> str_out(4);
sql << "select wide_text from soci_test", into(str_out);
CHECK(str_out.size() == str_in.size());
for (std::size_t i = 0; i != str_in.size(); ++i)
{
CHECK(str_out[i] == str_in[i]);
}
}
TEST_CASE("MS SQL table records count", "[odbc][mssql][count]")
{
soci::session sql(backEnd, connectString);
// Execute the provided SQL query to count records in tables
std::string sql_query = R"(
SET NOCOUNT ON;
DECLARE db_cursor CURSOR FOR
SELECT name FROM sys.databases
WHERE state_desc = 'ONLINE'
AND name IN ('master');
DECLARE @DatabaseName NVARCHAR(128);
DECLARE @outset TABLE(
INSTANCENAME varchar(50),
DATABASENAME varchar(100),
TABLENAME varchar(100),
NUMBEROFRECORDS_I bigint
);
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @DatabaseName;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @command nvarchar(1000) = 'USE ' + QUOTENAME(@DatabaseName) +
'; SELECT @@SERVERNAME, DB_NAME(), T.NAME, P.[ROWS] FROM sys.tables T ' +
'INNER JOIN sys.indexes I ON T.OBJECT_ID = I.OBJECT_ID ' +
'INNER JOIN sys.partitions P ON I.OBJECT_ID = P.OBJECT_ID AND I.INDEX_ID = P.INDEX_ID ' +
'INNER JOIN sys.allocation_units A ON P.PARTITION_ID = A.CONTAINER_ID ' +
'WHERE T.NAME NOT LIKE ''DT%'' AND I.OBJECT_ID > 255 AND I.INDEX_ID <= 1 ' +
'GROUP BY T.NAME, I.OBJECT_ID, I.INDEX_ID, I.NAME, P.[ROWS] ' +
'ORDER BY OBJECT_NAME(I.OBJECT_ID)';
INSERT INTO @outset EXEC (@command)
FETCH NEXT FROM db_cursor INTO @DatabaseName
END
CLOSE db_cursor
DEALLOCATE db_cursor
SELECT INSTANCENAME, DATABASENAME, TABLENAME, NUMBEROFRECORDS_I
FROM @outset;
)";
soci::rowset<soci::row> rs = (sql.prepare << sql_query);
// Check that we can access the results.
for (auto it = rs.begin(); it != rs.end(); ++it)
{
soci::row const& row = *it;
std::string instance_name = row.get<std::string>(0);
std::string database_name = row.get<std::string>(1);
std::string table_name = row.get<std::string>(2);
long long number_of_records = row.get<long long>(3);
// Use the variables above to avoid warnings about unused variables and
// check the only one of them that we can be sure about because we have
// "name IN ('master')" in the SQL query above.
INFO("Table " << instance_name << "." << table_name <<
" has " << number_of_records << " records");
CHECK( database_name == "master" );
return;
}
FAIL("No tables found in the master database");
}
// 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 smallint, ll bigint, ul numeric(20), "
"d float, num76 numeric(7,6), "
"tm datetime, 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 float, num_int integer,"
" name varchar(20), sometime datetime, 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_clob : table_creator_base
{
table_creator_for_clob(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(id integer, s text)";
}
};
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_get_last_insert_id : table_creator_base
{
table_creator_for_get_last_insert_id(soci::session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test (id integer identity(1, 1), val integer)";
}
};
//
// Support for SOCI Common Tests
//
class test_context : public test_context_common
{
public:
test_context() = default;
std::string get_example_connection_string() const override
{
return "FILEDSN=./test-mssql.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);
}
tests::table_creator_base* table_creator_clob(soci::session& s) const override
{
return new table_creator_for_clob(s);
}
tests::table_creator_base* table_creator_xml(soci::session& s) const override
{
return new table_creator_for_xml(s);
}
tests::table_creator_base* table_creator_get_last_insert_id(soci::session& s) const override
{
return new table_creator_for_get_last_insert_id(s);
}
bool has_real_xml_support() const override
{
return true;
}
std::string to_date_time(std::string const &datdt_string) const override
{
return "convert(datetime, \'" + datdt_string + "\', 120)";
}
bool has_multiple_select_bug() const override
{
// MS SQL does support MARS (multiple active result sets) since 2005
// version, but this support needs to be explicitly enabled and is not
// implemented in FreeTDS ODBC driver used under Unix currently, so err
// on the side of caution and suppose that it's not supported.
return true;
}
std::string sql_length(std::string const& s) const override
{
return "len(" + s + ")";
}
};
test_context tc_odbc_mssql;
|