]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/DbOpRunner.java
67d8a0b1b868b6b7b8e6c4963c010c55ff834f8f
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / DbOpRunner.java
1 /*
2  * Copyright © 2020 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.async;
19
20 import android.database.sqlite.SQLiteDatabase;
21
22 import net.ktnx.mobileledger.App;
23 import net.ktnx.mobileledger.BuildConfig;
24
25 import java.util.concurrent.BlockingQueue;
26
27 import static net.ktnx.mobileledger.utils.Logger.debug;
28
29 class DbOpRunner extends Thread {
30     private final BlockingQueue<DbOpItem> queue;
31     public DbOpRunner(BlockingQueue<DbOpItem> queue) {
32         this.queue = queue;
33     }
34     @Override
35     public void run() {
36         while (!interrupted()) {
37             try {
38                 DbOpItem item = queue.take();
39                 debug("opQrunner", "Got " + item.sql);
40                 {
41                     SQLiteDatabase db = App.getDatabase();
42                     if (BuildConfig.DEBUG) {
43                         StringBuilder b = new StringBuilder("Executing ");
44                         b.append(item.sql);
45                         if (item.params.length > 0) {
46                             boolean first = true;
47                             b.append(" [");
48                             for (Object p : item.params) {
49                                 if (first)
50                                     first = false;
51                                 else
52                                     b.append(", ");
53                                 b.append(p.toString());
54                             }
55                             b.append("]");
56                         }
57                         debug("opQrunner", b.toString());
58                     }
59                     db.execSQL(item.sql, item.params);
60                 }
61                 if (item.onReady != null)
62                     item.onReady.run();
63             }
64             catch (InterruptedException e) {
65                 break;
66             }
67         }
68     }
69 }