| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.content; |
| 18 |
|
| 19 |
import android.content.ContentValues; |
| 20 |
import android.database.Cursor; |
| 21 |
import android.database.sqlite.SQLiteDatabase; |
| 22 |
import android.text.TextUtils; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class SelectionBuilder { |
| 34 |
private StringBuilder mSelection = new StringBuilder(); |
| 35 |
private ArrayList<String> mSelectionArgs = new ArrayList<String>(); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public SelectionBuilder reset() { |
| 41 |
mSelection.setLength(0); |
| 42 |
mSelectionArgs.clear(); |
| 43 |
return this; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public SelectionBuilder append(String selection, Object... selectionArgs) { |
| 51 |
if (TextUtils.isEmpty(selection)) { |
| 52 |
if (selectionArgs != null && selectionArgs.length > 0) { |
| 53 |
throw new IllegalArgumentException( |
| 54 |
"Valid selection required when including arguments"); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
return this; |
| 59 |
} |
| 60 |
|
| 61 |
if (mSelection.length() > 0) { |
| 62 |
mSelection.append(" AND "); |
| 63 |
} |
| 64 |
|
| 65 |
mSelection.append("(").append(selection).append(")"); |
| 66 |
if (selectionArgs != null) { |
| 67 |
for (Object arg : selectionArgs) { |
| 68 |
|
| 69 |
|
| 70 |
mSelectionArgs.add(String.valueOf(arg)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return this; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public String getSelection() { |
| 83 |
return mSelection.toString(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public String[] getSelectionArgs() { |
| 92 |
return mSelectionArgs.toArray(new String[mSelectionArgs.size()]); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Cursor query(SQLiteDatabase db, String table, String[] columns, String orderBy) { |
| 100 |
return query(db, table, columns, null, null, orderBy, null); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public Cursor query(SQLiteDatabase db, String table, String[] columns, String groupBy, |
| 107 |
String having, String orderBy, String limit) { |
| 108 |
return db.query(table, columns, getSelection(), getSelectionArgs(), groupBy, having, |
| 109 |
orderBy, limit); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public int update(SQLiteDatabase db, String table, ContentValues values) { |
| 116 |
return db.update(table, values, getSelection(), getSelectionArgs()); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public int delete(SQLiteDatabase db, String table) { |
| 123 |
return db.delete(table, getSelection(), getSelectionArgs()); |
| 124 |
} |
| 125 |
} |