Skip to content

Dynamic parts

xcesco edited this page May 8, 2018 · 4 revisions

Usually, queries are defined at compile time with DAO's method annotated with @BindSqlSelect. The only thing that you can pass to queries are the parameters.

In some cases, it can be very useful to modify SQL at runtime, just to reduce the number of methods to define SQL SELECT. Kripton allows defining SQL queries specifying that a part of SELECT will be defined at runtime.

Just remember, at compile time Kripton work with JQL, at runtime with SQL.

@BindSqlSelect(jql="select * from person where id=${id} #{ DYNAMIC_WHERE } #{DYNAMIC_ORDER_BY}")
List<Person> select(long id, @BindSqlDynamicWhere String where, @BindSqlDynamicOrderBy String orderBy);

The dynamic parts can be defined with #{DYNAMIC_WHERE} and #{DYNAMIC_ORDER_BY}. The first one defines where conditions. The second one, the ORDER BY statement. It is possible to use them both in compact and explicit queries.

{
  ...  
  List<Person> list = dataSource.getPersonDAO().selectOne("nam", "name like 'name%'", "name asc");
}

You can use @BindSqlDynamicWhereParams to define arguments. In this case, if you need to pass to dynamic parts some arguments, in SQL just use ?. Remember that only String[] parameter can be marked as BindSqlDynamicWhereParams.

Security info

If you are planning to use dynamic parts, just remember that is not a good idea to take info from UI and pass them directly to SQL. This is to prevent SQL injection.

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally