1111
1212import net .sf .jsqlparser .statement .imprt .ImportColumn ;
1313import net .sf .jsqlparser .statement .select .PlainSelect ;
14+ import net .sf .jsqlparser .expression .Expression ;
1415
1516import java .io .Serializable ;
1617import java .util .ArrayList ;
1920import java .util .Collections ;
2021import java .util .List ;
2122import java .util .Optional ;
23+ import java .util .function .Consumer ;
2224
2325/**
2426 * Globally used definition class for columns.
@@ -54,7 +56,7 @@ public ColumnDefinition(String columnName, ColDataType colDataType, List<String>
5456 /**
5557 * Returns raw specifications, or a token snapshot when structured options are present. Use the
5658 * option API or {@link #addColumnSpecs(Collection)} to append without discarding structured
57- * references and constraints.
59+ * expressions, references and constraints. DEFAULT values use the expression's SQL rendering .
5860 */
5961 public List <String > getColumnSpecs () {
6062 if (columnOptions != null ) {
@@ -73,7 +75,7 @@ public void setColumnSpecs(List<String> list) {
7375 }
7476
7577 /**
76- * Returns column options in source order, including structured references and MySQL
78+ * Returns column options in source order, including structured defaults, references and MySQL
7779 * {@code SERIAL DEFAULT VALUE}.
7880 */
7981 public List <ColumnOption > getColumnOptions () {
@@ -134,17 +136,42 @@ public void setColumnName(String string) {
134136
135137 @ Override
136138 public String toString () {
137- return (columnName + " " + toStringDataTypeAndSpec ()).trim ();
139+ StringBuilder builder = new StringBuilder ();
140+ appendTo (builder , builder ::append );
141+ return builder .toString ().trim ();
142+ }
143+
144+ /** Appends a column definition using the supplied printer for structured expressions. */
145+ public void appendTo (StringBuilder builder , Consumer <Expression > expressionPrinter ) {
146+ builder .append (columnName );
147+ if (colDataType != null || withOptions ) {
148+ builder .append (' ' );
149+ }
150+ appendDataTypeAndSpecTo (builder , expressionPrinter );
138151 }
139152
140153 public String toStringDataTypeAndSpec () {
141- return (colDataType == null ? "" : colDataType )
142- + (withOptions ? "WITH OPTIONS" : "" )
143- + (columnOptions != null && !columnOptions .isEmpty ()
144- ? " " + PlainSelect .getStringList (columnOptions , false , false )
145- : columnSpecs != null && !columnSpecs .isEmpty ()
146- ? " " + PlainSelect .getStringList (columnSpecs , false , false )
147- : "" );
154+ StringBuilder builder = new StringBuilder ();
155+ appendDataTypeAndSpecTo (builder , builder ::append );
156+ return builder .toString ();
157+ }
158+
159+ protected void appendDataTypeAndSpecTo (StringBuilder builder ,
160+ Consumer <Expression > expressionPrinter ) {
161+ if (colDataType != null ) {
162+ builder .append (colDataType );
163+ }
164+ if (withOptions ) {
165+ builder .append ("WITH OPTIONS" );
166+ }
167+ if (columnOptions != null ) {
168+ for (ColumnOption option : columnOptions ) {
169+ builder .append (' ' );
170+ option .appendTo (builder , expressionPrinter );
171+ }
172+ } else if (columnSpecs != null && !columnSpecs .isEmpty ()) {
173+ builder .append (' ' ).append (PlainSelect .getStringList (columnSpecs , false , false ));
174+ }
148175 }
149176
150177 public ColumnDefinition withColumnName (String columnName ) {
0 commit comments