Subject Re: [firebird-support] Use generators instead of a next ID table.
Author Martijn Tonies
Jonas,

> I am currently working on a Java application that uses a Interbase6
> database via Firebird 2.0. Legacy applications in Delphi are still
> using the same database via Interbase
>
> The old applications used a table for getting the next id for other
> tables i.e
>
> SELECT ID FROM NEXTIDS WHERE TABLENAME = 'xyz'
> UPDATE NEXTIDS SET ID = ID+1 WHERE TABLENAME = 'xyz'

I would add this to the UPDATE statement:
AND ID = <old-id-value>

If this results in 0 records updated, it means someone else updated the
record, this will make sure that concurrent updates know of eachother.

>
> TABLE NEXTIDS(
> ID Integer NOT NULL,
> TABLENAME Varchar(31) CHARACTER SET NONE NOT NULL,
> ROW_FLAG Integer DEFAULT 0 NOT NULL
> )
>
> Is it somehow possible for me to use generators for the new
> application but still keep the nextids table up-to-date. This so
> legacy applications still can use the same database without need to
> change any code?

Have you thought of wrapping the getting of IDs into a Stored Procedure
instead of doing direct updates? This way, you can easily switch to a
Generator instead of a table. There's no way of knowing a Generator
is used (like a table trigger), so they'll never be update to date unless
you create a trigger on each table that will automatically update your
NEXTIDs table with the last number inserted.

Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, NexusDB, Oracle &
MS SQL Server
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com