· 8 years ago · Feb 19, 2018, 05:16 AM
1package com.zoliana.khampat.mizobible.storage;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.os.Build;
9import android.support.v4.util.LongSparseArray;
10import android.util.Log;
11import com.crashlytics.android.Crashlytics;
12import gnu.trove.map.hash.TIntObjectHashMap;
13import yuku.afw.App;
14import yuku.afw.storage.Preferences;
15import com.zoliana.khampat.mizobible.config.VersionConfig;
16import com.zoliana.khampat.mizobible.model.MVersionDb;
17import com.zoliana.khampat.mizobible.model.MVersionPreset;
18import com.zoliana.khampat.mizobible.model.ReadingPlan;
19import com.zoliana.khampat.mizobible.util.AddonManager;
20import com.zoliana.khampat.mizobible.util.AppLog;
21import yuku.alkitab.model.util.Gid;
22
23import java.io.File;
24
25import static com.zoliana.khampat.mizobible.util.Literals.Array;
26
27public class InternalDbHelper extends SQLiteOpenHelper {
28 public static final String TAG = InternalDbHelper.class.getSimpleName();
29
30 public InternalDbHelper(Context context) {
31 super(context, "AlkitabDb", null, App.getVersionCode());
32 if (Build.VERSION.SDK_INT >= 16) {
33 setWriteAheadLoggingEnabled(true);
34 }
35 }
36
37 @Override
38 public void onOpen(SQLiteDatabase db) {
39 if (Build.VERSION.SDK_INT < 16) {
40 db.enableWriteAheadLogging();
41 }
42 }
43
44 @Override public void onCreate(SQLiteDatabase db) {
45 AppLog.d(TAG, "@@onCreate");
46
47 createTableMarker(db);
48 createIndexMarker(db);
49 createTableDevotion(db);
50 createIndexDevotion(db);
51 createTableLabel(db);
52 createIndexLabel(db);
53 createTableMarker_Label(db);
54 createIndexMarker_Label(db);
55 createTableProgressMark(db);
56 createIndexProgressMark(db);
57 insertDefaultProgressMarks(db);
58 createTableProgressMarkHistory(db);
59 createIndexProgressMarkHistory(db);
60 createTableReadingPlan(db);
61 createTableReadingPlanProgress(db);
62 createIndexReadingPlanProgress(db);
63 createTableVersion(db);
64 createIndexVersion(db);
65 createTableSyncShadow(db);
66 createIndexSyncShadow(db);
67 createTableSyncLog(db);
68 createIndexSyncLog(db);
69 createTablePerVersion(db);
70 createIndexPerVersion(db);
71 }
72
73 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
74 AppLog.d(TAG, "@@onUpgrade oldVersion=" + oldVersion + " newVersion=" + newVersion);
75
76 // No more support for Bookmark (Bukmak) version 1 table (last published: 2010-06-14)
77 // if (oldVersion <= 23) {
78 // convertFromBookmarkToBookmark2(db);
79 // }
80
81 if (oldVersion <= 50) {
82 // recreate a temporary old-style table "Edisi"
83 // This will later be converted in version 14000166
84 db.execSQL("create table if not exists Edisi (" +
85 "_id integer primary key autoincrement, " +
86 "shortName text, " +
87 "judul text, " +
88 "jenis text, " +
89 "keterangan text, " +
90 "namafile text, " +
91 "namafile_pdbasal text, " +
92 "aktif integer, " +
93 "urutan integer)"
94 );
95 }
96
97 if (oldVersion <= 69) { // 70: 2.0.0
98 // new table Label
99 createTableLabel(db);
100 createIndexLabel(db);
101 }
102
103 if (oldVersion > 50 && oldVersion <= 102) { // 103: 2.7.1
104 addShortNameColumnAndIndexToEdisi(db);
105 }
106
107 if (oldVersion <= 126) { // 127: 3.2.0
108 createTableProgressMark(db);
109 createIndexProgressMark(db);
110 insertDefaultProgressMarks(db);
111 createTableProgressMarkHistory(db);
112 createIndexProgressMarkHistory(db);
113 }
114
115 // bug in 137 (3.3.3) where ReadingPlanProgress table is created with a wrong column name.
116 // so that column name is found, drop that table.
117 if (oldVersion >= 137 && oldVersion <= 142) {
118 boolean needDrop = false;
119 final Cursor c = db.rawQuery("pragma table_info(" + Db.TABLE_ReadingPlanProgress + ")", null);
120 if (c != null) {
121 while (c.moveToNext()) {
122 final String name = c.getString(1 /* "name" column */);
123 AppLog.d(TAG, "column name: " + name);
124 if ("checkedTime".equals(name)) { // this is a bad column name
125 needDrop = true;
126 }
127 }
128 c.close();
129 }
130 if (needDrop) {
131 AppLog.d(TAG, "table need to be dropped: " + Db.TABLE_ReadingPlanProgress);
132 db.execSQL("drop table " + Db.TABLE_ReadingPlanProgress);
133 }
134 }
135
136 if (oldVersion <= 142) { // 143: 3.4.4
137 // (These tables were first introduced in 3.4.0, but because of the above bug, this needs to be recreated)
138 createTableReadingPlan(db);
139 createTableReadingPlanProgress(db);
140 createIndexReadingPlanProgress(db);
141 }
142
143 if (oldVersion < 14000163) { // 4.0.0-beta1: last version that doesn't use Marker table
144 addGidColumnToLabelIfNeeded(db);
145
146 createTableMarker(db);
147 createIndexMarker(db);
148 createTableMarker_Label(db);
149 createIndexMarker_Label(db);
150
151 convertFromBookmark2ToMarker(db);
152 }
153
154 if (oldVersion < 14000166) { // 4.0.0-beta5: last version that doesn't use the new Version table
155 createTableVersion(db);
156 createIndexVersion(db);
157 convertFromEdisiToVersion(db);
158 }
159
160 if (oldVersion <= 14000170) { // support for sync starting from 4-beta10
161 createTableSyncShadow(db);
162 createIndexSyncShadow(db);
163 }
164
165 if (oldVersion < 14000173) { // sync logs
166 createTableSyncLog(db);
167 createIndexSyncLog(db);
168 }
169
170 if (oldVersion >= 14000163 && oldVersion < 14000172) {
171 db.execSQL("drop index if exists index_Marker_Label_03");
172 }
173
174 if (oldVersion < 14000200) { // 14000200: v4.1
175 // change Devotion table to new format
176 // ignore old data, no need to migrate
177 db.execSQL("drop table if exists Renungan");
178 createTableDevotion(db);
179 createIndexDevotion(db);
180 }
181
182 if (oldVersion > 142 && oldVersion < 14000225) { // 14000225: v4.2-beta5
183 // for syncing reading plan progress,
184 // ReadingPlanProgress table must be keyed by name (stored as gid), not by ReadingPlan._id.
185 // So we alter table and migrate old to new
186 migrateReadingPlanProgressTable(db);
187 }
188
189 if (oldVersion < 14000265) { // 14000265: v4.4-beta5
190 // new table PerVersion
191 createTablePerVersion(db);
192 createIndexPerVersion(db);
193 }
194 }
195
196 private void createTableMarker(SQLiteDatabase db) {
197 db.execSQL(
198 "create table if not exists " + Db.TABLE_Marker + " (" +
199 "_id integer primary key autoincrement, " +
200 Db.Marker.gid + " text," +
201 Db.Marker.ari + " integer, " +
202 Db.Marker.kind + " integer, " +
203 Db.Marker.caption + " text, " +
204 Db.Marker.verseCount + " integer, " +
205 Db.Marker.createTime + " integer, " +
206 Db.Marker.modifyTime + " integer" +
207 ")"
208 );
209 }
210
211 private void createIndexMarker(SQLiteDatabase db) {
212 db.execSQL("create index if not exists index_Marker_01 on " + Db.TABLE_Marker + " (" + Db.Marker.ari + ")");
213 db.execSQL("create index if not exists index_Marker_02 on " + Db.TABLE_Marker + " (" + Db.Marker.kind + ", " + Db.Marker.ari + ")");
214 db.execSQL("create index if not exists index_Marker_03 on " + Db.TABLE_Marker + " (" + Db.Marker.kind + ", " + Db.Marker.modifyTime + ")");
215 db.execSQL("create index if not exists index_Marker_04 on " + Db.TABLE_Marker + " (" + Db.Marker.kind + ", " + Db.Marker.createTime + ")");
216 db.execSQL("create index if not exists index_Marker_05 on " + Db.TABLE_Marker + " (" + Db.Marker.kind + ", " + Db.Marker.caption + " collate NOCASE)");
217 db.execSQL("create index if not exists index_Marker_06 on " + Db.TABLE_Marker + " (" + Db.Marker.gid + ")");
218 }
219
220 private void createTableDevotion(SQLiteDatabase db) {
221 final StringBuilder sb = new StringBuilder("create table if not exists " + Table.Devotion.tableName() + " ( _id integer primary key ");
222 for (Table.Devotion field: Table.Devotion.values()) {
223 sb.append(',');
224 sb.append(field.name());
225 sb.append(' ');
226 sb.append(field.type.name());
227 if (field.suffix != null) {
228 sb.append(' ');
229 sb.append(field.suffix);
230 }
231 }
232 sb.append(")");
233 db.execSQL(sb.toString());
234 }
235
236 private void createIndexDevotion(SQLiteDatabase db) {
237 db.execSQL("create index if not exists index_Devotion_01 on " + Table.Devotion.tableName() + " (" + Table.Devotion.name + ", " + Table.Devotion.date + ", " + Table.Devotion.dataFormatVersion + ")");
238 db.execSQL("create index if not exists index_Devotion_02 on " + Table.Devotion.tableName() + " (" + Table.Devotion.touchTime + ")");
239 }
240
241 private void createTablePerVersion(SQLiteDatabase db) {
242 final StringBuilder sb = new StringBuilder("create table if not exists " + Table.PerVersion.tableName() + " ( _id integer primary key ");
243 for (Table.PerVersion field: Table.PerVersion.values()) {
244 sb.append(',');
245 sb.append(field.name());
246 sb.append(' ');
247 sb.append(field.type.name());
248 if (field.suffix != null) {
249 sb.append(' ');
250 sb.append(field.suffix);
251 }
252 }
253 sb.append(")");
254 db.execSQL(sb.toString());
255 }
256
257 private void createIndexPerVersion(SQLiteDatabase db) {
258 db.execSQL("create unique index if not exists index_PerVersion_01 on " + Table.PerVersion.tableName() + " (" + Table.PerVersion.versionId + ")");
259 }
260
261 void createTableVersion(SQLiteDatabase db) {
262 db.execSQL("create table if not exists " + Db.TABLE_Version + " (" +
263 "_id integer primary key autoincrement, " +
264 Db.Version.locale + " text," +
265 Db.Version.shortName + " text," +
266 Db.Version.longName + " text," +
267 Db.Version.description + " text," +
268 Db.Version.filename + " text," +
269 Db.Version.preset_name + " text," +
270 Db.Version.modifyTime + " integer," +
271 Db.Version.active + " integer," +
272 Db.Version.ordering + " integer)"
273 );
274 }
275
276 void createIndexVersion(SQLiteDatabase db) {
277 db.execSQL("create index if not exists index_Version_01 on " + Db.TABLE_Version + " (" + Db.Version.ordering + ")");
278 db.execSQL("create index if not exists index_Version_02 on " + Db.TABLE_Version + " (" + Db.Version.active + "," + Db.Version.longName + ")");
279 db.execSQL("create index if not exists index_Version_03 on " + Db.TABLE_Version + " (" + Db.Version.preset_name + ")");
280 }
281
282 void createTableSyncShadow(final SQLiteDatabase db) {
283 final StringBuilder sb = new StringBuilder("create table " + Table.SyncShadow.tableName() + " ( _id integer primary key ");
284 for (Table.SyncShadow field: Table.SyncShadow.values()) {
285 sb.append(',');
286 sb.append(field.name());
287 sb.append(' ');
288 sb.append(field.type.name());
289 if (field.suffix != null) {
290 sb.append(' ');
291 sb.append(field.suffix);
292 }
293 }
294 sb.append(")");
295 db.execSQL(sb.toString());
296 }
297
298 void createIndexSyncShadow(final SQLiteDatabase db) {
299 db.execSQL("create index if not exists index_SyncShadow_01 on " + Table.SyncShadow.tableName() + " (" + Table.SyncShadow.syncSetName + ")");
300 }
301
302 void createTableSyncLog(final SQLiteDatabase db) {
303 final StringBuilder sb = new StringBuilder("create table " + Table.SyncLog.tableName() + " ( _id integer primary key ");
304 for (Table.SyncLog field: Table.SyncLog.values()) {
305 sb.append(',');
306 sb.append(field.name());
307 sb.append(' ');
308 sb.append(field.type.name());
309 if (field.suffix != null) {
310 sb.append(' ');
311 sb.append(field.suffix);
312 }
313 }
314 sb.append(")");
315 db.execSQL(sb.toString());
316 }
317
318 void createIndexSyncLog(final SQLiteDatabase db) {
319 // do not create many indexes for SyncLog
320 db.execSQL("create index if not exists index_SyncLog_01 on " + Table.SyncLog.tableName() + " (" + Table.SyncLog.createTime + ")");
321 }
322
323 private void createTableLabel(SQLiteDatabase db) {
324 db.execSQL("create table if not exists " + Db.TABLE_Label + " (" +
325 "_id integer primary key autoincrement, " +
326 Db.Label.gid + " text," +
327 Db.Label.title + " text, " +
328 Db.Label.ordering + " integer, " +
329 Db.Label.backgroundColor + " text" +
330 ")"
331 );
332 }
333
334 private void createIndexLabel(SQLiteDatabase db) {
335 db.execSQL("create index if not exists index_401 on " + Db.TABLE_Label + " (" + Db.Label.ordering + ")");
336 db.execSQL("create index if not exists index_402 on " + Db.TABLE_Label + " (" + Db.Label.gid + ")");
337 }
338
339 private void createTableMarker_Label(SQLiteDatabase db) {
340 db.execSQL("create table if not exists " + Db.TABLE_Marker_Label + " (" +
341 "_id integer primary key autoincrement, " +
342 Db.Marker_Label.gid + " text," +
343 Db.Marker_Label.marker_gid + " text, " +
344 Db.Marker_Label.label_gid + " text" +
345 ")"
346 );
347 }
348
349 private void createIndexMarker_Label(SQLiteDatabase db) {
350 db.execSQL("create index if not exists index_Marker_Label_01 on " + Db.TABLE_Marker_Label + " (" + Db.Marker_Label.marker_gid + ")");
351 db.execSQL("create index if not exists index_Marker_Label_02 on " + Db.TABLE_Marker_Label + " (" + Db.Marker_Label.label_gid + ")");
352 // unique index index_Marker_Label_03 on Marker_Label (marker_gid, label_gid) is no longer used as of versionCode 14000172
353 db.execSQL("create unique index if not exists index_Marker_Label_04 on " + Db.TABLE_Marker_Label + " (" + Db.Marker_Label.gid + ")");
354 }
355
356 private void createTableProgressMark(SQLiteDatabase db) {
357 db.execSQL("create table if not exists " + Db.TABLE_ProgressMark + " (" +
358 "_id integer primary key autoincrement, " +
359 Db.ProgressMark.preset_id + " integer, " +
360 Db.ProgressMark.caption + " text, " +
361 Db.ProgressMark.ari + " integer, " +
362 Db.ProgressMark.modifyTime + " integer)");
363 }
364
365 private void createIndexProgressMark(SQLiteDatabase db) {
366 db.execSQL("create index if not exists index_601 on " + Db.TABLE_ProgressMark + " (" + Db.ProgressMark.preset_id + ")");
367 }
368
369 private void createTableProgressMarkHistory(SQLiteDatabase db) {
370 db.execSQL("create table if not exists " + Db.TABLE_ProgressMarkHistory + " (" +
371 "_id integer primary key autoincrement, " +
372 Db.ProgressMarkHistory.progress_mark_preset_id + " integer, " +
373 Db.ProgressMarkHistory.progress_mark_caption + " integer, " +
374 Db.ProgressMarkHistory.ari + " integer, " +
375 Db.ProgressMarkHistory.createTime + " integer)");
376 }
377
378 private void createIndexProgressMarkHistory(SQLiteDatabase db) {
379 db.execSQL("create index if not exists index_701 on " + Db.TABLE_ProgressMarkHistory + " (" + Db.ProgressMarkHistory.progress_mark_preset_id + ", " + Db.ProgressMarkHistory.createTime + ")");
380 }
381
382 private void insertDefaultProgressMarks(SQLiteDatabase db) {
383 ContentValues cv = new ContentValues();
384 cv.put(Db.ProgressMark.ari, 0);
385 for (int i = 0; i < 5; i++) {
386 cv.put(Db.ProgressMark.preset_id, i);
387 db.insert(Db.TABLE_ProgressMark, null, cv);
388 }
389 }
390
391 private void createTableReadingPlan(final SQLiteDatabase db) {
392 db.execSQL("create table if not exists " + Db.TABLE_ReadingPlan + " (" +
393 "_id integer primary key autoincrement, " +
394 Db.ReadingPlan.version + " integer, " +
395 Db.ReadingPlan.name + " text, " +
396 Db.ReadingPlan.title + " text, " +
397 Db.ReadingPlan.description + " text, " +
398 Db.ReadingPlan.duration + " integer, " +
399 Db.ReadingPlan.startTime + " integer, " +
400 Db.ReadingPlan.data + " blob)");
401 }
402
403 private void createTableReadingPlanProgress(final SQLiteDatabase db) {
404 db.execSQL("create table if not exists " + Db.TABLE_ReadingPlanProgress + " (" +
405 "_id integer primary key autoincrement, " +
406 Db.ReadingPlanProgress.reading_plan_progress_gid + " text, " +
407 Db.ReadingPlanProgress.reading_code + " integer, " +
408 Db.ReadingPlanProgress.checkTime + " integer)"
409 );
410 }
411
412 private void createIndexReadingPlanProgress(SQLiteDatabase db) {
413 db.execSQL("create unique index if not exists index_902 on " + Db.TABLE_ReadingPlanProgress + " (" + Db.ReadingPlanProgress.reading_plan_progress_gid + ", " + Db.ReadingPlanProgress.reading_code + ")");
414 }
415
416 private void migrateReadingPlanProgressTable(SQLiteDatabase db) {
417 db.beginTransaction();
418 try {
419 // create mapping from id to name first
420 final TIntObjectHashMap<String> map = new TIntObjectHashMap<>();
421 try (Cursor c = db.rawQuery("select _id, " + Db.ReadingPlan.name + " from " + Db.TABLE_ReadingPlan, null)) {
422 while (c.moveToNext()) {
423 map.put(c.getInt(0), c.getString(1));
424 }
425 }
426
427 // https://www.sqlite.org/faq.html#q11
428 db.execSQL("CREATE TEMPORARY TABLE t1_backup(reading_plan_id, reading_code, checkTime)");
429 db.execSQL("INSERT INTO t1_backup SELECT reading_plan_id, reading_code, checkTime FROM " + Db.TABLE_ReadingPlanProgress);
430 db.execSQL("DROP TABLE " + Db.TABLE_ReadingPlanProgress); // also drops indexes
431 createTableReadingPlanProgress(db);
432 createIndexReadingPlanProgress(db);
433
434 try (Cursor c = db.rawQuery("select reading_plan_id, reading_code, checkTime from t1_backup", null)) {
435 final ContentValues cv = new ContentValues();
436
437 while (c.moveToNext()) {
438 final int _id = c.getInt(0);
439 final String name = map.get(_id);
440 if (name != null) {
441 cv.put(Db.ReadingPlanProgress.reading_plan_progress_gid, ReadingPlan.gidFromName(name));
442 cv.put(Db.ReadingPlanProgress.reading_code, c.getInt(1));
443 cv.put(Db.ReadingPlanProgress.checkTime, c.getLong(2));
444 db.insert(Db.TABLE_ReadingPlanProgress, null, cv);
445 }
446 }
447 }
448
449 // INSERT INTO t1 SELECT a,b FROM t1_backup;
450 db.execSQL("DROP TABLE t1_backup");
451
452 db.setTransactionSuccessful();
453 } finally {
454 db.endTransaction();
455 }
456 }
457
458 // This needs to be kept, for upgrading from version 51-102 to 14000165
459 private void addShortNameColumnAndIndexToEdisi(SQLiteDatabase db) {
460 db.execSQL("alter table Edisi add column shortName text");
461 }
462
463 private void addGidColumnToLabelIfNeeded(SQLiteDatabase db) {
464 boolean gidColumnExists = false;
465 try (Cursor c = db.rawQuery("pragma table_info(" + Db.TABLE_Label + ")", null)) {
466 while (c.moveToNext()) {
467 if ("gid".equals(c.getString(1 /* "name" column */))) {
468 gidColumnExists = true;
469 }
470 }
471 }
472 if (!gidColumnExists) {
473 db.execSQL("alter table " + Db.TABLE_Label + " add column " + Db.Label.gid + " text");
474 }
475
476 // make sure this one matches the one in createIndexLabel()
477 db.execSQL("create index if not exists index_402 on " + Db.TABLE_Label + " (" + Db.Label.gid + ")");
478 }
479
480 /**
481 * Converts Bookmark2 to Marker table
482 * and Bookmark2_Label to Marker_Label table
483 * and add gid to all labels
484 */
485 private void convertFromBookmark2ToMarker(final SQLiteDatabase db) {
486 final String TABLE_Bookmark2 = "Bukmak2";
487 class Bookmark2 {
488 public static final String ari = "ari";
489 public static final String kind = "jenis";
490 public static final String caption = "tulisan";
491 public static final String addTime = "waktuTambah";
492 public static final String modifyTime = "waktuUbah";
493 }
494
495 final String TABLE_Bookmark2_Label = "Bukmak2_Label";
496 class Bookmark2_Label {
497 public static final String bookmark2_id = "bukmak2_id";
498 public static final String label_id = "label_id";
499 }
500
501 // We need to maintain _id to prevent complications with Marker_Label
502 db.beginTransaction();
503 try {
504 final LongSparseArray<String> idToGid_marker = new LongSparseArray<>();
505 final LongSparseArray<String> idToGid_label = new LongSparseArray<>();
506
507 { // Bookmark2 -> Marker
508 final Cursor c = db.query(TABLE_Bookmark2,
509 new String[]{"_id", Bookmark2.ari, Bookmark2.kind, Bookmark2.caption, Bookmark2.addTime, Bookmark2.modifyTime},
510 null, null, null, null, "_id asc"
511 );
512
513 final ContentValues cv = new ContentValues();
514 while (c.moveToNext()) {
515 final long _id = c.getLong(0);
516 final String gid = Gid.newGid();
517
518 idToGid_marker.put(_id, gid);
519
520 cv.put("_id", _id);
521 cv.put(Db.Marker.ari, c.getInt(1));
522 cv.put(Db.Marker.kind, c.getInt(2));
523 cv.put(Db.Marker.caption, c.getString(3));
524 cv.put(Db.Marker.createTime, c.getLong(4));
525 cv.put(Db.Marker.modifyTime, c.getLong(5));
526 cv.put(Db.Marker.verseCount, 1);
527 cv.put(Db.Marker.gid, gid);
528 db.insert(Db.TABLE_Marker, null, cv);
529 }
530
531 c.close();
532 }
533
534 { // add gid to all Labels
535 final String[] args = {null};
536 final Cursor c = db.query(Db.TABLE_Label, new String[]{"_id"}, null, null, null, null, null);
537 final ContentValues cv = new ContentValues();
538 while (c.moveToNext()) {
539 final long _id = c.getLong(0);
540 final String gid = Gid.newGid();
541
542 idToGid_label.put(_id, gid);
543
544 cv.put(Db.Label.gid, gid);
545 args[0] = String.valueOf(_id);
546 db.update(Db.TABLE_Label, cv, "_id = ?", args);
547 }
548 c.close();
549 }
550
551 // Only if the upgrade old version is >= 2.0.0, where we have Bukmak2_Label table.
552 // In case that Bukmak2_Label table is not available, it means the onUpgrade old version is < 2.0.0,
553 // so we don't need to care about migrating labels.
554 boolean hasBookmark2_Label = false;
555 try (Cursor c = db.rawQuery("select tbl_name from sqlite_master where tbl_name=?", Array(TABLE_Bookmark2_Label))) {
556 if (c.moveToNext() && TABLE_Bookmark2_Label.equals(c.getString(0))) {
557 hasBookmark2_Label = true;
558 }
559 }
560
561 if (hasBookmark2_Label) { // Bookmark2_Label -> Marker_Label
562 final Cursor c = db.query(TABLE_Bookmark2_Label,
563 new String[] {"_id", Bookmark2_Label.bookmark2_id, Bookmark2_Label.label_id},
564 null, null, null, null, "_id asc"
565 );
566 final ContentValues cv = new ContentValues();
567 while (c.moveToNext()) {
568 final long _id = c.getLong(0);
569 final long marker_id = c.getLong(1);
570 final long label_id = c.getLong(2);
571
572 final String marker_gid = idToGid_marker.get(marker_id);
573 final String label_gid = idToGid_label.get(label_id);
574
575 cv.put("_id", _id);
576 cv.put(Db.Marker_Label.gid, Gid.newGid());
577 cv.put(Db.Marker_Label.marker_gid, marker_gid);
578 cv.put(Db.Marker_Label.label_gid, label_gid);
579 db.insert(Db.TABLE_Marker_Label, null, cv);
580 }
581 c.close();
582 }
583
584 db.setTransactionSuccessful();
585 } finally {
586 db.endTransaction();
587 }
588 }
589
590 /**
591 * Converts the old version (Edisi) table, to the new Version table.
592 *
593 * This will keep user's added yes file (excluding the preset versions)
594 */
595 private void convertFromEdisiToVersion(final SQLiteDatabase db) {
596 final String TABLE_Edisi = "Edisi";
597 class Edisi {
598 public static final String shortName = "shortName";
599 public static final String title = "judul";
600 public static final String description = "keterangan";
601 public static final String kind = "jenis";
602 public static final String filename = "namafile";
603 // unused: public static final String filename_originalpdb = "namafile_pdbasal";
604 public static final String active = "aktif";
605 public static final String ordering = "urutan";
606 }
607
608 db.beginTransaction();
609 try {
610 Preferences.hold();
611 final ContentValues cv = new ContentValues();
612 int ordering = MVersionDb.DEFAULT_ORDERING_START;
613
614 /**
615 * Automatically add v3 preset versions as {@link com.zoliana.khampat.mizobible.storage.Db.Version} table rows,
616 * if there are files with the same preset_name as those defined in {@link com.zoliana.khampat.mizobible.config.VersionConfig}.
617 * In version 3, preset versions are not stored in the database. In version 4, they are.
618 */
619 {
620 final VersionConfig vc = VersionConfig.get();
621 for (final MVersionPreset mv : vc.presets) {
622 final File yesFile = AddonManager.getReadableVersionFile(mv.preset_name + ".yes");
623 if (yesFile != null) {
624 cv.clear();
625 cv.put(Db.Version.locale, mv.locale);
626 cv.put(Db.Version.shortName, mv.shortName);
627 cv.put(Db.Version.longName, mv.longName);
628 cv.put(Db.Version.description, mv.description);
629 cv.put(Db.Version.filename, yesFile.getAbsolutePath());
630 cv.put(Db.Version.preset_name, mv.preset_name);
631 cv.put(Db.Version.modifyTime, (int) (yesFile.lastModified() / 1000L));
632 cv.put(Db.Version.active, Preferences.getBoolean("edisi/preset/" + mv.preset_name + ".yes/aktif", true) ? 1 : 0);
633 cv.put(Db.Version.ordering, ++ordering);
634 db.insert(Db.TABLE_Version, null, cv);
635 }
636 }
637 }
638
639 { // Remove all preferences about active state of preset versions. We don't need them any more.
640 for (String key : Preferences.getAllKeys()) {
641 if (key.startsWith("edisi/preset/") && key.endsWith(".yes/aktif")) {
642 Preferences.remove(key);
643 }
644 }
645 }
646
647 { // Edisi -> Version
648 final Cursor c = db.query(TABLE_Edisi,
649 new String[]{Edisi.shortName, Edisi.title, Edisi.description, Edisi.filename, Edisi.active},
650 null, null, null, null, Edisi.ordering + " asc"
651 );
652 while (c.moveToNext()) {
653 cv.clear();
654 cv.put(Db.Version.locale, (String) null);
655 cv.put(Db.Version.shortName, c.getString(0));
656 cv.put(Db.Version.longName, c.getString(1));
657 cv.put(Db.Version.description, c.getString(2));
658 cv.put(Db.Version.filename, c.getString(3));
659 cv.put(Db.Version.active, c.getInt(4));
660 cv.put(Db.Version.ordering, ++ordering);
661 db.insert(Db.TABLE_Version, null, cv);
662 }
663
664 c.close();
665 }
666
667 db.execSQL("drop table " + TABLE_Edisi);
668
669 db.setTransactionSuccessful();
670 } finally {
671 Preferences.unhold();
672 db.endTransaction();
673 }
674 }
675}