I need to parse SQL "CREATE TABLE" statements, to get the field list and their properties.
It is mostly MySQL syntax, which should be (with incontrolable nested brackets like this example :
CREATE TABLE 'tablename' (
field1 INT,
field2 VARCHAR(10),
field3 FLOAT NOT NULL DEFAULT 0,
PRIMARY KEY (field1, field2),
(CONSTRAINT fk FOREIGN KEY whatever)
) options;
I can easily isolate the column definitions but then I'd like to split the definitions. Splitting on commas is not possible as these can be in nested brackets.
I could iterate though the string to count opening and closing string delimiters (quotes etc.), count opening and closing brackets, so I can detect "first level" commas and split on them.
But I wonder if there could not be more effective ways, like for example using regular expressions, but I'm not good enough at them to determine if some regex could match such complex syntax ...