Browse Source

SimpleSQLParser supports subqueries-as-columns now

dependabot/maven/junit-junit-4.13.1
Travis Burtrum 4 years ago
parent
commit
27f26e6ef4
  1. 7
      jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/SimpleSQLParser.java
  2. 2
      test/runSnapshotTests.sh
  3. 1
      test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java

7
jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/SimpleSQLParser.java

@ -25,7 +25,12 @@ public class SimpleSQLParser extends AbstractSQLParser { @@ -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;

2
test/runSnapshotTests.sh

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

1
test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java

@ -30,6 +30,7 @@ public class SimpleSQLParserTest { @@ -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());

Loading…
Cancel
Save