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.
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.
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/>.
18 package net.ktnx.mobileledger.utils;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
23 import androidx.annotation.NonNull;
25 import net.ktnx.mobileledger.App;
26 import net.ktnx.mobileledger.async.DbOpQueue;
28 import org.jetbrains.annotations.NonNls;
30 import static net.ktnx.mobileledger.utils.Logger.debug;
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";
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));
43 return Long.parseLong(s);
46 debug("db", "returning default long value of " + name, e);
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}))
56 if (cursor.moveToFirst()) {
57 String result = cursor.getString(0);
60 result = default_value;
62 debug("db", "option " + name + "=" + result);
69 debug("db", "returning default value for " + name, e);
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});
78 @SuppressWarnings("unused")
79 static public void setLongOption(String name, long value) {
80 setOption(name, String.valueOf(value));
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();
88 SQLiteDatabase db = App.getDatabase();
90 try (Cursor cursor = db.rawQuery(statement, params)) {
91 boolean gotRow = false;
92 while (cursor.moveToNext()) {
94 if (!callbackHelper.onRow(cursor))
98 callbackHelper.onNoRows();
102 catch (Exception e) {
103 callbackHelper.onException(e);
106 callbackHelper.onDone();
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);
121 public void onDone() {}