Skip to content

Instantly share code, notes, and snippets.

@ussy
Created December 8, 2010 15:34
Show Gist options
  • Save ussy/733418 to your computer and use it in GitHub Desktop.
Save ussy/733418 to your computer and use it in GitHub Desktop.
for Android
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public abstract class AbstractSQLiteSupport {
protected SQLiteDatabase db;
public AbstractSQLiteSupport(SQLiteDatabase db) {
this.db = db;
}
protected Cursor query(Select cond) {
return db.query(cond.isDistinct(), cond.getTable(), cond.getColumns(), cond.getSelection(), cond.getSelectionArgs(),
cond.getGroupBy(), cond.getHaving(), cond.getOrderBy(), cond.getLimit());
}
protected Select select(String table, String... columns) {
return new Select(table, columns);
}
protected long insert(String table, ContentValues values) {
return insert(table, null, values);
}
protected long insert(String table, String nullColumnHack, ContentValues values) {
return db.insert(table, nullColumnHack, values);
}
protected int update(String table, ContentValues values, String whereClause, String... whereArgs) {
return db.update(table, values, whereClause, whereArgs);
}
protected int update(String table, ContentValues values, String whereClause, Object... whereArgs) {
String[] args = toStrings(whereArgs);
return update(table, values, whereClause, args);
}
protected int delete(String table) {
return delete(table, null);
}
protected int delete(String table, String whereClause, String... whereArgs) {
return db.delete(table, whereClause, whereArgs);
}
protected int delete(String table, String whereClause, Object... whereArgs) {
String[] args = toStrings(whereArgs);
return delete(table, whereClause, args);
}
protected String[] toStrings(Object[] args) {
String[] strings = (args == null) ? new String[0] : new String[args.length];
for (int i = 0; i < strings.length; i++) {
Object arg = args[i];
strings[i] = (arg == null) ? null : arg.toString();
}
return strings;
}
public class Select {
private String table;
public String getTable() {
return table;
}
private String[] columns;
public String[] getColumns() {
return columns;
}
private String selection;
public String getSelection() {
return selection;
}
private String[] selectionArgs;
public String[] getSelectionArgs() {
return selectionArgs;
}
private String groupBy;
public String getGroupBy() {
return groupBy;
}
private String having;
public String getHaving() {
return having;
}
private String orderBy;
public String getOrderBy() {
return orderBy;
}
private String limit;
public String getLimit() {
return limit;
}
private boolean distinct;
public boolean isDistinct() {
return distinct;
}
public Select(String table, String... columns) {
this.table = table;
this.columns = columns;
}
public Select where(String selection, Object... selectionArgs) {
this.selection = selection;
if (selectionArgs == null) {
this.selectionArgs = null;
return this;
}
String[] args = toStrings(selectionArgs);
this.selectionArgs = args;
return this;
}
public Select where(String selection, String... selectionArgs) {
this.selection = selection;
this.selectionArgs = selectionArgs;
return this;
}
public Select groupBy(String groupBy) {
this.groupBy = groupBy;
return this;
}
public Select having(String having) {
this.having = having;
return this;
}
public Select orderBy(String orderBy) {
this.orderBy = orderBy;
return this;
}
public Select limit(String limit) {
this.limit = limit;
return this;
}
public Select distinct(boolean distinct) {
this.distinct = distinct;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment