Skip to content

Commit d053544

Browse files
feat: allow CATALOG in CREATE SCHEMA and DROP SCHEMA (h2database#4277)
* feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - fixes h2database#4276 * feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - do not document the optional `catalogName` because actual support is limited * feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - do not carry the `catalogName` along * feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - add tests for invalid `catalog` names * feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - verify catalog name and encapsulate the logic * feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - do not carry the catalog name along * feat: allow `CATALOG` in `CREATE SCHEMA` and `DROP SCHEMA` - minor style recommendations
1 parent a448d91 commit d053544

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

h2/src/main/org/h2/command/Parser.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ private Prepared parseDrop() {
21932193
} else if (readIf("SCHEMA")) {
21942194
boolean ifExists = readIfExists(false);
21952195
DropSchema command = new DropSchema(session);
2196-
command.setSchemaName(readIdentifier());
2196+
command.setSchemaName(readIdentifierWithCatalog());
21972197
ifExists = readIfExists(ifExists);
21982198
command.setIfExists(ifExists);
21992199
ConstraintActionType dropAction = parseCascadeOrRestrict();
@@ -5532,6 +5532,31 @@ private String readIdentifierWithSchema() {
55325532
return readIdentifierWithSchema(session.getCurrentSchemaName());
55335533
}
55345534

5535+
/**
5536+
* <p>Reads the schema name with or without a catalog name.</p>
5537+
* <p>Merely for SQL:2016 compatibility.</p>
5538+
* <p>Since H2 does not support multiple catalogs:</p>
5539+
* <ul>
5540+
* <li>we verify against current catalog name and throw an exception when
5541+
* not matching</li>
5542+
* <li>we are going to ignore the catalog name because it is not needed
5543+
* anywhere</li>
5544+
* </ul>
5545+
*
5546+
* @return the SCHEMA name only (without the catalog name)
5547+
*/
5548+
private String readIdentifierWithCatalog() {
5549+
String name = readIdentifier();
5550+
if (readIf(DOT)) {
5551+
if (equalsToken(name, database.getShortName()) || database.getIgnoreCatalogs()) {
5552+
name = readIdentifier();
5553+
} else {
5554+
throw DbException.get(ErrorCode.INVALID_NAME_1, name);
5555+
}
5556+
}
5557+
return name;
5558+
}
5559+
55355560
private String readIdentifier() {
55365561
if (!isIdentifier()) {
55375562
/*
@@ -6708,7 +6733,7 @@ private CreateSchema parseCreateSchema() {
67086733
command.setSchemaName(authorization);
67096734
command.setAuthorization(authorization);
67106735
} else {
6711-
command.setSchemaName(readIdentifier());
6736+
command.setSchemaName(readIdentifierWithCatalog());
67126737
if (readIf(AUTHORIZATION)) {
67136738
authorization = readIdentifier();
67146739
} else {

h2/src/test/org/h2/test/scripts/ddl/createSchema.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ CREATE SCHEMA S1;
1515
CREATE SCHEMA S2 AUTHORIZATION TEST_USER;
1616
> ok
1717

18-
CREATE SCHEMA S3 AUTHORIZATION TEST_ROLE;
18+
CREATE SCHEMA SCRIPT.S3 AUTHORIZATION TEST_ROLE;
1919
> ok
2020

21+
-- invalid catalog name shall fail
22+
CREATE SCHEMA 1.S3 AUTHORIZATION TEST_ROLE;
23+
> exception SYNTAX_ERROR_2
24+
25+
-- valid catalog name, but shall fail since we do not support multiple catalogs
26+
CREATE SCHEMA UNNAMED.S3 AUTHORIZATION TEST_ROLE;
27+
> exception INVALID_NAME_1
28+
2129
CREATE SCHEMA AUTHORIZATION TEST_USER;
2230
> ok
2331

@@ -42,7 +50,7 @@ DROP SCHEMA S1;
4250
DROP SCHEMA S2;
4351
> ok
4452

45-
DROP SCHEMA S3;
53+
DROP SCHEMA SCRIPT.S3;
4654
> ok
4755

4856
DROP USER TEST_USER;

0 commit comments

Comments
 (0)