1+ /*
2+ * Copyright (c) 2011-2012 CommonsWare, LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .commonsware .cwac .loaderex ;
18+
19+ import java .io .FileDescriptor ;
20+ import java .io .PrintWriter ;
21+ import java .util .Arrays ;
22+ import android .content .ContentValues ;
23+ import android .content .Context ;
24+ import android .database .Cursor ;
25+ import android .database .sqlite .SQLiteDatabase ;
26+ import android .database .sqlite .SQLiteOpenHelper ;
27+
28+ public class SQLiteCursorLoader extends AbstractCursorLoader {
29+ SQLiteOpenHelper db =null ;
30+ String rawQuery =null ;
31+ String [] args =null ;
32+
33+ /**
34+ * Creates a fully-specified SQLiteCursorLoader. See
35+ * {@link SQLiteDatabase#rawQuery(SQLiteDatabase, String, String[])
36+ * SQLiteDatabase.rawQuery()} for documentation on the
37+ * meaning of the parameters. These will be passed as-is
38+ * to that call.
39+ */
40+ public SQLiteCursorLoader (Context context , SQLiteOpenHelper db ,
41+ String rawQuery , String [] args ) {
42+ super (context );
43+ this .db =db ;
44+ this .rawQuery =rawQuery ;
45+ this .args =args ;
46+ }
47+
48+ /**
49+ * Runs on a worker thread and performs the actual
50+ * database query to retrieve the Cursor.
51+ */
52+ @ Override
53+ protected Cursor buildCursor () {
54+ return (db .getReadableDatabase ().rawQuery (rawQuery , args ));
55+ }
56+
57+ /**
58+ * Writes a semi-user-readable roster of contents to
59+ * supplied output.
60+ */
61+ @ Override
62+ public void dump (String prefix , FileDescriptor fd ,
63+ PrintWriter writer , String [] args ) {
64+ super .dump (prefix , fd , writer , args );
65+ writer .print (prefix );
66+ writer .print ("rawQuery=" );
67+ writer .println (rawQuery );
68+ writer .print (prefix );
69+ writer .print ("args=" );
70+ writer .println (Arrays .toString (args ));
71+ }
72+
73+ public void insert (String table , String nullColumnHack ,
74+ ContentValues values ) {
75+ new InsertTask (this ).execute (db , table , nullColumnHack , values );
76+ }
77+
78+ public void update (String table , ContentValues values ,
79+ String whereClause , String [] whereArgs ) {
80+ new UpdateTask (this ).execute (db , table , values , whereClause ,
81+ whereArgs );
82+ }
83+
84+ public void replace (String table , String nullColumnHack ,
85+ ContentValues values ) {
86+ new ReplaceTask (this ).execute (db , table , nullColumnHack , values );
87+ }
88+
89+ public void delete (String table , String whereClause ,
90+ String [] whereArgs ) {
91+ new DeleteTask (this ).execute (db , table , whereClause , whereArgs );
92+ }
93+
94+ public void execSQL (String sql , Object [] bindArgs ) {
95+ new ExecSQLTask (this ).execute (db , sql , bindArgs );
96+ }
97+
98+ private class InsertTask extends
99+ ContentChangingTask <Object , Void , Void > {
100+ InsertTask (SQLiteCursorLoader loader ) {
101+ super (loader );
102+ }
103+
104+ @ Override
105+ protected Void doInBackground (Object ... params ) {
106+ SQLiteOpenHelper db =(SQLiteOpenHelper )params [0 ];
107+ String table =(String )params [1 ];
108+ String nullColumnHack =(String )params [2 ];
109+ ContentValues values =(ContentValues )params [3 ];
110+
111+ db .getWritableDatabase ().insert (table , nullColumnHack , values );
112+
113+ return (null );
114+ }
115+ }
116+
117+ private class UpdateTask extends
118+ ContentChangingTask <Object , Void , Void > {
119+ UpdateTask (SQLiteCursorLoader loader ) {
120+ super (loader );
121+ }
122+
123+ @ Override
124+ protected Void doInBackground (Object ... params ) {
125+ SQLiteOpenHelper db =(SQLiteOpenHelper )params [0 ];
126+ String table =(String )params [1 ];
127+ ContentValues values =(ContentValues )params [2 ];
128+ String where =(String )params [3 ];
129+ String [] whereParams =(String [])params [4 ];
130+
131+ db .getWritableDatabase ()
132+ .update (table , values , where , whereParams );
133+
134+ return (null );
135+ }
136+ }
137+
138+ private class ReplaceTask extends
139+ ContentChangingTask <Object , Void , Void > {
140+ ReplaceTask (SQLiteCursorLoader loader ) {
141+ super (loader );
142+ }
143+
144+ @ Override
145+ protected Void doInBackground (Object ... params ) {
146+ SQLiteOpenHelper db =(SQLiteOpenHelper )params [0 ];
147+ String table =(String )params [1 ];
148+ String nullColumnHack =(String )params [2 ];
149+ ContentValues values =(ContentValues )params [3 ];
150+
151+ db .getWritableDatabase ().replace (table , nullColumnHack , values );
152+
153+ return (null );
154+ }
155+ }
156+
157+ private class DeleteTask extends
158+ ContentChangingTask <Object , Void , Void > {
159+ DeleteTask (SQLiteCursorLoader loader ) {
160+ super (loader );
161+ }
162+
163+ @ Override
164+ protected Void doInBackground (Object ... params ) {
165+ SQLiteOpenHelper db =(SQLiteOpenHelper )params [0 ];
166+ String table =(String )params [1 ];
167+ String where =(String )params [2 ];
168+ String [] whereParams =(String [])params [3 ];
169+
170+ db .getWritableDatabase ().delete (table , where , whereParams );
171+
172+ return (null );
173+ }
174+ }
175+
176+ private class ExecSQLTask extends
177+ ContentChangingTask <Object , Void , Void > {
178+ ExecSQLTask (SQLiteCursorLoader loader ) {
179+ super (loader );
180+ }
181+
182+ @ Override
183+ protected Void doInBackground (Object ... params ) {
184+ SQLiteOpenHelper db =(SQLiteOpenHelper )params [0 ];
185+ String sql =(String )params [1 ];
186+ Object [] bindParams =(Object [])params [2 ];
187+
188+ db .getWritableDatabase ().execSQL (sql , bindParams );
189+
190+ return (null );
191+ }
192+ }
193+ }
0 commit comments