Serial columns and ADO

Serial columns and ADO

am 09.11.2006 11:32:27 von Dmitry Samokhin

As far as I understand, since the driver exposes 'serial' columns=20
as 'AutoNumber', ADO issues 'SELECT @@IDENTITY' to retreive the new=20
generated values just after inserts. The driver, in turn, has to modify t=
hat=20
query into 'SELECT currval(...)'.

When working with PG tables from inside MS Access, it all works perfectly=
!=20
But here is a small test suite that doesn't work as expected:

------------------------------------------------------------ -------------=
---
Server side:

CREATE TABLE t1
(
col1 serial NOT NULL,
col2 varchar(50)
)
WITHOUT OIDS;

Client side (Visual Basic 6.0 or VBA):

Sub Test()
Dim strSql As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.Open "Provider=3DMSDASQL.1;Data Source=3DTest_pg;"

strSql =3D "SELECT * FROM t1"

rs.ActiveConnection =3D cn
rs.CursorLocation =3D adUseClient
rs.CursorType =3D adOpenStatic
rs.LockType =3D adLockOptimistic
rs.Source =3D strSql

rs.Open

rs.AddNew
rs("col2") =3D "Sample string"
rs.Update
MsgBox rs("col1")

rs.Close
End Sub
------------------------------------------------------------ -------------=
---

Try this and you will see '0' in the messagebox shown and the entries in=20
psqlodbc_xxxx.log similar to the following:

..................
conn=3D04973D90, query=3D'INSERT INTO "public"."t1" ("col2") VALUES (E'Sa=
mple=20
string')'
conn=3D04973D90, query=3D'SELECT 0'
..................

My environment:
PostgreSQL 8.1.0 backend
ODBC driver 08.02.0200 Unicode (the latest at the moment of writing) on=20
WinXP SP2

Any ideas?
Dmitry.


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Re: Serial columns and ADO

am 10.11.2006 01:41:42 von Hiroshi Inoue

Dmitry Samokhin wrote:
> As far as I understand, since the driver exposes 'serial' columns
> as 'AutoNumber', ADO issues 'SELECT @@IDENTITY' to retreive the new
> generated values just after inserts. The driver, in turn, has to modify that
> query into 'SELECT currval(...)'.
>
> When working with PG tables from inside MS Access, it all works perfectly!
> But here is a small test suite that doesn't work as expected:

Please try the snapshot dll at
http://www.geocities.jp/inocchichichi/psqlodbc/index.html
..

regards,
Hiroshi Inoue

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Re: Serial columns and ADO

am 10.11.2006 10:25:17 von Dmitry Samokhin

Hiroshi-san,
I've tried your snapshot (8.2.0201) and found no difference in behaviour. It
sends 'SELECT 0' as before. Is it really corrected in the snapshot? Maybe I
have incorrect driver settings. Which ones affect most of all?
Dmitry.


"Hiroshi Inoue" wrote in message
news:4553CAC6.5020001@tpf.co.jp...
> Dmitry Samokhin wrote:
>> As far as I understand, since the driver exposes 'serial' columns as
>> 'AutoNumber', ADO issues 'SELECT @@IDENTITY' to retreive the new
>> generated values just after inserts. The driver, in turn, has to modify
>> that query into 'SELECT currval(...)'.
>>
>> When working with PG tables from inside MS Access, it all works
>> perfectly! But here is a small test suite that doesn't work as expected:
>
> Please try the snapshot dll at
> http://www.geocities.jp/inocchichichi/psqlodbc/index.html
> .
>
> regards,
> Hiroshi Inoue
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Re: Serial columns and ADO

am 10.11.2006 10:28:29 von Hiroshi Inoue

Dmitry Samokhin wrote:
> Hiroshi-san,
> I've tried your snapshot (8.2.0201) and found no difference in behaviour. It
> sends 'SELECT 0' as before.

Hmm could you send me the Mylog output ?

regards,
Hiroshi Inoue

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Re: Serial columns and ADO

am 10.11.2006 14:09:17 von Dmitry Samokhin

Just sent to your e-mail found at
http://www.geocities.jp/inocchichichi/psqlodbc/index.html

Dmitry.


"Hiroshi Inoue" wrote in message
news:4554463D.6000301@tpf.co.jp...
> Dmitry Samokhin wrote:
>> Hiroshi-san,
>> I've tried your snapshot (8.2.0201) and found no difference in behaviour.
>> It
>> sends 'SELECT 0' as before.
>
> Hmm could you send me the Mylog output ?
>
> regards,
> Hiroshi Inoue
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>



