-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlitegen.sh
More file actions
executable file
·185 lines (152 loc) · 5.95 KB
/
Copy pathsqlitegen.sh
File metadata and controls
executable file
·185 lines (152 loc) · 5.95 KB
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
#!/bin/bash
FN=$1
PK=${2:-com.a}
DATA=`basename -s .java ${FN}`
FNJ=${DATA}DbProvider.java
FNT=${DATA}.tmp
cat ${FN}|grep '^\s*private\s\+\w\+\s\+\w\+\s*[;=]' | sed -e's/^\s*private\s\+\(\w\+\)\s\+\(\w\+\)\s*[;=].*/\1 \2/'>$FNT
echo "//auto generated by OK tool">${FNJ}
echo "package ${PK}.db;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.Date;
import ${PK}.data.${DATA};
public class ${DATA}DbProvider {
private static final String TABLE=\"${DATA}\";
private static final String TAG = \"${DATA}DbProvider\";
// Variable to hold the database instance
protected SQLiteDatabase db;
// Database open/upgrade helper
private SQLiteOpenHelper dbHelper;
public ${DATA}DbProvider(SQLiteOpenHelper dbHelper) {
this.dbHelper = dbHelper;
}
public SQLiteDatabase open() throws SQLException {
db = dbHelper.getWritableDatabase();
return db;
}
public void close() {
db.close();
}
// Called when no database exists in disk and the helper class needs to create a new one..
public static void onCreate(SQLiteDatabase db) {
db.execSQL(\"CREATE TABLE \" +TABLE+ \"( '_id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\"+">>$FNJ
cat ${FNT}|grep -v "\w\+ _id" |sed -e 's/\(\w\+\) \(\w\+\)/ "\2 \1/'\
|sed -e's/long/INTEGER/' -e's/String/TEXT/' -e's/Date/INTEGER/' -e's/int/INTEGER/'\
|sed -e '$ ! s/$/," +/'|sed -e '$ s/$/" +/'>>$FNJ
echo " \");\");
}
// 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.
public static 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.
// db.execSQL(\"DROP TABLE IF EXISTS \" + TABLE + \";\"); // Create a new one.
// onCreate(db);
}
//ColumnIndexes">>${FNJ}
cat ${FNT}|sed -e 's/\(\w\+\) \(\w\+\)/ private int \2ColumnIndex;/' >>$FNJ
echo "
String[] Columns=new String[]{">>${FNJ}
cat ${FNT}|sed -e 's/\(\w\+\) \(\w\+\)/ "\2"/'|sed -e '$ ! s/$/,/' >>$FNJ
echo " };
private void Cursor2ColumnIndexes(Cursor cursor){">>${FNJ}
cat ${FNT}|sed -e 's/\(\w\+\) \(\w\+\)/ \2ColumnIndex=cursor.getColumnIndex("\2");/' >>$FNJ
echo " }
private ContentValues Data2CV (${DATA} data){
ContentValues cv= new ContentValues();" >>${FNJ}
cat ${FNT}|grep -v "\w\+ _id"|sed -e 's/\(\w\+\) \(\w\+\)/ cv.put("\2", data.get\2()\1);/' \
|sed -e's/get\([a-z]\)/get\U\1/' -e's/get_id/getId/' \
|sed -e's/()long/()/' -e's/()String/()/' -e's/()Date/().getTime()/' -e's/()int/()/'>>$FNJ
echo " return cv;
}
private ${DATA} Cursor2Data(Cursor cursor){
return new ${DATA}(">>${FNJ}
cat ${FNT}|sed -e 's/\(\w\+\) \(\w\+\)/ cursor.get\1(\2ColumnIndex)/' \
|sed -e's/\(cursor.get\)\(\w\)/\1\U\2/'\
|sed -e's/cursor.getDate\((\w\+)\)/new Date(cursor.getLong\1)/'\
|sed -e '$ ! s/$/,/' >>$FNJ
echo " );
}
private ArrayList<${DATA}> Cursor2DataArray(Cursor cursor) {
ArrayList<${DATA}> dataArray = new ArrayList<${DATA}>();
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
Cursor2ColumnIndexes(cursor);
do {
${DATA} data = Cursor2Data(cursor);
dataArray.add(data);
} while (cursor.moveToNext());
}
return dataArray;
}
public long Add(${DATA} data) {
ContentValues cv = Data2CV(data);
long id = db.insert(TABLE, null, cv);
data.setId(id);
return id;
}
public long Update(${DATA} data) {
String where = \"_id=\" + data.getId();
ContentValues cv = Data2CV(data);
long rows = db.update(TABLE, cv, where, null);
return rows;
}
public long Remove(${DATA} data) {
String where = \"_id=\" + data.getId();
long rows = db.delete(TABLE, where, null);
return rows;
}
public long Remove(long id) {
String where = \"_id=\" + id;
long rows = db.delete(TABLE, where, null);
return rows;
}
public long RemoveAll() {
long rows = db.delete(TABLE, null, null);
return rows;
}
public ${DATA} Get(long id) {
String where = \"_id=\" + id;
Cursor cursor = db.query(TABLE, Columns, where, null, null, null, null);
if (cursor == null)
return null;
cursor.moveToFirst();
Cursor2ColumnIndexes(cursor);
${DATA} data = Cursor2Data(cursor);
return data;
}
public ArrayList<${DATA}> Get(String where) {
return Get(where, null);
}
public ArrayList<${DATA}> Get(String where, String orderBy) {
Cursor cursor = db.query(TABLE, Columns, where, null, null, null, orderBy);
return Cursor2DataArray(cursor);
}
public ArrayList<${DATA}> GetAll() {
return Get(null, null);
}
public long count() {
return count(null);
}
public long count(String where) {
String query = "SELECT count(*) FROM " + TABLE;
if (where != null && !"".equals(where))
query += "WHERE " + where;
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
long cnt = cursor.getLong(0);
return cnt;
}
}">>${FNJ}