- SQLiteDatabase db = App.getDatabase();
- debug("db", String.format("removing profile %s from DB", uuid));
- db.beginTransactionNonExclusive();
- try {
- Object[] uuid_param = new Object[]{uuid};
- db.execSQL("delete from profiles where uuid=?", uuid_param);
- db.execSQL("delete from accounts where profile=?", uuid_param);
- db.execSQL("delete from account_values where profile=?", uuid_param);
- db.execSQL("delete from transactions where profile=?", uuid_param);
- db.execSQL("delete from transaction_accounts where profile=?", uuid_param);
- db.execSQL("delete from options where profile=?", uuid_param);
- db.setTransactionSuccessful();
- }
- finally {
- db.endTransaction();
- }
- }
- @NonNull
- public LedgerAccount loadAccount(String name) {
- SQLiteDatabase db = App.getDatabase();
- return loadAccount(db, name);
- }
- @Nullable
- public LedgerAccount tryLoadAccount(String acct_name) {
- SQLiteDatabase db = App.getDatabase();
- return tryLoadAccount(db, acct_name);
- }
- @NonNull
- public LedgerAccount loadAccount(SQLiteDatabase db, String accName) {
- LedgerAccount acc = tryLoadAccount(db, accName);
-
- if (acc == null)
- throw new RuntimeException("Unable to load account with name " + accName);
-
- return acc;
- }
- @Nullable
- public LedgerAccount tryLoadAccount(SQLiteDatabase db, String accName) {
- try (Cursor cursor = db.rawQuery("SELECT a.expanded, a.amounts_expanded, (select 1 from accounts a2 " +
- "where a2.profile = a.profile and a2.name like a" +
- ".name||':%' limit 1) " +
- "FROM accounts a WHERE a.profile = ? and a.name=?",
- new String[]{uuid, accName}))
- {
- if (cursor.moveToFirst()) {
- LedgerAccount acc = new LedgerAccount(this, accName);
- acc.setExpanded(cursor.getInt(0) == 1);
- acc.setAmountsExpanded(cursor.getInt(1) == 1);
- acc.setHasSubAccounts(cursor.getInt(2) == 1);
-
- try (Cursor c2 = db.rawQuery(
- "SELECT value, currency FROM account_values WHERE profile = ? " +
- "AND account = ?", new String[]{uuid, accName}))
- {
- while (c2.moveToNext()) {
- acc.addAmount(c2.getFloat(0), c2.getString(1));
- }
- }
-
- return acc;
- }
- return null;
- }