pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Inoue, Hiroshi <inoue@tpf.co.jp>
To: Philippe Champignon <philippe.champignon@evidian.com>
Cc: pgsql-odbc@postgresql.org <pgsql-odbc@postgresql.org>
Subject: Re: Cannot retrieve sequence number after an insert with OLE DB
Date: Sun, 21 Sep 2014 20:46:51 +0900
Message-ID: <541EBAAB.5050502@tpf.co.jp> (raw)
In-Reply-To: <3A06BF224C309F41A0C27532B1773641FAF55C25@BUMSG2WM.fr.ad.bull.net>
References: <3A06BF224C309F41A0C27532B1773641FAF55C25@BUMSG2WM.fr.ad.bull.net>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>

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

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

setInsertedTable.patch (text/x-diff, 1.5K) download 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; } }
setInsertedTable.txt (text/plain, 1.4K) download 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; } }

view thread (3+ messages)

Message-ID: <541EBAAB.5050502@tpf.co.jp>
Permalink:  ../541EBAAB.5050502@tpf.co.jp/
Also on:    postgresql.org/message-id/541EBAAB.5050502@tpf.co.jp

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: inoue@tpf.co.jp, philippe.champignon@evidian.com
  Subject: Re: Cannot retrieve sequence number after an insert with OLE DB
  In-Reply-To: <541EBAAB.5050502@tpf.co.jp>
  References: <3A06BF224C309F41A0C27532B1773641FAF55C25@BUMSG2WM.fr.ad.bull.net> <541EBAAB.5050502@tpf.co.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox