From 27f26e6ef43b9eaa687bcb5602b5d07fb66dafa5 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Fri, 12 Jul 2019 15:13:51 -0400 Subject: [PATCH] SimpleSQLParser supports subqueries-as-columns now --- .../com/moparisthebest/jdbc/codegen/SimpleSQLParser.java | 7 ++++++- test/runSnapshotTests.sh | 2 +- .../moparisthebest/jdbc/codegen/SimpleSQLParserTest.java | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/SimpleSQLParser.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/SimpleSQLParser.java index ac1eae7..bdb9eed 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/SimpleSQLParser.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/SimpleSQLParser.java @@ -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; diff --git a/test/runSnapshotTests.sh b/test/runSnapshotTests.sh index 97982f7..40a43ef 100755 --- a/test/runSnapshotTests.sh +++ b/test/runSnapshotTests.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e updateSnapshots=false diff --git a/test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java b/test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java index cf431fb..ddd388b 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java @@ -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());