Skip to content

Commit

Permalink
when calculating transactionSum respect aggregate Function for Db Saf…
Browse files Browse the repository at this point in the history
…e Mode

Cleanup DbUtils
  • Loading branch information
mtotschnig committed Sep 29, 2023
1 parent 969ec78 commit 5fafa59
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package org.totschnig.myexpenses.test.provider
import android.content.ContentUris
import android.content.ContentValues
import android.database.sqlite.SQLiteConstraintException
import androidx.core.database.getLongOrNull
import org.totschnig.myexpenses.provider.DatabaseConstants
import org.totschnig.myexpenses.provider.DatabaseConstants.TABLE_CATEGORIES
import org.totschnig.myexpenses.provider.DbUtils
import org.totschnig.myexpenses.provider.TransactionProvider
import org.totschnig.myexpenses.provider.insert
import org.totschnig.myexpenses.testutils.BaseDbTest
Expand Down Expand Up @@ -172,7 +172,7 @@ class CategoryTest : BaseDbTest() {
assertTrue(it.moveToFirst())
val labelIndex = it.getColumnIndex(DatabaseConstants.KEY_LABEL)
val parentIdIndex = it.getColumnIndex(DatabaseConstants.KEY_PARENTID)
assertEquals(transaction.parentId, DbUtils.getLongOrNull(it, parentIdIndex))
assertEquals(transaction.parentId, it.getLongOrNull(parentIdIndex))
assertEquals(transaction.label, it.getString(labelIndex))
}

Expand All @@ -181,14 +181,14 @@ class CategoryTest : BaseDbTest() {
try {
mockContentResolver.insert(TransactionProvider.CATEGORIES_URI, values)
fail("Expected insert failure for existing record but insert succeeded.")
} catch (e: Exception) {
} catch (_: Exception) {
}
values.remove(DatabaseConstants.KEY_ROWID)
values.put(DatabaseConstants.KEY_PARENTID, 100)
try {
mockContentResolver.insert(TransactionProvider.CATEGORIES_URI, values)
fail("Expected insert failure for link to non-existing parent but insert succeeded.")
} catch (e: Exception) {
} catch (_: Exception) {
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ class CategoryTest : BaseDbTest() {
category.contentValues
)
fail("Expected unique constraint to prevent main category from being created.")
} catch (e: SQLiteConstraintException) {
} catch (_: SQLiteConstraintException) {
}
}

Expand All @@ -266,7 +266,7 @@ class CategoryTest : BaseDbTest() {
category.contentValues
)
fail("Expected unique constraint to prevent sub category from being created.")
} catch (e: SQLiteConstraintException) {
} catch (_: SQLiteConstraintException) {
}
}

Expand Down Expand Up @@ -301,7 +301,7 @@ class CategoryTest : BaseDbTest() {
}, null, null
)
fail("Expected unique constraint to prevent sub category from being created.")
} catch (e: SQLiteConstraintException) {
} catch (_: SQLiteConstraintException) {
}
}

Expand Down Expand Up @@ -402,7 +402,7 @@ class CategoryTest : BaseDbTest() {
}, null, null
)
fail("Row with parentId equal to _id must not be allowed")
} catch (e: SQLiteConstraintException) {
} catch (_: SQLiteConstraintException) {
}
}

Expand All @@ -423,7 +423,7 @@ class CategoryTest : BaseDbTest() {
}, null, null
)
fail("Moving a category to its own descendant must be blocked.")
} catch (e: SQLiteConstraintException) {
} catch (_: SQLiteConstraintException) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.totschnig.myexpenses;

import static androidx.core.database.CursorKt.getLongOrNull;
import static org.totschnig.myexpenses.feature.WebUiFeatureKt.RESTART_ACTION;
import static org.totschnig.myexpenses.feature.WebUiFeatureKt.START_ACTION;
import static org.totschnig.myexpenses.feature.WebUiFeatureKt.STOP_ACTION;
Expand All @@ -24,6 +25,7 @@
import static org.totschnig.myexpenses.preference.PrefKey.WEBUI_PASSWORD;
import static org.totschnig.myexpenses.provider.DataBaseAccount.AGGREGATE_HOME_CURRENCY_CODE;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.CALENDAR_FULL_PATH_PROJECTION;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.requireString;

