1111
1212import java .io .Serializable ;
1313import java .util .ArrayList ;
14- import java .util .AbstractList ;
1514import java .util .Arrays ;
1615import java .util .Collection ;
1716import 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 );
0 commit comments