Skip to content

Commit 69e79cf

Browse files
committed
feat: share MySQL view options and definer account parsing
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent dac8159 commit 69e79cf

10 files changed

Lines changed: 379 additions & 51 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement;
11+
12+
import java.io.Serializable;
13+
import net.sf.jsqlparser.expression.StringValue;
14+
15+
/** Structured account shared by MySQL stored-object {@code DEFINER} clause. */
16+
public class MySqlDefiner implements Serializable {
17+
18+
private StringValue user;
19+
private StringValue host;
20+
private boolean currentUserParentheses;
21+
22+
public boolean isCurrentUserParentheses() {
23+
return currentUserParentheses;
24+
}
25+
26+
public void setCurrentUserParentheses(boolean currentUserParentheses) {
27+
this.currentUserParentheses = currentUserParentheses;
28+
}
29+
30+
31+
public StringValue getUser() {
32+
return user;
33+
}
34+
35+
public void setUser(StringValue user) {
36+
this.user = user;
37+
currentUserParentheses = false;
38+
}
39+
40+
public StringValue getHost() {
41+
return host;
42+
}
43+
44+
public void setHost(StringValue host) {
45+
this.host = host;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
StringBuilder builder = new StringBuilder().append(user);
51+
if (currentUserParentheses) {
52+
builder.append("()");
53+
}
54+
if (host != null) {
55+
builder.append("@").append(host);
56+
}
57+
return builder.toString();
58+
}
59+
60+
public MySqlDefiner withUser(StringValue user) {
61+
setUser(user);
62+
return this;
63+
}
64+
65+
public MySqlDefiner withHost(StringValue host) {
66+
setHost(host);
67+
return this;
68+
}
69+
}

‎src/main/java/net/sf/jsqlparser/statement/create/trigger/TriggerDefiner.java‎

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,18 @@
99
*/
1010
package net.sf.jsqlparser.statement.create.trigger;
1111

12-
import java.io.Serializable;
1312
import net.sf.jsqlparser.expression.StringValue;
13+
import net.sf.jsqlparser.statement.MySqlDefiner;
1414

