Skip to content

Generated Tables

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

For each table in the database schema SqliteMagic annotation processor will generate a type safe Java object (com.siimkinks.sqlitemagic.<Your-Table-Class-Name>Table). Each generated class has static field representing a table instance. You can use these tables in SQL clauses or in other SQL statements.

Each generated table will contain all table columns as typesafe fields. These can be used in SQL statements or to create expressions.

For example, for a table class like the following:

@Table(persistAll = true)
public class Author {
  @Id
  Long id;
  String firstName;
  String lastName;
}

The annotation processor will generate a class with the following structure:

AuthorTable {
  static AUTHOR
  ID
  FIRST_NAME
  LAST_NAME
}

The generated objects can then be used to build SQL in Java, as intuitively, as if using SQL directly:

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();

For a better readability import the generated static table fields statically.

See Next

Clone this wiki locally