@@ -71,48 +71,62 @@ public boolean hasParentheses() {
7171 * and expressions outside the supported call shapes return an empty result.
7272 */
7373 public static Optional <TemporalExpressionInfo > from (Expression source , Dialect dialect ) {
74- Expression expression = source ;
7574 if (dialect != Dialect .POSTGRESQL && dialect != Dialect .MYSQL ) {
7675 return Optional .empty ();
7776 }
78- while (expression instanceof ParenthesedExpressionList
79- && ((ParenthesedExpressionList <?>) expression ).size () == 1 ) {
80- expression = ((ParenthesedExpressionList <?>) expression ).get (0 );
81- }
82- String name ;
83- Integer precision = null ;
84- boolean parentheses = false ;
77+ Expression expression = unwrap (source );
8578 if (expression instanceof TimeKeyExpression ) {
86- name = ((TimeKeyExpression ) expression ).getStringValue ();
87- if (name != null && name .endsWith ("()" )) {
88- parentheses = true ;
89- name = name .substring (0 , name .length () - 2 );
90- }
91- } else if (expression instanceof Column ) {
79+ return fromKeyword (((TimeKeyExpression ) expression ).getStringValue (), dialect );
80+ }
81+ if (expression instanceof Column ) {
9282 Column column = (Column ) expression ;
9383 if (column .getTable () != null && column .getTable ().getName () != null ) {
9484 return Optional .empty ();
9585 }
96- name = column .getColumnName ();
97- } else if (expression instanceof Function && isPlainCall ((Function ) expression )) {
98- Function function = (Function ) expression ;
99- name = function .getName ();
100- parentheses = true ;
101- if (function .getParameters () != null && !function .getParameters ().isEmpty ()) {
102- if (function .getParameters ().size () != 1
103- || !(function .getParameters ().get (0 ) instanceof LongValue )) {
104- return Optional .empty ();
105- }
106- BigInteger value =
107- ((LongValue ) function .getParameters ().get (0 )).getBigIntegerValue ();
108- if (value .signum () < 0 || value .bitLength () >= Integer .SIZE ) {
109- return Optional .empty ();
110- }
111- precision = value .intValue ();
112- }
113- } else {
86+ return create (column .getColumnName (), null , false , dialect );
87+ }
88+ if (expression instanceof Function ) {
89+ return fromFunction ((Function ) expression , dialect );
90+ }
91+ return Optional .empty ();
92+ }
93+
94+ private static Expression unwrap (Expression source ) {
95+ Expression expression = source ;
96+ while (expression instanceof ParenthesedExpressionList
97+ && ((ParenthesedExpressionList <?>) expression ).size () == 1 ) {
98+ expression = ((ParenthesedExpressionList <?>) expression ).get (0 );
99+ }
100+ return expression ;
101+ }
102+
103+ private static Optional <TemporalExpressionInfo > fromKeyword (String name , Dialect dialect ) {
104+ boolean parentheses = name != null && name .endsWith ("()" );
105+ String keyword = parentheses ? name .substring (0 , name .length () - 2 ) : name ;
106+ return create (keyword , null , parentheses , dialect );
107+ }
108+
109+ private static Optional <TemporalExpressionInfo > fromFunction (Function function ,
110+ Dialect dialect ) {
111+ if (!isPlainCall (function )) {
112+ return Optional .empty ();
113+ }
114+ if (function .getParameters () == null || function .getParameters ().isEmpty ()) {
115+ return create (function .getName (), null , true , dialect );
116+ }
117+ if (function .getParameters ().size () != 1
118+ || !(function .getParameters ().get (0 ) instanceof LongValue )) {
119+ return Optional .empty ();
120+ }
121+ BigInteger value = ((LongValue ) function .getParameters ().get (0 )).getBigIntegerValue ();
122+ if (value .signum () < 0 || value .bitLength () >= Integer .SIZE ) {
114123 return Optional .empty ();
115124 }
125+ return create (function .getName (), value .intValue (), true , dialect );
126+ }
127+
128+ private static Optional <TemporalExpressionInfo > create (String name , Integer precision ,
129+ boolean parentheses , Dialect dialect ) {
116130 if (name == null ) {
117131 return Optional .empty ();
118132 }
0 commit comments