-
Notifications
You must be signed in to change notification settings - Fork 11
Aliased Tables
Siim Kinks edited this page Mar 21, 2017
·
3 revisions
Each generated table instance can be given an alias. Aliased tables can then be used in SQL building as usual:
SQL | SqliteMagic |
---|---|
SELECT *
FROM BOOK AS 'b'
LEFT JOIN AUHTOR AS 'a'
ON a.ID = b.AUTHOR
WHERE a.LAST_NAME = 'Foo'
AND b.TITLE LIKE '%Foo%'
ORDER BY b.TITLE ASC; |
import static com.siimkinks.sqlitemagic.AuthorTable.AUTHOR;
import static com.siimkinks.sqlitemagic.BookTable.BOOK;
BookTable b = BOOK.as("b");
AuthorTable a = AUTHOR.as("a");
List<Book> books = Select
.from(b)
.leftJoin(a.on(a.ID.is(b.AUTHOR)))
.where(a.LAST_NAME.is("Foo")
.and(b.TITLE.like("%Foo%")))
.orderBy(b.TITLE.asc())
.execute();
|
As seen in the example above, calling as(String)
on the generated table instance returns an object of the same type as the table. This means that the resulting object can be used to dereference columns from the aliased table. This is quite powerful in terms of having your Java compiler check the syntax of your SQL statements. If you remove a column from a table, dereferencing that column from that table alias will cause compilation errors.