15-
/** Structured account used by a MySQL trigger {@code DEFINER} clause. */
16-
public class TriggerDefiner implements Serializable {
17-
18-
private StringValue user;
19-
private StringValue host;
20-
21-
public StringValue getUser() {
22-
return user;
23-
}
24-
25-
public void setUser(StringValue user) {
26-
this.user = user;
27-
}
28-
29-
public StringValue getHost() {
30-
return host;
31-
}
32-
33-
public void setHost(StringValue host) {
34-
this.host = host;
35-
}
36-
15+
/** Retains the trigger API while sharing MySQL DEFINER account handling with views. */
16+
public class TriggerDefiner extends MySqlDefiner {
3717
@Override
38-
public String toString() {
39-
StringBuilder builder = new StringBuilder().append(user);
40-
if (host != null) {
41-
builder.append("@").append(host);
42-
}
43-
return builder.toString();
44-
}
45-
4618
public TriggerDefiner withUser(StringValue user) {
4719
setUser(user);
4820
return this;
4921
}
5022

23+
@Override
5124
public TriggerDefiner withHost(StringValue host) {
5225
setHost(host);
5326
return this;

‎src/main/java/net/sf/jsqlparser/statement/create/view/AlterView.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@
2323

2424
public class AlterView implements Statement {
2525

26+
private MySqlViewOptions mySqlOptions;
27+
28+
public MySqlViewOptions getMySqlOptions() {
29+
return mySqlOptions;
30+
}
31+
32+
public void setMySqlOptions(MySqlViewOptions mySqlOptions) {
33+
this.mySqlOptions = mySqlOptions;
34+
}
35+
36+
public void appendMySqlOptionsTo(StringBuilder builder) {
37+
if (mySqlOptions != null) {
38+
mySqlOptions.appendTo(builder);
39+
}
40+
}
41+
42+
private CreateView.CheckOption checkOption;
43+
44+
public CreateView.CheckOption getCheckOption() {
45+
return checkOption;
46+
}
47+
48+
public void setCheckOption(CreateView.CheckOption checkOption) {
49+
this.checkOption = checkOption;
50+
}
51+
52+
public void appendCheckOptionTo(StringBuilder sql) {
53+
CreateView.appendCheckOptionTo(sql, checkOption);
54+
}
55+
2656
private Table view;
2757
private Select select;
2858
private boolean useReplace = false;
@@ -68,12 +98,14 @@ public String toString() {
6898
} else {
6999
sql = new StringBuilder("ALTER ");
70100
}
101+
appendMySqlOptionsTo(sql);
71102
sql.append("VIEW ");
72103
sql.append(view);
73104
if (columnNames != null) {
74105
sql.append(PlainSelect.getStringList(columnNames, true, true));
75106
}
76107
sql.append(" AS ").append(select);
108+
appendCheckOptionTo(sql);
77109
return sql.toString();
78110
}
79111

‎src/main/java/net/sf/jsqlparser/statement/create/view/CreateView.java‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323

2424
public class CreateView implements Statement {
2525

26+
private MySqlViewOptions mySqlOptions;
27+
28+
public MySqlViewOptions getMySqlOptions() {
29+
return mySqlOptions;
30+
}
31+
32+
public void setMySqlOptions(MySqlViewOptions mySqlOptions) {
33+
this.mySqlOptions = mySqlOptions;
34+
}
35+
36+
public void appendMySqlOptionsTo(StringBuilder builder) {
37+
if (mySqlOptions != null) {
38+
mySqlOptions.appendTo(builder);
39+
}
40+
}
41+
2642
private Table view;
2743
private Select select;
2844
private boolean orReplace = false;
@@ -271,6 +287,7 @@ public String toString() {
271287
if (isOrReplace()) {
272288
sql.append("OR REPLACE ");
273289
}
290+
appendMySqlOptionsTo(sql);
274291
appendForceOptionIfApplicable(sql);
275292
if (secure) {
276293
sql.append("SECURE ");
@@ -328,14 +345,18 @@ public void appendParametersTo(StringBuilder sql) {
328345
}
329346
}
330347

331-
public void appendOptionsAfterQueryTo(StringBuilder sql) {
348+
public static void appendCheckOptionTo(StringBuilder sql, CheckOption checkOption) {
332349
if (checkOption != null) {
333350
sql.append(" WITH ");
334351
if (checkOption != CheckOption.DEFAULT) {
335352
sql.append(checkOption).append(' ');
336353
}
337354
sql.append("CHECK OPTION");
338355
}
356+
}
357+
358+
public void appendOptionsAfterQueryTo(StringBuilder sql) {
359+
appendCheckOptionTo(sql, checkOption);
339360
if (withData != null) {
340361
sql.append(withData ? " WITH DATA" : " WITH NO DATA");
341362
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 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.view;
11+
12+
import java.io.Serializable;
13+
import net.sf.jsqlparser.statement.MySqlDefiner;
14+
15+
/** Prefix options shared by MySQL CREATE VIEW and ALTER VIEW. */
16+
public class MySqlViewOptions implements Serializable {
17+
public enum Algorithm {
18+
UNDEFINED, MERGE, TEMPTABLE
19+
}
20+
21+
public enum Security {
22+
DEFINER, INVOKER
23+
}
24+
25+
private Algorithm algorithm;
26+
private MySqlDefiner definer;
27+
private Security security;
28+
29+
public Algorithm getAlgorithm() {
30+
return algorithm;
31+
}
32+
33+
public void setAlgorithm(Algorithm algorithm) {
34+
this.algorithm = algorithm;
35+
}
36+
37+
public MySqlDefiner getDefiner() {
38+
return definer;
39+
}
40+
41+
public void setDefiner(MySqlDefiner definer) {
42+
this.definer = definer;
43+
}
44+
45+
public Security getSecurity() {
46+
return security;
47+
}
48+
49+
public void setSecurity(Security security) {
50+
this.security = security;
51+
}
52+
53+
public void appendTo(StringBuilder builder) {
54+
if (algorithm != null) {
55+
builder.append("ALGORITHM = ").append(algorithm).append(' ');
56+
}
57+
if (definer != null) {
58+
builder.append("DEFINER = ").append(definer).append(' ');
59+
}
60+
if (security != null) {
61+
builder.append("SQL SECURITY ").append(security).append(' ');
62+
}
63+
}
64+
65+
@Override
66+
public String toString() {
67+
StringBuilder sql = new StringBuilder();
68+
appendTo(sql);
69+
return sql.toString().trim();
70+
}
71+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,10 @@ public void visit(TableFunction tableFunction) {
19471947

19481948
@Override
19491949
public <S> Void visit(AlterView alterView, S context) {
1950-
throwUnsupported(alterView);
1950+
visit(alterView.getView(), context);
1951+
if (alterView.getSelect() != null) {
1952+
alterView.getSelect().accept((SelectVisitor<?>) this, context);
1953+
}
19511954
return null;
19521955
}
19531956

‎src/main/java/net/sf/jsqlparser/util/deparser/AlterViewDeParser.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ public void deParse(AlterView alterView) {
3737
} else {
3838
builder.append("ALTER ");
3939
}
40+
alterView.appendMySqlOptionsTo(builder);
4041
builder.append("VIEW ").append(alterView.getView().getFullyQualifiedName());
4142
if (alterView.getColumnNames() != null) {
4243
builder.append(PlainSelect.getStringList(alterView.getColumnNames(), true, true));
4344
}
4445
builder.append(" AS ");
4546

4647
alterView.getSelect().accept(selectVisitor, null);
48+
alterView.appendCheckOptionTo(builder);
4749
}
4850

4951
}

‎src/main/java/net/sf/jsqlparser/util/deparser/CreateViewDeParser.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void deParse(CreateView createView) {
4141
if (createView.isOrReplace()) {
4242
builder.append("OR REPLACE ");
4343
}
44+
createView.appendMySqlOptionsTo(builder);
4445
switch (createView.getForce()) {
4546
case FORCE:
4647
builder.append("FORCE ");

0 commit comments

Comments
 (0)