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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.statement.ReferentialAction;
import net.sf.jsqlparser.statement.ReferentialAction.Action;
Expand Down Expand Up @@ -59,6 +58,7 @@ public class AlterExpression implements Serializable {
private String fkSourceTable;
private List<String> fkSourceColumns;
private boolean uk;
private boolean ukTypeSpecified;
private boolean useEqual;

private List<String> partitions;
Expand Down Expand Up @@ -533,6 +533,15 @@ public boolean getUk() {

public void setUk(boolean uk) {
this.uk = uk;
this.ukTypeSpecified = true;
}

public boolean isUkTypeSpecified() {
return ukTypeSpecified;
}

public void setUkTypeSpecified(boolean ukTypeSpecified) {
this.ukTypeSpecified = ukTypeSpecified;
}

public boolean isUseIfNotExists() {
Expand Down Expand Up @@ -929,10 +938,14 @@ public String toString() {
} else if (ukColumns != null) {
b.append("UNIQUE");
if (ukName != null) {
if (getUk()) {
b.append(" KEY ");
if (isUkTypeSpecified()) {
if (getUk()) {
b.append(" KEY ");
} else {
b.append(" INDEX ");
}
} else {
b.append(" INDEX ");
b.append(" ");
}
b.append(ukName);
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -10220,7 +10220,20 @@ AlterExpression AlterExpression():
")"
)
|
( <K_UNIQUE> ((<K_KEY> { alterExp.setUk(true); } | <K_INDEX>) (tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { alterExp.setUkName(tk.image); } )?
(
<K_UNIQUE>
(
(
<K_KEY> { alterExp.setUk(true); }
| <K_INDEX> { alterExp.setUk(false); }
)
[ (tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { alterExp.setUkName(tk.image); } ]
|
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) {
alterExp.setUkTypeSpecified(false);
alterExp.setUkName(tk.image);
}
)?
columnNames=ColumnsNamesList() { alterExp.setUkColumns(columnNames); }
[
<K_USING> { alterExp.addParameters("USING"); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ public void testAlterTableUniqueKey() throws JSQLParserException {
"ALTER TABLE `schema_migrations` ADD UNIQUE KEY `unique_schema_migrations` (`version`)");
}

@Test
public void testAlterTableUniqueNamedWithoutKeyword() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE `goods` ADD UNIQUE `aaa` (`cate_id`)");
}

@Test
public void testAlterTableForgeignKey() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
Expand Down