pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bugs related to getting @@identity
4+ messages / 2 participants
[nested] [flat]

* Bugs related to getting @@identity
@ 2015-07-31 18:30  Christian Ullrich <chris@chrullrich.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Christian Ullrich @ 2015-07-31 18:30 UTC (permalink / raw)
  To: pgsql-odbc

Hello,

as there have been ominous rumblings lately about making another 
release, I wanted to remind you that I reported two bugs on 23 April. 
One breaks @@identity entirely [1], the other makes it unreliable with 
INSERTs into schema-qualified tables [2].

I provided a patch for the second, but there has been no feedback at 
all, and it is possible that the first bug is not real but a mistake I 
made in building the driver from source.

[1] http://www.postgresql.org/message-id/mhaknj$e4g$1@ger.gmane.org
[2] http://www.postgresql.org/message-id/mhalik$s8k$1@ger.gmane.org

-- 
Christian Ullrich



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



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

* Re: Bugs related to getting @@identity
@ 2015-07-31 21:33  Heikki Linnakangas <hlinnaka@iki.fi>
  parent: Christian Ullrich <chris@chrullrich.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Heikki Linnakangas @ 2015-07-31 21:33 UTC (permalink / raw)
  To: Christian Ullrich <chris@chrullrich.net>; pgsql-odbc

On 07/31/2015 09:30 PM, Christian Ullrich wrote:
> Hello,
>
> as there have been ominous rumblings lately about making another
> release, I wanted to remind you that I reported two bugs on 23 April.
> One breaks @@identity entirely [1], the other makes it unreliable with
> INSERTs into schema-qualified tables [2].
>
> I provided a patch for the second, but there has been no feedback at
> all, and it is possible that the first bug is not real but a mistake I
> made in building the driver from source.
>
> [1] http://www.postgresql.org/message-id/mhaknj$e4g$1@ger.gmane.org
> [2] http://www.postgresql.org/message-id/mhalik$s8k$1@ger.gmane.org

How do you use @@identity? I don't know what the idiomatic way to use it is.

I wrote a little test case for it, see attached patch. But it runs OK 
with a fresh git checkout. Could you modify/expand this test case to 
tickle the bugs you're seeing?

- Heikki

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

Attachments:

identity-test.patch (application/x-patch, 2.3K) download diff --git a/test/expected/identity.out b/test/expected/identity.out new file mode 100644 index 0000000..71d3e2d --- /dev/null +++ b/test/expected/identity.out @@ -0,0 +1,7 @@ +connected +# of rows inserted: 1 +Result set: +1 +Result set: +1 foobar +disconnecting diff --git a/test/src/identity-test.c b/test/src/identity-test.c new file mode 100644 index 0000000..cc79e29 --- /dev/null +++ b/test/src/identity-test.c @@ -0,0 +1,61 @@ +/* + * Test @@identity + */ +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" + +int main(int argc, char **argv) +{ + SQLRETURN rc; + HSTMT hstmt = SQL_NULL_HSTMT; + char param1[100]; + SQLLEN cbParam1; + SQLCHAR *sql; + int i; + SQLLEN rowcount; + + test_connect(); + + rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + /* Prepare a test table */ + sql = "CREATE TEMPORARY TABLE tmptable (i serial primary key, t text)"; + rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating temp table", hstmt); + + /* Insert some rows, using a prepared statement */ + rc = SQLExecDirect(hstmt, (SQLCHAR *) "INSERT INTO tmptable (t) VALUES ('foobar'); ", + SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + + rc = SQLRowCount(hstmt, &rowcount); + CHECK_STMT_RESULT(rc, "SQLRowCount failed", hstmt); + printf("# of rows inserted: %d\n", (int) rowcount); + + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT @@IDENTITY AS last_insert", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + + /* Fetch result */ + print_result(hstmt); + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + + /* Print the contents of the table after the updates to verify */ + + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT * FROM tmptable", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + print_result(hstmt); + + /* Clean up */ + test_disconnect(); + + return 0; +} diff --git a/test/tests b/test/tests index e908771..d00c7f3 100644 --- a/test/tests +++ b/test/tests @@ -24,6 +24,7 @@ TESTBINS = src/connect-test \ src/notice-test \ src/arraybinding-test \ src/insertreturning-test \ + src/identity-test \ src/dataatexecution-test \ src/boolsaschar-test \ src/cvtnulldate-test \
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: Bugs related to getting @@identity
@ 2015-07-31 22:15  Christian Ullrich <chris@chrullrich.net>
  parent: Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 4+ messages in thread

