Search code examples
ignite

SqlQuery and SqlFieldsQuery


it looks that SqlQuery only supports sql that starts with select *? Doesn't it support other sql that only select some columns like select id, name from person and maps the columns to the corresponding POJO?

If I use SqlFieldQuery to run sql, the result is a QueryCursor of List(each List contains one record of the result). But if the sql starts with select *, the this list's contents would be different with field query like: select id,name,age from person

For the select *, each List is constructed with 3 parts:

  1. the first elment is the key of the cache

  2. the second element is the pojo object that contains the data

  3. the tailing element are the values for each column.

Why was it so designed? If I don't know what the sql that SqlFieldsQuery runs , then I need additional effort to figure out what the List contains.


Solution

  • SqlQuery returns key and value objects, while SqlFieldsQuery allows to select specific fields. Which one to use depends on your use case.

    Currently select * indeed includes predefined _key and _val fields, and this will be improved in the future. However, generally it's a good practice to list fields you want to fetch when running SQL queries (this is true for any SQL database, not only Ignite). This way your code will be protected from unexpected behavior in case schema is changed, for example.