- public void storeAccountValue(SQLiteDatabase db, String name, String currency, Float amount) {
- db.execSQL("replace into account_values(profile, account, " +
- "currency, value, keep) values(?, ?, ?, ?, 1);",
- new Object[]{uuid, name, Misc.emptyIsNull(currency), amount});
+ public void storeAccountValue(SQLiteDatabase db, int generation, String name, String currency,
+ Float amount) {
+ if (!TextUtils.isEmpty(currency)) {
+ boolean exists;
+ try (Cursor c = db.rawQuery("select 1 from currencies where name=?",
+ new String[]{currency}))
+ {
+ exists = c.moveToFirst();
+ }
+ if (!exists) {
+ db.execSQL(
+ "insert into currencies(id, name, position, has_gap) values((select max" +
+ "(id) from currencies)+1, ?, ?, ?)", new Object[]{currency,
+ Objects.requireNonNull(
+ Data.currencySymbolPosition.getValue()).toString(),
+ Data.currencyGap.getValue()
+ });
+ }
+ }
+
+ long accId = findAddAccount(db, name);
+
+ db.execSQL("replace into account_values(account_id, " +
+ "currency, value, generation) values(?, ?, ?, ?);",
+ new Object[]{accId, Misc.emptyIsNull(currency), amount, generation});
+ }
+ private long findAddAccount(SQLiteDatabase db, String accountName) {
+ try (Cursor c = db.rawQuery("select id from accounts where profile_id=? and name=?",
+ new String[]{String.valueOf(id), accountName}))
+ {
+ if (c.moveToFirst())
+ return c.getLong(0);
+
+ }
+
+ try (Cursor c = db.rawQuery(
+ "insert into accounts(profile_id, name, name_upper) values(?, ?, ?) returning id",
+ new String[]{String.valueOf(id), accountName, accountName.toUpperCase()}))
+ {
+ c.moveToFirst();
+ return c.getInt(0);
+ }