From: Christian Ullrich @ 2015-07-31 22:15 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnaka@iki.fi>; pgsql-odbc

* From: Heikki Linnakangas

> On 07/31/2015 09:30 PM, Christian Ullrich wrote:

> > as there have been ominous rumblings lately about making another
> > release, I wanted to remind you that I reported two bugs on 23 April.
> > One breaks @@identity entirely [1], the other makes it unreliable with
> > INSERTs into schema-qualified tables [2].

> How do you use @@identity? I don't know what the idiomatic way to use it
> is.
> 
> I wrote a little test case for it, see attached patch. But it runs OK
> with a fresh git checkout. Could you modify/expand this test case to
> tickle the bugs you're seeing?

Working on it; now I have to build the driver again. Joy!

-- 
Christian


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


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

* Re: Bugs related to getting @@identity
@ 2015-11-28 19:48  Christian Ullrich <chris@chrullrich.net>
  parent: Christian Ullrich <chris@chrullrich.net>
  0 siblings, 0 replies; 4+ messages in thread

From: Christian Ullrich @ 2015-11-28 19:48 UTC (permalink / raw)
  To: pgsql-odbc

* Christian Ullrich wrote on 2015-08-01:

> * From: Heikki Linnakangas
>
>> On 07/31/2015 09:30 PM, Christian Ullrich wrote:
>
>>> as there have been ominous rumblings lately about making another
>>> release, I wanted to remind you that I reported two bugs on 23 April.
>>> One breaks @@identity entirely [1], the other makes it unreliable with
>>> INSERTs into schema-qualified tables [2].
>
>> How do you use @@identity? I don't know what the idiomatic way to use it
>> is.
>>
>> I wrote a little test case for it, see attached patch. But it runs OK
>> with a fresh git checkout. Could you modify/expand this test case to
>> tickle the bugs you're seeing?
>
> Working on it; now I have to build the driver again. Joy!

It only took me four months (of procrastination), but now I know at 
least how to trigger the bug, if not where exactly it is. It has to do 
with how the "SELECT @@identity" is done. It only works if it is *not* 
prepared.

My application is actually using ADO via the OLEDB bridge, and it 
prepares all statements it generates. However, whether this statement is 
prepared or not influences how the driver processes it.

I set a breakpoint early in SC_execute() (statement.c:1823, to be exact) 
to print the statement text and connection state:

Unprepared:

SC_execute with CONN_CONNECTED (1): "INSERT INTO history ..." SC_execute 
with CONN_CONNECTED (1): "select n.nspname, ..."
SC_execute with CONN_CONNECTED (1): "SELECT @@identity"

Prepared:

SC_execute with CONN_CONNECTED (1): "INSERT INTO history ..." SC_execute 
with CONN_CONNECTED (1): "SELECT @@identity"
SC_execute with CONN_EXECUTING (3): "select n.nspname, ..."

The "select n.nspname" is the metadata query necessary to get the 
autogenerated ID from the earlier INSERT. The difference is that in the 
unprepared case, SQLExecDirect() is used, while the prepared statement 
is run using SQLExecute().

This is the call stack for the prepared statement, bottom to top:

*1      psqlodbc35w.dll!SC_execute(StatementClass_ * self=0x0033d048)
  2      psqlodbc35w.dll!Exec_with_parameters_resolved(StatementClass_ * 
stmt=0x0033d048, int * exec_end=0x0018af38)
  3      psqlodbc35w.dll!PGAPI_Execute(void * hstmt=0x0033d048, unsigned 
short flag=0)
  4      psqlodbc35w.dll!PGAPI_ExecDirect(void * hstmt=0x0033d048, const 
unsigned char * szSqlStr=0x0018b664, long cbSqlStr=-3, unsigned short 
flag=0)
  5      psqlodbc35w.dll!PGAPI_Columns(void * hstmt=0x0032aa20, const 
unsigned char * szTableQualifier=0x00000000, short cbTableQualifier=0, 
const unsigned char * szTableOwner=0x048b4c80, short cbTableOwner=-3, 
const unsigned char * szTableName=0x0489b428, short cbTableName=-3, 
const unsigned char * szColumnName=0x00000000, short cbColumnName=0, 
unsigned short flag=1, unsigned int reloid=0, short attnum=0)
  6      psqlodbc35w.dll!getColumnsInfo(ConnectionClass_ * 
conn=0x002c1fc0, TABLE_INFO * wti=0x0018db54, unsigned int greloid=0, 
StatementClass_ * stmt=0x002c15b8)
  7      psqlodbc35w.dll!getCOLIfromTI(const char * func=0x5dd05b38, 
ConnectionClass_ * conn=0x002c1fc0, StatementClass_ * stmt=0x002c15b8, 
const unsigned int reloid=0, TABLE_INFO * * pti=0x0018db48)
  8      psqlodbc35w.dll!inner_process_tokens(_QueryParse * 
qp=0x0018de44, _QueryBuild * qb=0x0018dde4)
  9      psqlodbc35w.dll!prepareParametersNoDesc(StatementClass_ * 
stmt=0x002c15b8, int fake_params=0)
  10     psqlodbc35w.dll!prepareParameters(StatementClass_ * 
stmt=0x002c15b8, int fake_params=0)
  11     psqlodbc35w.dll!libpq_bind_and_exec(StatementClass_ * 
stmt=0x002c15b8)
  12     psqlodbc35w.dll!SC_execute(StatementClass_ * self=0x002c15b8)
  13     psqlodbc35w.dll!Exec_with_parameters_resolved(StatementClass_ * 
stmt=0x002c15b8, int * exec_end=0x0018e84c)
  14     psqlodbc35w.dll!PGAPI_Execute(void * hstmt=0x002c15b8, unsigned 
short flag=1)
  15     psqlodbc35w.dll!SQLExecute(void * StatementHandle=0x002c15b8)


