Created
May 7, 2010 18:44
-
-
Save iamnoah/393842 to your computer and use it in GitHub Desktop.
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
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
/** | |
* Dear Android, why do I have to write this class myself? Is this to discourage SQL use? | |
*/ | |
public class QueryBuilder { | |
private String[] columns; | |
private boolean distinct; | |
private String table; | |
private String selection; | |
private String[] args; | |
private String groupBy; | |
private String having; | |
private String limit; | |
private String orderBy; | |
public QueryBuilder select(String... columns) { | |
this.columns = columns; | |
return this; | |
} | |
public QueryBuilder distinct() { | |
this.distinct = true; | |
return this; | |
} | |
public QueryBuilder from(String table) { | |
this.table = table; | |
return this; | |
} | |
public QueryBuilder where(String selection, String... args) { | |
this.selection = selection; | |
this.args = args; | |
return this; | |
} | |
public QueryBuilder groupBy(String groupBy) { | |
this.groupBy = groupBy; | |
return this; | |
} | |
public QueryBuilder having(String having) { | |
this.having = having; | |
return this; | |
} | |
public QueryBuilder orderBy(String orderBy) { | |
this.orderBy = orderBy; | |
return this; | |
} | |
public QueryBuilder limit(String limit) { | |
this.limit = limit; | |
return this; | |
} | |
public Cursor query(SQLiteDatabase db) { | |
return db.query(distinct, table, columns, selection, args, groupBy, | |
having, orderBy, limit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compare the actual APIs. SQLiteQueryBuilder is a huge pain to use.