@Test public void shouldNotChangeOriginalQueryWhenChangingACopy() throws Exception { Query originalQuery = buildComplexQuery().build(); originalQuery.perform(mDb); verify(mDb) .rawQuery( eq( "SELECT table_a.col_a FROM table_a LEFT JOIN table_b AS b ON (b.id > ?) GROUP BY b.id HAVING (col_a < ?) EXCEPT SELECT DISTINCT col_a FROM table_a WHERE (col_a == -1) ORDER BY table_a.col_a LIMIT 10 OFFSET 20"), eq(new String[] {"1", "2"})); QueryBuilder copy = originalQuery.buildUpon(); copy.where(column("a").is().not().nul()); copy.build().perform(mDb); verify(mDb) .rawQuery( eq( "SELECT table_a.col_a FROM table_a LEFT JOIN table_b AS b ON (b.id > ?) GROUP BY b.id HAVING (col_a < ?) EXCEPT SELECT DISTINCT col_a FROM table_a WHERE (col_a == -1) AND (a IS NOT NULL) ORDER BY table_a.col_a LIMIT 10 OFFSET 20"), eq(new String[] {"1", "2"})); originalQuery.perform(mDb); verify(mDb, times(2)) .rawQuery( eq( "SELECT table_a.col_a FROM table_a LEFT JOIN table_b AS b ON (b.id > ?) GROUP BY b.id HAVING (col_a < ?) EXCEPT SELECT DISTINCT col_a FROM table_a WHERE (col_a == -1) ORDER BY table_a.col_a LIMIT 10 OFFSET 20"), eq(new String[] {"1", "2"})); }
@Test public void shouldCopyTheQueryWithIncompleteJoinStatement() throws Exception { Query originalQuery = select().from("table_a").join("table_b").build(); QueryBuilder copy = originalQuery.buildUpon(); originalQuery.perform(mDb); copy.build().perform(mDb); verify(mDb, times(2)).rawQuery(eq("SELECT * FROM table_a JOIN table_b"), eq(new String[0])); }
@Test public void shouldCopyTheQueryWithJoinStatementWithUsingClause() throws Exception { Query originalQuery = select().from("table_a").left().join("table_b").using("id").build(); QueryBuilder copy = originalQuery.buildUpon(); originalQuery.perform(mDb); copy.build().perform(mDb); verify(mDb, times(2)) .rawQuery(eq("SELECT * FROM table_a LEFT JOIN table_b USING (id)"), eq(new String[0])); }
@Test public void shouldCopyTheQueryWithJoinStatementWithConstraint() throws Exception { Query originalQuery = select().from("table_a").join("table_b").on(column("id").eq().column("id_a")).build(); QueryBuilder copy = originalQuery.buildUpon(); originalQuery.perform(mDb); copy.build().perform(mDb); verify(mDb, times(2)) .rawQuery(eq("SELECT * FROM table_a JOIN table_b ON (id == id_a)"), eq(new String[0])); }