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;
22 import android.os.AsyncTask;
24 import androidx.annotation.NonNull;
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.async.DbOpQueue;
29 import org.jetbrains.annotations.NonNls;
31 import static net.ktnx.mobileledger.utils.Logger.debug;
33 public final class MLDB {
34 public static final String ACCOUNTS_TABLE = "accounts";
35 public static final String DESCRIPTION_HISTORY_TABLE = "description_history";
36 public static final String OPT_LAST_SCRAPE = "last_scrape";
38 public static final String OPT_PROFILE_UUID = "profile_uuid";
39 private static final String NO_PROFILE = "-";
40 @SuppressWarnings("unused")
41 static public int getIntOption(String name, int default_value) {
42 String s = getOption(name, String.valueOf(default_value));
44 return Integer.parseInt(s);
47 debug("db", "returning default int value of " + name, e);
51 @SuppressWarnings("unused")
52 static public long getLongOption(String name, long default_value) {
53 String s = getOption(name, String.valueOf(default_value));
55 return Long.parseLong(s);
58 debug("db", "returning default long value of " + name, e);
62 static public void getOption(String name, String defaultValue, GetOptCallback cb) {
63 AsyncTask<Void, Void, String> t = new AsyncTask<Void, Void, String>() {
65 protected String doInBackground(Void... params) {
66 SQLiteDatabase db = App.getDatabase();
67 try (Cursor cursor = db.rawQuery(
68 "select value from options where profile=? and name=?",
69 new String[]{NO_PROFILE, name}))
71 if (cursor.moveToFirst()) {
72 String result = cursor.getString(0);
75 result = defaultValue;
77 debug("async-db", "option " + name + "=" + result);
84 debug("db", "returning default value for " + name, e);
89 protected void onPostExecute(String result) {
94 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
96 static public String getOption(String name, String default_value) {
97 debug("db", "about to fetch option " + name);
98 SQLiteDatabase db = App.getDatabase();
99 try (Cursor cursor = db.rawQuery("select value from options where profile=? and name=?",
100 new String[]{NO_PROFILE, name}))
102 if (cursor.moveToFirst()) {
103 String result = cursor.getString(0);
106 result = default_value;
108 debug("db", "option " + name + "=" + result);
112 return default_value;
114 catch (Exception e) {
115 debug("db", "returning default value for " + name, e);
116 return default_value;
119 static public void setOption(String name, String value) {
120 debug("option", String.format("%s := %s", name, value));
121 DbOpQueue.add("insert or replace into options(profile, name, value) values(?, ?, ?);",
122 new String[]{NO_PROFILE, name, value});
124 @SuppressWarnings("unused")
125 static public void setLongOption(String name, long value) {
126 setOption(name, String.valueOf(value));
128 public static void queryInBackground(@NonNull String statement, String[] params,
129 @NonNull final CallbackHelper callbackHelper) {
130 /* All callbacks are called in the new (asynchronous) thread! */
131 Thread t = new Thread(() -> {
132 callbackHelper.onStart();
134 SQLiteDatabase db = App.getDatabase();
136 try (Cursor cursor = db.rawQuery(statement, params)) {
137 boolean gotRow = false;
138 while (cursor.moveToNext()) {
140 if (!callbackHelper.onRow(cursor))
144 callbackHelper.onNoRows();
148 catch (Exception e) {
149 callbackHelper.onException(e);
152 callbackHelper.onDone();
158 /* MLDB.CallbackHelper -- Abstract class for asynchronous SQL query callbacks */
159 @SuppressWarnings("WeakerAccess")
160 abstract public static class CallbackHelper {
161 public void onStart() {}
162 public abstract boolean onRow(@NonNull Cursor cursor);
163 public void onNoRows() {}
164 public void onException(Exception exception) {
165 Logger.debug("MLDB", "Exception in asynchronous SQL", exception);
167 public void onDone() {}