SqlBuilder extends AbstractBuilder { @Override public SqlQueryResult build(String query) throws IOException, ParserNotFoundException { Operation result = (Operation) super.parse(query); StringBuffer sql = new StringBuffer(result.getOperate()); sql.delete(sql.length() - 2, sql.length()); sql.delete(0, 2); return new SqlQueryResult(query, sql.toString(), (List<Object>) result.getValue()); } SqlBuilder(IGroupParser groupParser, List<IRuleParser> ruleParsers, List<IRuleFilter> ruleFilters); @Override SqlQueryResult build(String query);  }
@Test public void testOperatorEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-equal.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username = ?", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals("username = 'Mistic'", result.getQuery(true)); }
@Test public void testOperatorNotEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-equal.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username != ?", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals("username != 'Mistic'", result.getQuery(true)); }
@Test public void testOperatorIn() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-in.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age IN(?, ?, ?)", result.getQuery()); Assert.assertEquals(1, result.getParams().get(0)); Assert.assertEquals(5, result.getParams().get(1)); Assert.assertEquals(10, result.getParams().get(2)); Assert.assertEquals("age IN(1, 5, 10)", result.getQuery(true)); }
@Test public void testOperatorNotIn() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-in.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age NOT IN(?, ?, ?)", result.getQuery()); Assert.assertEquals(1, result.getParams().get(0)); Assert.assertEquals(5, result.getParams().get(1)); Assert.assertEquals(10, result.getParams().get(2)); Assert.assertEquals("age NOT IN(1, 5, 10)", result.getQuery(true)); }
@Test public void testOperatorLess() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-less.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age < ?", result.getQuery()); Assert.assertEquals(50, result.getParams().get(0)); Assert.assertEquals("age < 50", result.getQuery(true)); }
@Test public void testOperatorLessOrEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-less-or-equal.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age <= ?", result.getQuery()); Assert.assertEquals(50, result.getParams().get(0)); Assert.assertEquals("age <= 50", result.getQuery(true)); }
@Test public void testOperatorGreater() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-greater.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age > ?", result.getQuery()); Assert.assertEquals(50, result.getParams().get(0)); Assert.assertEquals("age > 50", result.getQuery(true)); }
@Test public void testOperatorGreaterOrEqual() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-greater-or-equal.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age >= ?", result.getQuery()); Assert.assertEquals(50, result.getParams().get(0)); Assert.assertEquals("age >= 50", result.getQuery(true)); }
@Test public void testOperatorBetween() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-between.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age BETWEEN ? AND ?", result.getQuery()); Assert.assertEquals(5, result.getParams().get(0)); Assert.assertEquals(10, result.getParams().get(1)); Assert.assertEquals("age BETWEEN 5 AND 10", result.getQuery(true)); }
@Test public void testOperatorNotBetween() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-between.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("age NOT BETWEEN ? AND ?", result.getQuery()); Assert.assertEquals(5, result.getParams().get(0)); Assert.assertEquals(10, result.getParams().get(1)); Assert.assertEquals("age NOT BETWEEN 5 AND 10", result.getQuery(true)); }
@Test public void testOperatorBeginsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-begins-with.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username LIKE(?)", result.getQuery()); Assert.assertEquals("Mistic%", result.getParams().get(0)); Assert.assertEquals("username LIKE('Mistic%')", result.getQuery(true)); }
@Test public void testOperatorNotBeginsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-begins-with.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username NOT LIKE(?)", result.getQuery()); Assert.assertEquals("Mistic%", result.getParams().get(0)); Assert.assertEquals("username NOT LIKE('Mistic%')", result.getQuery(true)); }
@Test public void testOperatorContains() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-contains.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username LIKE(?)", result.getQuery()); Assert.assertEquals("%Mistic%", result.getParams().get(0)); Assert.assertEquals("username LIKE('%Mistic%')", result.getQuery(true)); }
@Test public void testOperatorNotContains() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-contains.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username NOT LIKE(?)", result.getQuery()); Assert.assertEquals("%Mistic%", result.getParams().get(0)); Assert.assertEquals("username NOT LIKE('%Mistic%')", result.getQuery(true)); }
@Test public void testOperatorEndsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-ends-with.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username LIKE(?)", result.getQuery()); Assert.assertEquals("%Mistic", result.getParams().get(0)); Assert.assertEquals("username LIKE('%Mistic')", result.getQuery(true)); }
@Test public void testOperatorNotEndsWith() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-not-ends-with.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username NOT LIKE(?)", result.getQuery()); Assert.assertEquals("%Mistic", result.getParams().get(0)); Assert.assertEquals("username NOT LIKE('%Mistic')", result.getQuery(true)); }
@Test public void testOperatorIsEmpty() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-empty.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username = ''", result.getQuery()); Assert.assertEquals(0, result.getParams().size()); Assert.assertEquals("username = ''", result.getQuery(true)); }
@Test public void testOperatorIsNotEmpty() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-not-empty.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username != ''", result.getQuery()); Assert.assertEquals(0, result.getParams().size()); Assert.assertEquals("username != ''", result.getQuery(true)); }
@Test public void testOperatorIsNull() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-null.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username IS NULL", result.getQuery()); Assert.assertEquals(0, result.getParams().size()); Assert.assertEquals("username IS NULL", result.getQuery(true)); }
@Test public void testOperatorIsNotNull() throws IOException { String json = FileHelper.getStringFrom("tasks/operator-is-not-null.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username IS NOT NULL", result.getQuery()); Assert.assertEquals(0, result.getParams().size()); Assert.assertEquals("username IS NOT NULL", result.getQuery(true)); }
@Test public void testConditionAnd() throws IOException { String json = FileHelper.getStringFrom("tasks/condition-and-1.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username = ?", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals("username = 'Mistic'", result.getQuery(true)); json = FileHelper.getStringFrom("tasks/condition-and-more.json"); result = builder.build(json); Assert.assertEquals("username = ? AND age = ?", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals(10, result.getParams().get(1)); Assert.assertEquals("username = 'Mistic' AND age = 10", result.getQuery(true)); }
@Test public void testConditionOr() throws IOException { String json = FileHelper.getStringFrom("tasks/condition-or-1.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("username = ?", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals("username = 'Mistic'", result.getQuery(true)); json = FileHelper.getStringFrom("tasks/condition-or-more.json"); result = builder.build(json); Assert.assertEquals("username = ? OR age = ?", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals(10, result.getParams().get(1)); Assert.assertEquals("username = 'Mistic' OR age = 10", result.getQuery(true)); }
@Test public void testConditionNot() throws IOException { String json = FileHelper.getStringFrom("tasks/condition-not-and-1.json"); SqlQueryResult result = builder.build(json); Assert.assertEquals("NOT ( username = ? )", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals("NOT ( username = 'Mistic' )", result.getQuery(true)); json = FileHelper.getStringFrom("tasks/condition-not-and-more.json"); result = builder.build(json); Assert.assertEquals("NOT ( username = ? AND age = ? )", result.getQuery()); Assert.assertEquals("Mistic", result.getParams().get(0)); Assert.assertEquals(10, result.getParams().get(1)); Assert.assertEquals("NOT ( username = 'Mistic' AND age = 10 )", result.getQuery(true)); }