mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-12-22 07:18:51 -05:00
SimpleSQLParser supports subqueries-as-columns now
This commit is contained in:
parent
943ac3ac85
commit
27f26e6ef4
@ -25,7 +25,12 @@ public class SimpleSQLParser extends AbstractSQLParser {
|
|||||||
final boolean isSelect = sql.startsWith("SELECT");
|
final boolean isSelect = sql.startsWith("SELECT");
|
||||||
String[] columnNames = null;
|
String[] columnNames = null;
|
||||||
if (isSelect) {
|
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(",");
|
final String[] splitColumns = columns.split(",");
|
||||||
columnNames = new String[splitColumns.length + 1];
|
columnNames = new String[splitColumns.length + 1];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
updateSnapshots=false
|
updateSnapshots=false
|
||||||
|
@ -30,6 +30,7 @@ public class SimpleSQLParserTest {
|
|||||||
, "select some_bob bob, some_tom as tom from tom"
|
, "select some_bob bob, some_tom as tom from tom"
|
||||||
, "select tom.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, 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);
|
final SQLParser ret = getFactory().parse(sql);
|
||||||
assertTrue(ret.isSelect());
|
assertTrue(ret.isSelect());
|
||||||
|
Loading…
Reference in New Issue
Block a user