Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort the filtered transactions by timestamp. #147

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/src/main/java/org/gnucash/android/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.model.Commodity;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -353,4 +355,24 @@ private void createDatabaseTables(SQLiteDatabase db) {
throw new RuntimeException(e);
}
}

/**
* Escape the given argument for use in a {@code LIKE} statement.
* @hide
*/
public static String escapeForLike(@NonNull String arg) {
// Shamelessly borrowed from com.android.providers.media.util.DatabaseUtils
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < arg.length(); i++) {
final char c = arg.charAt(i);
switch (c) {
case '%': sb.append('\\');
break;
case '_': sb.append('\\');
break;
}
sb.append(c);
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package org.gnucash.android.db.adapter;

import static org.gnucash.android.db.DatabaseHelper.escapeForLike;
import static org.gnucash.android.db.DatabaseSchema.AccountEntry;
import static org.gnucash.android.db.DatabaseSchema.ScheduledActionEntry;
import static org.gnucash.android.db.DatabaseSchema.SplitEntry;
import static org.gnucash.android.db.DatabaseSchema.TransactionEntry;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
Expand Down Expand Up @@ -555,13 +557,17 @@ public Cursor fetchTransactionSuggestions(String prefix, String accountUID) {
String[] projectionIn = new String[]{TransactionEntry.TABLE_NAME + ".*"};
String selection = "(" + SplitEntry.TABLE_NAME + "." + SplitEntry.COLUMN_ACCOUNT_UID + " = ?"
+ " OR " + TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_TEMPLATE + "=1 )"
+ " AND " + TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_DESCRIPTION + " LIKE '" + prefix + "%'";
+ " AND " + TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_DESCRIPTION + " LIKE '" + escapeForLike(prefix) + "%'";
String[] selectionArgs = new String[]{accountUID};
String sortOrder = TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_TIMESTAMP + " DESC";
String subquery = queryBuilder.buildQuery(projectionIn, selection, null, null, sortOrder, null);

// Need to use inner subquery because ORDER BY must be before GROUP BY!
SQLiteQueryBuilder queryBuilder2 = new SQLiteQueryBuilder();
queryBuilder2.setTables("(" + subquery + ")");
String groupBy = TransactionEntry.COLUMN_DESCRIPTION;
String limit = Integer.toString(5);

return queryBuilder.query(mDb, projectionIn, selection, selectionArgs, groupBy, null, sortOrder, limit);
return queryBuilder2.query(mDb, null, null, selectionArgs, groupBy, null, null, limit);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.gnucash.android.ui.account;

import static org.gnucash.android.db.DatabaseHelper.escapeForLike;
import static org.gnucash.android.util.ColorExtKt.parseColor;

import android.app.Activity;
Expand Down Expand Up @@ -461,7 +462,7 @@ public Cursor loadInBackground() {
if (mFilter != null) {
cursor = adapter
.fetchAccounts(DatabaseSchema.AccountEntry.COLUMN_HIDDEN + "= 0 AND "
+ DatabaseSchema.AccountEntry.COLUMN_NAME + " LIKE '%" + mFilter + "%'",
+ DatabaseSchema.AccountEntry.COLUMN_NAME + " LIKE '%" + escapeForLike(mFilter) + "%'",
null, null);
} else if (!TextUtils.isEmpty(mParentAccountUID))
cursor = adapter.fetchSubAccounts(mParentAccountUID);
Expand Down