]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java
wipe retrieval progress when transaction retrieval is stopped
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MLDB.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22
23 import androidx.annotation.NonNull;
24
25 import net.ktnx.mobileledger.App;
26 import net.ktnx.mobileledger.async.DbOpQueue;
27
28 import org.jetbrains.annotations.NonNls;
29
30 import static net.ktnx.mobileledger.utils.Logger.debug;
31
32 public final class MLDB {
33     public static final String ACCOUNTS_TABLE = "accounts";
34     public static final String DESCRIPTION_HISTORY_TABLE = "description_history";
35     public static final String OPT_LAST_SCRAPE = "last_scrape";
36     @NonNls
37     public static final String OPT_PROFILE_ID = "profile_id";
38     public static final long NO_PROFILE = 0;
39     @SuppressWarnings("unused")
40     static public long getLongOption(String name, long default_value) {
41         String s = getOption(name, String.valueOf(default_value));
42         try {
43             return Long.parseLong(s);
44         }
45         catch (Exception e) {
46             debug("db", "returning default long value of " + name, e);
47             return default_value;
48         }
49     }
50     static public String getOption(String name, String default_value) {
51         debug("db", "about to fetch option " + name);
52         SQLiteDatabase db = App.getDatabase();
53         try (Cursor cursor = db.rawQuery("select value from options where profile_id=? and name=?",
54                 new String[]{String.valueOf(NO_PROFILE), name}))
55         {
56             if (cursor.moveToFirst()) {
57                 String result = cursor.getString(0);
58
59                 if (result == null)
60                     result = default_value;
61
62                 debug("db", "option " + name + "=" + result);
63                 return result;
64             }
65             else
66                 return default_value;
67         }
68         catch (Exception e) {
69             debug("db", "returning default value for " + name, e);
70             return default_value;
71         }
72     }
73     static public void setOption(String name, String value) {
74         debug("option", String.format("%s := %s", name, value));
75         DbOpQueue.add("insert or replace into options(profile_id, name, value) values(?, ?, ?);",
76                 new String[]{String.valueOf(NO_PROFILE), name, value});
77     }
78     @SuppressWarnings("unused")
79     static public void setLongOption(String name, long value) {
80         setOption(name, String.valueOf(value));
81     }
82     public static void queryInBackground(@NonNull String statement, String[] params,
83                                          @NonNull final CallbackHelper callbackHelper) {
84         /* All callbacks are called in the new (asynchronous) thread! */
85         Thread t = new Thread(() -> {
86             callbackHelper.onStart();
87             try {
88                 SQLiteDatabase db = App.getDatabase();
89
90                 try (Cursor cursor = db.rawQuery(statement, params)) {
91                     boolean gotRow = false;
92                     while (cursor.moveToNext()) {
93                         gotRow = true;
94                         if (!callbackHelper.onRow(cursor))
95                             break;
96                     }
97                     if (!gotRow) {
98                         callbackHelper.onNoRows();
99                     }
100                 }
101             }
102             catch (Exception e) {
103                 callbackHelper.onException(e);
104             }
105             finally {
106                 callbackHelper.onDone();
107             }
108         });
109
110         t.start();
111     }
112     /* MLDB.CallbackHelper -- Abstract class for asynchronous SQL query callbacks */
113     @SuppressWarnings("WeakerAccess")
114     abstract public static class CallbackHelper {
115         public void onStart() {}
116         public abstract boolean onRow(@NonNull Cursor cursor);
117         public void onNoRows() {}
118         public void onException(Exception exception) {
119             Logger.debug("MLDB", "Exception in asynchronous SQL", exception);
120         }
121         public void onDone() {}
122     }
123 }
124