From c6aef12b1e959c99d424ef3dcd6d6f74785390d2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 20 Jul 2017 19:25:28 +0200 Subject: [PATCH] Add test checking string length when using bulk insert This used to be broken, so add a test to check that it works after the changes of the last commit and to avoid breaking it again. --- tests/common-tests.h | 55 ++++++++++++++++++++++++++++ tests/db2/test-db2.cpp | 5 +++ tests/firebird/test-firebird.cpp | 5 +++ tests/mysql/test-mysql.h | 5 +++ tests/odbc/test-odbc-access.cpp | 5 +++ tests/odbc/test-odbc-db2.cpp | 5 +++ tests/odbc/test-odbc-mssql.cpp | 5 +++ tests/odbc/test-odbc-postgresql.cpp | 5 +++ tests/oracle/test-oracle.cpp | 5 +++ tests/postgresql/test-postgresql.cpp | 5 +++ tests/sqlite3/test-sqlite3.cpp | 5 +++ 11 files changed, 105 insertions(+) diff --git a/tests/common-tests.h b/tests/common-tests.h index 0cf0a3774..4037e9cc2 100644 --- a/tests/common-tests.h +++ b/tests/common-tests.h @@ -352,6 +352,11 @@ class test_context_base // whatever we do. virtual bool enable_std_char_padding(session&) const { return true; } + // Return the name of the function for determining the length of a string, + // i.e. "char_length" in standard SQL but often "len" or "length" in + // practice. + virtual std::string get_length_function_name() const = 0; + virtual ~test_context_base() { the_test_context_ = NULL; @@ -4205,6 +4210,56 @@ TEST_CASE_METHOD(common_tests, "Select without table", "[core][select][dummy_fro CHECK(plus17 == 17); } +TEST_CASE_METHOD(common_tests, "String length", "[core][string][length]") +{ + soci::session sql(backEndFactory_, connectString_); + + auto_table_creator tableCreator(tc_.table_creator_1(sql)); + + std::string s("123"); + sql << "insert into soci_test(str) values(:s)", use(s); + + const std::string& len_func = tc_.get_length_function_name(); + + std::string sout; + size_t slen; + sql << "select str," + len_func + "(str)" + " from soci_test", + into(sout), into(slen); + CHECK(slen == 3); + CHECK(sout.length() == 3); + CHECK(sout == s); + + sql << "delete from soci_test"; + + + std::vector v; + v.push_back("Hello"); + v.push_back(""); + v.push_back("whole of varchar(20)"); + + CHECK_NOTHROW( (sql << "insert into soci_test(str) values(:s)", use(v)) ); + + std::vector vout(10); + std::vector vlen(10); + sql << "select str," + len_func + "(str)" + " from soci_test" + " order by " + len_func + "(str)", + into(vout), into(vlen); + + REQUIRE(vout.size() == 3); + REQUIRE(vlen.size() == 3); + + CHECK(vlen[0] == 0); + CHECK(vout[0].length() == 0); + + CHECK(vlen[1] == 5); + CHECK(vout[1].length() == 5); + + CHECK(vlen[2] == 20); + CHECK(vout[2].length() == 20); +} + } // namespace test_cases } // namespace tests diff --git a/tests/db2/test-db2.cpp b/tests/db2/test-db2.cpp index 9d73cc965..d7d0d53d5 100644 --- a/tests/db2/test-db2.cpp +++ b/tests/db2/test-db2.cpp @@ -95,6 +95,11 @@ class test_context :public test_context_base { return "to_date('" + pi_datdt_string + "', 'YYYY-MM-DD HH24:MI:SS')"; } + + virtual std::string get_length_function_name() const + { + return "length"; + } }; diff --git a/tests/firebird/test-firebird.cpp b/tests/firebird/test-firebird.cpp index ff941e8a4..0e8804c7c 100644 --- a/tests/firebird/test-firebird.cpp +++ b/tests/firebird/test-firebird.cpp @@ -1311,6 +1311,11 @@ class test_context : public tests::test_context_base { sql.commit(); } + + virtual std::string get_length_function_name() const + { + return "char_length"; + } }; diff --git a/tests/mysql/test-mysql.h b/tests/mysql/test-mysql.h index 32037d0b0..c4b01aa1d 100644 --- a/tests/mysql/test-mysql.h +++ b/tests/mysql/test-mysql.h @@ -127,6 +127,11 @@ class test_context : public test_context_base return false; } } + + virtual std::string get_length_function_name() const + { + return "char_length"; + } }; #endif // SOCI_TESTS_MYSQL_H_INCLUDED diff --git a/tests/odbc/test-odbc-access.cpp b/tests/odbc/test-odbc-access.cpp index d46c55600..78c343868 100644 --- a/tests/odbc/test-odbc-access.cpp +++ b/tests/odbc/test-odbc-access.cpp @@ -107,6 +107,11 @@ test_context(backend_factory const &backEnd, std::string const &connectString) { return "#" + datdt_string + "#"; } + + virtual std::string get_length_function_name() const + { + return "len"; + } }; int main(int argc, char** argv) diff --git a/tests/odbc/test-odbc-db2.cpp b/tests/odbc/test-odbc-db2.cpp index 598ff32ec..fa1e92d20 100644 --- a/tests/odbc/test-odbc-db2.cpp +++ b/tests/odbc/test-odbc-db2.cpp @@ -93,6 +93,11 @@ class test_context : public test_context_base { return "\'" + datdt_string + "\'"; } + + virtual std::string get_length_function_name() const + { + return "length"; + } }; struct table_creator_bigint : table_creator_base diff --git a/tests/odbc/test-odbc-mssql.cpp b/tests/odbc/test-odbc-mssql.cpp index f4511c9eb..483be787c 100644 --- a/tests/odbc/test-odbc-mssql.cpp +++ b/tests/odbc/test-odbc-mssql.cpp @@ -160,6 +160,11 @@ class test_context : public test_context_base // on the side of caution and suppose that it's not supported. return true; } + + virtual std::string get_length_function_name() const + { + return "len"; + } }; int main(int argc, char** argv) diff --git a/tests/odbc/test-odbc-postgresql.cpp b/tests/odbc/test-odbc-postgresql.cpp index 6ff33ec45..55ace776a 100644 --- a/tests/odbc/test-odbc-postgresql.cpp +++ b/tests/odbc/test-odbc-postgresql.cpp @@ -179,6 +179,11 @@ class test_context : public test_context_base return !m_verDriver.is_initialized() || m_verDriver < odbc_version(9, 3, 400); } + virtual std::string get_length_function_name() const + { + return "char_length"; + } + private: odbc_version get_driver_version() const { diff --git a/tests/oracle/test-oracle.cpp b/tests/oracle/test-oracle.cpp index c436a520b..228023d9c 100644 --- a/tests/oracle/test-oracle.cpp +++ b/tests/oracle/test-oracle.cpp @@ -1555,6 +1555,11 @@ class test_context :public test_context_base { return "to_date('" + datdt_string + "', 'YYYY-MM-DD HH24:MI:SS')"; } + + virtual std::string get_length_function_name() const + { + return "length"; + } }; int main(int argc, char** argv) diff --git a/tests/postgresql/test-postgresql.cpp b/tests/postgresql/test-postgresql.cpp index 6f831ec20..9d7cded1b 100644 --- a/tests/postgresql/test-postgresql.cpp +++ b/tests/postgresql/test-postgresql.cpp @@ -1172,6 +1172,11 @@ class test_context : public test_context_base { return false; } + + virtual std::string get_length_function_name() const + { + return "char_length"; + } }; int main(int argc, char** argv) diff --git a/tests/sqlite3/test-sqlite3.cpp b/tests/sqlite3/test-sqlite3.cpp index 349c80972..f6dadf31c 100644 --- a/tests/sqlite3/test-sqlite3.cpp +++ b/tests/sqlite3/test-sqlite3.cpp @@ -379,6 +379,11 @@ class test_context : public test_context_base // SQLite does not support right padded char type. return false; } + + virtual std::string get_length_function_name() const + { + return "length"; + } }; int main(int argc, char** argv)