QueryBuilderImpl implements QueryBuilder { @Override public InsertBuilder insertInto(String table) { return new InsertQuery(table); }  @Override WhereBuilder<UpdateQueryExecution, ? extends LimitBuilder<? extends UpdateQueryExecution>> deleteFrom(
            String table); @Override WhereBuilder<UpdateQueryExecution, ? extends LimitBuilder<? extends UpdateQueryExecution>> deleteFrom(
            Outline<?> outline); @Override WhereBuilder<UpdateQueryExecution, ? extends LimitBuilder<? extends UpdateQueryExecution>> deleteFromNamed(
            String table); @Override WhereBuilder<UpdateQueryExecution, ? extends LimitBuilder<? extends UpdateQueryExecution>> deleteFromNamed(
            Outline<?> outline); @Override InsertBuilder insertInto(String table); @Override InsertBuilder insertInto(Outline<?> outline); @Override SelectBuilder select(String column); @Override SelectBuilder select(Member member); @Override SelectBuilder select(String... columns); @Override SelectBuilder select(Member... members); @Override SelectBuilder select(Iterable<String> columns); @Override SelectBuilder selectQualified(String table, String column); @Override SelectBuilder selectQualified(String table, String... columns); @Override SelectBuilder selectQualified(String table, Iterable<String> columns); @Override SelectBuilder selectAll(); @Override SelectBuilder selectAllIn(Outline<?> outline); @Override UpdateBuilder update(String table); @Override UpdateBuilder update(Outline<?> outline);  }
@Test public void insertQueries() { TestQueryBuilderImpl queryBuilder = new TestQueryBuilderImpl(); queryBuilder.insertInto("table_name").row(r -> r.put("int_column", 2).put("boolean_column", true).build()).count(); assertEquals("INSERT INTO table_name (int_column, boolean_column) VALUES (?, ?)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { 2, true }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").row(r -> r.put("boolean_column", true).put("int_column", 2).build()).count(); assertEquals("INSERT INTO table_name (boolean_column, int_column) VALUES (?, ?)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { true, 2 }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").row(r -> r.put("boolean_value", true).put("null_column", null).build()).count(); assertEquals("INSERT INTO table_name (boolean_value, null_column) VALUES (?, NULL)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { true }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").row(r -> r.put("string_column", "value").build()).count(); assertEquals("INSERT INTO table_name (string_column) VALUES (?)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { "value" }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").values( Arrays.asList(new InsertQueryBean("id1", "name1"), new InsertQueryBean("id2", "name2")), (r, b) -> r.put("id", b.getId()).put("name", b.getName()).build()).count(); assertEquals("INSERT INTO table_name (id, name) VALUES (?, ?), (?, ?)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { "id1", "name1", "id2", "name2" }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").values(ImmutableMap.of("int_column", 2, "boolean_column", true)).count(); assertEquals("INSERT INTO table_name (int_column, boolean_column) VALUES (?, ?)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { 2, true }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); HashMap<String, Object> values = new LinkedHashMap<>(); values.put("int_column", 2); values.put("boolean_column", null); queryBuilder.insertInto("table_name").values(values).count(); assertEquals("INSERT INTO table_name (int_column, boolean_column) VALUES (?, NULL)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { 2 }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); HashMap<String, Object> row1 = new LinkedHashMap<>(); row1.put("int_column", 2); row1.put("boolean_column", null); HashMap<String, Object> row2 = new LinkedHashMap<>(); row2.put("int_column", null); row2.put("string_column", "value"); queryBuilder.insertInto("table_name").values(row1, row2).count(); assertEquals("INSERT INTO table_name (int_column, boolean_column, string_column) VALUES (?, NULL, NULL), (NULL, NULL, ?)", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { 2, "value" }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").row(r -> r.put("boolean_column", true).put("int_column", 2).build()) .onDuplicateKeyUpdate(r -> r.put("boolean_column", false).build()).count(); assertEquals("INSERT INTO table_name (boolean_column, int_column) VALUES (?, ?) ON DUPLICATE KEY UPDATE boolean_column = ?", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { true, 2, false }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); queryBuilder.insertInto("table_name").values( Arrays.asList(new InsertQueryBean("id1", "name1"), new InsertQueryBean("id2", "name2")), (r, b) -> r.put("id", b.getId()).put("name", b.getName()).build()) .onDuplicateKeyUpdate(ImmutableMap.of("name", 12)) .count(); assertEquals("INSERT INTO table_name (id, name) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE name = ?", queryBuilder.getLastQuery()); assertArrayEquals(new Object[] { "id1", "name1", "id2", "name2", 12 }, queryBuilder.getLastParameters()); assertTrue(queryBuilder.getLastWasUpdate()); }