Skip to content

Commit f3ecdcb

Browse files
committed
fixes #855
1 parent 9ce74e2 commit f3ecdcb

File tree

9 files changed

+236
-1
lines changed

9 files changed

+236
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Also I would like to know about needed examples or documentation stuff.
6666

6767
## Extensions in the latest SNAPSHOT version 3.2
6868

69+
* first support for **CREATE SCHEMA** and **DROP SCHEMA**
6970
* allow **ON** as a value in a set statement (`SET myvalue = ON`)
7071
* support for **ALTER TABLE ONLY mytable ...**
7172
* allow foreign key definition in alter statements without referenced columns specification

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.sf.jsqlparser.statement.alter.Alter;
1313
import net.sf.jsqlparser.statement.comment.Comment;
1414
import net.sf.jsqlparser.statement.create.index.CreateIndex;
15+
import net.sf.jsqlparser.statement.create.schema.CreateSchema;
1516
import net.sf.jsqlparser.statement.create.table.CreateTable;
1617
import net.sf.jsqlparser.statement.create.view.AlterView;
1718
import net.sf.jsqlparser.statement.create.view.CreateView;
@@ -46,6 +47,8 @@ public interface StatementVisitor {
4647
void visit(Truncate truncate);
4748

4849
void visit(CreateIndex createIndex);
50+
51+
void visit(CreateSchema aThis);
4952

5053
void visit(CreateTable createTable);
5154

src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.sf.jsqlparser.statement.alter.Alter;
1313
import net.sf.jsqlparser.statement.comment.Comment;
1414
import net.sf.jsqlparser.statement.create.index.CreateIndex;
15+
import net.sf.jsqlparser.statement.create.schema.CreateSchema;
1516
import net.sf.jsqlparser.statement.create.table.CreateTable;
1617
import net.sf.jsqlparser.statement.create.view.AlterView;
1718
import net.sf.jsqlparser.statement.create.view.CreateView;
@@ -79,6 +80,10 @@ public void visit(CreateIndex createIndex) {
7980

8081
}
8182

83+
@Override
84+
public void visit(CreateSchema aThis) {
85+
}
86+
8287
@Override
8388
public void visit(CreateTable createTable) {
8489

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create.schema;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import net.sf.jsqlparser.statement.Statement;
15+
import net.sf.jsqlparser.statement.StatementVisitor;
16+
17+
public class CreateSchema implements Statement {
18+
19+
private String authorization;
20+
private String schemaName;
21+
private List<String> schemaPath;
22+
private List<Statement> statements = new ArrayList<>();
23+
24+
@Override
25+
public void accept(StatementVisitor statementVisitor) {
26+
statementVisitor.visit(this);
27+
}
28+
29+
/**
30+
* Add a statement to the schema definition
31+
*
32+
* @param statement The statement to be added
33+
* @return true if the operation was successful
34+
*
35+
*/
36+
public boolean addStatement(Statement statement) {
37+
return statements.add(statement);
38+
}
39+
40+
/**
41+
* The owner of the schema.
42+
*
43+
* @return Owner name
44+
*
45+
*/
46+
public String getAuthorization() {
47+
return authorization;
48+
}
49+
50+
/**
51+
* The name of the schema
52+
*
53+
* @return Schema name
54+
*/
55+
public String getSchemaName() {
56+
return schemaName;
57+
}
58+
59+
/**
60+
* The path of the schema
61+
*
62+
* @return Schema path
63+
*
64+
*/
65+
public List<String> getSchemaPath() {
66+
return schemaPath;
67+
}
68+
69+
/**
70+
* The statements executed as part of the schema creation
71+
*
72+
* @return the statements
73+
*
74+
*/
75+
public List<Statement> getStatements() {
76+
return statements;
77+
}
78+
79+
/**
80+
* The owner of the schems.
81+
*
82+
* @param authorization Owner name
83+
*
84+
*/
85+
public void setAuthorization(String authorization) {
86+
this.authorization = authorization;
87+
}
88+
89+
/**
90+
* Set the name of the schema
91+
*
92+
* @param schemaName Schema name
93+
*
94+
*/
95+
public void setSchemaName(String schemaName) {
96+
this.schemaName = schemaName;
97+
}
98+
99+
/**
100+
* Set the path of the schema
101+
*
102+
* @param schemaPath Schema path
103+
*
104+
*/
105+
public void setSchemaPath(List<String> schemaPath) {
106+
this.schemaPath = schemaPath;
107+
}
108+
109+
public String toString() {
110+
String sql = "CREATE SCHEMA";
111+
if (schemaName != null) {
112+
sql += " " + schemaName;
113+
}
114+
if (authorization != null) {
115+
sql += " AUTHORIZATION " + authorization;
116+
}
117+
return sql;
118+
}
119+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import net.sf.jsqlparser.statement.alter.Alter;
7272
import net.sf.jsqlparser.statement.comment.Comment;
7373
import net.sf.jsqlparser.statement.create.index.CreateIndex;
74+
import net.sf.jsqlparser.statement.create.schema.CreateSchema;
7475
import net.sf.jsqlparser.statement.create.table.CreateTable;
7576
import net.sf.jsqlparser.statement.create.view.AlterView;
7677
import net.sf.jsqlparser.statement.create.view.CreateView;
@@ -689,6 +690,11 @@ public void visit(Truncate truncate) {
689690
public void visit(CreateIndex createIndex) {
690691
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
691692
}
693+
694+
@Override
695+
public void visit(CreateSchema aThis) {
696+
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
697+
}
692698

693699
@Override
694700
public void visit(CreateTable create) {

src/main/java/net/sf/jsqlparser/util/deparser/StatementDeParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.sf.jsqlparser.statement.alter.Alter;
2626
import net.sf.jsqlparser.statement.comment.Comment;
2727
import net.sf.jsqlparser.statement.create.index.CreateIndex;
28+
import net.sf.jsqlparser.statement.create.schema.CreateSchema;
2829
import net.sf.jsqlparser.statement.create.table.CreateTable;
2930
import net.sf.jsqlparser.statement.create.view.AlterView;
3031
import net.sf.jsqlparser.statement.create.view.CreateView;
@@ -274,4 +275,9 @@ public void visit(DeclareStatement declare) {
274275
expressionDeParser.setBuffer(buffer);
275276
new DeclareStatementDeParser(expressionDeParser, buffer).deParse(declare);
276277
}
278+
279+
@Override
280+
public void visit(CreateSchema aThis) {
281+
buffer.append(aThis.toString());
282+
}
277283
}

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import net.sf.jsqlparser.statement.*;
3939
import net.sf.jsqlparser.statement.alter.*;
4040
import net.sf.jsqlparser.statement.comment.*;
4141
import net.sf.jsqlparser.statement.create.index.*;
42+
import net.sf.jsqlparser.statement.create.schema.*;
4243
import net.sf.jsqlparser.statement.create.table.*;
4344
import net.sf.jsqlparser.statement.create.view.*;
4445
import net.sf.jsqlparser.statement.delete.*;
@@ -124,6 +125,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
124125
| <K_APPLY:"APPLY">
125126
| <K_AS: "AS">
126127
| <K_ASC:"ASC">
128+
| <K_AUTHORIZATION:"AUTHORIZATION">
127129
| <K_BEGIN:"BEGIN">
128130
| <K_BETWEEN:"BETWEEN">
129131
| <K_BINARY: "BINARY">
@@ -152,6 +154,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
152154
| <K_DECLARE: "DECLARE">
153155
| <K_DATETIMELITERAL : ("DATE" | "TIME" | "TIMESTAMP") >
154156
| <K_DATE_LITERAL : ( "YEAR" | "MONTH" | "DAY" | "HOUR" | "MINUTE" | "SECOND" ) >
157+
| <K_DEFAULT : "DEFAULT">
155158
| <K_DEFERRABLE : "DEFERRABLE">
156159
| <K_DELAYED : "DELAYED">
157160
| <K_DELETE:"DELETE">
@@ -266,6 +269,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
266269
| <K_RIGHT:"RIGHT">
267270
| <K_ROW: "ROW">
268271
| <K_ROWS: "ROWS">
272+
| <K_SCHEMA: "SCHEMA">
269273
| <K_SELECT: ("SELECT" | "SEL")>
270274
| <K_SEMI : "SEMI">
271275
| <K_SEPARATOR:"SEPARATOR">
@@ -430,6 +434,9 @@ Statement SingleStatement() :
430434
LOOKAHEAD(CreateIndex())
431435
stm = CreateIndex()
432436
|
437+
LOOKAHEAD(CreateSchema())
438+
stm = CreateSchema()
439+
|
433440
LOOKAHEAD(CreateTable())
434441
stm = CreateTable()
435442
|
@@ -1136,7 +1143,7 @@ String RelObjectNameWithoutValue() :
11361143
| tk=<K_TEMP> | tk=<K_TEMPORARY> | tk=<K_TYPE> | tk=<K_ISNULL>
11371144
| tk=<K_ZONE> | tk=<K_COLUMNS> | tk=<K_DESCRIBE> | tk=<K_FN> | tk=<K_PATH>
11381145
| tk=<K_DATE_LITERAL> | tk=<K_NEXTVAL> | tk=<K_TRUE> | tk=<K_FALSE> | tk=<K_DUPLICATE>
1139-
| tk=<K_READ> | tk=<K_SIZE> | tk=<K_SESSION>
1146+
| tk=<K_READ> | tk=<K_SCHEMA> | tk=<K_SIZE> | tk=<K_SESSION>
11401147
| tk=<K_VIEW> | <K_NOLOCK>
11411148
/* | tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
11421149
)
@@ -3689,6 +3696,60 @@ ColumnDefinition ColumnDefinition(): {
36893696
}
36903697
}
36913698

3699+
CreateSchema CreateSchema():
3700+
{
3701+
Token tk = null;
3702+
CreateTable table = null;
3703+
CreateView view = null;
3704+
CreateSchema schema = new CreateSchema();
3705+
//schema.setSchemaName(System.getProperty("user.name"));
3706+
//schema.setAuthorization(System.getProperty("user.name"));
3707+
List<String> schemaPath = null;
3708+
List<Statement> statements = new ArrayList<Statement>();
3709+
}
3710+
{
3711+
<K_CREATE> <K_SCHEMA>
3712+
[ ( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setSchemaName(tk.image); } ]
3713+
[ <K_AUTHORIZATION>
3714+
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setAuthorization(tk.image); }
3715+
]
3716+
3717+
/* [ <K_DEFAULT> <K_CHARACTER> <K_SET> tk=<S_QUOTED_IDENTIFIER> | <S_IDENTIFIER> {} ] */
3718+
3719+
[schemaPath=PathSpecification() { schema.setSchemaPath(schemaPath); }]
3720+
3721+
(
3722+
LOOKAHEAD(3)
3723+
table = CreateTable()
3724+
{
3725+
table.getTable().setSchemaName(schema.getSchemaName());
3726+
schema.addStatement(table);
3727+
}
3728+
| view = CreateView()
3729+
{
3730+
view.getView().setSchemaName(schema.getSchemaName());
3731+
schema.addStatement(view);
3732+
}
3733+
3734+
)*
3735+
{
3736+
return schema;
3737+
}
3738+
}
3739+
3740+
List<String> PathSpecification():
3741+
{
3742+
Token tk;
3743+
List<String> pathList = new ArrayList<String>();
3744+
}
3745+
{
3746+
<K_PATH> (tk=<S_IDENTIFIER>|tk=<S_QUOTED_IDENTIFIER>) { pathList.add(tk.image); }
3747+
("," (tk=<S_IDENTIFIER>|tk=<S_QUOTED_IDENTIFIER>) { pathList.add(tk.image); })*
3748+
{
3749+
return pathList;
3750+
}
3751+
}
3752+
36923753
CreateTable CreateTable():
36933754
{
36943755
CreateTable createTable = new CreateTable();
@@ -3982,6 +4043,8 @@ List<String> CreateParameter():
39824043
|
39834044
tk=<K_PRIMARY> { param.add(tk.image); }
39844045
|
4046+
tk=<K_DEFAULT> { param.add(tk.image); }
4047+
|
39854048
tk=<K_FOREIGN> { param.add(tk.image); }
39864049
|
39874050
tk=<K_REFERENCES> { param.add(tk.image); }
@@ -4126,6 +4189,8 @@ Drop Drop():
41264189
tk=<K_INDEX>
41274190
|
41284191
tk=<K_VIEW>
4192+
|
4193+
tk=<K_SCHEMA>
41294194
)
41304195
{ drop.setType(tk.image); }
41314196

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2020 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create.schema;
11+
12+
import net.sf.jsqlparser.JSQLParserException;
13+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
14+
import org.junit.Test;
15+
16+
/**
17+
*
18+
* @author tw
19+
*/
20+
public class CreateSchemaTest {
21+
@Test
22+
public void testSimpleCreateSchema() throws JSQLParserException {
23+
assertSqlCanBeParsedAndDeparsed("CREATE SCHEMA myschema");
24+
}
25+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ public void testDropViewIssue545() throws JSQLParserException {
6363
public void testDropViewIssue545_2() throws JSQLParserException {
6464
assertSqlCanBeParsedAndDeparsed("DROP VIEW IF EXISTS myview");
6565
}
66+
67+
@Test
68+
public void testDropSchemaIssue855() throws JSQLParserException {
69+
assertSqlCanBeParsedAndDeparsed("DROP SCHEMA myschema");
70+
}
6671
}

0 commit comments

Comments
 (0)