Skip to content

Commit

Permalink
fix: assign Enum case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
manticore-projects committed May 2, 2023
1 parent 4ddc835 commit fc577ca
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public DateTimeLiteralExpression withType(DateTime type) {
}

public enum DateTime {
DATE, TIME, TIMESTAMP, TIMESTAMPTZ;
DATE("DATE")
, TIME("TIME")
, TIMESTAMP("TIMESTAMP")
, TIMESTAMPTZ("TIMESTAMPTZ");

public String getDateTime() {
return dateTime;
}

private final String dateTime;

DateTime(String dateTime) {
this.dateTime = dateTime;
}

private final DateTime from(String dateTimeStr) {
return Enum.valueOf(DateTime.class, dateTimeStr.toUpperCase());
}
}
}
32 changes: 18 additions & 14 deletions src/main/java/net/sf/jsqlparser/statement/ReferentialAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,22 @@ public boolean equals(Object obj) {
}

public enum Type {
DELETE,
UPDATE
DELETE("DELETE"),
UPDATE("UPDATE");

public String getType() {
return type;
}

private final String type;

Type(String type) {
this.type = type;
}

public static Type from(String name) {
return Enum.valueOf(Type.class, name.toUpperCase());
}
}

public enum Action {
Expand All @@ -108,23 +122,13 @@ public enum Action {
* @param action
* @return the {@link Action}, if found, otherwise <code>null</code>
*/
public static Action byAction(String action) {
for (Action a : values()) {
if (a.getAction().equals(action)) {
return a;
}
}
return null;
public static Action from(String action) {
return Enum.valueOf(Action.class, action.toUpperCase());
}

public String getAction() {
return action;
}

@Override
public String toString() {
return action;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
*/
package net.sf.jsqlparser.statement.create.table;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.ReferentialAction;
import net.sf.jsqlparser.statement.ReferentialAction.Action;
import net.sf.jsqlparser.statement.ReferentialAction.Type;
import net.sf.jsqlparser.statement.select.PlainSelect;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.ReferentialAction;
import net.sf.jsqlparser.statement.ReferentialAction.Action;
import net.sf.jsqlparser.statement.ReferentialAction.Type;
import net.sf.jsqlparser.statement.select.PlainSelect;

public class ForeignKeyIndex extends NamedConstraint {

Expand Down Expand Up @@ -96,7 +97,7 @@ public void setOnDeleteReferenceOption(String onDeleteReferenceOption) {
if (onDeleteReferenceOption == null) {
removeReferentialAction(Type.DELETE);
} else {
setReferentialAction(Type.DELETE, Action.byAction(onDeleteReferenceOption));
setReferentialAction(Type.DELETE, Action.from(onDeleteReferenceOption));
}
}

Expand All @@ -111,7 +112,7 @@ public void setOnUpdateReferenceOption(String onUpdateReferenceOption) {
if (onUpdateReferenceOption == null) {
removeReferentialAction(Type.UPDATE);
} else {
setReferentialAction(Type.UPDATE, Action.byAction(onUpdateReferenceOption));
setReferentialAction(Type.UPDATE, Action.from(onUpdateReferenceOption));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@
package net.sf.jsqlparser.statement.create.view;

public enum AutoRefreshOption {
NONE,
NONE("NONE"),

YES,
YES("YESY"),

NO("NO");

public String getOption() {
return option;
}

private final String option;

AutoRefreshOption(String option) {
this.option = option;
}

public static AutoRefreshOption from(String option) {
return Enum.valueOf(AutoRefreshOption.class, option.toUpperCase());
}

NO
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,22 @@
package net.sf.jsqlparser.statement.insert;

public enum InsertModifierPriority {
LOW_PRIORITY, DELAYED, HIGH_PRIORITY, IGNORE
LOW_PRIORITY("LOW_PRIORITY")
, DELAYED("DELAYED")
, HIGH_PRIORITY("HIGH_PRIORITY")
, IGNORE("IGNORE");

public String getPriority() {
return priority;
}

private final String priority;

InsertModifierPriority(String priority) {
this.priority = priority;
}

public final static InsertModifierPriority from(String priority) {
return Enum.valueOf(InsertModifierPriority.class, priority.toUpperCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ public KSQLJoinWindow withAfterTimeUnit(TimeUnit afterTimeUnit) {
this.setAfterTimeUnit(afterTimeUnit);
return this;
}

public final static TimeUnit from(String timeUnitStr) {
return Enum.valueOf(TimeUnit.class, timeUnitStr.toUpperCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.operators.conditional.XorExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
Expand Down Expand Up @@ -45,12 +44,12 @@ public void testInExpressionProblem() throws JSQLParserException {
public void visit(InExpression expr) {
super.visit(expr);
exprList.add(expr.getLeftExpression());
exprList.add(expr.getRightItemsList());
exprList.add(expr.getRightExpression());
}
});

assertTrue(exprList.get(0) instanceof Expression);
assertTrue(exprList.get(1) instanceof ExpressionList);
assertTrue(exprList.get(1) instanceof RowConstructor);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.sf.jsqlparser.expression.operators.relational;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class InExpressionTest {

@Test
void testOracleInWithoutBrackets() throws JSQLParserException {
String sqlStr="select 1 from dual where a in 1 ";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}


@Test
void testOracleInWithBrackets() throws JSQLParserException {
String sqlStr="select 1 from dual where a in (1) ";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ReferentialActionTest {

@Test
void testCaseSensitivity() throws JSQLParserException {
String sqlStr= "CREATE TABLE DATABASES\n"
+ "(\n"
+ "NAME VARCHAR(50) NOT NULL,\n"
+ "OWNER VARCHAR(50) NOT NULL,\n"
+ "PRIMARY KEY (NAME),\n"
+ "FOREIGN KEY(OWNER) REFERENCES USERS (USERNAME) ON delete cascade\n"
+ ")";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
*/
package net.sf.jsqlparser.util.deparser;

import java.util.ArrayList;
import java.util.List;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.execute.Execute.ExecType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;

Expand All @@ -42,12 +45,12 @@ public void shouldDeParseExecute() {
Execute execute = new Execute();
String name = "name";

List<Expression> expressions = new ArrayList<>();
ParenthesedExpressionList expressions = new ParenthesedExpressionList();
expressions.add(new JdbcParameter());
expressions.add(new JdbcParameter());

execute.withName(name)
.withExecType(ExecType.EXECUTE).withParenthesis(true)
.withExecType(ExecType.EXECUTE)
.withExprList(new ExpressionList().withExpressions(expressions));

executeDeParser.deParse(execute);
Expand Down

0 comments on commit fc577ca

Please sign in to comment.