Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added capability of using IN clauses. #26

Merged
merged 2 commits into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.chodavarapu.datamill.reflection.Member;

import java.util.Collection;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
Expand All @@ -13,4 +15,8 @@ public interface ConditionBuilder<R> {
<T> WhereBuilder<R> is(String column, T value);
<T> WhereBuilder<R> is(String table, String column, T value);
<T> WhereBuilder<R> is(Member member, T value);

<T> WhereBuilder<R> in(String column, Collection<T> value);
<T> WhereBuilder<R> in(String table, String column, Collection<T> value);
<T> WhereBuilder<R> in(Member member, Collection<T> value);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package org.chodavarapu.datamill.db.impl;

import com.google.common.base.Joiner;
import org.chodavarapu.datamill.db.*;
import org.chodavarapu.datamill.db.ConditionBuilder;
import org.chodavarapu.datamill.db.InsertBuilder;
import org.chodavarapu.datamill.db.InsertSuffixBuilder;
import org.chodavarapu.datamill.db.JoinBuilder;
import org.chodavarapu.datamill.db.QueryBuilder;
import org.chodavarapu.datamill.db.Row;
import org.chodavarapu.datamill.db.RowBuilder;
import org.chodavarapu.datamill.db.SelectBuilder;
import org.chodavarapu.datamill.db.UpdateBuilder;
import org.chodavarapu.datamill.db.UpdateQueryExecution;
import org.chodavarapu.datamill.db.WhereBuilder;
import org.chodavarapu.datamill.reflection.Member;
import org.chodavarapu.datamill.reflection.Outline;
import org.chodavarapu.datamill.values.Times;
Expand All @@ -14,6 +24,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -48,6 +59,11 @@ public abstract class QueryBuilderImpl implements QueryBuilder {
private static final String SQL_UPDATE = "UPDATE ";
private static final String SQL_IS = " IS ";
private static final String SQL_AND = " AND ";
private static final String SQL_IN = " IN ";

private static final String OPEN_PARENTHESIS = "(";
private static final String CLOSE_PARENTHESIS = ")";
private static final String COMMA = ",";

private static void appendUpdateAssignments(StringBuilder query, List<Object> parameters, Map<String, ?> values) {
List<String> setters = new ArrayList<>(values.size());
Expand Down Expand Up @@ -242,6 +258,12 @@ public <T> WhereBuilder<UpdateQueryExecution> is(String column, T value) {
return this;
}

@Override
public <T> WhereBuilder<UpdateQueryExecution> in(String column, Collection<T> values) {
addInClause(column, values);
return this;
}

@Override
public UpdateQueryExecution execute() {
return QueryBuilderImpl.this.update(query.toString(), parameters.toArray(new Object[parameters.size()]));
Expand Down Expand Up @@ -278,6 +300,12 @@ public <T> WhereBuilder<Observable<Row>> is(String column, T value) {
return this;
}

@Override
public <T> WhereBuilder<Observable<Row>> in(String column, Collection<T> values) {
addInClause(column, values);
return this;
}

@Override
public Observable<Row> execute() {
return QueryBuilderImpl.this.query(query.toString(), parameters.toArray(new Object[parameters.size()]));
Expand Down Expand Up @@ -321,6 +349,26 @@ protected <T> void addIsClause(String column, T value) {
}
}

protected <T> void addInClause(String column, Collection<T> values) {
if (!values.isEmpty()) {
query.append(column);
query.append(SQL_IN);

query.append(OPEN_PARENTHESIS);
Iterator<T> iterator = values.iterator();
while(iterator.hasNext()) {
query.append(SQL_PARAMETER_PLACEHOLDER);
iterator.next();
if (iterator.hasNext()) {
query.append(COMMA);
}
}
query.append(CLOSE_PARENTHESIS);

parameters.addAll(values);
}
}

@Override
public ConditionBuilder<R> and() {
query.append(SQL_AND);
Expand All @@ -347,6 +395,16 @@ public <T> WhereBuilder<R> is(Member member, T value) {
return is(member.outline().pluralName(), member.name(), value);
}

@Override
public <T> WhereBuilder<R> in(String table, String column, Collection<T> values) {
return in(qualifiedName(table, column), values);
}

@Override
public <T> WhereBuilder<R> in(Member member, Collection<T> values) {
return in(member.outline().pluralName(), member.name(), values);
}

@Override
public ConditionBuilder<R> where() {
query.append(SQL_WHERE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ public void selectQueries() {
assertArrayEquals(new Object[] { 2 }, queryBuilder.getLastParameters());
assertFalse(queryBuilder.getLastWasUpdate());

queryBuilder.select("column_name", "second_column").from("table_name").where().in("table_name", "int_column", Arrays.asList(1, 2)).execute();
assertEquals("SELECT column_name, second_column FROM table_name WHERE table_name.int_column IN (?,?)", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1, 2 }, queryBuilder.getLastParameters());
assertFalse(queryBuilder.getLastWasUpdate());

queryBuilder.select("column_name", "second_column").from("table_name").where().in("table_name", "int_column", Arrays.asList(1)).execute();
assertEquals("SELECT column_name, second_column FROM table_name WHERE table_name.int_column IN (?)", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1 }, queryBuilder.getLastParameters());
assertFalse(queryBuilder.getLastWasUpdate());

queryBuilder.select(Arrays.asList("column_name", "second_column")).from("table_name")
.leftJoin("second_table").onEq("second_column", "third_column").all();
assertEquals("SELECT column_name, second_column FROM table_name LEFT JOIN second_table ON second_column = third_column", queryBuilder.getLastQuery());
Expand Down Expand Up @@ -254,6 +264,16 @@ public void deleteQueries() {
assertEquals("DELETE table_name FROM table_name WHERE string_column = ?", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { "value" }, queryBuilder.getLastParameters());
assertTrue(queryBuilder.getLastWasUpdate());

queryBuilder.deleteFrom("table_name").where().in("string_column", Arrays.asList(1, 2)).execute();
assertEquals("DELETE FROM table_name WHERE string_column IN (?,?)", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1, 2 }, queryBuilder.getLastParameters());
assertTrue(queryBuilder.getLastWasUpdate());

queryBuilder.deleteFrom("table_name").where().in("string_column", Arrays.asList(1)).execute();
assertEquals("DELETE FROM table_name WHERE string_column IN (?)", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1 }, queryBuilder.getLastParameters());
assertTrue(queryBuilder.getLastWasUpdate());
}

@Test
Expand Down Expand Up @@ -365,6 +385,16 @@ public void updateQueries() {
assertArrayEquals(new Object[] { 1, 2 }, queryBuilder.getLastParameters());
assertTrue(queryBuilder.getLastWasUpdate());

queryBuilder.update("table_name").set(ImmutableMap.of("int_column", 1)).where().in("int_column", Arrays.asList(2, 3)).execute();
assertEquals("UPDATE table_name SET int_column = ? WHERE int_column IN (?,?)", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1, 2, 3 }, queryBuilder.getLastParameters());
assertTrue(queryBuilder.getLastWasUpdate());

queryBuilder.update("table_name").set(ImmutableMap.of("int_column", 1)).where().in("int_column", Arrays.asList(2)).execute();
assertEquals("UPDATE table_name SET int_column = ? WHERE int_column IN (?)", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1, 2 }, queryBuilder.getLastParameters());
assertTrue(queryBuilder.getLastWasUpdate());

queryBuilder.update("table_name").set(ImmutableMap.of("int_column", 1)).where().eq("boolean_column", true).execute();
assertEquals("UPDATE table_name SET int_column = ? WHERE boolean_column = ?", queryBuilder.getLastQuery());
assertArrayEquals(new Object[] { 1, true }, queryBuilder.getLastParameters());
Expand Down