]> git.ktnx.net Git - mobile-ledger.git/commitdiff
add continuation support to applyRevisionFile()
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 10 Feb 2021 21:34:10 +0000 (23:34 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:19:43 +0000 (07:19 +0000)
even if the plan is to migrate fully to Room, writing DB structure
revisions in a series of SQL files is much more convenient compared to a
sequence od db.execSQL() calls with very long arguments

except that writing SQL text that is very long is a problem for the SQL
files too. Continuations solve this in a way, much better than splitting
long SQL arguments to execSQL() in a series of concatenated strings

app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java

index 1c66f07d99c44351aace1e100ecdd198eaade6d3..32269fea612e7904b8daebade3fd2a87a47d3f24 100644 (file)
@@ -33,6 +33,8 @@ 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;
 
@@ -87,7 +89,10 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper {
             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 +103,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,6 +125,10 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper {
                 line_no++;
             }
 
+            if (sqlStatement != null)
+                throw new RuntimeException(
+                        String.format("Error applying %s: EOF after continuation", rev_file));
+
             db.setTransactionSuccessful();
         }
         catch (IOException e) {