/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package at.lingu.jrela;

import at.lingu.jrela.generator.SqlGenerator;
import at.lingu.jrela.generator.SqlResult;
import at.lingu.jrela.restriction.Restriction;
import static at.lingu.jrela.restriction.Restriction.and;
import static at.lingu.jrela.restriction.Restriction.or;
import at.lingu.jrela.source.SubselectAlias;
import at.lingu.jrela.source.Table;
import at.lingu.jrela.source.TableAlias;
import org.junit.Test;

/**
 *
 * @author flo
 */
public class CommonTests {

	@Test
	public void testSelect() {
		Table userTable = new Table("users");
		Table addressTable = new Table("addresses");

		TableAlias u = userTable.alias("u");
		//Table u = userTable;
		TableAlias a = addressTable.alias("a");

		Restriction isAdmin = u.column("role").eq("admin");

		Restriction isActive = u.column("active").eq(true);

		Restriction hasNoSuspension = or(
				u.column("suspension").eq(""),
				u.column("suspension").eq(null));

		SelectStatement activeUsers = u
				.leftJoin(a, u.column("id").eq(a.column("user_id")))
				.whereAny(
						isAdmin,
						and(isActive, hasNoSuspension))
				.projectFullQualified(u.column("username"))
				.project(u.column("id").as("userid"));

		activeUsers.projectFullQualified(a.column("city"));

		SqlGenerator generator = new SqlGenerator();
		SqlResult result = generator.generate(activeUsers);

		System.out.println(result.getSql());	// TODO: Make real test of result
	}

	@Test
	public void testSubselectInJoin() {
		Table users = new Table("users");

		SubselectAlias preselection = new SubselectAlias(
				getPreselectedUsers(),
				"fu");

		SelectStatement detailsQuery = users.project(
				users.column("id"),
				users.column("first_name"),
				users.column("last_name"))
				.from(users)
				.leftJoin(
						preselection,
						users.column("id").eq(preselection.column("id")));
		SqlGenerator generator = new SqlGenerator();
		SqlResult result = generator.generate(detailsQuery);

		System.out.println(result.getSql());	// TODO: Make real test of result
	}

	private SelectStatement getPreselectedUsers() {
		Table userTable = new Table("users");

		TableAlias u = userTable.alias("u");
		Restriction isAdmin = u.column("role").eq("admin");
		Restriction isActive = u.column("active").eq(true);

		SelectStatement filteredUsers = u.project(u.column("id"))
				.where(isAdmin, isActive)
				.from(u);

		return filteredUsers;
	}
}

@@ -654,16 +654,127 @@
 ests %7B%0A%0A
+%09public enum AdminFilter %7B%0A%0A%09%09ONLY_ADMIN,%0A%09%09ANY%0A%09%7D%0A%0A%09public enum ActiveUsersFilter %7B%0A%0A%09%09ONLY_ACTIVE,%0A%09%09ANY%0A%09%7D%0A%0A
 %09@Test%0A%09
@@ -1857,16 +1857,82 @@
 edUsers(
+%0A%09%09%09%09%09%09ActiveUsersFilter.ONLY_ACTIVE,%0A%09%09%09%09%09%09AdminFilter.ONLY_ADMIN
 ),%0A%09%09%09%09%22
@@ -2407,20 +2407,88 @@
 edUsers(
+%0A%09%09%09ActiveUsersFilter activeUsersFilter,%0A%09%09%09AdminFilter adminFilter
 ) %7B%0A
+%0A
 %09%09Table 
@@ -2483,37 +2483,33 @@
  %7B%0A%0A%09%09Table user
-Table
+s
  = new Table(%22us
@@ -2522,70 +2522,145 @@
 %0A%0A%09%09
-TableAlias u = userTable.alias(%22u%22);%0A%09%09Restriction isAdmin = u
+SelectStatement stmt = users.project(users.column(%22id%22)).from(users);%0A%0A%09%09if (adminFilter == AdminFilter.ONLY_ADMIN) %7B%0A%09%09%09stmt.where(users
 .col
@@ -2686,192 +2686,142 @@
 in%22)
+)
 ;%0A%09%09
-Restriction isActive = u.column(%22active%22).eq(true);%0A%0A%09%09SelectStatement filteredU
+%7D%0A%0A%09%09if (activeUsersFilter == ActiveUsersFilter.ONLY_ACTIVE) %7B%0A%09%09%09stmt.where(u
 sers
- = u.project(u.column(%22id%22))%0A%09%09%09%09.where(isAdmin, isActive)%0A%09%09%09%09.from(u);%0A%0A%09%09return filteredUsers
+.column(%22active%22).eq(true));%0A%09%09%7D%0A%0A%09%09return stmt
 ;%0A%09%7D

Improve test code