FirebirdSQL / firebird

Firebird server, client and tools
https://www.firebirdsql.org/
1.24k stars 215 forks source link

Parameterized Generator [CORE4446] #4766

Open firebird-automations opened 10 years ago

firebird-automations commented 10 years ago

Submitted by: @livius2

Real life case - i do not know why it not exists in every database ;-)

Many times we need to have numbered documents. Documents can be grouped by Kind, departments, month and any other criteria

Now when we need generate in concurrent environment values for such documents and we must do some trick and elimination of conflicts. Whe have possible ways: 1) create generators with name like GEN_SCAN_DEP1_2014_01, GEN_SCAN_DEP1_2014_02, GEN_SCAN_DEP1_2014_03 .. GEN_SCAN_DEP2_2014_01, GEN_SCAN_DEP2_2014_02 ... 2) create table with numbers - but here we need elimination of conflicts 3) very old way Select max().. - and also we need elimination of conflicts ...

will be good to have posibility to create parameterized generator below the idea

CREATE GENERATOR GEN_FOR_DOC(INTEGER) GEN_ID(GEN_FOR_DOC[1], 1);

CREATE GENERATOR GEN_FOR_SCAN(INTEGER, INTEGER, INTEGER) GEN_ID(GEN_FOR_SCAN[1, 2014, 1], 1); //department 1, year 2014, month 1

may be this can be supported in very clear way CREATE GENERATOR GEN_FOR_SCAN(DEPART INTEGER, YEAR INTEGER, MONTH INTEGER); GEN_ID(GEN_FOR_SCAN[DEPART=1, YEAR=2014, MONTH=1], 1);

firebird-automations commented 9 years ago

Commented by: @livius2

I analysed this and this is simplification

1. Extend table RDB$GENERATORS and add field

RDB$IS_COMPLEX RDB$SYSTEM_FLAG

2. add table with detailed info about generator values CREATE TABLE RDB$GENERATORS_DETAIL ( RDB$GENERATOR_NAME RDB$GENERATOR_NAME, RDB$GENERATOR_ID RDB$GENERATOR_ID, RDB$SYSTEM_FLAG RDB$SYSTEM_FLAG, RDB$COMPLEX_KEY VARCHAR(120) )

2. When we call CREATE COMPLEX_GENERATOR GEN_MY_COMPLEX_GENERATOR; then it store info in RDB$IS_COMPLEX field = 1

and system function must be called with KEY value - or should be provided new function GEN_ID_COMPLEX..

GEN_ID(GEN_MY_COMPLEX_GENERATOR, '2015_01_SOMETHING_ELSE', 1)

and in memory of FB process should be simple dictionary with values for each complex generator and acces to this dictionary should be protected by mutex like i suppose is now for normal generator

i suppose that this is simple to provide pity that I do not know C ++ :( This is one day for coding - and benefit will be very big :)