Since the framework is still much in its infancy, I urge you to download, trial it and comment on it in the forums.
Dynamic Query Builder
The role of dynamic query builder is to assist you in creating dynamic sql queries.Creating sql queries using raw string concatenation is a sure way into the flames of hell,
as logic about correctly concatenating string, adding commas etc. become intermixed with the actual SQL to express.
Tests as documentation
Since this module is still in its infancy, I'd rather convey the modules use through existing Junit test cases rather than elaborating on an interface that may radically change. Also, it is often claimed, that with a good test suite, documentation is not needed at all... So let's see if you can agree with that ;-).Notice that the tests for simplicity of testing removes all white space in the generated SQL!
package org.sqlorm.querybuilder; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.sqlorm.helper.TestHelper; /** * @author kasper graversen */ public class SelectTest { @Test public void select() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam"); assertEquals("SELECTp.nameASnaamFROM", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_distinct() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").distinct(); assertEquals("SELECTDISTINCTp.nameASnaamFROM", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); assertEquals("SELECTp.nameASnaamFROMpersonASp", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_crossjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.crossJoin("job"); assertEquals("SELECTp.nameASnaamFROMpersonASpCROSSJOINjobON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_crossjoin_crossjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.crossJoin("cross", "in1"); select.crossJoin("cross", "in2"); assertEquals("refering to the same table using cross joins", // "SELECTp.nameASnaamFROMpersonASpCROSSJOINcrossASin1ONCROSSJOINcrossASin2ON", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_crossjoin_crossjoin_duplicate_removed() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.crossJoin("cross"); select.crossJoin("cross"); assertEquals("refering to the same table using cross joins", // "SELECTp.nameASnaamFROMpersonASpCROSSJOINcrossON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_from() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p").from("job"); assertEquals("SELECTp.nameASnaamFROMpersonASp,job", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_from_duplicate_removed() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p") .from("person", "p"); assertEquals("refering to the same table using from", // "SELECTp.nameASnaamFROMpersonASp", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("job"); assertEquals("SELECTp.nameASnaamFROMpersonASpINNERJOINjobON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_1_condition() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and("a.id = p.addressId"); assertEquals("SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaONa.id=p.addressId", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_2_and_conditions() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and("a.id = p.addressId").and("a.zip = nn"); assertEquals("SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaONa.id=p.addressIdANDa.zip=nn", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_and_and_or_conditions() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and("a.id = p.addressId").and("a.zip = nn").or("a.zip = ng72"); assertEquals("SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaONa.id=p.addressIdANDa.zip=nnORa.zip=ng72", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_innerjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("inner", "in1"); select.innerJoin("inner", "in2"); assertEquals("refering to the same table using inner joins", // "SELECTp.nameASnaamFROMpersonASpINNERJOINinnerASin1ONINNERJOINinnerASin2ON", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_innerjoin_and_and_or_and_and_conditions_by_multiple_same_innerjoins() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and("a.id = p.addressId").and("a.zip = nn"); select.innerJoin("address", "a").on().or("a.id = p.addressId").and("a.zip = ld99"); assertEquals( "correct sql, but maybe unintended grouping of all constraints", "SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaONa.id=p.addressIdANDa.zip=nnORa.id=p.addressIdANDa.zip=ld99", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_innerjoin_duplicate_removed() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("inner"); select.innerJoin("inner"); assertEquals("refering to the same table using inner joins", // "SELECTp.nameASnaamFROMpersonASpINNERJOINinnerON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_not_in() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and("a.id NOT IN(#)", QueryBuilderHelper.numbers(1, 2, 3, 4)); assertEquals("SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaONa.idNOTIN(1,2,3,4)", TestHelper .removeAllFormatting(select.toSql())); } @Test(expected = NumberFormatException.class) public void select_from_innerjoin_not_in_fail() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and("a.id NOT IN(#)", QueryBuilderHelper.numbers(1, 2, 3, "v")); assertEquals("SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaONa.idNOTIN(1,2,3,4)", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_par_and_and_or_par_and_and_conditions() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on()// .and(new AndOrList().and("a.id = p.addressId").and("a.zip = nn"))// .or(new AndOrList().and("a.id = p.addressId").and("a.zip = ld99")); assertEquals( "SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaON(a.id=p.addressIdANDa.zip=nn)OR(a.id=p.addressIdANDa.zip=ld99)", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_par_and_and_or_par_and_and_conditions_by_multiple_same_innerjoins() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().and(new AndOrList().and("a.id = p.addressId").and("a.zip = nn")); select.innerJoin("address", "a").on().or(new AndOrList().and("a.id = p.addressId").and("a.zip = ld99")); assertEquals( "SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaON(a.id=p.addressIdANDa.zip=nn)OR(a.id=p.addressIdANDa.zip=ld99)", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_innerjoin_par_and_and_or_par_and_and_conditions_by_multiple_same_innerjoins_can_start_with_or() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.innerJoin("address", "a").on().or(new AndOrList().and("a.id = p.addressId").and("a.zip = nn")); select.innerJoin("address", "a").on().or(new AndOrList().and("a.id = p.addressId").and("a.zip = ld99")); assertEquals( "SELECTp.nameASnaamFROMpersonASpINNERJOINaddressASaON(a.id=p.addressIdANDa.zip=nn)OR(a.id=p.addressIdANDa.zip=ld99)", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_leftjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.leftJoin("job"); assertEquals("SELECTp.nameASnaamFROMpersonASpLEFTJOINjobON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_leftjoin_leftjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.leftJoin("left", "in1"); select.leftJoin("left", "in2"); assertEquals("refering to the same table using left joins", // "SELECTp.nameASnaamFROMpersonASpLEFTJOINleftASin1ONLEFTJOINleftASin2ON", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_leftjoin_leftjoin_duplicate_removed() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.leftJoin("left"); select.leftJoin("left"); assertEquals("refering to the same table using left joins", // "SELECTp.nameASnaamFROMpersonASpLEFTJOINleftON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_outerjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.outerJoin("job"); assertEquals("SELECTp.nameASnaamFROMpersonASpOUTERJOINjobON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_outerjoin_outerjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.outerJoin("outer", "in1"); select.outerJoin("outer", "in2"); assertEquals("refering to the same table using outer joins", // "SELECTp.nameASnaamFROMpersonASpOUTERJOINouterASin1ONOUTERJOINouterASin2ON", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_outerjoin_outerjoin_duplicate_removed() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.outerJoin("outer"); select.outerJoin("outer"); assertEquals("refering to the same table using outer joins", // "SELECTp.nameASnaamFROMpersonASpOUTERJOINouterON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_rightjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.rightJoin("job"); assertEquals("SELECTp.nameASnaamFROMpersonASpRIGHTJOINjobON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_rightjoin_rightjoin() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.rightJoin("right", "in1"); select.rightJoin("right", "in2"); assertEquals("refering to the same table using right joins", // "SELECTp.nameASnaamFROMpersonASpRIGHTJOINrightASin1ONRIGHTJOINrightASin2ON", TestHelper .removeAllFormatting(select.toSql())); } @Test public void select_from_rightjoin_rightjoin_duplicate_removed() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").from("person", "p"); select.rightJoin("right"); select.rightJoin("right"); assertEquals("refering to the same table using right joins", // "SELECTp.nameASnaamFROMpersonASpRIGHTJOINrightON", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_select() { final ItfSelectBuilder select = new SelectBuilder().select("a").from(new SelectBuilder().select("b").from("B"), "bb"); assertEquals("SELECTaFROM(SELECTbFROMB)ASbb", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_from_where() { final ItfSelectBuilder select = new SelectBuilder(); select.select("a").from("A").where().and("a = 1"); assertEquals("SELECTaFROMAWHEREa=1", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_group_by_a() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").groupBy("a"); assertEquals("SELECTp.nameASnaamFROMGROUPBYa", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_group_by_a_b() { final ItfSelectBuilder select = new SelectBuilder().select("p.name", "naam").groupBy("a").groupBy("b"); assertEquals("SELECTp.nameASnaamFROMGROUPBYa,b", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_innerjoin_select() { final ItfSelectBuilder select = new SelectBuilder().select("a").from("A"); final ItfSelectBuilder inSelect = new SelectBuilder(); inSelect.select("f.id").from("f").where().and("f=10"); select.innerJoin("i").on().and("i.id IN (#)", inSelect); assertEquals("SELECTaFROMAINNERJOINiONi.idIN(SELECTf.idFROMfWHEREf=10)", TestHelper.removeAllFormatting(select .toSql())); } @Test public void select_orderby_duplicate_should_be_filtered_away() { final ItfSelectBuilder select = new SelectBuilder(); select.select("a").orderBy("a").orderBy("a"); assertEquals("SELECTaFROMORDERBYa", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_orderby_orderbyasc_orderbydesc() { final ItfSelectBuilder select = new SelectBuilder(); select.select("a").orderBy("a").orderByAsc("b").orderByDesc("c"); assertEquals("SELECTaFROMORDERBYa,bASC,cDESC", TestHelper.removeAllFormatting(select.toSql())); } @Test public void select_having() { final ItfSelectBuilder select = new SelectBuilder(); select.select("a").having().and("count(*) > 3").or("a = 3").orderBy("a"); assertEquals("SELECTaFROMHAVINGcount(*)>3ORa=3ORDERBYa", TestHelper.removeAllFormatting(select.toSql())); } }