Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.

Fixed retrieving long strings from MySQL #212

Merged
merged 6 commits into from
Nov 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ before_script:
- if [[ "$DB" == "mysql" || "$DB" == "mariadb" ]]; then sudo odbcinst -i -d -f /usr/share/libmyodbc/odbcinst.ini; fi
- if [[ "$DB" == "mysql" || "$DB" == "mariadb" ]]; then mysql -e "DROP DATABASE IF EXISTS nanodbc_tests; CREATE DATABASE IF NOT EXISTS nanodbc_tests;" -uroot; fi
- if [[ "$DB" == "mysql" || "$DB" == "mariadb" ]]; then mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';" -uroot; fi
- if [[ "$DB" == "mysql" || "$DB" == "mariadb" ]]; then export NANODBC_TEST_CONNSTR_MYSQL="Driver=MySQL;Server=localhost;Database=nanodbc_tests;User=root;Password=;Option=3;"; fi
- if [[ "$DB" == "mysql" || "$DB" == "mariadb" ]]; then export NANODBC_TEST_CONNSTR_MYSQL="Driver=MySQL;Server=localhost;Database=nanodbc_tests;User=root;Password=;Option=3;big_packets=1;"; fi
- if [[ "$DB" == "postgresql" ]]; then sudo odbcinst -i -d -f /usr/share/psqlodbc/odbcinst.ini.template; fi
- if [[ "$DB" == "postgresql" ]]; then psql -c "CREATE DATABASE nanodbc_tests;" -U postgres; fi
- if [[ "$DB" == "postgresql" ]]; then export NANODBC_TEST_CONNSTR_PGSQL="DRIVER={PostgreSQL ANSI};Server=localhost;Port=5432;Database=nanodbc_tests;UID=postgres;"; fi
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test_script:
$env:NANODBC_TEST_CONNSTR_MSSQL="Driver={ODBC Driver 11 for SQL Server};Server=(local)\SQL2016;Database=master;UID=sa;PWD=Password12!;"
$test_name = "mssql_test"
} elseif ($env:DB -Match "MySQL") {
$env:NANODBC_TEST_CONNSTR_MYSQL="Driver={MySQL ODBC 5.3 ANSI Driver};Server=127.0.0.1;Database=nanodbc_test;User=root;Password=Password12!;"
$env:NANODBC_TEST_CONNSTR_MYSQL="Driver={MySQL ODBC 5.3 ANSI Driver};Server=127.0.0.1;Database=nanodbc_test;User=root;Password=Password12!;big_packets=1;"
$test_name = "mysql_test"
} elseif ($env:DB -Match "PostgreSQL") {
$env:NANODBC_TEST_CONNSTR_PGSQL="Driver={PostgreSQL ANSI(x64)};Server=127.0.0.1;Port=5432;Database=nanodbc_test;Uid=postgres;Pwd=Password12!;"
Expand Down
7 changes: 6 additions & 1 deletion src/nanodbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
#define SQL_SS_UDT (-151) // from sqlncli.h
#endif

#ifndef SQL_NVARCHAR
#define SQL_NVARCHAR (-10)
#endif

// Default to ODBC version defined by NANODBC_ODBC_VERSION if provided.
#ifndef NANODBC_ODBC_VERSION
#ifdef SQL_OV_ODBC3_80
Expand Down Expand Up @@ -2489,6 +2493,7 @@ class result::result_impl
break;
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_NVARCHAR:
col.ctype_ = SQL_C_CHAR;
col.clen_ = (col.sqlsize_ + 1) * sizeof(SQLCHAR);
if (is_blob)
Expand Down Expand Up @@ -2642,7 +2647,7 @@ inline void result::result_impl::get_ref_impl<string_type>(short column, string_
{
bound_column& col = bound_columns_[column];
const SQLULEN column_size = col.sqlsize_;

switch (col.ctype_)
{
case SQL_C_CHAR:
Expand Down
26 changes: 26 additions & 0 deletions test/mysql_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "test/base_test_fixture.h"
#include <cstdio>
#include <cstdlib>
#include <string>

namespace
{
Expand Down Expand Up @@ -59,11 +60,36 @@ TEST_CASE_METHOD(mysql_fixture, "test_affected_rows", "[mysql][affected_rows]")
auto result = execute(conn, NANODBC_TEXT("DELETE FROM nanodbc_test_temp_table"));
REQUIRE(result.affected_rows() == 2);
}
// Inseting/retrieving long strings
{
nanodbc::string_type long_string(1024, '\0');
for (unsigned i=0; i<1024; i++)
long_string[i]=(i%64)+32;

nanodbc::result result;
result = execute(conn, NANODBC_TEXT("CREATE TABLE nanodbc_longstring (t TEXT NOT NULL)"));
REQUIRE(result.affected_rows() == 0);

nanodbc::statement stmt(conn, NANODBC_TEXT("INSERT INTO nanodbc_longstring VALUES (?)"));
stmt.bind(0, long_string.c_str());
result = stmt.execute();
REQUIRE(result.affected_rows() == 1);

result = execute(conn, NANODBC_TEXT("SELECT t FROM nanodbc_longstring LIMIT 1"));
REQUIRE(result.affected_rows() == 1);

if (result.next()) {
nanodbc::string_type str_from_db = result.get<nanodbc::string_type>(0);
REQUIRE(str_from_db == long_string);
}
}
// DROP DATABASE|TABLE
{
nanodbc::result result;
result = execute(conn, NANODBC_TEXT("DROP TABLE nanodbc_test_temp_table"));
REQUIRE(result.affected_rows() == 0);
result = execute(conn, NANODBC_TEXT("DROP TABLE nanodbc_longstring"));
REQUIRE(result.affected_rows() == 0);
result = execute(conn, NANODBC_TEXT("DROP DATABASE nanodbc_test_temp_db"));
REQUIRE(result.affected_rows() == 0);
}
Expand Down