- }
- }
-
- static public void set_option_value(Context context, String name, String value) {
- Log.d("db", "setting option " + name + "=" + value);
- try (SQLiteDatabase db = getWritableDatabase(context)) {
- db.execSQL("insert or replace into options(name, value) values(?, ?);",
- new String[]{name, value});
- }
- }
-
- static public void set_option_value(Context context, String name, long value) {
- set_option_value(context, name, String.valueOf(value));
- }
-}
-
-class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {
- public static final String DB_NAME = "mobile-ledger.db";
- public static final int LATEST_REVISION = 10;
-
- private final Context mContext;
-
- public MobileLedgerDatabase(Context context) {
- super(context, DB_NAME, null, LATEST_REVISION);
- Log.d("db", "creating helper instance");
- mContext = context;
- super.setWriteAheadLoggingEnabled(true);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- Log.d("db", "onCreate called");
- onUpgrade(db, -1, LATEST_REVISION);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.d("db", "onUpgrade called");
- for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i);
- }
-
- private void applyRevision(SQLiteDatabase db, int rev_no) {
- final Resources rm = mContext.getResources();
- String rev_file = String.format(Locale.US, "sql_%d", rev_no);
-
- int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
- if (res_id == 0) throw new SQLException(
- String.format(Locale.US, "No resource for revision %d", rev_no));
- db.beginTransaction();
- try (InputStream res = rm.openRawResource(res_id)) {
- Log.d("db", "Applying revision " + String.valueOf(rev_no));
- InputStreamReader isr = new InputStreamReader(res);
- BufferedReader reader = new BufferedReader(isr);
-
- String line;
- while ((line = reader.readLine()) != null) {
- db.execSQL(line);