1010package net .sf .jsqlparser .statement .create .table ;
1111
1212import net .sf .jsqlparser .statement .imprt .ImportColumn ;
13- import net .sf .jsqlparser .statement .select .PlainSelect ;
1413import net .sf .jsqlparser .expression .Expression ;
1514
1615import java .io .Serializable ;
1918import java .util .Collection ;
2019import java .util .Collections ;
2120import java .util .List ;
22- import java .util .Optional ;
21+ import java .util .Objects ;
2322import java .util .function .Consumer ;
2423
2524/**
@@ -29,7 +28,6 @@ public class ColumnDefinition implements ImportColumn, TableElement, Serializabl
2928
3029 private String columnName ;
3130 private ColDataType colDataType ;
32- private List <String > columnSpecs ;
3331 private List <ColumnOption > columnOptions ;
3432 private boolean withOptions ;
3533
@@ -50,42 +48,54 @@ public ColumnDefinition(String columnName, ColDataType colDataType) {
5048
5149 public ColumnDefinition (String columnName , ColDataType colDataType , List <String > columnSpecs ) {
5250 this (columnName , colDataType );
53- this . columnSpecs = columnSpecs ;
51+ columnOptions = rawOptions ( columnSpecs ) ;
5452 }
5553
5654 /**
57- * Returns raw specifications, or a token snapshot when structured options are present. Use the
58- * option API or {@link #addColumnSpecs(Collection)} to append without discarding structured
59- * expressions, references and constraints. DEFAULT values use the expression's SQL rendering.
55+ * Returns an unmodifiable token snapshot for both raw and structured options, or null when
56+ * options are unset. Use {@link #getColumnOptions()} to edit the nodes, or
57+ * {@link #addColumnSpecs(Collection)} to append without discarding structured expressions,
58+ * references and constraints. DEFAULT values use the expression's SQL rendering.
6059 */
6160 public List <String > getColumnSpecs () {
6261 if (columnOptions != null ) {
6362 List <String > tokens = new ArrayList <>();
6463 for (ColumnOption option : columnOptions ) {
6564 tokens .addAll (option .getTokens ());
6665 }
67- return tokens ;
66+ return Collections . unmodifiableList ( tokens ) ;
6867 }
69- return columnSpecs ;
68+ return null ;
7069 }
7170
71+ /**
72+ * Replaces every option with opaque tokens, without parsing them. The supplied list is copied;
73+ * null clears the options, while an empty list stores an explicitly empty option list.
74+ */
7275 public void setColumnSpecs (List <String > list ) {
73- columnSpecs = list ;
74- columnOptions = null ;
76+ columnOptions = rawOptions (list );
77+ }
78+
79+ private static List <ColumnOption > rawOptions (List <String > list ) {
80+ List <ColumnOption > options = list == null ? null : new ArrayList <>();
81+ if (list != null && !list .isEmpty ()) {
82+ options .add (ColumnOption .raw (new ArrayList <>(list )));
83+ }
84+ return options ;
7585 }
7686
7787 /**
78- * Returns column options in source order, including defaults, references, generated columns,
79- * nullability, collation, comments, visibility and MySQL attributes. Unrecognized options
80- * remain raw. Structured option keywords use canonical capitalization when rendered.
88+ * Returns the mutable option list in source order, or null when options are unset. Opaque
89+ * specifications are included as {@link ColumnOption.Kind#OTHER} nodes. Structured option
90+ * keywords use canonical capitalization when rendered.
8191 */
8292 public List <ColumnOption > getColumnOptions () {
8393 return columnOptions ;
8494 }
8595
96+ /** Copies the list membership while retaining the supplied mutable option nodes. */
8697 public void setColumnOptions (List <ColumnOption > columnOptions ) {
87- this .columnOptions = columnOptions ;
88- this .columnSpecs = null ;
98+ this .columnOptions = columnOptions == null ? null : new ArrayList <>(columnOptions );
8999 }
90100
91101 public boolean isSerialDefaultValue () {
@@ -110,13 +120,12 @@ public ColumnDefinition withColumnOptions(List<ColumnOption> columnOptions) {
110120 }
111121
112122 public ColumnDefinition addColumnOptions (ColumnOption ... columnOptions ) {
113- List <ColumnOption > collection =
114- Optional .ofNullable (getColumnOptions ()).orElseGet (ArrayList ::new );
115- if (this .columnOptions == null && columnSpecs != null && !columnSpecs .isEmpty ()) {
116- collection .add (ColumnOption .raw (new ArrayList <>(columnSpecs )));
123+ Objects .requireNonNull (columnOptions , "columnOptions" );
124+ if (this .columnOptions == null ) {
125+ this .columnOptions = new ArrayList <>();
117126 }
118- Collections .addAll (collection , columnOptions );
119- return withColumnOptions ( collection ) ;
127+ Collections .addAll (this . columnOptions , columnOptions );
128+ return this ;
120129 }
121130
122131 public ColDataType getColDataType () {
@@ -170,8 +179,6 @@ protected void appendDataTypeAndSpecTo(StringBuilder builder,
170179 builder .append (' ' );
171180 option .appendTo (builder , expressionPrinter );
172181 }
173- } else if (columnSpecs != null && !columnSpecs .isEmpty ()) {
174- builder .append (' ' ).append (PlainSelect .getStringList (columnSpecs , false , false ));
175182 }
176183 }
177184
@@ -194,15 +201,15 @@ public ColumnDefinition addColumnSpecs(String... columnSpecs) {
194201 return addColumnSpecs (Arrays .asList (columnSpecs ));
195202 }
196203
204+ /** Appends copied opaque tokens after the existing nodes, preserving their structure. */
197205 public ColumnDefinition addColumnSpecs (Collection <String > columnSpecs ) {
198- if (columnOptions != null ) {
199- if (!columnSpecs .isEmpty ()) {
200- columnOptions .add (ColumnOption .raw (new ArrayList <>(columnSpecs )));
201- }
202- return this ;
206+ Objects .requireNonNull (columnSpecs , "columnSpecs" );
207+ if (columnOptions == null ) {
208+ columnOptions = new ArrayList <>();
203209 }
204- List <String > collection = Optional .ofNullable (getColumnSpecs ()).orElseGet (ArrayList ::new );
205- collection .addAll (columnSpecs );
206- return this .withColumnSpecs (collection );
210+ if (!columnSpecs .isEmpty ()) {
211+ columnOptions .add (ColumnOption .raw (new ArrayList <>(columnSpecs )));
212+ }
213+ return this ;
207214 }
208215}
0 commit comments