@@ -20,16 +20,13 @@ public class CreateIndex implements Statement {
2020
2121 private Table table ;
2222 private Index index ;
23+ private Index detachedOptions ;
2324 private List <String > tailParameters ;
2425 private boolean indexTypeBeforeOn = false ;
2526 private boolean usingIfNotExists = false ;
2627 private boolean concurrently ;
2728 private boolean only ;
2829 private boolean nullFiltered ;
29- private List <String > includeColumns ;
30- private Boolean nullsDistinct ;
31- private List <Index .Option > storageParameters ;
32- private String tableSpace ;
3330 private Expression where ;
3431
3532 public boolean isIndexTypeBeforeOn () {
@@ -80,35 +77,62 @@ public CreateIndex withNullFiltered(boolean nullFiltered) {
8077 }
8178
8279 public List <String > getIncludeColumns () {
83- return includeColumns ;
80+ Index options = getOptions ();
81+ return options == null ? null : options .getIncludeColumns ();
8482 }
8583
84+ /**
85+ * Copies the supplied list as {@link Index#setIncludeColumns(List)} does; null clears it.
86+ * {@link #getIncludeColumns()} returns the live, mutable list held by the index options.
87+ */
8688 public void setIncludeColumns (List <String > includeColumns ) {
87- this . includeColumns = includeColumns ;
89+ getOrCreateOptions (). setIncludeColumns ( includeColumns ) ;
8890 }
8991
9092 public Boolean getNullsDistinct () {
91- return nullsDistinct ;
93+ Index options = getOptions ();
94+ return options == null ? null : options .getNullsDistinct ();
9295 }
9396
9497 public void setNullsDistinct (Boolean nullsDistinct ) {
95- this . nullsDistinct = nullsDistinct ;
98+ getOrCreateOptions (). setNullsDistinct ( nullsDistinct ) ;
9699 }
97100
98101 public List <Index .Option > getStorageParameters () {
99- return storageParameters ;
102+ Index options = getOptions ();
103+ return options == null ? null : options .getStorageParameters ();
100104 }
101105
106+ /**
107+ * Copies the list container as {@link Index#setStorageParameters(List)} does, retaining the
108+ * option objects; null clears it. {@link #getStorageParameters()} returns the live, mutable
109+ * list held by the index options.
110+ */
102111 public void setStorageParameters (List <Index .Option > storageParameters ) {
103- this . storageParameters = storageParameters ;
112+ getOrCreateOptions (). setStorageParameters ( storageParameters ) ;
104113 }
105114
106115 public String getTableSpace () {
107- return tableSpace ;
116+ Index options = getOptions ();
117+ return options == null ? null : options .getTableSpace ();
108118 }
109119
110120 public void setTableSpace (String tableSpace ) {
111- this .tableSpace = tableSpace ;
121+ getOrCreateOptions ().setTableSpace (tableSpace );
122+ }
123+
124+ private Index getOptions () {
125+ return index == null ? detachedOptions : index ;
126+ }
127+
128+ private Index getOrCreateOptions () {
129+ if (index != null ) {
130+ return index ;
131+ }
132+ if (detachedOptions == null ) {
133+ detachedOptions = new Index ();
134+ }
135+ return detachedOptions ;
112136 }
113137
114138 public Expression getWhere () {
@@ -128,7 +152,40 @@ public Index getIndex() {
128152 return index ;
129153 }
130154
155+ /**
156+ * Replaces the index definition. Options supplied by the new index take precedence; omitted
157+ * options inherit the current statement options, including those set before an index was
158+ * attached. Passing null detaches the definition without discarding its options. The detached
159+ * option lists have independent containers, with their elements retained. Use the option
160+ * setters with null to clear individual options.
161+ */
131162 public void setIndex (Index index ) {
163+ Index previousOptions = getOptions ();
164+ if (index == null ) {
165+ if (this .index != null ) {
166+ detachedOptions = new Index ();
167+ detachedOptions .setIncludeColumns (previousOptions .getIncludeColumns ());
168+ detachedOptions .setNullsDistinct (previousOptions .getNullsDistinct ());
169+ detachedOptions .setStorageParameters (previousOptions .getStorageParameters ());
170+ detachedOptions .setTableSpace (previousOptions .getTableSpace ());
171+ }
172+ } else {
173+ if (previousOptions != null && previousOptions != index ) {
174+ if (index .getIncludeColumns () == null ) {
175+ index .setIncludeColumns (previousOptions .getIncludeColumns ());
176+ }
177+ if (index .getNullsDistinct () == null ) {
178+ index .setNullsDistinct (previousOptions .getNullsDistinct ());
179+ }
180+ if (index .getStorageParameters () == null ) {
181+ index .setStorageParameters (previousOptions .getStorageParameters ());
182+ }
183+ if (index .getTableSpace () == null ) {
184+ index .setTableSpace (previousOptions .getTableSpace ());
185+ }
186+ }
187+ detachedOptions = null ;
188+ }
132189 this .index = index ;
133190 }
134191
@@ -225,18 +282,18 @@ private void appendIndexColumns(StringBuilder buffer, Consumer<Expression> expre
225282
226283 private void appendPostgreSqlTail (StringBuilder buffer ,
227284 Consumer <Expression > expressionPrinter ) {
228- if (includeColumns != null ) {
229- buffer .append (" INCLUDE (" ).append (String .join (", " , includeColumns )).append (")" );
285+ if (getIncludeColumns () != null ) {
286+ buffer .append (" INCLUDE (" ).append (String .join (", " , getIncludeColumns () )).append (")" );
230287 }
231- if (nullsDistinct != null ) {
232- buffer .append (" NULLS " ).append (nullsDistinct ? "DISTINCT" : "NOT DISTINCT" );
288+ if (getNullsDistinct () != null ) {
289+ buffer .append (" NULLS " ).append (getNullsDistinct () ? "DISTINCT" : "NOT DISTINCT" );
233290 }
234- if (storageParameters != null ) {
291+ if (getStorageParameters () != null ) {
235292 buffer .append (" WITH " );
236- Index .Option .appendListTo (buffer , storageParameters , expressionPrinter );
293+ Index .Option .appendListTo (buffer , getStorageParameters () , expressionPrinter );
237294 }
238- if (tableSpace != null ) {
239- buffer .append (" TABLESPACE " ).append (tableSpace );
295+ if (getTableSpace () != null ) {
296+ buffer .append (" TABLESPACE " ).append (getTableSpace () );
240297 }
241298 if (where != null ) {
242299 buffer .append (" WHERE " );
0 commit comments