1414import net .sf .jsqlparser .statement .create .table .ColDataType ;
1515
1616import java .util .Objects ;
17+ import java .util .function .Consumer ;
1718
1819public class TranscodingFunction extends ASTNodeAccessImpl implements Expression {
1920 private String keyword = "CONVERT" ;
20- private boolean isTranscodeStyle = true ;
21+
22+ public enum Syntax {
23+ USING , TYPE_FIRST , TYPE_LAST
24+ }
25+
26+ private Syntax syntax = Syntax .USING ;
2127 private ColDataType colDataType ;
2228 private Expression expression ;
2329 private String transcodingName ;
@@ -39,15 +45,15 @@ public TranscodingFunction(String keyword, ColDataType colDataType, Expression e
3945 this .colDataType = colDataType ;
4046 this .expression = expression ;
4147 this .transcodingName = transcodingName ;
42- this .isTranscodeStyle = false ;
48+ this .syntax = Syntax . TYPE_FIRST ;
4349 }
4450
4551 public TranscodingFunction (ColDataType colDataType , Expression expression ,
4652 String transcodingName ) {
4753 this .colDataType = colDataType ;
4854 this .expression = expression ;
4955 this .transcodingName = transcodingName ;
50- this .isTranscodeStyle = false ;
56+ this .syntax = Syntax . TYPE_FIRST ;
5157 }
5258
5359 public TranscodingFunction () {
@@ -100,39 +106,45 @@ public TranscodingFunction setColDataType(ColDataType colDataType) {
100106 }
101107
102108 public boolean isTranscodeStyle () {
103- return isTranscodeStyle ;
109+ return syntax == Syntax . USING ;
104110 }
105111
106112 public TranscodingFunction setTranscodeStyle (boolean transcodeStyle ) {
107- isTranscodeStyle = transcodeStyle ;
113+ syntax = transcodeStyle ? Syntax . USING : Syntax . TYPE_FIRST ;
108114 return this ;
109115 }
110116
111117 public <T , S > T accept (ExpressionVisitor <T > expressionVisitor , S context ) {
112118 return expressionVisitor .visit (this , context );
113119 }
114120
121+ public Syntax getSyntax () {
122+ return syntax ;
123+ }
124+
125+ public TranscodingFunction setSyntax (Syntax syntax ) {
126+ this .syntax = Objects .requireNonNull (syntax );
127+ return this ;
128+ }
129+
115130 public StringBuilder appendTo (StringBuilder builder ) {
116- if (isTranscodeStyle ) {
117- return builder
118- .append (keyword )
119- .append ("( " )
120- .append (expression )
121- .append (" USING " )
122- .append (transcodingName )
123- .append (" )" );
124- } else {
125- return builder
126- .append (keyword )
127- .append ("( " )
128- .append (colDataType )
129- .append (", " )
130- .append (expression )
131- .append (transcodingName != null && !transcodingName .isEmpty ()
132- ? ", " + transcodingName
133- : "" )
134- .append (" )" );
131+ return appendTo (builder , builder ::append );
132+ }
133+
134+ public StringBuilder appendTo (StringBuilder builder , Consumer <Expression > expressionRenderer ) {
135+ builder .append (keyword ).append ("( " );
136+ if (syntax == Syntax .TYPE_FIRST ) {
137+ builder .append (colDataType ).append (", " );
138+ }
139+ expressionRenderer .accept (expression );
140+ if (syntax == Syntax .USING ) {
141+ builder .append (" USING " ).append (transcodingName );
142+ } else if (syntax == Syntax .TYPE_LAST ) {
143+ builder .append (", " ).append (colDataType );
144+ } else if (transcodingName != null && !transcodingName .isEmpty ()) {
145+ builder .append (", " ).append (transcodingName );
135146 }
147+ return builder .append (" )" );
136148 }
137149
138150 @ Override
0 commit comments