Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XVfbe-0005QG-F6 for pgsql-odbc@arkaria.postgresql.org; Sun, 21 Sep 2014 11:47:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XVfbd-0008EW-Li for pgsql-odbc@arkaria.postgresql.org; Sun, 21 Sep 2014 11:47:05 +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 1XVfbc-0008EP-N4 for pgsql-odbc@postgresql.org; Sun, 21 Sep 2014 11:47:04 +0000 Received: from sd.tpf.co.jp ([210.161.239.34]) by magus.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XVfbJ-0006FZ-T4 for pgsql-odbc@postgresql.org; Sun, 21 Sep 2014 11:47:03 +0000 Received: (qmail 5579 invoked from network); 21 Sep 2014 11:46:39 -0000 X-Authentication: inoue was authenticated by sd.tpf.co.jp at 21 Sep 2014 11:46:39 -0000 Received: from unknown (HELO ?192.168.1.8?) (183.76.63.206) by sd.tpf.co.jp with SMTP; 21 Sep 2014 11:46:39 -0000 Message-ID: <541EBAAB.5050502@tpf.co.jp> Date: Sun, 21 Sep 2014 20:46:51 +0900 From: "Inoue, Hiroshi" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.0 MIME-Version: 1.0 To: Philippe Champignon CC: "pgsql-odbc@postgresql.org" Subject: Re: Cannot retrieve sequence number after an insert with OLE DB References: <3A06BF224C309F41A0C27532B1773641FAF55C25@BUMSG2WM.fr.ad.bull.net> In-Reply-To: <3A06BF224C309F41A0C27532B1773641FAF55C25@BUMSG2WM.fr.ad.bull.net> Content-Type: multipart/mixed; boundary="------------090305090404050404070502" X-Pg-Spam-Score: -2.7 (--) 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 This is a multi-part message in MIME format. --------------090305090404050404070502 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Hi Philippe, On 2014/09/18 18:51, Philippe Champignon wrote: > Hello, > > I just submit a bug in the psqlODBC driver 9.2/9.3 (32-bit and 64-bit). > > *_The context :_* > > I have a Windows program that uses the database postgresql through OLE > DB and psqlODBC driver. In the database, I have a table with a sequence > that generates a number whenever you insert a new row in the table. The > program inserts data into the table and retrieves the auto incremented > sequence value. > > With Postgresql 8.2 and the psqlODBC 8.02.0400 driver, the program works > perfectly. But with Postgresql 9.2 or 9.3 and the psqlODBC 9.02.0100 or > 9.03.0300 driver it is impossible to retrieve the sequence number after > the insertion of the line. There are always 0. > > *_Tests:_* > > *__* > > To identify the problem, I did several tests, which allowed me to find > that it was the ODBC driver that is problematic. > > PsqlODBC version > > > > Postgresql version > > > > Result > > 8.02.0400 > > > > 8.2 > > > > OK > > 9.02.0100 > > > > 9.2.4 > > > > KO > > 8.02.0400 > > > > 9.2.4 > > > > OK > > 8.02.0500 > > > > 9.2.5 > > > > KO > > 9.03.0300 > > > > 9.3.2 > > > > KO > > With version 8.02.04 of the driver, the problem does not occur. But with > the 8.02.05 driver version, I reproduce the problem. > > So I compared the 8.02.04 driver and driver 8.02.05 sources and > positioned the traces of the driver to find the bug: it is in the > SC_setInsertedTable function, when parsing the name of the table to find > the schema. > > *_Explanation :_* > > With the driver 8.02.04, in traces, we see this: > > [11.819]conn=02354D10, query='INSERT INTO > "iamaudit"."public"."AuditEvents" > ("EvtCategory","EvtType","EvtOperation","TimeStamp","ResultCode","UserAuditID","SessionID","AccessPointID","AccessPointName","ClientIP","ClientHostname","ExtendedInfo") > VALUES (1,1234,3,'2013-09-23 > 12:30:52'::timestamp,-2147467259,'champi','','','DEROBEE','','','')' > > [11.841]conn=02354D10, query='SELECT lastval()' > > [11.856] [ fetched 1 rows ] > > [11.892]conn=02354D10, query='COMMIT' > > With the driver 8.02.05, in traces, we see this: > > [17.297]conn=02354D10, query='INSERT INTO > "iamaudit"."public"."AuditEvents" > ("EvtCategory","EvtType","EvtOperation","TimeStamp","ResultCode","UserAuditID","SessionID","AccessPointID","AccessPointName","ClientIP","ClientHostname","ExtendedInfo") > VALUES (1,1234,3,'2013-09-23 > 12:41:27'::timestamp,-2147467259,'champi','','','DEROBEE','','','')' > > [17.336]conn=02354D10, query='select n.nspname, c.relname, a.attname, > a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull, > c.relhasrules, c.relkind, c.oid, d.adsrc from (((pg_catalog.pg_class c > inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and > c.relname = 'public' and n.nspname = 'iamaudit') inner join > pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and > a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = > a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = > a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum' > > [17.351] [ fetched 0 rows ] > > [17.401]conn=02354D10, query='SELECT NULL' > > [17.405] [ fetched 1 rows ] > > [17.439]conn=02354D10, query='COMMIT' > > The problem is revealed by the correction in 8.02.0402: > > 3.) Put back the @@IDENTITY implementation so as not to use lastval(). > > During this correction, changed the use of lastval() that is dangerous > by the use of curval (sequence) much more secure. Then, why this > correction does not work in my case? In the trace, seen just after the > insert, there is a select that returns no rows! This select is directed > to search the sequences in the table. It does not work because it passes > 'public' as table name and 'iamaudit' as schema name instead of > 'AuditEvents' and 'public'. Because in the insert, I use the string > "iamaudit"."public"."AuditEvents"to identify the table. The table name > is prefixed by the name of the base and the schema, which was not > expected. Note that it is the OLE DB driver which writes the query and > that uses this syntax. > > The analysis of the insert query is performed in the function > SC_setInsertedTable (file execute.c). So I modified this function to > update the parsing to my case and I tested successfully this correction. Thanks for your report. Could you please try the attached patch setInsertedTable.patch or replace the content of SC_setInsertedTable() function by the attached setInsertedTable.txt? regards, Hiroshi Inoue --------------090305090404050404070502 Content-Type: text/x-diff; name="setInsertedTable.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="setInsertedTable.patch" diff --git a/execute.c b/execute.c index 9afcdf7..a6900b7 100644 --- a/execute.c +++ b/execute.c @@ -863,45 +863,34 @@ SC_setInsertedTable(StatementClass *stmt, RETCODE retval) NULL_THE_NAME(conn->tableIns); if (!SQL_SUCCEEDED(retval)) return; - ptr = NULL; - if (IDENTIFIER_QUOTE == *cmd) + while (TRUE) { - if (ptr = strchr(cmd + 1, IDENTIFIER_QUOTE), NULL == ptr) - return; - if ('.' == ptr[1]) + if (IDENTIFIER_QUOTE == *cmd) { + if (ptr = strchr(cmd + 1, IDENTIFIER_QUOTE), NULL == ptr) + return; len = ptr - cmd - 1; - STRN_TO_NAME(conn->schemaIns, cmd + 1, len); - cmd = ptr + 2; - ptr = NULL; + cmd++; + ptr++; } - } - else - { - if (ptr = strchr(cmd + 1, '.'), NULL != ptr) + else { - len = ptr - cmd; - STRN_TO_NAME(conn->schemaIns, cmd, len); - cmd = ptr + 1; - ptr = NULL; + if (ptr = strchr(cmd + 1, '.'), NULL != ptr) + len = ptr - cmd; + else + { + ptr = cmd; + while (*ptr && !isspace((UCHAR) *ptr)) ptr++; + len = ptr - cmd; + } } - } - if (IDENTIFIER_QUOTE == *cmd && NULL == ptr) - { - if (ptr = strchr(cmd + 1, IDENTIFIER_QUOTE), NULL == ptr) - return; - } - if (IDENTIFIER_QUOTE == *cmd) - { - len = ptr - cmd - 1; - STRN_TO_NAME(conn->tableIns, cmd + 1, len); - } - else - { - ptr = cmd; - while (*ptr && !isspace((UCHAR) *ptr)) ptr++; - len = ptr - cmd; + if (NAME_IS_VALID(conn->tableIns)) + MOVE_NAME(conn->schemaIns, conn->tableIns); STRN_TO_NAME(conn->tableIns, cmd, len); + if ('.' == *ptr) + cmd = ptr + 1; + else + break; } } --------------090305090404050404070502 Content-Type: text/plain; charset=Shift_JIS; name="setInsertedTable.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="setInsertedTable.txt" void SC_setInsertedTable(StatementClass *stmt, RETCODE retval) { const char *cmd = stmt->statement, *ptr; ConnectionClass *conn; size_t len; if (STMT_TYPE_INSERT != stmt->statement_type) return; if (SQL_NEED_DATA == retval) return; conn = SC_get_conn(stmt); #ifdef NOT_USED /* give up the use of lastval() */ if (PG_VERSION_GE(conn, 8.1)) /* lastval() is available */ return; #endif /* NOT_USED */ /*if (!CC_fake_mss(conn)) return;*/ while (isspace((UCHAR) *cmd)) cmd++; if (!*cmd) return; len = 6; if (strnicmp(cmd, "insert", len)) return; cmd += len; while (isspace((UCHAR) *(++cmd))); if (!*cmd) return; len = 4; if (strnicmp(cmd, "into", len)) return; cmd += len; while (isspace((UCHAR) *(++cmd))); if (!*cmd) return; NULL_THE_NAME(conn->schemaIns); NULL_THE_NAME(conn->tableIns); if (!SQL_SUCCEEDED(retval)) return; while (TRUE) { if (IDENTIFIER_QUOTE == *cmd) { if (ptr = strchr(cmd + 1, IDENTIFIER_QUOTE), NULL == ptr) return; len = ptr - cmd - 1; cmd++; ptr++; } else { if (ptr = strchr(cmd + 1, '.'), NULL != ptr) len = ptr - cmd; else { ptr = cmd; while (*ptr && !isspace((UCHAR) *ptr)) ptr++; len = ptr - cmd; } } if (NAME_IS_VALID(conn->tableIns)) MOVE_NAME(conn->schemaIns, conn->tableIns); STRN_TO_NAME(conn->tableIns, cmd, len); if ('.' == *ptr) cmd = ptr + 1; else break; } } --------------090305090404050404070502 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------090305090404050404070502--