Created
April 30, 2012 15:26
-
-
Save fluxtah/2559252 to your computer and use it in GitHub Desktop.
A fluent android sqlite selection query builder useful for constructing content provider selections with arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SelectionQueryBuilder q = new SelectionQueryBuilder() | |
.expr("is_awesome", EQ, true) | |
.expr("money", GT, 50.0f) | |
.expr("speed", LT, 21.1f) | |
.or() | |
.expr("door_number", EQ, 123) | |
.or().expr( | |
new SelectionQueryBuilder() | |
.expr("a", GT, 0) | |
.expr("a", LT, 100) | |
) | |
.expr( | |
new SelectionQueryBuilder() | |
.opt("b", EQ, 0) | |
.opt("c", EQ, false) | |
) | |
.opt("apples", EQ, 0); | |
String[] args = q.getArgsArray(); | |
String selection = q.toString(); | |
*/ | |
* Produces: | |
* selection: is_awesome = ? AND money > ? AND speed < ? OR door_number = ? OR (a > ? AND a < ?) | |
* args: [1, 50.0, 21.1, 123] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nike.oneplussdk.sample; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Example: | |
* SelectionQueryBuilder q = new SelectionQueryBuilder() | |
* .expr("is_awesome", EQ, true) | |
* .expr("money", GT, 50.0f) | |
* .expr("speed", LT, 21.1f) | |
* .expr("door_number", EQ, 123) | |
* .opt("apples", EQ, 0); | |
* | |
*/ | |
public class SelectionQueryBuilder { | |
public interface Op { | |
public String EQ = " = "; | |
public String NEQ = " != "; | |
public String GT = " > "; | |
public String LT = " < "; | |
public String GTEQ = " >= "; | |
public String LTEQ = " <= "; | |
public String LIKE = " LIKE "; | |
public String IS = " IS "; | |
public String ISNOT = " IS NOT "; | |
public String REGEXP = " REGEXP "; | |
} | |
private static final String AND = " AND "; | |
private static final String OR = " OR "; | |
private StringBuilder mBuilder; | |
private List<String> mArgs = new ArrayList<String>(); | |
private String mNextOp = null; | |
public List<String> getArgs() { | |
return mArgs; | |
} | |
public String[] getArgsArray() { | |
return mArgs.toArray(new String[0]); | |
} | |
public SelectionQueryBuilder() { | |
mBuilder = new StringBuilder(); | |
} | |
public SelectionQueryBuilder expr(String column, String op, String arg) { | |
ensureOp(); | |
mBuilder.append(column).append(op).append("?"); | |
mArgs.add(arg); | |
mNextOp = null; | |
return this; | |
} | |
public SelectionQueryBuilder expr(SelectionQueryBuilder builder) { | |
List<String> args = builder.getArgs(); | |
if(args.size() > 0) { | |
ensureOp(); | |
mBuilder.append("(").append(builder).append(")"); | |
mArgs.addAll(args); | |
} | |
mNextOp = null; | |
return this; | |
} | |
public SelectionQueryBuilder expr(String column, String op, boolean arg) { | |
return expr(column, op, arg ? "1" : "0"); | |
} | |
public SelectionQueryBuilder expr(String column, String op, int arg) { | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder expr(String column, String op, long arg) { | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder expr(String column, String op, float arg) { | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder expr(String column, String op, double arg) { | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder optExpr(String column, String op, String arg) { | |
if(arg == null) { | |
return this; | |
} | |
return expr(column, op, arg); | |
} | |
public SelectionQueryBuilder optExpr(String column, String op, boolean arg) { | |
if(!arg) { | |
return this; | |
} | |
return expr(column, op, arg ? "1" : "0"); | |
} | |
public SelectionQueryBuilder opt(String column, String op, int arg) { | |
if(arg == 0) { | |
return this; | |
} | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder opt(String column, String op, boolean arg) { | |
if(!arg) { | |
return this; | |
} | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder opt(String column, String op, long arg) { | |
if(arg == 0) { | |
return this; | |
} | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder opt(String column, String op, float arg) { | |
if(arg == 0) { | |
return this; | |
} | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder opt(String column, String op, double arg) { | |
if(arg == 0) { | |
return this; | |
} | |
return expr(column, op, String.valueOf(arg)); | |
} | |
public SelectionQueryBuilder and() { | |
mNextOp = AND; | |
return this; | |
} | |
public SelectionQueryBuilder or() { | |
mNextOp = OR; | |
return this; | |
} | |
private void ensureOp() { | |
if(mBuilder.length() == 0) { | |
return; | |
} | |
if(mNextOp == null) { | |
mBuilder.append(AND); | |
} else { | |
mBuilder.append(mNextOp); | |
mNextOp = null; | |
} | |
} | |
@Override | |
public String toString() { | |
return mBuilder.toString(); | |
} | |
} |
@ fluxtah Can you please add the IN and NOT IN operator in query builder class.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@fluxtah what's the license on this code?