pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Cannot retrieve sequence number after an insert with OLE DB
3+ messages / 2 participants
[nested] [flat]

* Cannot retrieve sequence number after an insert with OLE DB
@ 2014-09-18 09:51 Philippe Champignon <philippe.champignon@evidian.com>
  2014-09-21 11:46 ` Re: Cannot retrieve sequence number after an insert with OLE DB Inoue, Hiroshi <inoue@tpf.co.jp>
  0 siblings, 1 reply; 3+ messages in thread

From: Philippe Champignon @ 2014-09-18 09:51 UTC (permalink / raw)
  To: pgsql-odbc

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.

Here in attachment some traces, a test case and the correction I made. Beware, it is a correction "quick & dirty" just to check that the bug was in this function. And I have not tested it in all possible cases.

I can provide a testcase as needed.

Best Regards,
Philippe CHAMPIGNON

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

Attachments:

  driverODBC.7z (application/octet-stream, 348.2K) download

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Cannot retrieve sequence number after an insert with OLE DB
  2014-09-18 09:51 Cannot retrieve sequence number after an insert with OLE DB Philippe Champignon <philippe.champignon@evidian.com>
@ 2014-09-21 11:46 ` Inoue, Hiroshi <inoue@tpf.co.jp>
  0 siblings, 0 replies; 3+ messages in thread

From: Inoue, Hiroshi @ 2014-09-21 11:46 UTC (permalink / raw)
  To: Philippe Champignon <philippe.champignon@evidian.com>; +Cc: pgsql-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; } }
^ permalink raw reply [nested|flat] 3+ messages in thread

* Cannot retrieve sequence number after an insert with OLE DB
@ 2014-09-18 14:26 Philippe Champignon <philippe.champignon@evidian.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Philippe Champignon @ 2014-09-18 14:26 UTC (permalink / raw)
  To: pgsql-odbc

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.

Trace:
I see  in the trace (ODBC driver  9.03.0300), the insert statement (line 1130):
INSERT INTO "iamaudit"."public"."AuditEvents" ("EvtCategory","EvtType","EvtOperation","TimeStamp","ResultCode","UserAuditID","SessionID","AccessPointID","AccessPointName","ClientIP","ClientHostname","ExtendedInfo") VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
And after, for retrieve the value of the sequence (line 1311):
SELECT @@IDENTITY
Then (line 1349):
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, pg_get_expr(d.adbin, d.adrelid), case t.typtype when 'd' then t.typbasetype else 0 end, t.typtypmod, c.relhasoids 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
But, if I execute this latest request, I found no row !
When I modify some values, I found the correct result:
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, pg_get_expr(d.adbin, d.adrelid), case t.typtype when 'd' then t.typbasetype else 0 end, t.typtypmod, c.relhasoids from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'AuditEvents' and n.nspname = 'public') 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

Best Regards,
Philippe CHAMPIGNON

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

Attachments:

  mylog_6068.7z (application/octet-stream, 156.2K) download

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-09-21 11:46 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-09-18 09:51 Cannot retrieve sequence number after an insert with OLE DB Philippe Champignon <philippe.champignon@evidian.com>
2014-09-21 11:46 ` Inoue, Hiroshi <inoue@tpf.co.jp>
2014-09-18 14:26 Cannot retrieve sequence number after an insert with OLE DB Philippe Champignon <philippe.champignon@evidian.com>

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