GreenDAO QueryBuilder dynamically add conditions - android

I currently have a list of userIds and I am trying to create a query to get all of those from my DB.
this is what I have in mind, I'm just not that sure that it's possible:
ArrayList<Users> listOfUsers = getCurrentUsers();
// lets assume that by now I have a list of users
QueryBuilder<Users> qb = getUsersDao().queryBuilder();
for(Users usr : listOfUsers) {
qb.where(Properties.userId.eq(usr.getUserId());
}
List result = qb.list();
I haven't seen any documentation about what is the right way of doing this and I want to know if this is the correct way of creating a dynamic query in GreenDAO.
EDIT:
I tried this and the result was a NullPointerException in the line of the declaration on the QueryBuilder

try using the IN query instead, it will run faster + you can cache your Query object.
so lets say you have
List<String> userIds;
you can get the list with:
qb.where(Properties.UserId.in(userIds))
if this is an operation that you do frequently, it is better to cache the Query. to do that, prepare the query as follows for only once:
Query<User> query = qb.where(Properties.UserId.in("?")).build();
then when you need to run it :
query.setParameter(0, userIds);
return query.list();

Related

Kotlin Firestore Queries Issue

Im using FirestorePagingAdapter in the FireStoreUI framework to create a simple query and get results into a recycler view.
I am able to fetch results and display them if I use a simple query:
var mQuery = FirebaseFirestore.getInstance().collection("test")
But in my application I have a searchView which I will be using to add queries options in mQuery eg:
var mQuery : Query = FirebaseFirestore.getInstance().collection("test")
if (!searchString.isNullOrEmpty()) {
println("SearchView: $searchString")
mQuery.whereGreaterThanOrEqualTo("name", searchString)
}
But this does not work.
Can we not add options to Query after it has been assigned?
Query objects are immutable. They can't be changed after created. Notice from the API documentation that whereGreaterThanOrEqualTo() returns a new Query object (as well as all filters) with the new condition added to it. So, you can just reassign mQuery with the new Query it built.
See also: Conditional where clause in firestore queries

Sorting Realm Results in Realm Base Adapter?

In my android app, I am using the realmBaseAdapter to dynamically display data from a realm query. I was wondering if there is any way to sort these results within the adapter so that I can group them based on certain properties.
Yes, you can :)
You can you build your RealmResults like this:
RealmResults<User> results = realm.where(User.class).findAllSorted(
"name", RealmResults.SORT_ORDER_ASCENDING);
or sort the it after build it like:
RealmResults<User> result = realm.where(User.class).findAll();
result.sort("age"); // Sort ascending
result.sort("age", RealmResults.SORT_ORDER_DESCENDING);
You can also sort by multi fields like:
RealmResults<AllTypes> results = testRealm.where(AllTypes.class)
.findAllSorted(new String[]{"name", "age"},
new boolean[]{RealmResults.SORT_ORDER_ASCENDING,
RealmResults.SORT_ORDER_ASCENDING});
See Doc of RealmQuery and RealmResults.
You don't have to sort the result every time before using it. Once the results is sorted, it will be sorted even when the database changed and results get updated.

How to get the document ID after replicating database

I am currently making a database program using couchbase on android. After I replicating the data, how can I get the document ID so I can list down all the things in database.
You need to make use of Queries!
If you would like to retrieve all the documents (or their IDs), you could use All-documents query.
Query query = database.createAllDocumentsQuery();
QueryEnumerator result = query.run();
for (Iterator<QueryRow> it = result; it.hasNext(); ) {
QueryRow row = it.next();
Log.w("MYAPP", "document ID: %s", row.getDocumentId()); // or QueryRow also has a way to get its source document.
}
You could write your own Views as per your need and later query them for results.
Also look at how to deal with document(s) in a database over here. In fact, I recommend going through entire guide.

How to retrieve data from multiple Parse.com Tables/Classes

I have two tables (Classes):
StudentInformation: with columns RollNumber, address, name, school
StudentMarks : with columns RollNumber, Marks1, Marks2, Marks3
I've been able to save the data from a single form into these two simultaneously, but not getting a clue on how to put a query while retrieving into a listview or any other view something like
'return rows (from both tables together) where roll number = 1234' / 'return rows (from both tables together) where Marks2 > 50'
I'm using Parse.com backend for Android
Kindly help
Thanks
First, the UI aspect of showing in a ListView is provided by ParseQueryAdapter. https://parse.com/docs/android_guide#ui-queryadapter
Regarding the query, I do not think you can join tables in the way you want. Instead, you could create a pointer in StudentMarks to StudentInformation.
Then you can query something like:
ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentMarks");
query.include('studentInformation'); // include the pointer to get StudentInformation
query.whereEqualTo("RollNumber", 1234);
query.whereGreaterThan("Marks2", 50);
... // perform query
In the results StudentInformation will be available like this:
List<ParseObject> objects; // the result from the query
ParseObject studentMark = objects.get(0); // example using first object
ParseObject studentInformation = studentMark.get("studentInformation");
String studentName = studentInformation.get("name");
String studentAddress = studentInformation.get("address");
... // etc
Alternatively you could also store a Relation of StudentMarks on StudentInformation, just to let you know that this is also an option, though I do not feel like it fits your current need as well as the solution presented above.

How to iterate through greendao list?

Please help, i'm kinda newbie when it comes to android app with a database.
In Greendao documentation there's a point of having this code:
List users = usersDao.queryBuilder().orderDesc(Properties.Id).list();
But somehow, it is not documented well on how to exactly get the values of rows from that query builder? or is there even a point where I could only get the first row, or last row from the database?
Thanks in advance for someone who will help.
Entity class are similar to all other classes. So greendao generates properties for all rows and you can access them as you access all others properties form "normal" classes.
To access Id property of UserEntity you can use getter user.getId().
From this query you are getting list of users. You can access it as you are accessing any other list in Java.
To get first user from database you can use code similar to:
List<User> users = usersDao.queryBuilder().orderDesc(...).limit(1).list();
if (users.size() < 1) return null;
return users.get(0);
To get last user I suggest to use similar query with reversed order:
List<User> users = usersDao.queryBuilder().orderAsc(...).limit(1).list();
if (users.size() < 1) return null;
return users.get(0);
If your query returns only one unique entity you can use unique method:
User user = usersDao.queryBuilder().where(...).unique();
You don't need to build these queries all the time. You can build it once, and then reuse it:
private Query<User> mSomeUserQuery;
...
// initialization
mSomeUserQuery = usersDao.queryBuilder().where(...).build();
// usage
User user = mSomeUserQuery.forCurrentThread().unique();
Greendao lists are not different to other lists in Java. You just have to google how to iterate through lists. Result: Ways to iterate over a List in java?
But here is the answer with the example to get the name value from an user:
for(User user : users){
String name = user.getName();
}

Categories

Resources