---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Re: Serial columns and ADO

am 10.11.2006 15:53:12 von Hiroshi Inoue

Dmitry Samokhin wrote:
> Just sent to your e-mail found at
> http://www.geocities.jp/inocchichichi/psqlodbc/index.html

Could you try to set the Extra Opts option to 0x2 ?

regards,
Hiroshi Inoue

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Re: Serial columns and ADO

am 13.11.2006 00:44:05 von Hiroshi Inoue

Hiroshi Inoue wrote:
> Dmitry Samokhin wrote:
>> Just sent to your e-mail found at
>> http://www.geocities.jp/inocchichichi/psqlodbc/index.html
>
> Could you try to set the Extra Opts option to 0x2 ?

With the current snapshot, please.
I was able to reproduce the behavior here.

regards,
Hiroshi Inoue

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Re: Serial columns and ADO

am 13.11.2006 10:24:09 von Dmitry Samokhin

>> Could you try to set the Extra Opts option to 0x2 ?
>
> With the current snapshot, please.
> I was able to reproduce the behavior here.
>
> regards,
> Hiroshi Inoue

OK, now it works! Thanks for your attention.
But with Extra Opts = 0x0 it still issues 'SELECT 0' and assignes 0 to an
identity field. IMHO, if I don't want to read any server-assigned defaults
for a newly inserted row, any field not set explicitly should contain NULL
both before and after insert operation, not 0.

Regards,
Dmitry.



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Re: Serial columns and ADO

am 16.11.2006 15:02:07 von Hiroshi Inoue

Dmitry Samokhin wrote:
>>> Could you try to set the Extra Opts option to 0x2 ?
>>>
>> With the current snapshot, please.
>> I was able to reproduce the behavior here.
>>
>> regards,
>> Hiroshi Inoue
>>
>
> OK, now it works! Thanks for your attention.
> But with Extra Opts = 0x0 it still issues 'SELECT 0' and assignes 0 to an
> identity field. IMHO, if I don't want to read any server-assigned defaults
> for a newly inserted row, any field not set explicitly should contain NULL
> both before and after insert operation, not 0.
>

OK try the snapshot dll.
0x2 Extra Opts option is no longer needed for the case.
Also changed to SELECT NULL in case of failure.

regards,
Hiroshi Inoue


---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Re: Serial columns and ADO

am 17.11.2006 09:22:56 von Dmitry Samokhin

> OK try the snapshot dll.
> 0x2 Extra Opts option is no longer needed for the case.

>
> regards,
> Hiroshi Inoue

OK, 'SELECT 0' has been swept out.

> Also changed to SELECT NULL in case of failure.
What failure do you mean?

But there's still another problem. Let our table 't1' be initially empty and
the sequence reset to 1. Consider the following ADO operations:

rs.AddNew
rs("col2") = "Sample string"
rs.Update
MsgBox rs("col1")

rs.AddNew
rs("col1") = 2
rs("col2") = "Sample string"
rs.Update
MsgBox rs("col1")

Look, the second insert operation sets the identity field explicitly. But
the driver still lets 'SELECT currval...' to pass. As a result, my explicit
value '2' is replaced by the currval()=1. Such distortion should be avoided
anyway.

Dmitry.



---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Re: Serial columns and ADO

am 17.11.2006 18:22:34 von Dmitriy Ivanov

Hello Dmitry,
"Dmitry Samokhin" wrote:

DS> But there's still another problem. Let our table 't1' be initially
DS> empty and the sequence reset to 1. Consider the following ADO operations:
DS>
DS> rs.AddNew
DS> rs("col2") = "Sample string"
DS> rs.Update
DS> MsgBox rs("col1")
DS>
DS> rs.AddNew
DS> rs("col1") = 2
DS> rs("col2") = "Sample string"
DS> rs.Update
DS> MsgBox rs("col1")
DS>
DS> Look, the second insert operation sets the identity field
DS> explicitly. But the driver still lets 'SELECT currval...' to pass.
DS> As a result, my explicit value '2' is replaced by the currval()=1.
DS> Such distortion should be avoided anyway.

Generally, if you declared the column type as serial, you should not set it
explicitly while inserting records. In your example, rs.Update does not
guarantee changing the cursor position to the record inserted most recently.
(At least, with DAO against MS Access database.) IMHO, it is not the ODBC
driver to blame.
--
Sincerely,
Dmitriy Ivanov



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq