X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FMobileLedgerDatabase.java;h=0ddaa1dc0fc2da4cb5f3c1f9a2dd9fa5684b37f1;hp=307d4bc9f93425a2bdeb930f6476abb5f36ef5d2;hb=55f4f1b5f101d0f9874fe3d3406d53c6df931a40;hpb=f0fecef867dd49fe41fc733c11418f95a270be4a 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 307d4bc9..0ddaa1dc 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 © 2020 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 @@ -22,7 +22,6 @@ import android.content.res.Resources; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.util.Log; import androidx.lifecycle.MutableLiveData; @@ -33,13 +32,15 @@ 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 { public static final MutableLiveData initComplete = new MutableLiveData<>(false); - private static final String DB_NAME = "MoLe.db"; - private static final int LATEST_REVISION = 40; + public static final String DB_NAME = "MoLe.db"; + private static final int LATEST_REVISION = 57; private static final String CREATE_DB_SQL = "create_db"; private final Application mContext; @@ -56,6 +57,13 @@ 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", @@ -64,59 +72,69 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i); } - @Override - public void onOpen(SQLiteDatabase db) { - super.onOpen(db); - db.execSQL("pragma case_sensitive_like=ON;"); - if (BuildConfig.DEBUG) - db.execSQL("PRAGMA foreign_keys=ON"); - } - private void applyRevision(SQLiteDatabase db, int rev_no) { String rev_file = String.format(Locale.US, "sql_%d", rev_no); applyRevisionFile(db, rev_file); } - private void applyRevisionFile(SQLiteDatabase db, String rev_file) { + private void applyRevisionFile(SQLiteDatabase db, String revFile) { final Resources rm = mContext.getResources(); - int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName()); + int res_id = rm.getIdentifier(revFile, "raw", mContext.getPackageName()); if (res_id == 0) - throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file)); - db.beginTransaction(); + throw new SQLException(String.format(Locale.US, "No resource for %s", revFile)); + try (InputStream res = rm.openRawResource(res_id)) { - debug("db", "Applying " + rev_file); + debug("db", "Applying " + revFile); InputStreamReader isr = new InputStreamReader(res); BufferedReader reader = new BufferedReader(isr); + Pattern continuation = Pattern.compile("\\\\\\s*$"); + String line; - int line_no = 1; + String sqlStatement = null; + boolean eolPending; + int lineNo = 0; while ((line = reader.readLine()) != null) { - if (line.startsWith("--")) { - line_no++; + lineNo++; + if (line.startsWith("--")) continue; - } - if (line.isEmpty()) { - line_no++; + if (line.isEmpty()) continue; + + Matcher m = continuation.matcher(line); + eolPending = false; + if (m.find()) { + line = m.replaceFirst(""); + eolPending = true; } + + if (sqlStatement == null) + sqlStatement = line; + else + sqlStatement = sqlStatement.concat(line); + + if (eolPending) + continue; + try { - db.execSQL(line); + db.execSQL(sqlStatement); + sqlStatement = null; } catch (Exception e) { throw new RuntimeException( - String.format("Error applying %s, line %d", rev_file, line_no), e); + String.format("Error applying %s, line %d, statement: %s", revFile, + lineNo, sqlStatement), e); } - line_no++; } - db.setTransactionSuccessful(); + if (sqlStatement != null) + throw new RuntimeException( + String.format("Error applying %s: EOF after continuation", revFile)); + } catch (IOException e) { - Log.e("db", String.format("Error opening raw resource for %s", rev_file)); - e.printStackTrace(); - } - finally { - db.endTransaction(); + throw new RuntimeException(String.format("Error opening raw resource for %s", revFile), + e); } } }