+ SQLiteDatabase db = App.getDatabase();
+ debug("db", String.format(Locale.ROOT, "removing profile %d from DB", id));
+ db.beginTransactionNonExclusive();
+ try {
+ Object[] id_param = new Object[]{id};
+ db.execSQL("delete from transaction_accounts where profile=?", id_param);
+ db.execSQL("delete from transactions where profile=?", id_param);
+ db.execSQL("delete from account_values where profile=?", id_param);
+ db.execSQL("delete from accounts where profile=?", id_param);
+ db.execSQL("delete from options where profile=?", id_param);
+ db.execSQL("delete from profiles where uuid=?", id_param);
+ db.setTransactionSuccessful();
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ public LedgerTransaction loadTransaction(int transactionId) {
+ LedgerTransaction tr = new LedgerTransaction(transactionId, this.id);
+ tr.loadData(App.getDatabase());
+
+ return tr;
+ }
+ public int getThemeHue() {
+// debug("profile", String.format("Profile.getThemeHue() returning %d", themeHue));
+ return this.themeHue;
+ }
+ public void setThemeHue(Object o) {
+ setThemeId(Integer.parseInt(String.valueOf(o)));
+ }
+ public void setThemeId(int themeHue) {
+// debug("profile", String.format("Profile.setThemeHue(%d) called", themeHue));
+ this.themeHue = themeHue;
+ }
+ public int getNextTransactionsGeneration(SQLiteDatabase db) {
+ int generation = 1;
+ try (Cursor c = db.rawQuery("SELECT generation FROM transactions WHERE profile=? LIMIT 1",
+ new String[]{String.valueOf(id)}))
+ {
+ if (c.moveToFirst()) {
+ generation = c.getInt(0) + 1;
+ }
+ }
+ return generation;
+ }
+ private int getNextAccountsGeneration(SQLiteDatabase db) {
+ int generation = 1;
+ try (Cursor c = db.rawQuery("SELECT generation FROM accounts WHERE profile=? LIMIT 1",
+ new String[]{String.valueOf(id)}))
+ {
+ if (c.moveToFirst()) {
+ generation = c.getInt(0) + 1;
+ }
+ }
+ return generation;
+ }
+ private void deleteNotPresentAccounts(SQLiteDatabase db, int generation) {
+ Logger.debug("db/benchmark", "Deleting obsolete accounts");
+ db.execSQL("DELETE FROM account_values WHERE profile=? AND generation <> ?",
+ new Object[]{id, generation});
+ db.execSQL("DELETE FROM accounts WHERE profile=? AND generation <> ?",
+ new Object[]{id, generation});
+ Logger.debug("db/benchmark", "Done deleting obsolete accounts");
+ }
+ private void deleteNotPresentTransactions(SQLiteDatabase db, int generation) {
+ Logger.debug("db/benchmark", "Deleting obsolete transactions");
+ db.execSQL("DELETE FROM transaction_accounts WHERE profile=? AND generation <> ?",
+ new Object[]{id, generation});
+ db.execSQL("DELETE FROM transactions WHERE profile=? AND generation <> ?",
+ new Object[]{id, generation});
+ Logger.debug("db/benchmark", "Done deleting obsolete transactions");
+ }
+ public void wipeAllData() {
+ SQLiteDatabase db = App.getDatabase();
+ db.beginTransaction();
+ try {
+ String[] pUuid = new String[]{String.valueOf(id)};
+ db.execSQL("delete from options where profile=?", pUuid);
+ db.execSQL("delete from accounts where profile=?", pUuid);
+ db.execSQL("delete from account_values where profile=?", pUuid);
+ db.execSQL("delete from transactions where profile=?", pUuid);
+ db.execSQL("delete from transaction_accounts where profile=?", pUuid);
+ db.setTransactionSuccessful();
+ debug("wipe", String.format(Locale.ENGLISH, "Profile %s wiped out", pUuid[0]));
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+ public List<Currency> getCurrencies() {
+ SQLiteDatabase db = App.getDatabase();
+
+ ArrayList<Currency> result = new ArrayList<>();
+
+ try (Cursor c = db.rawQuery("SELECT c.id, c.name, c.position, c.has_gap FROM currencies c",
+ new String[]{}))
+ {
+ while (c.moveToNext()) {
+ Currency currency = new Currency(c.getInt(0), c.getString(1),
+ Currency.Position.valueOf(c.getString(2)), c.getInt(3) == 1);
+ result.add(currency);
+ }
+ }
+
+ return result;
+ }
+ Currency loadCurrencyByName(String name) {
+ SQLiteDatabase db = App.getDatabase();
+ Currency result = tryLoadCurrencyByName(db, name);
+ if (result == null)
+ throw new RuntimeException(String.format("Unable to load currency '%s'", name));
+ return result;
+ }
+ private Currency tryLoadCurrencyByName(SQLiteDatabase db, String name) {
+ try (Cursor cursor = db.rawQuery(
+ "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.name=?",
+ new String[]{name}))