|
30 | 30 | import net.sf.jsqlparser.parser.feature.Feature; |
31 | 31 | import net.sf.jsqlparser.statement.Statement; |
32 | 32 | import net.sf.jsqlparser.statement.Statements; |
| 33 | +import net.sf.jsqlparser.statement.create.table.ColDataType; |
33 | 34 |
|
34 | 35 | /** |
35 | 36 | * Toolfunctions to start and use JSqlParser. |
@@ -199,6 +200,61 @@ public static Expression parseExpression(String expression, boolean allowPartial |
199 | 200 | }); |
200 | 201 | } |
201 | 202 |
|
| 203 | + /** |
| 204 | + * Parses a column data type fragment. The complete input must represent the data type; trailing |
| 205 | + * tokens are rejected. |
| 206 | + * |
| 207 | + * @param columnDataType the column data type fragment to parse |
| 208 | + * @return the parsed column data type, or {@code null} for a null or empty input |
| 209 | + * @throws JSQLParserException when the input cannot be parsed completely |
| 210 | + * @see #parseColDataType(String, Consumer) |
| 211 | + */ |
| 212 | + public static ColDataType parseColDataType(String columnDataType) throws JSQLParserException { |
| 213 | + return parseColDataType(columnDataType, null); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Parses a column data type fragment while allowing the parser to be configured. The complete |
| 218 | + * input must represent the data type; trailing tokens are rejected. |
| 219 | + * |
| 220 | + * @param columnDataType the column data type fragment to parse |
| 221 | + * @param consumer parser configuration callback, or {@code null} |
| 222 | + * @return the parsed column data type, or {@code null} for a null or empty input |
| 223 | + * @throws JSQLParserException when the input cannot be parsed completely |
| 224 | + */ |
| 225 | + public static ColDataType parseColDataType(String columnDataType, |
| 226 | + Consumer<CCJSqlParser> consumer) throws JSQLParserException { |
| 227 | + if (columnDataType == null || columnDataType.isEmpty()) { |
| 228 | + return null; |
| 229 | + } |
| 230 | + |
| 231 | + try { |
| 232 | + return parseColDataType(columnDataType, false, consumer); |
| 233 | + } catch (JSQLParserException ex) { |
| 234 | + return parseColDataType(columnDataType, true, consumer); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + private static ColDataType parseColDataType(String columnDataType, boolean allowComplexParsing, |
| 239 | + Consumer<CCJSqlParser> consumer) throws JSQLParserException { |
| 240 | + CCJSqlParser parser = newParser(columnDataType) |
| 241 | + .withAllowComplexParsing(allowComplexParsing); |
| 242 | + if (consumer != null) { |
| 243 | + consumer.accept(parser); |
| 244 | + } |
| 245 | + |
| 246 | + try { |
| 247 | + ColDataType result = parser.ColDataType(); |
| 248 | + if (parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) { |
| 249 | + throw new JSQLParserException( |
| 250 | + "could only parse partial column data type " + result); |
| 251 | + } |
| 252 | + return result; |
| 253 | + } catch (ParseException ex) { |
| 254 | + throw new JSQLParserException(columnDataType, ex); |
| 255 | + } |
| 256 | + } |
| 257 | + |
202 | 258 | @SuppressWarnings("PMD.CyclomaticComplexity") |
203 | 259 | public static Expression parseExpression(String expressionStr, boolean allowPartialParse, |
204 | 260 | Consumer<CCJSqlParser> consumer) throws JSQLParserException { |
|
0 commit comments