Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WqS6Y-0005tf-4Q for pgsql-odbc@arkaria.postgresql.org; Fri, 30 May 2014 19:04:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WqS6X-00040y-7t for pgsql-odbc@arkaria.postgresql.org; Fri, 30 May 2014 19:04:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WqS6W-0003zT-DM for pgsql-odbc@postgresql.org; Fri, 30 May 2014 19:04:36 +0000 Received: from dub004-omc4s1.hotmail.com ([157.55.2.76]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WqS6R-00040W-RD for pgsql-odbc@postgresql.org; Fri, 30 May 2014 19:04:35 +0000 Received: from DUB121-W2 ([157.55.2.72]) by DUB004-OMC4S1.hotmail.com with Microsoft SMTPSVC(7.5.7601.22678); Fri, 30 May 2014 12:04:30 -0700 X-TMN: [0n6JHDKjuHE6e701OcmZNZbxLw+bmI2v] X-Originating-Email: [nukemd@hotmail.com] Message-ID: From: John Smith To: "pgsql-odbc@postgresql.org" Subject: Re: Problems using SQLFetch on prepared INSERT with RETURNING clause Date: Fri, 30 May 2014 21:04:30 +0200 Importance: Normal In-Reply-To: References: , , <5384A41A.1000608@vmware.com>, Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 30 May 2014 19:04:30.0720 (UTC) FILETIME=[FC6FD000:01CF7C39] X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org Greetings I finally got the time to dig deeper into this, and the problem seems to be= with SQLMoreResults. In results.c, function PGAPI_MoreResults, around line 1921, there is this c= onstruct: mylog("%s: entering...\n", func); if (stmt && (res =3D SC_get_Curres(stmt))) SC_set_Curres(stmt, res->next); SC_set_Curres here causes the curres field of the statement to become NULL,= and that is not liked when the next SQLFetch is executed. Not sure why PGA= PI_MoreResults would do this, though. Seems like an error, but I really can= 't say fore sure. As I said before, I am not an ODBC expert, what I know I have gathered from= bits and pieces around the internet, and it seems common to recommend that= SQLMoreResults is called to consume the results before you do something mo= re with the query. It seems I can work around the problem by doing=A0SQLFreeStmt(hstmt, SQL_CL= OSE) instead of SQLMoreResults, but I will be monitoring the mailing list f= or opinions about this. ---------------------------------------- From: nukemd@hotmail.com To: pgsql-odbc@postgresql.org Subject: Re: [ODBC] Problems using SQLFetch on prepared INSERT with RETURNI= NG clause Date: Tue, 27 May 2014 22:25:58 +0200 Hi Heikki, Thanks for the reply. I tried returning a bigint constant instead, and got = the same error. I haven't yet managed to set up the driver itself for debug= ging, but I did manage to refactor the test case to your existing kind. Her= e it is. (Not sure if attachments work here, so I'll paste it below too). ---------------------------------------- > Date: Tue, 27 May 2014 17:41:30 +0300 > From: hlinnakangas@vmware.com > To: nukemd@hotmail.com; pgsql-odbc@postgresql.org > Subject: Re: [ODBC] Problems using SQLFetch on prepared INSERT with RETUR= NING clause > > On 05/24/2014 06:47 PM, John Smith wrote: >> Hi again, >> >> I'm not an ODBC expert, but I would think the below is an error, either = in the ODBC driver or maybe even somewhere else. If someone agrees that thi= s should work, I can take a deeper look at fixing it. > > Yeah, it does look like a bug at a quick glance. Could you refactor your > test program into a regression test, like the ones in the psqlodbc's > test/src/* source tree, please? That would make it easier for me and > others to run and debug. > > Does it make a difference if you use something else than "xmin" in the > test case? Like, just return a bigint constant in the RETURNING clause. > I would imagine it does not, but you never know.. > >> ---------------------------------------- >>> From: nukemd@hotmail.com >>> To: pgsql-odbc@postgresql.org >>> Subject: [ODBC] Problems using SQLFetch on prepared INSERT with RETURNI= NG clause >>> Date: Sun, 18 May 2014 18:22:15 +0200 >>> >>> Hi, >>> >>> I am having some difficulties with a prepared INSERT statement with a r= eturning clause. I made a small example that reproduces the problem: >>> >>> One database: CREATE DATABASE my_test_db >>> One table: CREATE TABLE my_test_table(mycol integer) >>> >>> The query I am attempting to use: INSERT INTO my_test_table VALUES(?) R= ETURNING xmin::text::bigint >>> >>> First i use SQLPrepare to prepare my insert statement. After this, I us= e SQLBindCol to bind a 64-bit integer to the result that would be xmin. The= n I use SQLBindParam to bind a 32 bit integer variable for the input to the= column. >>> >>> Then I call SQLExecute and SQLFetch. The first time it works fine, and = xmin is returned into the bound variable. Then I call SQLMoreResults until = all results are consumed. >>> >>> After this I call SQLExecute and SQLFetch again, and here I would expec= t another row to be inserted, and the xmin for that row to be returned on S= QLFetch. >>> >>> The new row is indeed inserted, but SQLFetch fails with SQLSTATE 07005,= native error 15 and message "Null statement result in PGAPI_ExtendedFetch." >>> >>> I am using the PostgreSQL Unicode(x64) driver version on Windows 7. I t= ried versions 9.03.02.10 and the just released 9.03.03.00. >>> >>> Any ideas? I'll paste my complete test program (C++, uses boost unit te= st) below. >>> >>> //--------------------------------------------------------------------- >>> #include >>> #include >>> >>> namespace >>> { >>> SQLHENV henv =3D NULL; >>> SQLHDBC hdbc =3D NULL; >>> SQLHANDLE hstmt =3D NULL; >>> >>> SQLWCHAR sqlstate[25]; >>> SQLWCHAR message[255]; >>> SQLINTEGER native_error; >>> >>> //This test program will create a database named my_test_db with one ta= ble, my_test_table, containing a single integer column named mycol. >>> SQLWCHAR crete_db_sql[] =3D L"CREATE DATABASE my_test_db"; >>> SQLWCHAR create_table_sql[] =3D L"CREATE TABLE my_test_table(mycol inte= ger)"; >>> //It will attempt to prepare a query that inserts an integer, and retur= ns the xmin value as a bigint. >>> //This will work fine the first time, but subsequent calls to SQLExec w= ill fail on the SQLFetch that should fetch xmin. >>> SQLWCHAR insert_sql[] =3D L"INSERT INTO my_test_table VALUES(?) RETURNI= NG xmin::text::bigint"; >>> >>> SQLINTEGER input =3D 0; >>> SQLBIGINT xmin_output =3D 0; >>> >>> std::wstring get_connection_string(const std::wstring& dbname) >>> { >>> return L"Driver=3D{PostgreSQL Unicode(x64)};Server=3D127.0.0.1;Port=3D5= 432;Database=3D" + dbname + L";Uid=3Dpostgres;Pwd=3Dpassword;"; >>> } >>> } >>> >>> #define ODBC_CHECK(x) if (!SQL_SUCCEEDED(x)) { \ >>> BOOST_CHECK_IMPL(false, BOOST_TEST_STRINGIZE(x), CHECK, CHECK_PRED); \ >>> SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, sqlstate, &native_error, messa= ge, 255, nullptr); \ >>> std::wcerr << L"SQLSTATE: " << sqlstate << L" NATIVE ERROR: " << native= _error << std::endl << L"MESSAGE: " << message << std::endl; } >>> >>> BOOST_AUTO_TEST_CASE( aaa_postgres_odbc ) >>> { >>> >>> //------Boring setup code that creates the database etc. >>> BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDL= E, &henv))); >>> BOOST_CHECK(SQL_SUCCEEDED(SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, >>> reinterpret_cast(SQL_OV_ODBC3), 0))); >>> >>> BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc))); >>> BOOST_CHECK(SQL_SUCCEEDED(SQLDriverConnect(hdbc, NULL, const_cast(get_connection_string(L"postgres").c_str()), >>> SQL_NTS, nullptr, 0, nullptr, SQL_DRIVER_NOPROMPT))); >>> >>> BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)= )); >>> ODBC_CHECK(SQLExecDirect(hstmt, crete_db_sql, SQL_NTS)); >>> BOOST_CHECK(SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_STMT, hstmt))); >>> BOOST_CHECK(SQL_SUCCEEDED(SQLDisconnect(hdbc))); >>> //------Database should now be set up, some more boring code to create = our table goes here >>> >>> BOOST_CHECK(SQL_SUCCEEDED(SQLDriverConnect(hdbc, NULL, const_cast(get_connection_string(L"my_test_db").c_str()), >>> SQL_NTS, nullptr, 0, nullptr, SQL_DRIVER_NOPROMPT))); >>> BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)= )); >>> ODBC_CHECK(SQLExecDirect(hstmt, create_table_sql, SQL_NTS)); >>> >>> ODBC_CHECK(SQLPrepare(hstmt, insert_sql, SQL_NTS)); >>> >>> ODBC_CHECK(SQLBindCol(hstmt, 1, SQL_C_SBIGINT, &xmin_output, 0, nullptr= )); >>> ODBC_CHECK(SQLBindParam(hstmt, 1, SQL_C_SLONG, SQL_PARAM_INPUT, 0, 0, &= input, nullptr)); >>> >>> //Execute first insert >>> ++input; >>> ODBC_CHECK(SQLExecute(hstmt)); >>> ODBC_CHECK(SQLFetch(hstmt)); >>> BOOST_CHECK(xmin_output> 0); >>> >>> while (SQL_SUCCEEDED(SQLMoreResults(hstmt))); >>> //Execute second insert >>> ++input; >>> ODBC_CHECK(SQLExecute(hstmt)); >>> ODBC_CHECK(SQLFetch(hstmt)); //This is where it fails >>> BOOST_CHECK(xmin_output> 0); >>> >>> BOOST_CHECK(SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_STMT, hstmt))); >>> >>> BOOST_CHECK(SQL_SUCCEEDED(SQLDisconnect(hdbc))); >>> BOOST_CHECK(SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_ENV, henv))); >>> } >>> >>> -- >>> Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) >>> To make changes to your subscription: >>> http://www.postgresql.org/mailpref/pgsql-odbc >> >> > > > -- > - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc =20=09=09=20=09=20=20=20=09=09=20=20 --=20 Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc