]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
fix duplicate accounts in template account autocompletion
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / AccountDAO.java
index 80bab0cc93dbfa0903cfa6f9a310a724fb55349c..31aafbf200fdf9dc6a796c55666ced9816e33ee5 100644 (file)
@@ -19,6 +19,7 @@ package net.ktnx.mobileledger.dao;
 
 import androidx.annotation.NonNull;
 import androidx.lifecycle.LiveData;
+import androidx.room.ColumnInfo;
 import androidx.room.Dao;
 import androidx.room.Delete;
 import androidx.room.Insert;
@@ -27,6 +28,7 @@ import androidx.room.Update;
 
 import net.ktnx.mobileledger.db.Account;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @Dao
@@ -51,4 +53,51 @@ public abstract class AccountDAO extends BaseDAO<Account> {
 //    @Transaction
 //    @Query("SELECT * FROM patterns")
 //    List<PatternWithAccounts> getPatternsWithAccounts();
+
+    static public List<String> unbox(List<AccountNameContainer> list) {
+        ArrayList<String> result = new ArrayList<>(list.size());
+        for (AccountNameContainer item : list) {
+            result.add(item.name);
+        }
+
+        return result;
+    }
+    @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " +
+           "ORDER BY ordering, name_upper, rowid ")
+    public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(
+            @NonNull String profileUUID, @NonNull String term);
+
+    @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " +
+           "ORDER BY ordering, name_upper, rowid ")
+    public abstract List<AccountNameContainer> lookupInProfileByNameSync(
+            @NonNull String profileUUID, @NonNull String term);
+
+    @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
+    public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
+
+    @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
+    public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
+
+    static public class AccountNameContainer {
+        @ColumnInfo
+        public String name;
+        @ColumnInfo
+        public int ordering;
+    }
 }