]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
let SQL revision files control the transaction
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MobileLedgerDatabase.java
index 3785ddd3324ad57c9aa3d9b2d1eac3494c03b3de..be31ebdf0991790c33e9fd939ee147a9e93146d8 100644 (file)
@@ -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<Boolean> initComplete = new MutableLiveData<>(false);
     public static final String DB_NAME = "MoLe.db";
-    private static final int LATEST_REVISION = 54;
+    private static final int LATEST_REVISION = 55;
     private static final String CREATE_DB_SQL = "create_db";
     private final Application mContext;
 
@@ -81,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("--")) {
@@ -98,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(
@@ -108,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();
-        }
     }
 }