Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/MySqlDefiner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement;

import java.io.Serializable;
import net.sf.jsqlparser.expression.StringValue;

/** Structured account shared by MySQL stored-object {@code DEFINER} clause. */
public class MySqlDefiner implements Serializable {

private StringValue user;
private StringValue host;
private boolean currentUserParentheses;

public boolean isCurrentUserParentheses() {
return currentUserParentheses;
}

public void setCurrentUserParentheses(boolean currentUserParentheses) {
this.currentUserParentheses = currentUserParentheses;
}


public StringValue getUser() {
return user;
}

public void setUser(StringValue user) {
this.user = user;
currentUserParentheses = false;
}

public StringValue getHost() {
return host;
}

public void setHost(StringValue host) {
this.host = host;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder().append(user);
if (currentUserParentheses) {
builder.append("()");
}
if (host != null) {
builder.append("@").append(host);
}
return builder.toString();
}

public MySqlDefiner withUser(StringValue user) {
setUser(user);
return this;
}

public MySqlDefiner withHost(StringValue host) {
setHost(host);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,18 @@
*/
package net.sf.jsqlparser.statement.create.trigger;

import java.io.Serializable;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.statement.MySqlDefiner;

/** Structured account used by a MySQL trigger {@code DEFINER} clause. */
public class TriggerDefiner implements Serializable {

private StringValue user;
private StringValue host;

public StringValue getUser() {
return user;
}

public void setUser(StringValue user) {
this.user = user;
}

public StringValue getHost() {
return host;
}

public void setHost(StringValue host) {
this.host = host;
}

/** Retains the trigger API while sharing MySQL DEFINER account handling with views. */
public class TriggerDefiner extends MySqlDefiner {
@Override
public String toString() {
StringBuilder builder = new StringBuilder().append(user);
if (host != null) {
builder.append("@").append(host);
}
return builder.toString();
}

public TriggerDefiner withUser(StringValue user) {
setUser(user);
return this;
}

@Override
public TriggerDefiner withHost(StringValue host) {
setHost(host);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@

public class AlterView implements Statement {

private MySqlViewOptions mySqlOptions;

public MySqlViewOptions getMySqlOptions() {
return mySqlOptions;
}

public void setMySqlOptions(MySqlViewOptions mySqlOptions) {
this.mySqlOptions = mySqlOptions;
}

public void appendMySqlOptionsTo(StringBuilder builder) {
if (mySqlOptions != null) {
mySqlOptions.appendTo(builder);
}
}

private CreateView.CheckOption checkOption;

public CreateView.CheckOption getCheckOption() {
return checkOption;
}

public void setCheckOption(CreateView.CheckOption checkOption) {
this.checkOption = checkOption;
}

public void appendCheckOptionTo(StringBuilder sql) {
CreateView.appendCheckOptionTo(sql, checkOption);
}

private Table view;
private Select select;
private boolean useReplace = false;
Expand Down Expand Up @@ -68,12 +98,14 @@ public String toString() {
} else {
sql = new StringBuilder("ALTER ");
}
appendMySqlOptionsTo(sql);
sql.append("VIEW ");
sql.append(view);
if (columnNames != null) {
sql.append(PlainSelect.getStringList(columnNames, true, true));
}
sql.append(" AS ").append(select);
appendCheckOptionTo(sql);
return sql.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@

public class CreateView implements Statement {

private MySqlViewOptions mySqlOptions;

public MySqlViewOptions getMySqlOptions() {
return mySqlOptions;
}

public void setMySqlOptions(MySqlViewOptions mySqlOptions) {
this.mySqlOptions = mySqlOptions;
}

public void appendMySqlOptionsTo(StringBuilder builder) {
if (mySqlOptions != null) {
mySqlOptions.appendTo(builder);
}
}

private Table view;
private Select select;
private boolean orReplace = false;
Expand Down Expand Up @@ -271,6 +287,7 @@ public String toString() {
if (isOrReplace()) {
sql.append("OR REPLACE ");
}
appendMySqlOptionsTo(sql);
appendForceOptionIfApplicable(sql);
if (secure) {
sql.append("SECURE ");
Expand Down Expand Up @@ -328,14 +345,18 @@ public void appendParametersTo(StringBuilder sql) {
}
}

public void appendOptionsAfterQueryTo(StringBuilder sql) {
public static void appendCheckOptionTo(StringBuilder sql, CheckOption checkOption) {
if (checkOption != null) {
sql.append(" WITH ");
if (checkOption != CheckOption.DEFAULT) {
sql.append(checkOption).append(' ');
}
sql.append("CHECK OPTION");
}
}

public void appendOptionsAfterQueryTo(StringBuilder sql) {
appendCheckOptionTo(sql, checkOption);
if (withData != null) {
sql.append(withData ? " WITH DATA" : " WITH NO DATA");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.view;

import java.io.Serializable;
import net.sf.jsqlparser.statement.MySqlDefiner;

/** Prefix options shared by MySQL CREATE VIEW and ALTER VIEW. */
public class MySqlViewOptions implements Serializable {
public enum Algorithm {
UNDEFINED, MERGE, TEMPTABLE
}

public enum Security {
DEFINER, INVOKER
}

private Algorithm algorithm;
private MySqlDefiner definer;
private Security security;

public Algorithm getAlgorithm() {
return algorithm;
}

public void setAlgorithm(Algorithm algorithm) {
this.algorithm = algorithm;
}

public MySqlDefiner getDefiner() {
return definer;
}

public void setDefiner(MySqlDefiner definer) {
this.definer = definer;
}

public Security getSecurity() {
return security;
}

public void setSecurity(Security security) {
this.security = security;
}

public void appendTo(StringBuilder builder) {
if (algorithm != null) {
builder.append("ALGORITHM = ").append(algorithm).append(' ');
}
if (definer != null) {
builder.append("DEFINER = ").append(definer).append(' ');
}
if (security != null) {
builder.append("SQL SECURITY ").append(security).append(' ');
}
}

@Override
public String toString() {
StringBuilder sql = new StringBuilder();
appendTo(sql);
return sql.toString().trim();
}
}
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,10 @@ public void visit(TableFunction tableFunction) {

@Override
public <S> Void visit(AlterView alterView, S context) {
throwUnsupported(alterView);
visit(alterView.getView(), context);
if (alterView.getSelect() != null) {
alterView.getSelect().accept((SelectVisitor<?>) this, context);
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public void deParse(AlterView alterView) {
} else {
builder.append("ALTER ");
}
alterView.appendMySqlOptionsTo(builder);
builder.append("VIEW ").append(alterView.getView().getFullyQualifiedName());
if (alterView.getColumnNames() != null) {
builder.append(PlainSelect.getStringList(alterView.getColumnNames(), true, true));
}
builder.append(" AS ");

alterView.getSelect().accept(selectVisitor, null);
alterView.appendCheckOptionTo(builder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void deParse(CreateView createView) {
if (createView.isOrReplace()) {
builder.append("OR REPLACE ");
}
createView.appendMySqlOptionsTo(builder);
switch (createView.getForce()) {
case FORCE:
builder.append("FORCE ");
Expand Down
Loading
Loading