ORMLite auto-mapping of results to entity in custom query - android

I need to perform complex query with joins - seems to be too complex to use QueryBuilder, so I'll SQL my way around. I know however, that result of this query will be a list containing only a single entity type. Is there a way to leave mapping of results to ORMLite? Usually I parsed results myself, but in this case entity contains a lot of fields and I really don't want to go into parsing those from List<String[]>...

... that result of this query will be a list containing only a single entity type. Is there a way to leave mapping of results to ORMLite?
There certainly is. If you take a look at the "raw query" section of the documentation, you can see that it talks about the use of the RawRowMapper class. To quote from the docs:
You can also map the results into your own object by passing in a RawRowMapper object. This will call the mapping object with an array of strings and allow it to convert the strings into an object. The DAO provides a default RawRowMapper that can be gotten from orderDao.getRawRowMapper() that knows how to convert the string array into the object.
So for this you would call dao.queryRaw(...) with the RawRowMapper arg which maps from an array of strings. There are other dao.queryRaw(...) methods including:
queryRaw(String, DatabaseResultsMapper, String...) which allows you to map an object directly from the database results.
queryRaw(String, DataType[], RawRowObjectMapper, String...) which maps from an array of objects if you specify the result types.

Related

Firebase matching substring

I have the JSON structure above
I want to get the list of vehicles where their ids ends with "123"
I had tried to use Query.endAt() method but i'm not sure if i'm using it right or it shouldn't give the required output
Query vehiclesRef;
vehiclesRef = db.getReference("vehicles").orderByKey().endAt("\uf8ff123");
Firebase Database queries can only perform prefix matches, so strings starting with a specific string or starting with a range of string. It is not possible to query for strings ending with a specific value, nor those ending with a range of values.
If you really want to do this within Firebase, consider also storing the reversed strings in the database:
"321_1_0"
"321_3_0"
"654_52_0"
Then you can query for strings starting with 321 with
vehiclesQuery = db.getReference("vehicles").orderByKey().startAt("321").endAt("321\uf8ff");

Execute Realm Query against multiple model classes

I am using Realm database. I have an average of five models. I want to call all the models when I search. Is it possible?
ClassOne
ClassTwo
ClassThree
ClassFour
ClassFive
When querying is not one by one as follows.
RealmResult<ClassOne> list = realm.where(ClassOne.class).equelsTo("key", "a").findAll();
what kind of query I can make calls in all models?
you can't do this because every query return a RealmResuts of the type used on "Where"
The documentation says:
Returns a typed RealmQuery, which can be used to query for specific objects of this type
https://realm.io/docs/java/latest/api/io/realm/Realm.html#where-java.lang.Class-

Room; Store integer array to a separate table?

Using Room ORM, I have declared an entity EQPreset using #Entity annotation. The entity contains an array int[]. It gives following error:
error: Cannot figure out how to save this field (int[] arr) into database. You can consider adding a type converter for it.
Normally saving EQPreset instance to a database, I would create a separate table to store values of the array and have a foreign key pointing to the relevant EQPreset.
However, I need to find what will be the way of storing this int[] arr of EQPreset using Room, that is, either by making a separate table or using any good approach/way.
Option #1: Get rid of int[] arr. Have some other entity represent this integer, with a foreign key back to the EQPreset entity. Have methods on your DAO be able to give you the integer-entities for a given EQPreset entity.
Option #2: Use a #TypeConverter to convert the int[] into something that can go into a single column (e.g., convert it to and from a JSON array, represented as a string).

Querying selected fields in an entity in objectify - appengine

I am using appengine and objectify as a backend for my app. And when i query in the datastore i get a Entity object which has the data of required row. But, using objectify how will i query entities and get oly selected fileds from it? because querying the whole entity will be heavy and it needs more data bandwidth.
Eg : In a entity with 4 columns, --> Id,name,description,age. I should query oly Id,name,age. I dont want description to be queried.
The GAE datastore does not work like an RDBMS; you can't arbitrarily pick and choose which fields to query out of an entity. The standard behavior of a datastore query is to follow an index (which maps attribute value to entity key), then fetch-by-key all the entities found.
There is a feature called "projection queries" (which Objectify supports; look for the project() method on the query command object), however it is not a general purpose SELECT statement like you get in SQL. Projection queries capitalize on the fact that the index itself contains the index values, so if you only want data that's in the index, you don't need to perform a subsequent fetch of the whole Entity. However, this comes with some restrictions:
You must maintain a multiproperty index with all the data you wish to project.
You must maintain single-property indexes for each of the fields in the multiproperty index.
You can only project on queries that follow that particular index.
Queries bypass Objectify's memcache-based entity cache.
Be aware of the cost of using projection queries. In your example, you will need single-property indexes on Name and Age plus a multiproperty index on {__key__, Name, Age}. Instead of 3 write operations per entity written, your new entity will cost 8 write ops. On the other hand, the cost of a projection query is a constant 1 read op.
On the other other hand, the cost of a batch get from memcache is 0, and the worst-case cost is 1 read op. Unless your description field is known to be causing you problems, this is a massive premature optimization.
If you are looking for projection queries, they are not yet implemented in Objectify.
https://groups.google.com/forum/#!topic/objectify-appengine/uvLIHhHMEM0

Make android sql lite datatype always text

I am trying to create sqllite db for my Android application use
Is it bad habit to have all the data types as text? The reason is even though some data can be integer in nature (like number of items.. Etc) but many times I need to display the values as a string or get it as input from user. So I thought for easier manipulation I will just make the datatype in the db as text
Thoughts?
The first thing to understand here is SQLite Storage Classes. SQLite allows any data type to be stored in a table's columns, the actual data type defined by the table is just a hint to the database engine.
This means that even if you define a column as type INTEGER, you can still store text in it.
That being said, I haven't found a use case for storing arbitrary data types in a column, or a case for disregarding the defined data types. For readability purposes, it would probably be a good idea to type things properly and obey the defined types.

Categories

Resources