And here for the unprepared one:

*1      psqlodbc35w.dll!SC_execute(StatementClass_ * self=0x006801b8)
  2      psqlodbc35w.dll!Exec_with_parameters_resolved(StatementClass_ * 
stmt=0x006801b8, int * exec_end=0x0018b3a0)
  3      psqlodbc35w.dll!PGAPI_Execute(void * hstmt=0x006801b8, unsigned 
short flag=0)
  4      psqlodbc35w.dll!PGAPI_ExecDirect(void * hstmt=0x006801b8, const 
unsigned char * szSqlStr=0x0018bacc, long cbSqlStr=-3, unsigned short 
flag=0)
  5      psqlodbc35w.dll!PGAPI_Columns(void * hstmt=0x0067ff48, const 
unsigned char * szTableQualifier=0x00000000, short cbTableQualifier=0, 
const unsigned char * szTableOwner=0x0460ca68, short cbTableOwner=-3, 
const unsigned char * szTableName=0x045f4ef0, short cbTableName=-3, 
const unsigned char * szColumnName=0x00000000, short cbColumnName=0, 
unsigned short flag=1, unsigned int reloid=0, short attnum=0)
  6      psqlodbc35w.dll!getColumnsInfo(ConnectionClass_ * 
conn=0x0063efd0, TABLE_INFO * wti=0x0018dfbc, unsigned int greloid=0, 
StatementClass_ * stmt=0x0063fa50)
  7      psqlodbc35w.dll!getCOLIfromTI(const char * func=0x5dd05b38, 
ConnectionClass_ * conn=0x0063efd0, StatementClass_ * stmt=0x0063fa50, 
const unsigned int reloid=0, TABLE_INFO * * pti=0x0018dfb0)
  8      psqlodbc35w.dll!inner_process_tokens(_QueryParse * 
qp=0x0018e3c4, _QueryBuild * qb=0x0018e364)
  9      psqlodbc35w.dll!copy_statement_with_parameters(StatementClass_ 
* stmt=0x0063fa50, int buildPrepareStatement=0)
  10     psqlodbc35w.dll!Exec_with_parameters_resolved(StatementClass_ * 
stmt=0x0063fa50, int * exec_end=0x0018e740)
  11     psqlodbc35w.dll!PGAPI_Execute(void * hstmt=0x0063fa50, unsigned 
short flag=1)
  12     psqlodbc35w.dll!PGAPI_ExecDirect(void * hstmt=0x0063fa50, const 
unsigned char * szSqlStr=0x0067b448, long cbSqlStr=17, unsigned short 
flag=1)
  13     psqlodbc35w.dll!SQLExecDirectW(void * 
StatementHandle=0x0063fa50, unsigned short * StatementText=0x040b2db8, 
long TextLength=17)

-- 
Christian




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



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


end of thread, other threads:[~2015-11-28 19:48 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-07-31 18:30 Bugs related to getting @@identity Christian Ullrich <chris@chrullrich.net>
2015-07-31 21:33 ` Heikki Linnakangas <hlinnaka@iki.fi>
2015-07-31 22:15   ` Christian Ullrich <chris@chrullrich.net>
2015-11-28 19:48     ` Christian Ullrich <chris@chrullrich.net>

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