import android.app.Activity;
import android.app.ActivityManager;
Expand Down Expand Up @@ -68,7 +70,6 @@
import org.totschnig.myexpenses.preference.PrefKey;
import org.totschnig.myexpenses.provider.BaseTransactionProvider;
import org.totschnig.myexpenses.provider.DatabaseConstants;
import org.totschnig.myexpenses.provider.DbUtils;
import org.totschnig.myexpenses.provider.MoreDbUtilsKt;
import org.totschnig.myexpenses.provider.TransactionProvider;
import org.totschnig.myexpenses.service.AutoBackupWorker;
Expand Down Expand Up @@ -364,7 +365,7 @@ private String checkPlannerInternal(String calendarId) {
return null;
} else {
if (c.moveToFirst()) {
String found = DbUtils.getString(c, 0);
String found = requireString(c, 0);
String expected = prefHandler.getString(PrefKey.PLANNER_CALENDAR_PATH, "");
if (!found.equals(expected)) {
CrashHandler.report(new Exception("found calendar, but path did not match"));
Expand Down Expand Up @@ -512,10 +513,10 @@ public static String[] buildEventProjection() {
* @param eventValues ContentValues where the extracted data is copied to
*/
public static void copyEventData(Cursor eventCursor, ContentValues eventValues) {
eventValues.put(Events.DTSTART, DbUtils.getLongOrNull(eventCursor, 0));
eventValues.put(Events.DTSTART, getLongOrNull(eventCursor, 0));
//older Android versions have populated both dtend and duration
//restoring those on newer versions leads to IllegalArgumentException
Long dtEnd = DbUtils.getLongOrNull(eventCursor, 1);
Long dtEnd = getLongOrNull(eventCursor, 1);
String duration = null;
if (dtEnd == null) {
duration = eventCursor.getString(6);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import androidx.core.database.getLongOrNull
import org.totschnig.myexpenses.model.CurrencyContext
import org.totschnig.myexpenses.preference.PrefHandler
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ACCOUNTID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ATTACHMENT_ID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_CATID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_TRANSACTIONID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_URI
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_UUID
import org.totschnig.myexpenses.provider.TransactionProvider
import org.totschnig.myexpenses.provider.TransactionProvider.AUTOFILL_URI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.totschnig.myexpenses.provider.DatabaseConstants.VIEW_COMMITTED
import org.totschnig.myexpenses.provider.DatabaseConstants.VIEW_EXTENDED
import org.totschnig.myexpenses.provider.DatabaseConstants.WHERE_NOT_SPLIT_PART
import org.totschnig.myexpenses.provider.DatabaseConstants.WHERE_NOT_VOID
import org.totschnig.myexpenses.provider.DbUtils
import org.totschnig.myexpenses.provider.TransactionProvider
import org.totschnig.myexpenses.provider.filter.FilterPersistence
import org.totschnig.myexpenses.provider.filter.WhereFilter
Expand Down Expand Up @@ -167,7 +168,7 @@ fun Repository.getTransactionSum(accountId: Long, filter: WhereFilter? = null):
}
return contentResolver.query(
TransactionProvider.TRANSACTIONS_URI,
arrayOf("sum($KEY_AMOUNT)"),
arrayOf("${DbUtils.aggregateFunction(prefHandler)}($KEY_AMOUNT)"),
selection,
selectionArgs,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import static org.totschnig.myexpenses.provider.DatabaseConstants.KEY_YEAR_OF_WEEK_START;
import static org.totschnig.myexpenses.provider.DatabaseConstants.SPLIT_CATID;
import static org.totschnig.myexpenses.provider.DbConstantsKt.CTE_TRANSACTION_GROUPS;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.getLongOrNull;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.getString;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.requireString;
import static org.totschnig.myexpenses.util.ArrayUtilsKt.joinArrays;
import static org.totschnig.myexpenses.util.CurrencyFormatterKt.convAmount;
import static org.totschnig.myexpenses.util.CurrencyFormatterKt.formatMoney;
Expand Down Expand Up @@ -66,7 +69,6 @@
import org.totschnig.myexpenses.model.Transfer;
import org.totschnig.myexpenses.model2.Account;
import org.totschnig.myexpenses.provider.DatabaseConstants;
import org.totschnig.myexpenses.provider.DbUtils;
import org.totschnig.myexpenses.provider.MoreDbUtilsKt;
import org.totschnig.myexpenses.provider.filter.WhereFilter;
import org.totschnig.myexpenses.util.AppDirHelper;
Expand Down Expand Up @@ -232,7 +234,6 @@ private void addTransactionList(Document document, Cursor transactionCursor, Pdf
int columnIndexComment = transactionCursor.getColumnIndex(KEY_COMMENT);
int columnIndexReferenceNumber = transactionCursor.getColumnIndex(KEY_REFERENCE_NUMBER);
int columnIndexPayee = transactionCursor.getColumnIndex(KEY_PAYEE_NAME);
int columnIndexTransferPeer = transactionCursor.getColumnIndex(KEY_TRANSFER_PEER);
int columnIndexDate = transactionCursor.getColumnIndex(KEY_DATE);
int columnIndexCrStatus = transactionCursor.getColumnIndex(KEY_CR_STATUS);

Expand Down Expand Up @@ -384,28 +385,28 @@ public void tableLayout(PdfPTable table1, float[][] widths, float[] heights, int
}

String catText = transactionCursor.getString(columnIndexLabel);
if (DbUtils.getLongOrNull(transactionCursor, columnIndexTransferPeer) != null) {
if (getLongOrNull(transactionCursor, KEY_TRANSFER_PEER) != null) {
catText = Transfer.getIndicatorPrefixForLabel(amount) + catText;
} else {
Long catId = DbUtils.getLongOrNull(transactionCursor, KEY_CATID);
Long catId = getLongOrNull(transactionCursor, KEY_CATID);
if (SPLIT_CATID.equals(catId)) {
Cursor splits = context.getContentResolver().query(Transaction.CONTENT_URI, null,
KEY_PARENTID + " = " + transactionCursor.getLong(columnIndexRowId), null, null);
splits.moveToFirst();
StringBuilder catTextBuilder = new StringBuilder();
while (splits.getPosition() < splits.getCount()) {
String splitText = DbUtils.getString(splits, KEY_LABEL);
String splitText = getString(splits, KEY_LABEL);
if (splitText.length() > 0) {
if (DbUtils.getLongOrNull(splits, KEY_TRANSFER_PEER) != null) {
if (getLongOrNull(splits, KEY_TRANSFER_PEER) != null) {
splitText = "[" + splitText + "]";
}
} else {
splitText = Category.NO_CATEGORY_ASSIGNED_LABEL;
}
splitText += " " + convAmount(currencyFormatter, splits.getLong(
splits.getColumnIndexOrThrow(KEY_DISPLAY_AMOUNT)), currencyUnit());
String splitComment = DbUtils.getString(splits, KEY_COMMENT);
if (splitComment != null && splitComment.length() > 0) {
String splitComment = getString(splits, KEY_COMMENT);
if (splitComment.length() > 0) {
splitText += " (" + splitComment + ")";
}
catTextBuilder.append(splitText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
import static org.totschnig.myexpenses.provider.DatabaseConstants.TABLE_PLAN_INSTANCE_STATUS;
import static org.totschnig.myexpenses.provider.DatabaseConstants.VIEW_TEMPLATES_UNCOMMITTED;
import static org.totschnig.myexpenses.provider.DbConstantsKt.FULL_LABEL;
import static org.totschnig.myexpenses.provider.DbUtils.getLongOrNull;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.getLongOrNull;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.getString;

import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
Expand All @@ -71,7 +72,6 @@
import org.totschnig.myexpenses.preference.PrefKey;
import org.totschnig.myexpenses.provider.CalendarProviderProxy;
import org.totschnig.myexpenses.provider.DatabaseConstants;
import org.totschnig.myexpenses.provider.DbUtils;
import org.totschnig.myexpenses.provider.TransactionProvider;
import org.totschnig.myexpenses.service.PlanExecutor;
import org.totschnig.myexpenses.util.TextUtils;
Expand Down Expand Up @@ -321,37 +321,37 @@ public Template(Cursor c) {
boolean isTransfer = !c.isNull(c.getColumnIndexOrThrow(KEY_TRANSFER_ACCOUNT));
Long catId = getLongOrNull(c, KEY_CATID);
if (isTransfer) {
template = new Transfer(accountId, amount, DbUtils.getLongOrNull(c, KEY_TRANSFER_ACCOUNT));
template = new Transfer(accountId, amount, getLongOrNull(c, KEY_TRANSFER_ACCOUNT));
} else {
if (DatabaseConstants.SPLIT_CATID.equals(catId)) {
template = new SplitTransaction(accountId, amount);
} else {
template = new Transaction(accountId, amount);
setCatId(catId);
}
setMethodId(DbUtils.getLongOrNull(c, KEY_METHODID));
setPayee(DbUtils.getString(c, KEY_PAYEE_NAME));
setMethodLabel(DbUtils.getString(c, KEY_METHOD_LABEL));
setMethodId(getLongOrNull(c, KEY_METHODID));
setPayee(getString(c, KEY_PAYEE_NAME));
setMethodLabel(getString(c, KEY_METHOD_LABEL));
}
setId(c.getLong(c.getColumnIndexOrThrow(KEY_ROWID)));
setComment(DbUtils.getString(c, KEY_COMMENT));
setLabel(DbUtils.getString(c, KEY_LABEL));
setTitle(DbUtils.getString(c, KEY_TITLE));
planId = DbUtils.getLongOrNull(c, KEY_PLANID);
setParentId(DbUtils.getLongOrNull(c, KEY_PARENTID));
setComment(getString(c, KEY_COMMENT));
setLabel(getString(c, KEY_LABEL));
setTitle(getString(c, KEY_TITLE));
planId = getLongOrNull(c, KEY_PLANID);
setParentId(getLongOrNull(c, KEY_PARENTID));
setPlanExecutionAutomatic(c.getInt(c.getColumnIndexOrThrow(KEY_PLAN_EXECUTION)) > 0);
planExecutionAdvance = c.getInt(c.getColumnIndexOrThrow(KEY_PLAN_EXECUTION_ADVANCE));
int uuidColumnIndex = c.getColumnIndexOrThrow(KEY_UUID);
if (c.isNull(uuidColumnIndex)) {//while upgrade to DB schema 47, uuid is still null
setUuid(generateUuid());
} else {
setUuid(DbUtils.getString(c, KEY_UUID));
setUuid(getString(c, KEY_UUID));
}
setSealed(c.getInt(c.getColumnIndexOrThrow(KEY_SEALED)) > 0);
try {
defaultAction = Action.valueOf(c.getString(c.getColumnIndexOrThrow(KEY_DEFAULT_ACTION)));
} catch (IllegalArgumentException ignored) {}
setDebtId(DbUtils.getLongOrNull(c, KEY_DEBT_ID));
setDebtId(getLongOrNull(c, KEY_DEBT_ID));
}

public Template(ContentResolver contentResolver, long id, CurrencyUnit currencyUnit, int operationType, Long parentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
import static org.totschnig.myexpenses.provider.DatabaseConstants.VIEW_UNCOMMITTED;
import static org.totschnig.myexpenses.provider.DbConstantsKt.FULL_LABEL;
import static org.totschnig.myexpenses.provider.DbConstantsKt.checkSealedWithAlias;
import static org.totschnig.myexpenses.provider.DbUtils.getLongOrNull;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.getLongOrNull;
import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.getString;
import static org.totschnig.myexpenses.provider.TransactionProvider.TRANSACTIONS_TAGS_URI;
import static org.totschnig.myexpenses.provider.TransactionProvider.UNCOMMITTED_URI;
import static org.totschnig.myexpenses.util.CurrencyFormatterKt.formatMoney;
Expand All @@ -85,7 +86,6 @@
import org.totschnig.myexpenses.R;
import org.totschnig.myexpenses.db2.RepositoryPartyKt;
import org.totschnig.myexpenses.provider.DatabaseConstants;
import org.totschnig.myexpenses.provider.DbUtils;
import org.totschnig.myexpenses.provider.TransactionProvider;
import org.totschnig.myexpenses.util.AppDirHelper;
import org.totschnig.myexpenses.util.ICurrencyFormatter;
Expand Down Expand Up @@ -418,19 +418,19 @@ KEY_METHOD_LABEL, KEY_STATUS, TRANSFER_AMOUNT(VIEW_ALL), KEY_TEMPLATEID, KEY_UUI
t.setCrStatus(CrStatus.UNRECONCILED);
}
t.setMethodId(getLongOrNull(c, KEY_METHODID));
t.setMethodLabel(DbUtils.getString(c, KEY_METHOD_LABEL));
t.setMethodLabel(getString(c, KEY_METHOD_LABEL));
t.setCatId(catId);
t.setPayee(DbUtils.getString(c, KEY_PAYEE_NAME));
t.setPayee(getString(c, KEY_PAYEE_NAME));
t.setPayeeId(getLongOrNull(c, KEY_PAYEEID));
t.setDebtId(getLongOrNull(c, KEY_DEBT_ID));
t.setId(id);
final long date = c.getLong(c.getColumnIndexOrThrow(KEY_DATE));
t.setDate(date);
final Long valueDate = getLongOrNull(c, KEY_VALUE_DATE);
t.setValueDate(valueDate == null ? date : valueDate);
t.setComment(DbUtils.getString(c, KEY_COMMENT));
t.setReferenceNumber(DbUtils.getString(c, KEY_REFERENCE_NUMBER));
t.setLabel(DbUtils.getString(c, KEY_LABEL));
t.setComment(getString(c, KEY_COMMENT));
t.setReferenceNumber(getString(c, KEY_REFERENCE_NUMBER));
t.setLabel(getString(c, KEY_LABEL));

Long originalAmount = getLongOrNull(c, KEY_ORIGINAL_AMOUNT);
if (originalAmount != null) {
Expand All @@ -445,7 +445,7 @@ KEY_METHOD_LABEL, KEY_STATUS, TRANSFER_AMOUNT(VIEW_ALL), KEY_TEMPLATEID, KEY_UUI

t.status = c.getInt(c.getColumnIndexOrThrow(KEY_STATUS));
t.originTemplateId = getLongOrNull(c, KEY_TEMPLATEID);
t.setUuid(DbUtils.getString(c, KEY_UUID));
t.setUuid(getString(c, KEY_UUID));
t.setSealed(c.getInt(c.getColumnIndexOrThrow(KEY_SEALED)) > 0);
c.close();
return t;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.totschnig.myexpenses.preference;

import static org.totschnig.myexpenses.provider.MoreDbUtilsKt.requireString;

import android.content.DialogInterface;
import android.database.Cursor;
import android.database.MatrixCursor;
Expand All @@ -15,7 +17,6 @@
import org.totschnig.myexpenses.MyApplication;
import org.totschnig.myexpenses.R;
import org.totschnig.myexpenses.activity.ProtectedFragmentActivity;
import org.totschnig.myexpenses.provider.DbUtils;

public class CalendarListPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {
@Override
Expand Down Expand Up @@ -51,9 +52,9 @@ protected void onPrepareDialogBuilder(@NonNull AlertDialog.Builder builder) {
if (calCursor.getString(0).equals(value)) {
selectedIndex = calCursor.getPosition();
}
if (DbUtils.getString(calCursor, 1).equals(MyApplication.PLANNER_ACCOUNT_NAME)
&& DbUtils.getString(calCursor, 2).equals(CalendarContract.ACCOUNT_TYPE_LOCAL)
&& DbUtils.getString(calCursor, 3).equals(MyApplication.PLANNER_CALENDAR_NAME))
if (requireString(calCursor, 1).equals(MyApplication.PLANNER_ACCOUNT_NAME)
&& requireString(calCursor, 2).equals(CalendarContract.ACCOUNT_TYPE_LOCAL)
&& requireString(calCursor, 3).equals(MyApplication.PLANNER_CALENDAR_NAME))
localExists = true;
} while (calCursor.moveToNext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ abstract class BaseTransactionProvider : ContentProvider() {
)

val aggregateFunction: String
get() = if (prefHandler.getBoolean(
PrefKey.DB_SAFE_MODE,
false
)
) "total" else "sum"
get() = DbUtils.aggregateFunction(prefHandler)

fun buildAccountQuery(
minimal: Boolean,
Expand Down
Loading

0 comments on commit 5fafa59

Please sign in to comment.