Skip to content

Commit cc6e528

Browse files
committed
Simplify ALTER key adapters and document replacement semantics
1 parent b289ef6 commit cc6e528

4 files changed

Lines changed: 162 additions & 130 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java‎

Lines changed: 31 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import java.io.Serializable;
1313
import java.util.ArrayList;
14-
import java.util.AbstractList;
1514
import java.util.Arrays;
1615
import java.util.Collection;
1716
import java.util.Collections;
@@ -506,28 +505,49 @@ public void setUsingIfExists(boolean usingIfExists) {
506505
this.usingIfExists = usingIfExists;
507506
}
508507

509-
/** Returns a live view when this action has a structured PRIMARY_KEY definition. */
508+
/**
509+
* Returns a snapshot of the rendered key elements for a structured PRIMARY_KEY, or the legacy
510+
* list otherwise. Changing the snapshot does not change the index; use
511+
* {@link #setPkColumns(List)} to replace its keys, or {@link Index#getColumns()} for structured
512+
* edits.
513+
*/
510514
public List<String> getPkColumns() {
511-
return hasKeyIndex(Index.Kind.PRIMARY_KEY) ? new KeyColumnNames(index) : pkColumns;
515+
return hasKeyIndex(Index.Kind.PRIMARY_KEY) ? index.getColumnsNames() : pkColumns;
512516
}
513517

518+
/**
519+
* Replaces a structured PRIMARY_KEY's elements using {@link Index#setColumnsNames(List)}. The
520+
* strings are not parsed and existing element attributes are not retained, even when the
521+
* rendered SQL is unchanged. Use {@link Index#setColumns(List)} to preserve structured
522+
* elements. Without a matching index, stores the legacy list.
523+
*/
514524
public void setPkColumns(List<String> pkColumns) {
515525
if (hasKeyIndex(Index.Kind.PRIMARY_KEY)) {
516-
replaceKeyColumns(pkColumns);
526+
index.setColumnsNames(pkColumns);
517527
this.pkColumns = null;
518528
} else {
519529
this.pkColumns = pkColumns;
520530
}
521531
}
522532

523-
/** Returns a live view when this action has a structured UNIQUE definition. */
533+
/**
534+
* Returns a snapshot of the rendered key elements for a structured UNIQUE, or the legacy list
535+
* otherwise. Changing the snapshot does not change the index; use {@link #setUkColumns(List)}
536+
* to replace its keys, or {@link Index#getColumns()} for structured edits.
537+
*/
524538
public List<String> getUkColumns() {
525-
return hasKeyIndex(Index.Kind.UNIQUE) ? new KeyColumnNames(index) : ukColumns;
539+
return hasKeyIndex(Index.Kind.UNIQUE) ? index.getColumnsNames() : ukColumns;
526540
}
527541

