GithubHelp home page GithubHelp logo

Comments (3)

HGeorge-Adapdix avatar HGeorge-Adapdix commented on June 10, 2024

I came across this when looking at reporting a similar issue.

Environment

nanodbc version:2.14
DBMS name/version:Postgres
ODBC connection string:Driver={PostgreSQL ANSI};Uid=postgres;Server=localhost;Port=36947
OS and Compiler: Ubuntu 22.04
CMake settings:
TEST(Example, TestStringBindSingleNullRowFails)
{
	const std::string pgConnectionString{
		"Driver={PostgreSQL ANSI};Uid=postgres;Server=localhost;Port=36947"};
	nanodbc::connection conn(pgConnectionString);

	nanodbc::just_execute(conn,
						  "create temporary table test_table (id integer, name varchar(255))");
	nanodbc::just_execute(conn, "truncate test_table");

	nanodbc::transaction transaction(conn);

	nanodbc::statement statement(conn, "insert into test_table (id, name) values (?, ?)");

	std::vector<std::string> ids;
	ids.emplace_back("1");
	const std::array<bool, 1> notNull{{false}};

	statement.bind_strings(0, ids, notNull.data());
	bool pass(false);
	if (pass) {
		// Works if bind null
		statement.bind_null(1, 1);
	} else {
		// Fails if bind string with all nulls
		const std::array<bool, 1> null{{true}};
		statement.bind_strings(1, ids, null.data());
	}

	nanodbc::just_execute(statement, static_cast<int>(ids.size()));

	transaction.commit();

	nanodbc::result result(nanodbc::execute(conn, "select * from test_table"));
	ASSERT_TRUE(result.next());
	ASSERT_FALSE(result.is_null(0));

	// This fails
	ASSERT_TRUE(result.is_null(1));
}

If you use the bind_null interface it works, but bind strings does not insert a null and instead will insert the string ignoring the "null" aspect.

from nanodbc.

HGeorge-Adapdix avatar HGeorge-Adapdix commented on June 10, 2024

I believe the issue is in bind_parameter

diff --git a/nanodbc/nanodbc.cpp b/nanodbc/nanodbc.cpp
index 9ece354..ed5ad3c 100644
--- a/nanodbc/nanodbc.cpp
+++ b/nanodbc/nanodbc.cpp
@@ -2239,7 +2239,7 @@ public:
             param.scale_,        // decimal digits
             (SQLPOINTER)buffer.values_, // parameter value
             buffer_size,                // buffer length
-            (buffer.size_ <= 1 ? nullptr : bind_len_or_null_[param.index_].data()));
+            (buffer_size <= 1 ? nullptr : bind_len_or_null_[param.index_].data()));
 
         if (!success(rc))
             NANODBC_THROW_DATABASE_ERROR(stmt_, SQL_HANDLE_STMT);

In the one row example the buffer.size_ is 1 and the buffer_size is 2.

Changing it fixes it, so that code is what is causing the issue but I am not 100% sure what the code is supposed to be doing (hence no PR). I am unsure as to why you wouldn't just always pass in the bind_len_or_null_ value but looking through the history that appears to have been there for many years (tracked it back as far as 2bd4e2e).

Not sure if anyone has any insight here.

from nanodbc.

mloskot avatar mloskot commented on June 10, 2024

@x-awaken & @HGeorge-Adapdix Thank you for your reports.
Hmm, this is interesting as it looks like an overlooked place that miss the change from buffer.size_ to local variable buffer_size:

(buffer.size_ <= 1 ? nullptr : bind_len_or_null_[param.index_].data()));

(buffer.size_ <= 1 ? nullptr : bind_len_or_null_[param.index_].data()));

I do not recall anything specific on the use of buffer.size_ in those places, instead of buffer_size_.

What I'd suggest is to submit PR with

and let's see what the CI builds think about it.
I will also try to run local tests of such PR and perhaps add more test cases.

from nanodbc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.