SimpleSQLParser supports subqueries-as-columns now

dependabot/maven/junit-junit-4.13.1
Travis Burtrum 2019-07-12 15:13:51 -04:00
parent 943ac3ac85
commit 27f26e6ef4
3 changed files with 8 additions and 2 deletions

View File

@ -25,7 +25,12 @@ public class SimpleSQLParser extends AbstractSQLParser {
final boolean isSelect = sql.startsWith("SELECT");
String[] columnNames = null;
if (isSelect) {
final String columns = parenPattern.matcher(sql.substring(sql.indexOf("SELECT") + 6, sql.indexOf("FROM"))).replaceAll("").trim();
// remove anything in parens, examples:
// COALESCE(some_tom, 'UNKNOWN') as tom -> COALESCE() as tom
// (SELECT some_column from some_table where other_column = 'YAY') as tom -> () as tom
sql = parenPattern.matcher(sql).replaceAll("()").trim();
// 6 is length of "SELECT" which we already verified it starts with...
final String columns = sql.substring(6, sql.indexOf("FROM"));
final String[] splitColumns = columns.split(",");
columnNames = new String[splitColumns.length + 1];
int index = 0;

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -e
updateSnapshots=false

View File

@ -30,6 +30,7 @@ public class SimpleSQLParserTest {
, "select some_bob bob, some_tom as tom from tom"
, "select tom.bob, some_tom as tom from tom"
, "select tom.bob, COALESCE(some_tom, 'UNKNOWN') as tom from tom"
, "select tom.bob, (SELECT some_column from some_table where other_column = 'YAY') as tom from tom"
}) {
final SQLParser ret = getFactory().parse(sql);
assertTrue(ret.isSelect());