542+
/**
543+
* Replaces a structured UNIQUE's elements using {@link Index#setColumnsNames(List)}. The
544+
* strings are not parsed and existing element attributes are not retained, even when the
545+
* rendered SQL is unchanged. Use {@link Index#setColumns(List)} to preserve structured
546+
* elements. Without a matching index, stores the legacy list.
547+
*/
528548
public void setUkColumns(List<String> ukColumns) {
529549
if (hasKeyIndex(Index.Kind.UNIQUE)) {
530-
replaceKeyColumns(ukColumns);
550+
index.setColumnsNames(ukColumns);
531551
this.ukColumns = null;
532552
} else {
533553
this.ukColumns = ukColumns;
@@ -538,75 +558,6 @@ private boolean hasKeyIndex(Index.Kind kind) {
538558
return index != null && index.getKind() == kind;
539559
}
540560

541-
private void replaceKeyColumns(List<String> names) {
542-
List<Index.ColumnParams> replacement = new ArrayList<>();
543-
if (names != null) {
544-
List<Index.ColumnParams> previous = index.getColumns();
545-
for (int i = 0; i < names.size(); i++) {
546-
String name = names.get(i);
547-
if (previous != null && i < previous.size()) {
548-
Index.ColumnParams previousColumn = previous.get(i);
549-
if (previousColumn.toString().equals(name)) {
550-
replacement.add(previousColumn);
551-
continue;
552-
}
553-
}
554-
replacement.add(new Index.ColumnParams(name));
555-
}
556-
}
557-
index.setColumns(replacement);
558-
}
559-
560-
/** Adapts the legacy mutable name list without copying structured expressions to strings. */
561-
private static class KeyColumnNames extends AbstractList<String> {
562-
private final Index index;
563-
564-
KeyColumnNames(Index index) {
565-
this.index = index;
566-
}
567-
568-
@Override
569-
public String get(int position) {
570-
Index.ColumnParams column = index.getColumns().get(position);
571-
return column.toString();
572-
}
573-
574-
@Override
575-
public int size() {
576-
return index.getColumns() == null ? 0 : index.getColumns().size();
577-
}
578-
579-
@Override
580-
public String set(int position, String name) {
581-
String previous = get(position);
582-
if (!previous.equals(name)) {
583-
List<Index.ColumnParams> columns = new ArrayList<>(index.getColumns());
584-
columns.set(position, new Index.ColumnParams(name));
585-
index.setColumns(columns);
586-
}
587-
return previous;
588-
}
589-
590-
@Override
591-
public void add(int position, String name) {
592-
List<Index.ColumnParams> columns = index.getColumns() == null ? new ArrayList<>()
593-
: new ArrayList<>(index.getColumns());
594-
columns.add(position, new Index.ColumnParams(name));
595-
index.setColumns(columns);
596-
modCount++;
597-
}
598-
599-
@Override
600-
public String remove(int position) {
601-
List<Index.ColumnParams> columns = new ArrayList<>(index.getColumns());
602-
Index.ColumnParams removedColumn = columns.remove(position);
603-
String previous = removedColumn.toString();
604-
index.setColumns(columns);
605-
modCount++;
606-
return previous;
607-
}
608-
}
609-
610561
public String getUkName() {
611562
return ukName;
612563
}
@@ -1443,24 +1394,28 @@ public AlterExpression withColumnOldName(String columnOldName) {
14431394
return this;
14441395
}
14451396

1397+
/** Adds strings using the replacement semantics of {@link #setPkColumns(List)}. */
14461398
public AlterExpression addPkColumns(String... pkColumns) {
14471399
List<String> collection = Optional.ofNullable(getPkColumns()).orElseGet(ArrayList::new);
14481400
Collections.addAll(collection, pkColumns);
14491401
return this.withPkColumns(collection);
14501402
}
14511403

1404+
/** Adds strings using the replacement semantics of {@link #setPkColumns(List)}. */
14521405
public AlterExpression addPkColumns(Collection<String> pkColumns) {
14531406
List<String> collection = Optional.ofNullable(getPkColumns()).orElseGet(ArrayList::new);
14541407
collection.addAll(pkColumns);
14551408
return this.withPkColumns(collection);
14561409
}
14571410

1411+
/** Adds strings using the replacement semantics of {@link #setUkColumns(List)}. */
14581412
public AlterExpression addUkColumns(String... ukColumns) {
14591413
List<String> collection = Optional.ofNullable(getUkColumns()).orElseGet(ArrayList::new);
14601414
Collections.addAll(collection, ukColumns);
14611415
return this.withUkColumns(collection);
14621416
}
14631417

1418+
/** Adds strings using the replacement semantics of {@link #setUkColumns(List)}. */
14641419
public AlterExpression addUkColumns(Collection<String> ukColumns) {
14651420
List<String> collection = Optional.ofNullable(getUkColumns()).orElseGet(ArrayList::new);
14661421
collection.addAll(ukColumns);

‎src/main/java/net/sf/jsqlparser/statement/create/table/Index.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ public void appendConstraintAttributesTo(StringBuilder sql) {
134134
}
135135
}
136136

137+
/**
138+
* Returns a mutable snapshot of the rendered key elements, including their options. An index
139+
* without columns returns an empty snapshot. Use {@link #getColumns()} for structured edits.
140+
*/
137141
public List<String> getColumnsNames() {
138-
return columns.stream()
139-
.map(ColumnParams::toString)
140-
.collect(toList());
142+
return columns == null ? new ArrayList<>()
143+
: columns.stream()
144+
.map(ColumnParams::toString)
145+
.collect(toList());
141146
}
142147

148+
/**
149+
* Replaces all key elements with plain {@link ColumnParams} wrapping the supplied strings, or
150+
* clears them for null. The strings are not parsed and existing expressions and element options
151+
* are not retained. Use {@link #setColumns(List)} to preserve structured elements.
152+
*/
143153
public void setColumnsNames(List<String> list) {
144154
if (list == null) {
145155
this.columns = Collections.emptyList();

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,13 +1562,10 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
15621562
return false;
15631563
}
15641564

1565-
/** Keeps legacy ALTER key accessors populated from the common structured definition. */
1565+
/** Attaches the structured index and populates the remaining legacy constraint metadata. */
15661566
private static void setAlterTableIndex(AlterExpression alterExp, Index index) {
15671567
alterExp.setIndex(index);
1568-
if (index.getKind() == Index.Kind.PRIMARY_KEY) {
1569-
alterExp.setPkColumns(index.getColumnsNames());
1570-
} else if (index.getKind() == Index.Kind.UNIQUE) {
1571-
alterExp.setUkColumns(index.getColumnsNames());
1568+
if (index.getKind() == Index.Kind.UNIQUE) {
15721569
alterExp.setUkName(index instanceof NamedConstraint
15731570
? ((NamedConstraint) index).getIndexName() : index.getName());
15741571
alterExp.setUk(index.getType().toUpperCase(Locale.ROOT).contains("KEY"));

0 commit comments

Comments
 (0)