X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FMobileLedgerDatabase.java;h=e0a0a421c58ca3e22495b17c6218e07bbe88c92e;hb=926ab67da2663c9405f79659e1c82efbbb661388;hp=f08f73028d4e3c369fb3c335113edbd45b9e8084;hpb=b2f45c881924d9a172241700e682f1051128bd59;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 f08f7302..e0a0a421 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java @@ -33,13 +33,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); public static final String DB_NAME = "MoLe.db"; - private static final int LATEST_REVISION = 53; + private static final int LATEST_REVISION = 55; private static final String CREATE_DB_SQL = "create_db"; private final Application mContext; @@ -76,46 +78,64 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { 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); } } }