X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FMobileLedgerDatabase.java;h=be31ebdf0991790c33e9fd939ee147a9e93146d8;hb=390a653a4424bd146f73bca3e421a0be252ee443;hp=eeb6873e722f00e30c651e269d84549d77663e86;hpb=d385bacc411bc15cfd8fcaa1a3419b3cb24235d1;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java index eeb6873e..be31ebdf 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -24,19 +24,25 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import androidx.lifecycle.MutableLiveData; + +import net.ktnx.mobileledger.BuildConfig; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static net.ktnx.mobileledger.utils.Logger.debug; public class MobileLedgerDatabase extends SQLiteOpenHelper { - private static final String DB_NAME = "MoLe.db"; - private static final int LATEST_REVISION = 33; + public static final MutableLiveData initComplete = new MutableLiveData<>(false); + public static final String DB_NAME = "MoLe.db"; + private static final int LATEST_REVISION = 55; private static final String CREATE_DB_SQL = "create_db"; - private final Application mContext; public MobileLedgerDatabase(Application context) { @@ -52,12 +58,21 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { applyRevisionFile(db, CREATE_DB_SQL); } + @Override + public void onConfigure(SQLiteDatabase db) { + super.onConfigure(db); + db.execSQL("pragma case_sensitive_like=ON;"); + if (BuildConfig.DEBUG) + db.execSQL("PRAGMA foreign_keys=ON"); + } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - debug("db", "onUpgrade called"); - for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i); + debug("db", + String.format(Locale.US, "needs upgrade from version %d to version %d", oldVersion, + newVersion)); + for (int i = oldVersion + 1; i <= newVersion; i++) + applyRevision(db, i); } - private void applyRevision(SQLiteDatabase db, int rev_no) { String rev_file = String.format(Locale.US, "sql_%d", rev_no); @@ -68,13 +83,15 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName()); if (res_id == 0) throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file)); - db.beginTransaction(); try (InputStream res = rm.openRawResource(res_id)) { debug("db", "Applying " + rev_file); InputStreamReader isr = new InputStreamReader(res); BufferedReader reader = new BufferedReader(isr); + Pattern continuation = Pattern.compile("\\\\\\s*$"); + String line; + String sqlStatement = null; int line_no = 1; while ((line = reader.readLine()) != null) { if (line.startsWith("--")) { @@ -85,8 +102,20 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { line_no++; continue; } + if (sqlStatement == null) + sqlStatement = line; + else + sqlStatement = sqlStatement.concat(line); + + Matcher m = continuation.matcher(line); + if (m.matches()) { + line_no++; + continue; + } + try { - db.execSQL(line); + db.execSQL(sqlStatement); + sqlStatement = null; } catch (Exception e) { throw new RuntimeException( @@ -95,14 +124,14 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { line_no++; } - db.setTransactionSuccessful(); + if (sqlStatement != null) + throw new RuntimeException( + String.format("Error applying %s: EOF after continuation", rev_file)); + } catch (IOException e) { Log.e("db", String.format("Error opening raw resource for %s", rev_file)); e.printStackTrace(); } - finally { - db.endTransaction(); - } } }