Skip to content

Querying Objects

Siim Kinks edited this page Mar 21, 2017 · 5 revisions

SqliteMagic ships with its own DSL (or Domain Specific Language) that emulates SQL in Java (inspired by JOOQ). The goal of the query API is to be as intuitive, as if using SQL directly. SELECT statement builder has a guiding builder API, meaning that it is impossible to call methods in the wrong order.

A more or less complete example of the "standard" SQL syntax is provided by a query like this:

SQL SqliteMagic
   SELECT *
     FROM BOOK
LEFT JOIN AUTHOR
          ON AUTHOR.ID = BOOK.AUTHOR
    WHERE BOOK.TITLE LIKE '%Foo%' 
          AND BOOK.PAGES > 200
 GROUP BY AUTHOR.FIRST_NAME,
          AUTHOR.LAST_NAME
   HAVING COUNT(*) > 42
 ORDER BY AUTHOR.LAST_NAME ASC
    LIMIT 2
   OFFSET 1;
import static com.siimkinks.sqlitemagic.AuthorTable.AUTHOR;
import static com.siimkinks.sqlitemagic.BookTable.BOOK;
import static com.siimkinks.sqlitemagic.Select.OrderingTerm.by;
import static com.siimkinks.sqlitemagic.Select.count;

List<Book> books = Select
  .from(BOOK)
  .leftJoin(AUTHOR
          .on(AUTHOR.ID.is(BOOK.AUTHOR)))
  .where(BOOK.TITLE.like("%Foo%")
          .and(BOOK.PAGES.greaterThan(200)))
  .groupBy(AUTHOR.FIRST_NAME,
           AUTHOR.LAST_NAME)
  .having(count().greaterThan(4L))
  .orderBy(AUTHOR.LAST_NAME.asc())
  .limit(2)
  .offset(1)
  .execute();

SELECT statement builder starts with any static method on com.siimkinks.sqlitemagic.Select object and must end with an "executive" method - either execute() for synchronous execution; observe() for starting point into the reactive world; or compile() method for later use.

See Next

Clone this wiki locally