Skip to content

Commit ac46c43

Browse files
feat: CREATE SCHEMA with catalog
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent 30144e7 commit ac46c43

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

src/main/java/net/sf/jsqlparser/statement/create/schema/CreateSchema.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public class CreateSchema implements Statement {
2222

2323
private String authorization;
24+
private String catalogName = null;
2425
private String schemaName;
2526
private List<String> schemaPath;
2627
private List<Statement> statements = new ArrayList<>();
@@ -59,6 +60,15 @@ public void setAuthorization(String authorization) {
5960
this.authorization = authorization;
6061
}
6162

63+
public String getCatalogName() {
64+
return catalogName;
65+
}
66+
67+
public CreateSchema setCatalogName(String catalogName) {
68+
this.catalogName = catalogName;
69+
return this;
70+
}
71+
6272
/**
6373
* The name of the schema
6474
*
@@ -119,7 +129,12 @@ public String toString() {
119129
sql += " IF NOT EXISTS";
120130
}
121131
if (schemaName != null) {
122-
sql += " " + schemaName;
132+
sql += " ";
133+
134+
if (catalogName!=null) {
135+
sql += catalogName + ".";
136+
}
137+
sql += schemaName;
123138
}
124139
if (authorization != null) {
125140
sql += " AUTHORIZATION " + authorization;

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7866,15 +7866,21 @@ CreateSchema CreateSchema():
78667866
CreateTable table = null;
78677867
CreateView view = null;
78687868
CreateSchema schema = new CreateSchema();
7869-
//schema.setSchemaName(System.getProperty("user.name"));
7870-
//schema.setAuthorization(System.getProperty("user.name"));
78717869
List<String> schemaPath = null;
78727870
List<Statement> statements = new ArrayList<Statement>();
78737871
}
78747872
{
78757873
<K_SCHEMA>
78767874
[ LOOKAHEAD(2) <K_IF> <K_NOT> <K_EXISTS> { schema.setIfNotExists(true); } ]
7877-
[ ( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setSchemaName(tk.image); } ]
7875+
[
7876+
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setSchemaName(tk.image); }
7877+
7878+
(
7879+
"." { schema.setCatalogName(tk.image); }
7880+
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setSchemaName(tk.image); }
7881+
)?
7882+
]
7883+
78787884
[ <K_AUTHORIZATION>
78797885
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setAuthorization(tk.image); }
78807886
]

src/test/java/net/sf/jsqlparser/statement/SessionStatementTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SessionStatementTest {
2121
@ValueSource(strings = {
2222
"SESSION START 1234", "SESSION START", "SESSION APPLY 'test'", "SESSION APPLY",
2323
"SESSION DROP \"test\"", "SESSION DROP", "SESSION SHOW test", "SESSION SHOW",
24-
"SESSION DESCRIBE 1234", "SESSION DESCRIBE", "SESSION APPLY unnamed.session1"
24+
"SESSION DESCRIBE 1234", "SESSION DESCRIBE", "SESSION START unnamed.session1"
2525
})
2626
void testStartSession(String sqlStr) throws JSQLParserException {
2727
SessionStatement sessionStatement =

src/test/java/net/sf/jsqlparser/statement/create/schema/CreateSchemaTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public void testSimpleCreateSchema() throws JSQLParserException {
2727
assertDeparse(new CreateSchema().withSchemaName("myschema"), statement);
2828
}
2929

30+
@Test
31+
public void testCreateSchemaWithcatalog() throws JSQLParserException {
32+
String statement = "CREATE SCHEMA unnamed.myschema";
33+
assertSqlCanBeParsedAndDeparsed(statement);
34+
}
35+
3036
@Test
3137
public void testSimpleCreateWithAuth() throws JSQLParserException {
3238
String statement = "CREATE SCHEMA myschema AUTHORIZATION myauth";

src/test/java/net/sf/jsqlparser/statement/drop/DropTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public void testDropMaterializedView() throws JSQLParserException {
9898
@Test
9999
public void testDropSchemaIssue855() throws JSQLParserException {
100100
assertSqlCanBeParsedAndDeparsed("DROP SCHEMA myschema");
101+
assertSqlCanBeParsedAndDeparsed("DROP SCHEMA unnamed.myschema");
101102
}
102103

103104
@Test

src/test/java/net/sf/jsqlparser/statement/select/OrderByCollateTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2025 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.statement.select;
211

312
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
@@ -30,4 +39,4 @@ public void testOrderByWithCollateAndNulls() throws JSQLParserException {
3039
String sql = "SELECT * FROM a ORDER BY col COLLATE \"C\" DESC NULLS LAST";
3140
assertSqlCanBeParsedAndDeparsed(sql);
3241
}
33-
}
42+
}

0 commit comments

Comments
 (0)