1616import java .util .Objects ;
1717import java .util .function .Consumer ;
1818import net .sf .jsqlparser .expression .Expression ;
19+ import net .sf .jsqlparser .expression .StringValue ;
1920import net .sf .jsqlparser .statement .select .PlainSelect ;
2021
2122/** A structured option following a column data type. */
2223public class ColumnOption implements Serializable {
2324
2425 public enum Kind {
25- SERIAL_DEFAULT_VALUE , REFERENCE , IDENTITY , CONSTRAINT , DEFAULT , OTHER
26+ SERIAL_DEFAULT_VALUE , REFERENCE , IDENTITY , CONSTRAINT , DEFAULT , NULLABILITY , COLLATE , COMMENT , ON_UPDATE , GENERATED , AUTO_INCREMENT , VISIBILITY , OTHER
2627 }
2728
2829 private Kind kind = Kind .OTHER ;
@@ -31,6 +32,122 @@ public enum Kind {
3132 private IdentityDefinition identityDefinition ;
3233 private Index constraint ;
3334 private Expression defaultExpression ;
35+ private Boolean nullable ;
36+ private Boolean visible ;
37+ private String collation ;
38+ private StringValue comment ;
39+ private Expression onUpdateExpression ;
40+ private GeneratedColumnDefinition generatedDefinition ;
41+
42+ public static ColumnOption nullability (boolean nullable ) {
43+ ColumnOption option = new ColumnOption ();
44+ option .kind = Kind .NULLABILITY ;
45+ option .nullable = nullable ;
46+ return option ;
47+ }
48+
49+ public Boolean getNullable () {
50+ return nullable ;
51+ }
52+
53+ public void setNullable (boolean nullable ) {
54+ this .nullable = nullable ;
55+ }
56+
57+ public static ColumnOption visibility (boolean visible ) {
58+ ColumnOption option = new ColumnOption ();
59+ option .kind = Kind .VISIBILITY ;
60+ option .visible = visible ;
61+ return option ;
62+ }
63+
64+ public Boolean getVisible () {
65+ return visible ;
66+ }
67+
68+ public void setVisible (boolean visible ) {
69+ this .visible = visible ;
70+ }
71+
72+ public static ColumnOption autoIncrement () {
73+ ColumnOption option = new ColumnOption ();
74+ option .kind = Kind .AUTO_INCREMENT ;
75+ return option ;
76+ }
77+
78+ public static ColumnOption collate (String collation ) {
79+ ColumnOption option = new ColumnOption ();
80+ option .kind = Kind .COLLATE ;
81+ option .setCollation (collation );
82+ return option ;
83+ }
84+
85+ public String getCollation () {
86+ return collation ;
87+ }
88+
89+ public void setCollation (String collation ) {
90+ this .collation = Objects .requireNonNull (collation , "collation" );
91+ }
92+
93+ public static ColumnOption comment (StringValue comment ) {
94+ ColumnOption option = new ColumnOption ();
95+ option .kind = Kind .COMMENT ;
96+ option .setComment (comment );
97+ return option ;
98+ }
99+
100+ public StringValue getComment () {
101+ return comment ;
102+ }
103+
104+ public void setComment (StringValue comment ) {
105+ this .comment = Objects .requireNonNull (comment , "comment" );
106+ }
107+
108+ public static ColumnOption onUpdate (Expression expression ) {
109+ ColumnOption option = new ColumnOption ();
110+ option .kind = Kind .ON_UPDATE ;
111+ option .setOnUpdateExpression (expression );
112+ return option ;
113+ }
114+
115+ public Expression getOnUpdateExpression () {
116+ return onUpdateExpression ;
117+ }
118+
119+ public void setOnUpdateExpression (Expression expression ) {
120+ onUpdateExpression = Objects .requireNonNull (expression , "expression" );
121+ }
122+
123+ public static ColumnOption generated (GeneratedColumnDefinition definition ) {
124+ ColumnOption option = new ColumnOption ();
125+ option .kind = Kind .GENERATED ;
126+ option .setGeneratedDefinition (definition );
127+ return option ;
128+ }
129+
130+ public GeneratedColumnDefinition getGeneratedDefinition () {
131+ return generatedDefinition ;
132+ }
133+
134+ public void setGeneratedDefinition (GeneratedColumnDefinition definition ) {
135+ generatedDefinition = Objects .requireNonNull (definition , "definition" );
136+ }
137+
138+ /** Visits expressions of the selected option kind, including comment literals. */
139+ public void visitExpressions (Consumer <Expression > visitor ) {
140+ if (kind == Kind .DEFAULT ) {
141+ visitor .accept (defaultExpression );
142+ } else if (kind == Kind .COMMENT ) {
143+ visitor .accept (comment );
144+ } else if (kind == Kind .ON_UPDATE ) {
145+ visitor .accept (onUpdateExpression );
146+ } else if (kind == Kind .GENERATED ) {
147+ visitor .accept (generatedDefinition .getExpression ());
148+ }
149+ }
150+
34151
35152 /** Creates a DEFAULT option. Use a NullValue expression for SQL NULL. */
36153 public static ColumnOption defaultValue (Expression expression ) {
@@ -99,11 +216,30 @@ public Kind getKind() {
99216 }
100217
101218 public List <String > getTokens () {
102- if (kind == Kind .DEFAULT ) {
103- return Arrays .asList ("DEFAULT" , String .valueOf (defaultExpression ));
219+ switch (kind ) {
220+ case DEFAULT :
221+ return Arrays .asList ("DEFAULT" , String .valueOf (defaultExpression ));
222+ case NULLABILITY :
223+ return nullable ? Collections .singletonList ("NULL" ) : Arrays .asList ("NOT" , "NULL" );
224+ case COLLATE :
225+ return Arrays .asList ("COLLATE" , collation );
226+ case COMMENT :
227+ return Arrays .asList ("COMMENT" , comment .toString ());
228+ case ON_UPDATE :
229+ return Arrays .asList ("ON" , "UPDATE" , onUpdateExpression .toString ());
230+ case GENERATED :
231+ return generatedDefinition .getTokens ();
232+ case CONSTRAINT :
233+ if ("PRIMARY KEY" .equals (constraint .toString ())) {
234+ return Arrays .asList ("PRIMARY" , "KEY" );
235+ }
236+ return Collections .singletonList (toString ());
237+ case OTHER :
238+ case SERIAL_DEFAULT_VALUE :
239+ return tokens ;
240+ default :
241+ return Collections .singletonList (toString ());
104242 }
105- return kind == Kind .OTHER || kind == Kind .SERIAL_DEFAULT_VALUE ? tokens
106- : Collections .singletonList (toString ());
107243 }
108244
109245 public ForeignKeyReference getForeignKeyReference () {
@@ -124,6 +260,29 @@ public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrint
124260 builder .append ("DEFAULT " );
125261 expressionPrinter .accept (defaultExpression );
126262 break ;
263+ case NULLABILITY :
264+ builder .append (nullable ? "NULL" : "NOT NULL" );
265+ break ;
266+ case VISIBILITY :
267+ builder .append (visible ? "VISIBLE" : "INVISIBLE" );
268+ break ;
269+ case AUTO_INCREMENT :
270+ builder .append ("AUTO_INCREMENT" );
271+ break ;
272+ case COLLATE :
273+ builder .append ("COLLATE " ).append (collation );
274+ break ;
275+ case COMMENT :
276+ builder .append ("COMMENT " );
277+ expressionPrinter .accept (comment );
278+ break ;
279+ case ON_UPDATE :
280+ builder .append ("ON UPDATE " );
281+ expressionPrinter .accept (onUpdateExpression );
282+ break ;
283+ case GENERATED :
284+ generatedDefinition .appendTo (builder , expressionPrinter );
285+ break ;
127286 case REFERENCE :
128287 builder .append (foreignKeyReference );
129288 break ;
0 commit comments