taocpp / taopq

C++ client library for PostgreSQL
Boost Software License 1.0
265 stars 40 forks source link

BYTEA columns results wrong with tablewriter #50

Closed skaae closed 2 years ago

skaae commented 2 years ago

I think that BYTEA colums are not written correctly to the database when using a table writer.

I created a small example, mostly copied from the bytea tests:

    auto conn = create_db_connection()
    conn->execute("DROP TABLE IF EXISTS test_table;");
    conn->execute("CREATE TABLE test_table (  buffer_bytes BYTEA NOT NULL);");

    const unsigned char bdata[] = { 'v', 255, 0, 'a', 1, 'b', 0 }; 

    conn->execute( "INSERT INTO test_table VALUES ( $1 )", tao::pq::to_binary_view(bdata) );

    tao::pq::table_writer tw(conn->direct(), "COPY test_table ( buffer_bytes ) FROM STDIN");
    tw.insert(tao::pq::to_binary_view(bdata));   
    tw.commit();

    const auto result_0 = conn->execute( "SELECT * FROM test_table" )[ 0 ][ 0 ].as< std::basic_string<unsigned char > >();
    const auto result_1 = conn->execute( "SELECT * FROM test_table" )[ 1 ][ 0 ].as< std::basic_string<unsigned char > >();
    fmt::print("result_0 = {}\n", fmt::join(result_0, ", ")); // result_0 = 118, 255, 0, 97, 1, 98, 0
    fmt::print("result_1 = {}\n", fmt::join(result_1, ", ")); // result_1 = 125, 48, 48, 48, 48, 54, 55, 48, 49, 54, 56, 48, 48

I I write { 'v', 255, 0, 'a', 1, 'b', 0 } to the db and using an INSERT statement I get 118, 255, 0, 97, 1, 98, 0 which is correct (?). If i write the same data using a table writer i get 125, 48, 48, 48, 48, 54, 55, 48, 49, 54, 56, 48, 48 .

I tried changing the column type to TEXT and insert two strings instead, this seems work.

Am I doing something wrong?

I'm using a621cbba1f63c599819466f3da7ef7d352bdaf0d