From 3ed17d6ccfa6aba1795d77ef24167fb9ef6a9ca1 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 10 Feb 2021 23:34:10 +0200 Subject: [PATCH] add continuation support to applyRevisionFile() 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 --- .../utils/MobileLedgerDatabase.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 1c66f07d..32269fea 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java @@ -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) { -- 2.39.5