QuerySchemaParserFunction implements Function<String,Pair<String,String>> { @Override public Pair<String, String> apply(final String selectStatement) { Preconditions.checkNotNull(selectStatement); Preconditions.checkArgument(!selectStatement.isEmpty(), "Select Query is empty!!"); Connection connection = null; try { connection = ConnectionUtil.getConnection(this.configuration); final Statement statement = connection.createStatement(); final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class); final QueryPlan queryPlan = pstmt.compileQuery(selectStatement); isValidStatement(queryPlan); final String tableName = queryPlan.getTableRef().getTable().getName().getString(); final List<? extends ColumnProjector> projectedColumns = queryPlan.getProjector().getColumnProjectors(); final List<String> columns = Lists.transform(projectedColumns, new Function<ColumnProjector,String>() { @Override public String apply(ColumnProjector column) { return column.getName(); } }); final String columnsAsStr = Joiner.on(",").join(columns); return new Pair<String, String>(tableName, columnsAsStr); } catch (SQLException e) { LOG.error(String.format(" Error [%s] parsing SELECT query [%s] ",e.getMessage(),selectStatement)); throw new RuntimeException(e); } finally { if(connection != null) { try { connection.close(); } catch(SQLException sqle) { LOG.error(" Error closing connection "); throw new RuntimeException(sqle); } } } } QuerySchemaParserFunction(Configuration configuration); @Override Pair<String, String> apply(final String selectStatement);  }
@Test(expected=RuntimeException.class) public void testSelectQuery() { final String selectQuery = "SELECT col1 FROM test"; function.apply(selectQuery); fail("Should fail as the table [test] doesn't exist"); }
@Test public void testValidSelectQuery() throws SQLException { String ddl = "CREATE TABLE EMPLOYEE " + " (id integer not null, name varchar, age integer,location varchar " + " CONSTRAINT pk PRIMARY KEY (id))\n"; createTestTable(getUrl(), ddl); final String selectQuery = "SELECT name,age,location FROM EMPLOYEE"; Pair<String,String> pair = function.apply(selectQuery); assertEquals(pair.getFirst(), "EMPLOYEE"); assertEquals(pair.getSecond(),Joiner.on(',').join("NAME","AGE","LOCATION")); }
@Test(expected=RuntimeException.class) public void testUpsertQuery() throws SQLException { String ddl = "CREATE TABLE EMPLOYEE " + " (id integer not null, name varchar, age integer,location varchar " + " CONSTRAINT pk PRIMARY KEY (id))\n"; createTestTable(getUrl(), ddl); final String upsertQuery = "UPSERT INTO EMPLOYEE (ID, NAME) VALUES (?, ?)"; function.apply(upsertQuery); fail(" Function call successful despite passing an UPSERT query"); }
@Test(expected=IllegalArgumentException.class) public void testAggregationQuery() throws SQLException { String ddl = "CREATE TABLE EMPLOYEE " + " (id integer not null, name varchar, age integer,location varchar " + " CONSTRAINT pk PRIMARY KEY (id))\n"; createTestTable(getUrl(), ddl); final String selectQuery = "SELECT MAX(ID) FROM EMPLOYEE"; function.apply(selectQuery); fail(" Function call successful despite passing an aggreagate query"); }