@Test public void testAddPredicate() { Assert.assertEquals(SqlQueryUtils.addPredicate("SELECT foo FROM bar", "foo != 'blah'"), "SELECT foo FROM bar where (foo != 'blah')"); Assert.assertEquals(SqlQueryUtils.addPredicate("SELECT foo,whereTo FROM bar WHERE whereTo==foo", "foo != 'blah'"), "SELECT foo,whereTo FROM bar WHERE whereTo==foo and (foo != 'blah')"); Assert.assertEquals(SqlQueryUtils.addPredicate("SELECT foo,andThis FROM bar WHERE andThis>foo", "foo != 'blah'"), "SELECT foo,andThis FROM bar WHERE andThis>foo and (foo != 'blah')"); Assert.assertEquals(SqlQueryUtils.addPredicate("SELECT foo FROM bar", null), "SELECT foo FROM bar"); Assert.assertEquals(SqlQueryUtils.addPredicate("SELECT foo FROM bar", ""), "SELECT foo FROM bar"); try { SqlQueryUtils.addPredicate("SELECT foo,foo1 WHERE foo1==foo", "foo != 'blah'"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.toString().contains("'from'")); } try { SqlQueryUtils.addPredicate("SELECT foo,foo1 FROM blah WHERE foo1==foo ORDER by foo", "foo != 'blah'"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.toString().contains("'order by'")); } try { SqlQueryUtils.addPredicate("SELECT foo,foo1 FROM blah WHERE foo1==foo GROUP BY foo", "foo != 'blah'"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.toString().contains("'group by'")); } try { SqlQueryUtils.addPredicate("SELECT foo,foo1 FROM blah WHERE foo1==foo HAVING foo1 is null", "foo != 'blah'"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.toString().contains("'having'")); } try { SqlQueryUtils.addPredicate("SELECT foo,foo1 FROM blah WHERE foo1==foo LIMIT 10", "foo != 'blah'"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.toString().contains("'limit'")); } }
public static String addPredicate(String query, String predicateCond) { if (Strings.isNullOrEmpty(predicateCond)) { return query; } String normalizedQuery = query.toLowerCase().trim(); checkArgument(normalizedQuery.contains(" from "), "query does not contain 'from': " + query); checkArgument(! normalizedQuery.contains(" by "), "query contains 'order by' or 'group by': " + query); checkArgument(! normalizedQuery.contains(" having "), "query contains 'having': " + query); checkArgument(! normalizedQuery.contains(" limit "), "query contains 'limit': " + query); String keyword = " where "; if (normalizedQuery.contains(keyword)) { keyword = " and "; } query = query + keyword + "(" + predicateCond + ")"; return query; }
SqlQueryUtils { public static String addPredicate(String query, String predicateCond) { if (Strings.isNullOrEmpty(predicateCond)) { return query; } String normalizedQuery = query.toLowerCase().trim(); checkArgument(normalizedQuery.contains(" from "), "query does not contain 'from': " + query); checkArgument(! normalizedQuery.contains(" by "), "query contains 'order by' or 'group by': " + query); checkArgument(! normalizedQuery.contains(" having "), "query contains 'having': " + query); checkArgument(! normalizedQuery.contains(" limit "), "query contains 'limit': " + query); String keyword = " where "; if (normalizedQuery.contains(keyword)) { keyword = " and "; } query = query + keyword + "(" + predicateCond + ")"; return query; } }
SqlQueryUtils { public static String addPredicate(String query, String predicateCond) { if (Strings.isNullOrEmpty(predicateCond)) { return query; } String normalizedQuery = query.toLowerCase().trim(); checkArgument(normalizedQuery.contains(" from "), "query does not contain 'from': " + query); checkArgument(! normalizedQuery.contains(" by "), "query contains 'order by' or 'group by': " + query); checkArgument(! normalizedQuery.contains(" having "), "query contains 'having': " + query); checkArgument(! normalizedQuery.contains(" limit "), "query contains 'limit': " + query); String keyword = " where "; if (normalizedQuery.contains(keyword)) { keyword = " and "; } query = query + keyword + "(" + predicateCond + ")"; return query; }  }
SqlQueryUtils { public static String addPredicate(String query, String predicateCond) { if (Strings.isNullOrEmpty(predicateCond)) { return query; } String normalizedQuery = query.toLowerCase().trim(); checkArgument(normalizedQuery.contains(" from "), "query does not contain 'from': " + query); checkArgument(! normalizedQuery.contains(" by "), "query contains 'order by' or 'group by': " + query); checkArgument(! normalizedQuery.contains(" having "), "query contains 'having': " + query); checkArgument(! normalizedQuery.contains(" limit "), "query contains 'limit': " + query); String keyword = " where "; if (normalizedQuery.contains(keyword)) { keyword = " and "; } query = query + keyword + "(" + predicateCond + ")"; return query; }  static String addPredicate(String query, String predicateCond); static boolean castToBoolean(String fieldValue); }
SqlQueryUtils { public static String addPredicate(String query, String predicateCond) { if (Strings.isNullOrEmpty(predicateCond)) { return query; } String normalizedQuery = query.toLowerCase().trim(); checkArgument(normalizedQuery.contains(" from "), "query does not contain 'from': " + query); checkArgument(! normalizedQuery.contains(" by "), "query contains 'order by' or 'group by': " + query); checkArgument(! normalizedQuery.contains(" having "), "query contains 'having': " + query); checkArgument(! normalizedQuery.contains(" limit "), "query contains 'limit': " + query); String keyword = " where "; if (normalizedQuery.contains(keyword)) { keyword = " and "; } query = query + keyword + "(" + predicateCond + ")"; return query; }  static String addPredicate(String query, String predicateCond); static boolean castToBoolean(String fieldValue);  }