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.
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.model;
20 import android.content.res.Resources;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.text.TextUtils;
24 import android.util.SparseArray;
26 import androidx.annotation.Nullable;
28 import net.ktnx.mobileledger.App;
29 import net.ktnx.mobileledger.R;
30 import net.ktnx.mobileledger.async.DbOpQueue;
31 import net.ktnx.mobileledger.async.SendTransactionTask;
32 import net.ktnx.mobileledger.utils.Logger;
33 import net.ktnx.mobileledger.utils.Misc;
34 import net.ktnx.mobileledger.utils.SimpleDate;
36 import org.jetbrains.annotations.Contract;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Locale;
43 import java.util.Objects;
45 import static net.ktnx.mobileledger.utils.Logger.debug;
47 public final class MobileLedgerProfile {
48 // N.B. when adding new fields, update the copy-constructor below
49 private final String uuid;
51 private boolean permitPosting;
52 private boolean showCommentsByDefault;
53 private boolean showCommodityByDefault;
54 private String defaultCommodity;
55 private String preferredAccountsFilter;
57 private boolean authEnabled;
58 private String authUserName;
59 private String authPassword;
61 private int orderNo = -1;
62 private SendTransactionTask.API apiVersion = SendTransactionTask.API.auto;
63 private FutureDates futureDates = FutureDates.None;
64 private boolean accountsLoaded;
65 private boolean transactionsLoaded;
66 private HledgerVersion detectedVersion;
67 // N.B. when adding new fields, update the copy-constructor below
68 transient private AccountAndTransactionListSaver accountAndTransactionListSaver;
69 public MobileLedgerProfile(String uuid) {
72 public MobileLedgerProfile(MobileLedgerProfile origin) {
75 permitPosting = origin.permitPosting;
76 showCommentsByDefault = origin.showCommentsByDefault;
77 showCommodityByDefault = origin.showCommodityByDefault;
78 preferredAccountsFilter = origin.preferredAccountsFilter;
80 authEnabled = origin.authEnabled;
81 authUserName = origin.authUserName;
82 authPassword = origin.authPassword;
83 themeHue = origin.themeHue;
84 orderNo = origin.orderNo;
85 futureDates = origin.futureDates;
86 apiVersion = origin.apiVersion;
87 defaultCommodity = origin.defaultCommodity;
88 accountsLoaded = origin.accountsLoaded;
89 transactionsLoaded = origin.transactionsLoaded;
90 if (origin.detectedVersion != null)
91 detectedVersion = new HledgerVersion(origin.detectedVersion);
93 // loads all profiles into Data.profiles
94 // returns the profile with the given UUID
95 public static MobileLedgerProfile loadAllFromDB(@Nullable String currentProfileUUID) {
96 MobileLedgerProfile result = null;
97 ArrayList<MobileLedgerProfile> list = new ArrayList<>();
98 SQLiteDatabase db = App.getDatabase();
99 try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
100 "auth_password, permit_posting, theme, order_no, " +
101 "preferred_accounts_filter, future_dates, api_version, " +
102 "show_commodity_by_default, default_commodity, " +
103 "show_comments_by_default, detected_version_pre_1_19, " +
104 "detected_version_major, detected_version_minor FROM " +
105 "profiles order by order_no", null))
107 while (cursor.moveToNext()) {
108 MobileLedgerProfile item = new MobileLedgerProfile(cursor.getString(0));
109 item.setName(cursor.getString(1));
110 item.setUrl(cursor.getString(2));
111 item.setAuthEnabled(cursor.getInt(3) == 1);
112 item.setAuthUserName(cursor.getString(4));
113 item.setAuthPassword(cursor.getString(5));
114 item.setPostingPermitted(cursor.getInt(6) == 1);
115 item.setThemeId(cursor.getInt(7));
116 item.orderNo = cursor.getInt(8);
117 item.setPreferredAccountsFilter(cursor.getString(9));
118 item.setFutureDates(cursor.getInt(10));
119 item.setApiVersion(cursor.getInt(11));
120 item.setShowCommodityByDefault(cursor.getInt(12) == 1);
121 item.setDefaultCommodity(cursor.getString(13));
122 item.setShowCommentsByDefault(cursor.getInt(14) == 1);
124 boolean pre_1_20 = cursor.getInt(15) == 1;
125 int major = cursor.getInt(16);
126 int minor = cursor.getInt(17);
128 if (!pre_1_20 && major == 0 && minor == 0) {
129 item.detectedVersion = null;
132 item.detectedVersion = new HledgerVersion(true);
135 item.detectedVersion = new HledgerVersion(major, minor);
140 .equals(currentProfileUUID))
144 Data.profiles.postValue(list);
147 public static void storeProfilesOrder() {
148 SQLiteDatabase db = App.getDatabase();
149 db.beginTransactionNonExclusive();
152 for (MobileLedgerProfile p : Objects.requireNonNull(Data.profiles.getValue())) {
153 db.execSQL("update profiles set order_no=? where uuid=?",
154 new Object[]{orderNo, p.getUuid()});
158 db.setTransactionSuccessful();
164 public HledgerVersion getDetectedVersion() {
165 return detectedVersion;
167 public void setDetectedVersion(HledgerVersion detectedVersion) {
168 this.detectedVersion = detectedVersion;
170 @Contract(value = "null -> false", pure = true)
172 public boolean equals(@Nullable Object obj) {
177 if (obj.getClass() != this.getClass())
180 MobileLedgerProfile p = (MobileLedgerProfile) obj;
181 if (!uuid.equals(p.uuid))
183 if (!name.equals(p.name))
185 if (permitPosting != p.permitPosting)
187 if (showCommentsByDefault != p.showCommentsByDefault)
189 if (showCommodityByDefault != p.showCommodityByDefault)
191 if (!Objects.equals(defaultCommodity, p.defaultCommodity))
193 if (!Objects.equals(preferredAccountsFilter, p.preferredAccountsFilter))
195 if (!Objects.equals(url, p.url))
197 if (authEnabled != p.authEnabled)
199 if (!Objects.equals(authUserName, p.authUserName))
201 if (!Objects.equals(authPassword, p.authPassword))
203 if (themeHue != p.themeHue)
205 if (apiVersion != p.apiVersion)
207 if (!Objects.equals(detectedVersion, p.detectedVersion))
209 return futureDates == p.futureDates;
211 public boolean getShowCommentsByDefault() {
212 return showCommentsByDefault;
214 public void setShowCommentsByDefault(boolean newValue) {
215 this.showCommentsByDefault = newValue;
217 public boolean getShowCommodityByDefault() {
218 return showCommodityByDefault;
220 public void setShowCommodityByDefault(boolean showCommodityByDefault) {
221 this.showCommodityByDefault = showCommodityByDefault;
223 public String getDefaultCommodity() {
224 return defaultCommodity;
226 public void setDefaultCommodity(String defaultCommodity) {
227 this.defaultCommodity = defaultCommodity;
229 public void setDefaultCommodity(CharSequence defaultCommodity) {
230 if (defaultCommodity == null)
231 this.defaultCommodity = null;
233 this.defaultCommodity = String.valueOf(defaultCommodity);
235 public SendTransactionTask.API getApiVersion() {
238 public void setApiVersion(SendTransactionTask.API apiVersion) {
239 this.apiVersion = apiVersion;
241 public void setApiVersion(int apiVersion) {
242 this.apiVersion = SendTransactionTask.API.valueOf(apiVersion);
244 public FutureDates getFutureDates() {
247 public void setFutureDates(int anInt) {
248 futureDates = FutureDates.valueOf(anInt);
250 public void setFutureDates(FutureDates futureDates) {
251 this.futureDates = futureDates;
253 public String getPreferredAccountsFilter() {
254 return preferredAccountsFilter;
256 public void setPreferredAccountsFilter(String preferredAccountsFilter) {
257 this.preferredAccountsFilter = preferredAccountsFilter;
259 public void setPreferredAccountsFilter(CharSequence preferredAccountsFilter) {
260 setPreferredAccountsFilter(String.valueOf(preferredAccountsFilter));
262 public boolean isPostingPermitted() {
263 return permitPosting;
265 public void setPostingPermitted(boolean permitPosting) {
266 this.permitPosting = permitPosting;
268 public String getUuid() {
271 public String getName() {
274 public void setName(CharSequence text) {
275 setName(String.valueOf(text));
277 public void setName(String name) {
280 public String getUrl() {
283 public void setUrl(CharSequence text) {
284 setUrl(String.valueOf(text));
286 public void setUrl(String url) {
289 public boolean isAuthEnabled() {
292 public void setAuthEnabled(boolean authEnabled) {
293 this.authEnabled = authEnabled;
295 public String getAuthUserName() {
298 public void setAuthUserName(CharSequence text) {
299 setAuthUserName(String.valueOf(text));
301 public void setAuthUserName(String authUserName) {
302 this.authUserName = authUserName;
304 public String getAuthPassword() {
307 public void setAuthPassword(CharSequence text) {
308 setAuthPassword(String.valueOf(text));
310 public void setAuthPassword(String authPassword) {
311 this.authPassword = authPassword;
313 public void storeInDB() {
314 SQLiteDatabase db = App.getDatabase();
315 db.beginTransactionNonExclusive();
317 // debug("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
318 // "url=%s, permit_posting=%s, authEnabled=%s, " +
319 // "themeHue=%d", uuid, name, url,
320 // permitPosting ? "TRUE" : "FALSE", authEnabled ? "TRUE" : "FALSE", themeHue));
321 db.execSQL("REPLACE INTO profiles(uuid, name, permit_posting, url, " +
322 "use_authentication, auth_user, auth_password, theme, order_no, " +
323 "preferred_accounts_filter, future_dates, api_version, " +
324 "show_commodity_by_default, default_commodity, show_comments_by_default," +
325 "detected_version_pre_1_19, detected_version_major, " +
326 "detected_version_minor) " +
327 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
328 new Object[]{uuid, name, permitPosting, url, authEnabled,
329 authEnabled ? authUserName : null,
330 authEnabled ? authPassword : null, themeHue, orderNo,
331 preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(),
332 showCommodityByDefault, defaultCommodity, showCommentsByDefault,
333 (detectedVersion != null) && detectedVersion.isPre_1_20(),
334 (detectedVersion == null) ? 0 : detectedVersion.getMajor(),
335 (detectedVersion == null) ? 0 : detectedVersion.getMinor()
337 db.setTransactionSuccessful();
343 public void storeAccount(SQLiteDatabase db, int generation, LedgerAccount acc,
344 boolean storeUiFields) {
345 // replace into is a bad idea because it would reset hidden to its default value
346 // we like the default, but for new accounts only
347 String sql = "update accounts set generation = ?";
348 List<Object> params = new ArrayList<>();
349 params.add(generation);
351 sql += ", expanded=?";
352 params.add(acc.isExpanded() ? 1 : 0);
354 sql += " where profile=? and name=?";
356 params.add(acc.getName());
357 db.execSQL(sql, params.toArray());
359 db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level, " +
360 "expanded, generation) select ?,?,?,?,?,0,? where (select changes() = 0)",
361 new Object[]{uuid, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
362 acc.getLevel(), generation
364 // debug("accounts", String.format("Stored account '%s' in DB [%s]", acc.getName(), uuid));
366 public void storeAccountValue(SQLiteDatabase db, int generation, String name, String currency,
368 if (!TextUtils.isEmpty(currency)) {
370 try (Cursor c = db.rawQuery("select 1 from currencies where name=?",
371 new String[]{currency}))
373 exists = c.moveToFirst();
377 "insert into currencies(id, name, position, has_gap) values((select max" +
378 "(id) from currencies)+1, ?, ?, ?)", new Object[]{currency,
379 Objects.requireNonNull(
380 Data.currencySymbolPosition.getValue()).toString(),
381 Data.currencyGap.getValue()
386 db.execSQL("replace into account_values(profile, account, " +
387 "currency, value, generation) values(?, ?, ?, ?, ?);",
388 new Object[]{uuid, name, Misc.emptyIsNull(currency), amount, generation});
390 public void storeTransaction(SQLiteDatabase db, int generation, LedgerTransaction tr) {
392 // Logger.debug("storeTransaction", String.format(Locale.US, "ID %d", tr.getId()));
393 SimpleDate d = tr.getDate();
394 db.execSQL("UPDATE transactions SET year=?, month=?, day=?, description=?, comment=?, " +
395 "data_hash=?, generation=? WHERE profile=? AND id=?",
396 new Object[]{d.year, d.month, d.day, tr.getDescription(), tr.getComment(),
397 tr.getDataHash(), generation, uuid, tr.getId()
399 db.execSQL("INSERT INTO transactions(profile, id, year, month, day, description, " +
400 "comment, data_hash, generation) " +
401 "select ?,?,?,?,?,?,?,?,? WHERE (select changes() = 0)",
402 new Object[]{uuid, tr.getId(), tr.getDate().year, tr.getDate().month,
403 tr.getDate().day, tr.getDescription(), tr.getComment(),
404 tr.getDataHash(), generation
407 int accountOrderNo = 1;
408 for (LedgerTransactionAccount item : tr.getAccounts()) {
409 db.execSQL("UPDATE transaction_accounts SET account_name=?, amount=?, currency=?, " +
410 "comment=?, generation=? " +
411 "WHERE profile=? AND transaction_id=? AND order_no=?",
412 new Object[]{item.getAccountName(), item.getAmount(),
413 Misc.nullIsEmpty(item.getCurrency()), item.getComment(),
414 generation, uuid, tr.getId(), accountOrderNo
416 db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
417 "order_no, account_name, amount, currency, comment, generation) " +
418 "select ?, ?, ?, ?, ?, ?, ?, ? WHERE (select changes() = 0)",
419 new Object[]{uuid, tr.getId(), accountOrderNo, item.getAccountName(),
420 item.getAmount(), Misc.nullIsEmpty(item.getCurrency()),
421 item.getComment(), generation
426 // debug("profile", String.format("Transaction %d stored", tr.getId()));
428 public String getOption(String name, String default_value) {
429 SQLiteDatabase db = App.getDatabase();
430 try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
431 new String[]{uuid, name}))
433 if (cursor.moveToFirst()) {
434 String result = cursor.getString(0);
436 if (result == null) {
437 debug("profile", "returning default value for " + name);
438 result = default_value;
441 debug("profile", String.format("option %s=%s", name, result));
446 return default_value;
448 catch (Exception e) {
449 debug("db", "returning default value for " + name, e);
450 return default_value;
453 public long getLongOption(String name, long default_value) {
455 String result = getOption(name, "");
456 if ((result == null) || result.isEmpty()) {
457 debug("profile", String.format("Returning default value for option %s", name));
458 longResult = default_value;
462 longResult = Long.parseLong(result);
463 debug("profile", String.format("option %s=%s", name, result));
465 catch (Exception e) {
466 debug("profile", String.format("Returning default value for option %s", name), e);
467 longResult = default_value;
473 public void setOption(String name, String value) {
474 debug("profile", String.format("setting option %s=%s", name, value));
475 DbOpQueue.add("insert or replace into options(profile, name, value) values(?, ?, ?);",
476 new String[]{uuid, name, value});
478 public void setLongOption(String name, long value) {
479 setOption(name, String.valueOf(value));
481 public void removeFromDB() {
482 SQLiteDatabase db = App.getDatabase();
483 debug("db", String.format("removing profile %s from DB", uuid));
484 db.beginTransactionNonExclusive();
486 Object[] uuid_param = new Object[]{uuid};
487 db.execSQL("delete from transaction_accounts where profile=?", uuid_param);
488 db.execSQL("delete from transactions where profile=?", uuid_param);
489 db.execSQL("delete from account_values where profile=?", uuid_param);
490 db.execSQL("delete from accounts where profile=?", uuid_param);
491 db.execSQL("delete from options where profile=?", uuid_param);
492 db.execSQL("delete from profiles where uuid=?", uuid_param);
493 db.setTransactionSuccessful();
499 public LedgerTransaction loadTransaction(int transactionId) {
500 LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid);
501 tr.loadData(App.getDatabase());
505 public int getThemeHue() {
506 // debug("profile", String.format("Profile.getThemeHue() returning %d", themeHue));
507 return this.themeHue;
509 public void setThemeHue(Object o) {
510 setThemeId(Integer.parseInt(String.valueOf(o)));
512 public void setThemeId(int themeHue) {
513 // debug("profile", String.format("Profile.setThemeHue(%d) called", themeHue));
514 this.themeHue = themeHue;
516 public int getNextTransactionsGeneration(SQLiteDatabase db) {
518 try (Cursor c = db.rawQuery("SELECT generation FROM transactions WHERE profile=? LIMIT 1",
521 if (c.moveToFirst()) {
522 generation = c.getInt(0) + 1;
527 private int getNextAccountsGeneration(SQLiteDatabase db) {
529 try (Cursor c = db.rawQuery("SELECT generation FROM accounts WHERE profile=? LIMIT 1",
532 if (c.moveToFirst()) {
533 generation = c.getInt(0) + 1;
538 private void deleteNotPresentAccounts(SQLiteDatabase db, int generation) {
539 Logger.debug("db/benchmark", "Deleting obsolete accounts");
540 db.execSQL("DELETE FROM account_values WHERE profile=? AND generation <> ?",
541 new Object[]{uuid, generation});
542 db.execSQL("DELETE FROM accounts WHERE profile=? AND generation <> ?",
543 new Object[]{uuid, generation});
544 Logger.debug("db/benchmark", "Done deleting obsolete accounts");
546 private void deleteNotPresentTransactions(SQLiteDatabase db, int generation) {
547 Logger.debug("db/benchmark", "Deleting obsolete transactions");
548 db.execSQL("DELETE FROM transaction_accounts WHERE profile=? AND generation <> ?",
549 new Object[]{uuid, generation});
550 db.execSQL("DELETE FROM transactions WHERE profile=? AND generation <> ?",
551 new Object[]{uuid, generation});
552 Logger.debug("db/benchmark", "Done deleting obsolete transactions");
554 public void wipeAllData() {
555 SQLiteDatabase db = App.getDatabase();
556 db.beginTransaction();
558 String[] pUuid = new String[]{uuid};
559 db.execSQL("delete from options where profile=?", pUuid);
560 db.execSQL("delete from accounts where profile=?", pUuid);
561 db.execSQL("delete from account_values where profile=?", pUuid);
562 db.execSQL("delete from transactions where profile=?", pUuid);
563 db.execSQL("delete from transaction_accounts where profile=?", pUuid);
564 db.setTransactionSuccessful();
565 debug("wipe", String.format(Locale.ENGLISH, "Profile %s wiped out", pUuid[0]));
571 public List<Currency> getCurrencies() {
572 SQLiteDatabase db = App.getDatabase();
574 ArrayList<Currency> result = new ArrayList<>();
576 try (Cursor c = db.rawQuery("SELECT c.id, c.name, c.position, c.has_gap FROM currencies c",
579 while (c.moveToNext()) {
580 Currency currency = new Currency(c.getInt(0), c.getString(1),
581 Currency.Position.valueOf(c.getString(2)), c.getInt(3) == 1);
582 result.add(currency);
588 Currency loadCurrencyByName(String name) {
589 SQLiteDatabase db = App.getDatabase();
590 Currency result = tryLoadCurrencyByName(db, name);
592 throw new RuntimeException(String.format("Unable to load currency '%s'", name));
595 private Currency tryLoadCurrencyByName(SQLiteDatabase db, String name) {
596 try (Cursor cursor = db.rawQuery(
597 "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.name=?",
600 if (cursor.moveToFirst()) {
601 return new Currency(cursor.getInt(0), cursor.getString(1),
602 Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1);
607 public void storeAccountAndTransactionListAsync(List<LedgerAccount> accounts,
608 List<LedgerTransaction> transactions) {
609 if (accountAndTransactionListSaver != null)
610 accountAndTransactionListSaver.interrupt();
612 accountAndTransactionListSaver =
613 new AccountAndTransactionListSaver(this, accounts, transactions);
614 accountAndTransactionListSaver.start();
617 public enum FutureDates {
618 None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),
619 SixMonths(180), OneYear(365), All(-1);
620 private static final SparseArray<FutureDates> map = new SparseArray<>();
623 for (FutureDates item : FutureDates.values()) {
624 map.put(item.value, item);
629 FutureDates(int value) {
632 public static FutureDates valueOf(int i) {
633 return map.get(i, None);
638 public String getText(Resources resources) {
641 return resources.getString(R.string.future_dates_7);
643 return resources.getString(R.string.future_dates_14);
645 return resources.getString(R.string.future_dates_30);
647 return resources.getString(R.string.future_dates_60);
649 return resources.getString(R.string.future_dates_90);
651 return resources.getString(R.string.future_dates_180);
653 return resources.getString(R.string.future_dates_365);
655 return resources.getString(R.string.future_dates_all);
657 return resources.getString(R.string.future_dates_none);
662 private static class AccountAndTransactionListSaver extends Thread {
663 private final MobileLedgerProfile profile;
664 private final List<LedgerAccount> accounts;
665 private final List<LedgerTransaction> transactions;
666 AccountAndTransactionListSaver(MobileLedgerProfile profile, List<LedgerAccount> accounts,
667 List<LedgerTransaction> transactions) {
668 this.accounts = accounts;
669 this.transactions = transactions;
670 this.profile = profile;
672 public int getNextDescriptionsGeneration(SQLiteDatabase db) {
674 try (Cursor c = db.rawQuery("SELECT generation FROM description_history LIMIT 1",
677 if (c.moveToFirst()) {
678 generation = c.getInt(0) + 1;
683 void deleteNotPresentDescriptions(SQLiteDatabase db, int generation) {
684 Logger.debug("db/benchmark", "Deleting obsolete descriptions");
685 db.execSQL("DELETE FROM description_history WHERE generation <> ?",
686 new Object[]{generation});
687 db.execSQL("DELETE FROM description_history WHERE generation <> ?",
688 new Object[]{generation});
689 Logger.debug("db/benchmark", "Done deleting obsolete descriptions");
693 SQLiteDatabase db = App.getDatabase();
694 db.beginTransactionNonExclusive();
696 int accountsGeneration = profile.getNextAccountsGeneration(db);
700 int transactionsGeneration = profile.getNextTransactionsGeneration(db);
704 for (LedgerAccount acc : accounts) {
705 profile.storeAccount(db, accountsGeneration, acc, false);
708 for (LedgerAmount amt : acc.getAmounts()) {
709 profile.storeAccountValue(db, accountsGeneration, acc.getName(),
710 amt.getCurrency(), amt.getAmount());
716 for (LedgerTransaction tr : transactions) {
717 profile.storeTransaction(db, transactionsGeneration, tr);
722 profile.deleteNotPresentTransactions(db, transactionsGeneration);
723 if (isInterrupted()) {
726 profile.deleteNotPresentAccounts(db, accountsGeneration);
730 Map<String, Boolean> unique = new HashMap<>();
732 debug("descriptions", "Starting refresh");
733 int descriptionsGeneration = getNextDescriptionsGeneration(db);
734 try (Cursor c = db.rawQuery("SELECT distinct description from transactions",
737 while (c.moveToNext()) {
738 String description = c.getString(0);
739 String descriptionUpper = description.toUpperCase();
740 if (unique.containsKey(descriptionUpper))
743 storeDescription(db, descriptionsGeneration, description, descriptionUpper);
745 unique.put(descriptionUpper, true);
748 deleteNotPresentDescriptions(db, descriptionsGeneration);
750 db.setTransactionSuccessful();
756 private void storeDescription(SQLiteDatabase db, int generation, String description,
757 String descriptionUpper) {
758 db.execSQL("UPDATE description_history SET description=?, generation=? WHERE " +
759 "description_upper=?", new Object[]{description, generation, descriptionUpper
762 "INSERT INTO description_history(description, description_upper, generation) " +
763 "select ?,?,? WHERE (select changes() = 0)",
764 new Object[]{description, descriptionUpper, generation