AUTO_INCREMENT patch

From: des(at)des(dot)no (Dag-Erling =?iso-8859-1?q?Sm=F8rgrav?=)
To: pgsql-patches(at)postgresql(dot)org
Subject: AUTO_INCREMENT patch
Date: 2003-08-02 16:43:23
Message-ID: xzpispgxb6c.fsf@dwp.des.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

[apologies if this comes through twice]

I'm working on getting auto-incrementing CMP fields in JBoss 3.2.1 to
work with PostgreSQL data sources. There are currently two obstacles
to this.

The first problem is that the syntax JBoss uses for mapping Java types
onto SQL types isn't powerful enough to understand PostgreSQL's SERIAL
pseudo-types. The attached patch therefore adds support for MySQL's
AUTO_INCREMENT syntax to PostgreSQL. It works by defining a new
column constraint (CONSTR_AUTO_INCREMENT) which is handled specially
by transformColumnDefinition() - after it has transformed SERIAL
pseudo-types to the corresponding INT types, but before it actually
creates the sequence and adds the synthetic DEFAULT constraint. It is
thus possible to specify AUTO_INCREMENT on a SERIAL column; this will
generate a warning but does not have any other negative side effects.
It will however generate an error if AUTO_INCREMENT is specified on a
non-integer column.

(given that this is my first experience with PostgreSQL sources, I'd
like some feedback about the use of elog() / ereport())

Usage test:

des=# create table test1 ( foo int auto_increment, bar text );
NOTICE: CREATE TABLE will create implicit sequence "test1_foo_seq" for SERIAL column "test1.foo"
CREATE TABLE
des=# \d test1
Table "public.test1"
Column | Type | Modifiers
--------+---------+--------------------------------------------------------
foo | integer | not null default nextval('public.test1_foo_seq'::text)
bar | text |

des=# create table test2 ( foo int auto_increment, bar text );
NOTICE: CREATE TABLE will create implicit sequence "test2_foo_seq" for SERIAL column "test2.foo"
CREATE TABLE
des=# \d test2
Table "public.test2"
Column | Type | Modifiers
--------+---------+--------------------------------------------------------
foo | integer | not null default nextval('public.test2_foo_seq'::text)
bar | text |

des=# create table test3 ( foo serial auto_increment, bar text );
WARNING: both SERIAL and AUTO_INCREMENT specified for column 'test3.foo'
NOTICE: CREATE TABLE will create implicit sequence "test3_foo_seq" for SERIAL column "test3.foo"
CREATE TABLE
des=# \d test3
Table "public.test3"
Column | Type | Modifiers
--------+---------+--------------------------------------------------------
foo | integer | not null default nextval('public.test3_foo_seq'::text)
bar | text |

des=# create table test4 ( foo text auto_increment, bar text );
ERROR: AUTO_INCREMENT columns must be of integer type

The second problem is an issue which is probably best solved in JBoss
and not in PostgreSQL (JBoss insists on inserting null into the auto-
incrementing column, which obviously doesn't work)

DES
--
Dag-Erling Smørgrav - des(at)des(dot)no

Attachment Content-Type Size
auto_increment.diff text/x-patch 3.0 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Rod Taylor 2003-08-02 20:26:35 Re: AUTO_INCREMENT patch
Previous Message Bruce Momjian 2003-08-02 15:55:52 pgindent run coming

Browse pgsql-patches by date

  From Date Subject
Next Message Guillaume LELARGE 2003-08-02 19:42:30 Updated french po files
Previous Message Serguei Mokhov 2003-08-01 18:18:52 Re: [pgsql-rus] Russian NLS update: pg_dump, libpq, pg_controldata, pg_resetxlog, pgscripts (fwd)