test-odbc-mssql.cpp 5.71 KB
Newer Older
1 2 3 4 5 6 7
//
// 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
// http://www.boost.org/LICENSE_1_0.txt)
//

8 9
#include "soci/soci.h"
#include "soci/odbc/soci-odbc.h"
10
#include "common-tests.h"
11 12 13 14 15
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>

16 17
using namespace soci;
using namespace soci::tests;
18 19

std::string connectString;
20
backend_factory const &backEnd = *soci::factory_odbc();
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
// MS SQL-specific tests
TEST_CASE("MS SQL long string", "[odbc][mssql][long]")
{
    session sql(backEnd, connectString);

    struct long_text_table_creator : public table_creator_base
    {
        explicit long_text_table_creator(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();
    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
    );
}

76
// DDL Creation objects for common tests
msobczak's avatar
msobczak committed
77
struct table_creator_one : public table_creator_base
78
{
79
    table_creator_one(soci::session & sql)
msobczak's avatar
msobczak committed
80
        : table_creator_base(sql)
81
    {
msobczak's avatar
msobczak committed
82
        sql << "create table soci_test(id integer, val integer, c char, "
83
                 "str varchar(20), sh smallint, ul numeric(20), d float, "
84
                 "num76 numeric(7,6), "
85
                 "tm datetime, i1 integer, i2 integer, i3 integer, "
86
                 "name varchar(20))";
87
    }
88
};
89

msobczak's avatar
msobczak committed
90
struct table_creator_two : public table_creator_base
91
{
92
    table_creator_two(soci::session & sql)
msobczak's avatar
msobczak committed
93
        : table_creator_base(sql)
94
    {
msobczak's avatar
msobczak committed
95
        sql  << "create table soci_test(num_float float, num_int integer,"
96
                     " name varchar(20), sometime datetime, chr char)";
97
    }
98
};
99

msobczak's avatar
msobczak committed
100
struct table_creator_three : public table_creator_base
101
{
102
    table_creator_three(soci::session & sql)
msobczak's avatar
msobczak committed
103
        : table_creator_base(sql)
104
    {
msobczak's avatar
msobczak committed
105
        sql << "create table soci_test(name varchar(100) not null, "
106
            "phone varchar(15))";
107
    }
108
};
109

110 111
struct table_creator_for_get_affected_rows : table_creator_base
{
112
    table_creator_for_get_affected_rows(soci::session & sql)
113 114 115 116 117 118
        : table_creator_base(sql)
    {
        sql << "create table soci_test(val integer)";
    }
};

119 120 121 122
//
// Support for SOCI Common Tests
//

msobczak's avatar
msobczak committed
123
class test_context : public test_context_base
124
{
125
public:
msobczak's avatar
msobczak committed
126
    test_context(backend_factory const &backEnd,
127
                std::string const &connectString)
128
        : test_context_base(backEnd, connectString) {}
129

130
    table_creator_base* table_creator_1(soci::session& s) const
131
    {
msobczak's avatar
msobczak committed
132
        return new table_creator_one(s);
133 134
    }

135
    table_creator_base* table_creator_2(soci::session& s) const
136
    {
msobczak's avatar
msobczak committed
137
        return new table_creator_two(s);
138 139
    }

140
    table_creator_base* table_creator_3(soci::session& s) const
141
    {
msobczak's avatar
msobczak committed
142
        return new table_creator_three(s);
143 144
    }

145
    table_creator_base * table_creator_4(soci::session& s) const
146 147 148 149
    {
        return new table_creator_for_get_affected_rows(s);
    }

150
    std::string to_date_time(std::string const &datdt_string) const
151
    {
152
        return "convert(datetime, \'" + datdt_string + "\', 120)";
153
    }
154 155 156 157 158 159 160 161 162

    virtual bool has_multiple_select_bug() const
    {
        // 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;
    }
163

164
    virtual std::string sql_length(std::string const& s) const
165
    {
166
        return "len(" + s + ")";
167
    }
168
};
169 170 171

int main(int argc, char** argv)
{
172 173 174
#ifdef _MSC_VER
    // Redirect errors, unrecoverable problems, and assert() failures to STDERR,
    // instead of debug message window.
175
    // This hack is required to run assert()-driven tests by Buildbot.
176 177 178 179 180
    // NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER

181
    if (argc >= 2 && argv[1][0] != '-')
182 183
    {
        connectString = argv[1];
184 185 186 187 188 189 190

        // Replace the connect string with the process name to ensure that
        // CATCH uses the correct name in its messages.
        argv[1] = argv[0];

        argc--;
        argv++;
191 192 193 194 195
    }
    else
    {
        connectString = "FILEDSN=./test-mssql.dsn";
    }
196

197
    test_context tc(backEnd, connectString);
198

199
    return Catch::Session().run(argc, argv);
200
}