feat: support CREATE DATABASE (#2070) - #2471
Open
fudianchn wants to merge 1 commit into
Open
Conversation
Parse CREATE DATABASE [IF NOT EXISTS] <name> into a typed CreateDatabase statement instead of the generic UnsupportedStatement fallback. Vendor-specific options after the name (e.g. MySQL's DEFAULT CHARACTER SET / COLLATE) are captured verbatim and round-trip unchanged. Signed-off-by: 付典 <fudianchn@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a typed
CreateDatabasestatement forCREATE DATABASE [IF NOT EXISTS] <name> [options], which previously fell through to the genericUnsupportedStatementcapture.Why
The grammar has no production for
CREATE DATABASE(#2070), so users cannot access the database name from the AST.CREATE SCHEMAalready has a typed statement and MySQL treatsCREATE SCHEMAas a synonym ofCREATE DATABASE.How
CreateDatabase()production dispatched fromCreate()right afterCreateSchema(), following the same shape:LOOKAHEAD(2)-guardedIF NOT EXISTSandRelObjectName()for the name.captureRest()helper (same approach asUnsupportedStatementandCreateFunctionalStatement), soCREATE DATABASE mydb DEFAULT CHARACTER SET utf8mb4keeps parsing and round-trips unchanged instead of failing.net.sf.jsqlparser.statement.create.database.CreateDatabaseplus wiring inStatementVisitor,StatementVisitorAdapter,StatementDeParser,StatementValidator(newFeature.createDatabasevalidated againstNamedObject.database) andTablesNamesFinder.Root cause
Missing feature:
CREATE DATABASEhad no grammar production and always hit thecaptureRest()fallback.Testing
CreateDatabaseTest(5 cases): the issue's SQL,IF NOT EXISTS, quoted names, options capture with AST assertions;./gradlew test --tests ...CreateDatabaseTestpasses.Verification of the original issue
Before (master
406a4d4):CCJSqlParserUtil.parse("CREATE DATABASE USERS")returnsUnsupportedStatementwith no access to the database name.After: returns
CreateDatabasewithgetDatabaseName() = "USERS"and the deparse round-trips; same forCREATE DATABASE IF NOT EXISTS mydb DEFAULT CHARACTER SET utf8mb4.Grammar change, paired
gradle jmhrun (JSQLParserBenchmark.parseSQLStatementsonperformance.sql,version=latest, 10 forks x 10 iterations = 100 samples, 32-core host):406a4d4Delta within the confidence intervals -> no regression.
Limitation: options are kept as raw tokens rather than structured AST nodes (the option sets differ heavily between MySQL and PostgreSQL); they can be modelled individually in a follow-up if preferred. The bare form
CREATE DATABASE(no name) now throws aParseExceptioninstead of returningUnsupportedStatement; both MySQL and PostgreSQL require the name.Fixes #2070