Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZcBxK-0002pJ-KV for pgsql-odbc@arkaria.postgresql.org; Wed, 16 Sep 2015 12:36:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZcBxK-00071p-0y for pgsql-odbc@arkaria.postgresql.org; Wed, 16 Sep 2015 12:36:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZcBxJ-00071Q-B4 for pgsql-odbc@postgresql.org; Wed, 16 Sep 2015 12:36:57 +0000 Received: from mail-pa0-x22d.google.com ([2607:f8b0:400e:c03::22d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZcBxF-000454-84 for pgsql-odbc@postgresql.org; Wed, 16 Sep 2015 12:36:56 +0000 Received: by padhk3 with SMTP id hk3so208461524pad.3 for ; Wed, 16 Sep 2015 05:36:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=SYXTGaI1NzkpacM/KbiHYf8Bu3ToUIATCyTw518PbMQ=; b=Q3jgU9r5k9jggdEWvZ3u4uLuwYIfIB42FVK/zrGXmmyrIWhj2ZhZ9BPv/ti4d3mCJI RJ9hctXco6IsYf4syeRrguqfinrzWsu1nQzkM+AuQ/bH5jJzjHmT/okMPRD7TbiDnuDS wZ1krVB74FZh+KIuLf2kiLDJvUGXiSomGRdH+nPURsx2AlTP5bp0ly6++qZU+BQkJM5W C3D2Vc0JbjXRHTu4dtLPFqQ5eHz6KruzJWyXXL7KB/d41dLY7X4/UlgtXgcrXkJ6oAb/ OmocuJ7za9gkuXa713UL8oWaHjUUGeRa8JzWLIiWkRbwr/Vbm4PultsQ8vkTgEN+J2AI 9sfQ== X-Received: by 10.67.30.136 with SMTP id ke8mr60387153pad.16.1442407010817; Wed, 16 Sep 2015 05:36:50 -0700 (PDT) Received: from [192.168.1.110] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by smtp.gmail.com with ESMTPSA id ch3sm27771566pbb.18.2015.09.16.05.36.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Sep 2015 05:36:49 -0700 (PDT) Message-ID: <55F9625D.6040001@iki.fi> Date: Wed, 16 Sep 2015 15:36:45 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 MIME-Version: 1.0 To: PSequel Support , pgsql-odbc@postgresql.org Subject: Re: Bug in the SQLGetDiagRec function of psqlodbc References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 On 08/18/2015 07:19 AM, PSequel Support wrote: > There seems to be a bug in the SQLGetDiagRec function - it always returns > the oldest error message. For example, after I run two invalid queries, > calling SQLGetDiagRec gives me the error message for the first query > instead of the second one. However, according to the specs > , > > *SQLGetDiagRec* retrieves only the diagnostic information most recently >> associated with the handle specified in the *Handle* argument. If the >> application calls another ODBC function, except *SQLGetDiagRec*, >> *SQLGetDiagField*, or *SQLError*, any diagnostic information from the >> previous calls on the same handle is lost. > > > My psqlodbcw.so file was built from the HEAD of > http://git.postgresql.org/git/psqlodbc.git using Homebrew > > on > Mac OS X. Here is the code for demonstrating this issue: > > #include > #include > #include > > void printError(SQLHANDLE handle) { > char message[256]; > SQLSMALLINT i = 0; > while (SQLGetDiagRec(SQL_HANDLE_DBC, handle, ++i, NULL, NULL, > (SQLCHAR*)message, sizeof(message), NULL) != SQL_NO_DATA) { > printf("%s\n", message); > } > } > > BOOL runQuery(SQLHSTMT stmt, char* query) { > SQLRETURN ret = SQLExecDirect(stmt, (SQLCHAR*)query, SQL_NTS); > printf("Query: %s\n", query); > return SQL_SUCCEEDED(ret); > } > > int main(int argc, const char * argv[]) { > SQLHENV env; > SQLHDBC dbc; > SQLHSTMT stmt; > > SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); > SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); > SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc); > > char* connStr = > "Driver=/usr/local/Cellar/psqlodbc/HEAD/lib/psqlodbcw.so;Server=localhost;Database=postgres"; > if (SQLDriverConnect(dbc, NULL, (SQLCHAR*)connStr, SQL_NTS, NULL, 0, > NULL, SQL_DRIVER_NOPROMPT) != SQL_SUCCESS) { > printf("Unable to connect to database\n"); > return 1; > } > > SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt); > for (int i = 0; i < 3; ++i) { > char query[50]; > sprintf(query, "SELECT * FROM t%d", i); > if (!runQuery(stmt, query)) { > printError(dbc); // this always prints 'ERROR: relation "t0" does not > exist' - that's wrong! > } > } > > SQLDisconnect(dbc); > SQLFreeHandle(SQL_HANDLE_STMT, stmt); > SQLFreeHandle(SQL_HANDLE_DBC, dbc); > SQLFreeHandle(SQL_HANDLE_ENV, env); > return 0; > } > Hmm. You're not calling SQLFreeStmt(SQL_CLOSE) after the failed query, so the second SQLExecDirect() is returning SQL_INVALID_HANDLE. According to the ODBC docs, SQLExecDirect() sets the diagnostics if it returns SQL_ERROR. I'm not 100% sure that's correct behaviour - perhaps it should return SQL_ERROR, with an error message complaining that the statement is busy. Given that no-one's complained about that before, I'm guessing that it's not supposed to execute the query, anyway, i.e. this an application error. - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc