Skip to content

Incorrect index type for indices parsed from create table statement #1060

Open
@reboot

Description

@reboot

I notice that the parser seems to create incorrect index types from create table statements when multiple constraints are defined. The index type then depends on the order of constraints. For example:

CREATE TABLE EXAMPLE (
  ...
  CONSTRAINT EXAMPLE_UK UNIQUE (COLUMN1, COLUMN2)
  CONSTRAINT EXAMPLEPK PRIMARY KEY (ID),
)

The index EXAMPLE_UK will have the type UNIQUE and EXAMPLEPK will have the type PRIMARY KEY. But if you change the order of statemets like this

CREATE TABLE EXAMPLE (
  ...
  CONSTRAINT EXAMPLEPK PRIMARY KEY (ID),
  CONSTRAINT EXAMPLE_UK UNIQUE (COLUMN1, COLUMN2)
)

then EXAMPLEPK will have the type PRIMARY KEY and EXAMPLE_UK will have the type UNIQUE KEY.

I believe this is caused by the parsing of the unique key tokens. The KEY-Keyword is optional for UNIQUE constraints, but the token variable is never cleared.

In JSqlParser:4099 the code

                    (tk=<K_PRIMARY> tk2=<K_KEY> {index.setType(tk.image + " " + tk2.image);}
                     | tk=<K_UNIQUE> [ tk2=<K_KEY> ] {index.setType(tk.image + (tk2!=null?" " + tk2.image:""));}

is adding the tk2.image to the index type, if it is not null. But if a primary key has been parsed before the unique constraint, then tk2 still contains the old token and is added the index type.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions