-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprovider_client.jinja
95 lines (80 loc) · 4.2 KB
/
provider_client.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
/**********************************************************************************************************************************************************************
****** 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.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import java.util.Date;
public class {{ name }}ProviderClient{
{% for table in tables %}
// ------------- {{ table.upper_name }}_HELPERS ------------
public static Uri add{{ table.name }} ( {%- for field in table.fields -%}
{{ field.type }} {{ field.name }},
{% endfor -%} Context c) {
ContentValues contentValues = new ContentValues();
{% for field in table.fields -%}
{% if field.type == 'Date' -%}
contentValues.put({{ provider }}.{{ field.key_name }}, {{field.name}}.getTime());
{% else -%}
contentValues.put({{ provider }}.{{ field.key_name }}, {{field.name}});
{% endif %}
{%- endfor -%}
ContentResolver cr = c.getContentResolver();
return cr.insert({{ provider }}.{{ table.upper_name }}_URI, contentValues);
}
public static int remove{{ table.name }}(long rowIndex, Context c){
ContentResolver cr = c.getContentResolver();
Uri rowAddress = ContentUris.withAppendedId({{ provider }}.{{ table.upper_name }}_URI, rowIndex);
return cr.delete(rowAddress, null, null);
}
public static int removeAll{{ table.name }}(Context c){
ContentResolver cr = c.getContentResolver();
return cr.delete({{ provider }}.{{ table.upper_name }}_URI, null, null);
}
public static Cursor getAll{{ table.name }}(Context c){
ContentResolver cr = c.getContentResolver();
String[] resultColumns = new String[] {
{{ provider }}.ROW_ID,
{% for field in table.fields -%}
{{ provider}}.{{ field.key_name }}{{ ',' if not loop.last }}
{% endfor %}};
Cursor resultCursor = cr.query({{ provider }}.{{ table.upper_name }}_URI, resultColumns, null, null, null);
return resultCursor;
}
public static Cursor get{{ table.name }}(long rowId, Context c){
ContentResolver cr = c.getContentResolver();
String[] resultColumns = new String[] {
{{ provider }}.ROW_ID,
{% for field in table.fields -%}
{{ provider }}.{{ field.key_name }}{{ ',' if not loop.last }}
{% endfor %}};
Uri rowAddress = ContentUris.withAppendedId({{ provider }}.{{ table.upper_name }}_URI, rowId);
String where = null;
String whereArgs[] = null;
String order = null;
Cursor resultCursor = cr.query(rowAddress, resultColumns, where, whereArgs, order);
return resultCursor;
}
public static int update{{ table.name }} (int rowId,
{% for field in table.fields -%}
{{ field.type }} {{ field.name }},
{% endfor -%} Context c) {
ContentValues contentValues = new ContentValues();
{% for field in table.fields -%}
{% if field.type == 'Date' -%}
contentValues.put({{ provider }}.{{ field.key_name }}, {{field.name}}.getTime());
{% else -%}
contentValues.put({{ provider }}.{{ field.key_name }}, {{field.name}});
{% endif %}
{%- endfor -%}
Uri rowAddress = ContentUris.withAppendedId({{ provider }}.{{ table.upper_name }}_URI, rowId);
ContentResolver cr = c.getContentResolver();
int updatedRowCount = cr.update(rowAddress, contentValues, null, null);
return updatedRowCount;
}
{% endfor %}
}