Skip to content

Instantly share code, notes, and snippets.

@confile
Last active September 21, 2015 14:42
Show Gist options
  • Save confile/a8f74bf355e22b052f47 to your computer and use it in GitHub Desktop.
Save confile/a8f74bf355e22b052f47 to your computer and use it in GitHub Desktop.
DBQueryBuilder a query builder for SQlighter
public class DBQueryBuilder {
public enum QueryType {
SELECT, DELETE, UPDATE, INSERT;
}
private SQLighterDb db;
public DBQueryBuilder(SQLighterDb db) {
this.db = db;
}
public static final class ValueBindExpression {
private final SQLighterDb db;
private final String tableName;
private final List<String> bindNames = new ArrayList<>();
private final QueryType type;
public ValueBindExpression(SQLighterDb db, String tableName, QueryType type) {
this.db = db;
this.tableName = tableName;
this.type = type;
}
public ValueBindExpression bindParam(String name, String value) {
bindNames.add(name);
db.addParam(value);
return this;
}
public ValueBindExpression bindParam(String name, double value) {
bindNames.add(name);
db.addParam(value);
return this;
}
public ValueBindExpression bindParam(String name, long value) {
bindNames.add(name);
db.addParam(value);
return this;
}
public ValueBindExpression bindParam(String name, byte[] value) {
bindNames.add(name);
db.addParam(value);
return this;
}
public ValueBindExpression bindParamToNull(String name) {
bindNames.add(name);
db.addParamNull();
return this;
}
public void build() {
if (type == QueryType.INSERT) {
String query = "insert into ";
query += tableName;
query += " (";
int bindSize = bindNames.size();
for (int i = 0; i < bindSize; i++) {
query += bindNames.get(i);
if (i < bindSize - 1) {
query += ",";
}
}
query += ") values (";
for (int i = 0; i < bindSize; i++) {
query += "?";
if (i < bindSize - 1) {
query += ",";
}
}
query += ")";
Log.d("TEST: ", "query1: " + query);
db.executeChange(query);
}
}
public WhereExpression where() {
if (type == QueryType.UPDATE) {
return new WhereExpression(db, tableName, bindNames, type);
}
return null;
}
}
public static final class WhereExpression {
private final SQLighterDb db;
private final List<String> bindWhereNames = new ArrayList<>();
private final List<String> bindExpressions = new ArrayList<>();
private final String tableName;
private String selectParams;
private final QueryType type;
private List<String> updateParams = new ArrayList<>();
public WhereExpression(SQLighterDb db, String tableName, String selectParams, QueryType type) {
this.db = db;
this.tableName = tableName;
this.selectParams = selectParams;
this.type = type;
}
public WhereExpression(SQLighterDb db, String tableName, QueryType type) {
this.db = db;
this.tableName = tableName;
this.type = type;
}
public WhereExpression(SQLighterDb db, String tableName, List<String> updateParams, QueryType type) {
this.db = db;
this.tableName = tableName;
this.type = type;
this.updateParams = updateParams;
}
public WhereExpression bindWhereParam(String name, String value) {
bindWhereNames.add(name);
db.addParam(value);
return this;
}
public WhereExpression bindWhereParam(String name, double value) {
bindWhereNames.add(name);
db.addParam(value);
return this;
}
public WhereExpression bindWhereParam(String name, long value) {
bindWhereNames.add(name);
db.addParam(value);
return this;
}
public WhereExpression bindWhereParam(String name, byte[] value) {
bindWhereNames.add(name);
db.addParam(value);
return this;
}
public WhereExpression bindWhereParam(String expression) {
bindExpressions.add(expression);
return this;
}
private String getWhereParamsString() {
String query = "";
int whereParamsSize = bindWhereNames.size();
int bindExpressionsSize = bindExpressions.size();
if ((whereParamsSize + bindExpressionsSize) > 0) {
query += " where ";
for (int i = 0; i < whereParamsSize; i++) {
query += bindWhereNames.get(i) + "=?";
if (i < whereParamsSize - 1) {
query += " and ";
}
}
for (int i = 0; i < bindExpressionsSize; i++) {
query += bindExpressions.get(i);
if (i < bindExpressionsSize - 1) {
query += " and ";
}
}
}
return query;
}
public SQLighterRs build() {
String query = "";
if (type == QueryType.SELECT) {
query += "select " + selectParams + " from " + tableName;
query += getWhereParamsString();
Log.d("TEST: ", "query2: " + query);
return db.executeSelect(query);
}
else if (type == QueryType.DELETE) {
query += "delete from "+tableName;
query += getWhereParamsString();
Log.d("TEST: ", "query3: " + query);
db.executeChange(query);
return null;
}
else if (type == QueryType.UPDATE) {
query += "update "+tableName;
int updateParamsSize = updateParams.size();
if (updateParamsSize > 0) {
query += " set ";
for(int i=0; i < updateParamsSize; i++) {
query += updateParams.get(i) + "=?";
if (i < updateParamsSize - 1) {
query += ", ";
}
}
}
query += getWhereParamsString();
Log.d("TEST: ", "query4: " + query);
db.executeChange(query);
}
return null;
}
}
public static final class SelectExpression {
private SQLighterDb db;
private String selectParams;
public SelectExpression(SQLighterDb db, String selectParams) {
this.db = db;
this.selectParams = selectParams;
}
public WhereExpression from(String tableName) {
return new WhereExpression(db, tableName, selectParams, QueryType.SELECT);
}
}
public static final class CreateTableExpression {
private SQLighterDb db;
private String tableName;
private Map<String, String> columns = new LinkedHashMap<>();
public CreateTableExpression(SQLighterDb db, String tableName) {
this.db = db;
this.tableName = tableName;
}
public CreateTableExpression addColumn(String name, String type) {
columns.put(name, type);
return this;
}
public void build() {
String query = "create table "+tableName;
int columnSize = columns.size();
if (columnSize > 0) {
query += " (";
int i = 0;
for(Map.Entry<String,String> column : columns.entrySet()) {
query += column.getKey() + " " + column.getValue();
if (i < columnSize - 1) {
query += ",";
}
i++;
}
query += ")";
}
Log.d("TEST: ", "query5: " + query);
db.executeChange(query);
}
}
public ValueBindExpression insertInto(String tableName) {
return new ValueBindExpression(db, tableName, QueryType.INSERT);
}
public SelectExpression select(String selectParams) {
return new SelectExpression(db, selectParams);
}
public WhereExpression deleteFrom(String tableName) {
return new WhereExpression(db, tableName, QueryType.DELETE);
}
public ValueBindExpression update(String tableName) {
return new ValueBindExpression(db, tableName, QueryType.UPDATE);
}
public CreateTableExpression createTable(String tableName) {
return new CreateTableExpression(db, tableName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment