-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprovider.jinja
269 lines (226 loc) · 10.1 KB
/
provider.jinja
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**********************************************************************************************************************************************************************
****** AUTO GENERATED FILE BY ANDROID SQLITE HELPER SCRIPT BY FEDERICO PAOLINELLI. ANY CHANGE WILL BE WIPED OUT IF THE SCRIPT IS PROCESSED AGAIN. *******
**********************************************************************************************************************************************************************/
package {{ package }};
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import java.util.Date;
public class {{ name }}Provider extends ContentProvider {
private static final String TAG = "{{ name }}Provider";
protected static final String DATABASE_NAME = "{{ name }}.db";
protected static final int DATABASE_VERSION = {{ dbversion }};
// --------------- URIS --------------------
{% for table in tables -%}
public static final Uri {{ table.upper_name }}_URI = Uri.parse("content://{{ authority }}/{{ table.name }}");
{% endfor -%}
{% for table in tables %}
// -------------- {{ table.upper_name}} DEFINITIONS ------------
public static final String {{ table.table_name }} = "{{ table.name }}";
{% for field in table.fields -%}
public static final String {{ field.key_name }} = "{{ field.name }}";
public static final int {{ field.column_name }} = {{ loop.index }};
{% endfor -%}
public static final int ALL_{{ table.upper_name }} = {{ (loop.index - 1)* 2 }};
public static final int SINGLE_{{ table.upper_name }} = {{ (loop.index - 1) * 2 + 1}};
{% endfor %}
public static final String ROW_ID = "_id";
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
{% for table in tables %}
uriMatcher.addURI("{{ authority }}", "{{ table.name }}", ALL_{{ table.upper_name }});
uriMatcher.addURI("{{ authority }}", "{{ table.name }}/#", SINGLE_{{ table.upper_name }});
{% endfor -%}
}
// -------- TABLES CREATION ----------
{% for table in tables %}
// {{ table.name }} CREATION
private static final String DATABASE_{{ table.upper_name}}_CREATE = "create table " + {{ table.upper_name }}_TABLE + " (" +
"_id integer primary key autoincrement, " +
{% for field in table.fields -%}
{{ field.key_name }} + " {{ field.sql_lite_type }}{{', ' if not loop.last }}" +
{% endfor -%}
")";
{% endfor %}
protected DbHelper myOpenHelper;
@Override
public boolean onCreate() {
myOpenHelper = new DbHelper(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
return true;
}
/**
* Returns the right table name for the given uri
* @param uri
* @return
*/
private String getTableNameFromUri(Uri uri){
switch (uriMatcher.match(uri)) {
{%- for table in tables %}
case ALL_{{ table.upper_name}}:
case SINGLE_{{ table.upper_name}}:
return {{ table.upper_name }}_TABLE;
{%- endfor %}
default: break;
}
return null;
}
/**
* Returns the parent uri for the given uri
* @param uri
* @return
*/
private Uri getContentUriFromUri(Uri uri){
switch (uriMatcher.match(uri)) {
{%- for table in tables %}
case ALL_{{ table.upper_name}}:
case SINGLE_{{ table.upper_name}}:
return {{ table.upper_name }}_URI;
{%- endfor %}
default: break;
}
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// Open thedatabase.
SQLiteDatabase db;
try {
db = myOpenHelper.getWritableDatabase();
} catch (SQLiteException ex) {
db = myOpenHelper.getReadableDatabase();
}
// Replace these with valid SQL statements if necessary.
String groupBy = null;
String having = null;
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
// If this is a row query, limit the result set to the passed in row.
switch (uriMatcher.match(uri)) {
{%- for table in tables %}
case SINGLE_{{table.upper_name}}:
{%- endfor %}
String rowID = uri.getPathSegments().get(1);
queryBuilder.appendWhere(ROW_ID + "=" + rowID);
default: break;
}
// Specify the table on which to perform the query. This can
// be a specific table or a join as required.
queryBuilder.setTables(getTableNameFromUri(uri));
// Execute the query.
Cursor cursor = queryBuilder.query(db, projection, selection,
selectionArgs, groupBy, having, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
// Return the result Cursor.
return cursor;
}
@Override
public String getType(Uri uri) {
// Return a string that identifies the MIME type
// for a Content Provider URI
switch (uriMatcher.match(uri)) {
{%- for table in tables %}
case ALL_{{table.upper_name}}:
return "vnd.android.cursor.dir/vnd.{{ package }}.{{table.name}}";
case SINGLE_{{table.upper_name}}:
return "vnd.android.cursor.dir/vnd.{{ package }}.{{table.name}}";
{%- endfor %}
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
switch (uriMatcher.match(uri)) {
{%- for table in tables %}
case SINGLE_{{ table.upper_name }}:
{%- endfor %}
String rowID = uri.getPathSegments().get(1);
selection = ROW_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
default: break;
}
if (selection == null)
selection = "1";
int deleteCount = db.delete(getTableNameFromUri(uri),
selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return deleteCount;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
String nullColumnHack = null;
long id = db.insert(getTableNameFromUri(uri), nullColumnHack, values);
if (id > -1) {
Uri insertedId = ContentUris.withAppendedId(getContentUriFromUri(uri), id);
getContext().getContentResolver().notifyChange(insertedId, null);
getContext().getContentResolver().notifyChange(insertedId, null);
return insertedId;
} else {
return null;
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// Open a read / write database to support the transaction.
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
// If this is a row URI, limit the deletion to the specified row.
switch (uriMatcher.match(uri)) {
{%- for table in tables %}
case SINGLE_{{ table.upper_name }}:
{%- endfor %}
String rowID = uri.getPathSegments().get(1);
selection = ROW_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
default: break;
}
// Perform the update.
int updateCount = db.update(getTableNameFromUri(uri), values, selection, selectionArgs);
// Notify any observers of the change in the data set.
getContext().getContentResolver().notifyChange(uri, null);
return updateCount;
}
protected static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase db) {
{%- for table in tables %}
db.execSQL(DATABASE_{{ table.upper_name }}_CREATE);
{%- endfor %}
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Log the version upgrade.
Log.w(TAG, "Upgrading from version " +
oldVersion + " to " +
newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
{%- for table in tables %}
db.execSQL("DROP TABLE IF EXISTS " + {{ table.upper_name }}_TABLE + ";");
{%- endfor %}
// Create a new one.
onCreate(db);
}
}
}