package com.querydsl.sql.dml;

import static com.querydsl.sql.SQLExpressions.select;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.google.common.collect.ImmutableList;
import com.querydsl.sql.KeyAccessorsTest.QEmployee;
import com.querydsl.sql.SQLBindings;
import com.querydsl.sql.SQLTemplates;

public class SQLUpdateClauseTest {

    @Test(expected = IllegalStateException.class)
    public void noConnection() {
        QEmployee emp1 = new QEmployee("emp1");
        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1);
        update.set(emp1.id, 1);
        update.execute();
    }

    @Test
    public void getSQL() {
        QEmployee emp1 = new QEmployee("emp1");
        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1);
        update.set(emp1.id, 1);

        SQLBindings sql = update.getSQL().get(0);
        assertEquals("update EMPLOYEE\nset ID = ?", sql.getSQL());
        assertEquals(ImmutableList.of(1), sql.getBindings());
    }

    @Test
    public void intertable() {
        QEmployee emp1 = new QEmployee("emp1");
        QEmployee emp2 = new QEmployee("emp2");
        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1);
        update.set(emp1.id, 1)
              .where(emp1.id.eq(select(emp2.id).from(emp2)
                      .where(emp2.superiorId.isNotNull())));

        SQLBindings sql = update.getSQL().get(0);
        assertEquals("update EMPLOYEE\n" +
                "set ID = ?\n" +
                "where EMPLOYEE.ID = (select emp2.ID\n" +
                "from EMPLOYEE emp2\n" +
                "where emp2.SUPERIOR_ID is not null)", sql.getSQL());
    }

    @Test
    public void intertable2() {
        QEmployee emp1 = new QEmployee("emp1");
        QEmployee emp2 = new QEmployee("emp2");
        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1);
        update.set(emp1.id, select(emp2.id).from(emp2)
              .where(emp2.superiorId.isNotNull()));

        SQLBindings sql = update.getSQL().get(0);
        assertEquals("update EMPLOYEE\n" +
                "set ID = (select emp2.ID\n" +
                "from EMPLOYEE emp2\n" +
                "where emp2.SUPERIOR_ID is not null)", sql.getSQL());
    }

    @Test
    public void intertable3() {
        QEmployee emp1 = new QEmployee("emp1");
        QEmployee emp2 = new QEmployee("emp2");
        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1);
        update.set(emp1.superiorId, select(emp2.id).from(emp2)
                .where(emp2.id.eq(emp1.id)));

        SQLBindings sql = update.getSQL().get(0);
        assertEquals("update EMPLOYEE\n" +
                "set SUPERIOR_ID = (select emp2.ID\n" +
                "from EMPLOYEE emp2\n" +
                "where emp2.ID = EMPLOYEE.ID)", sql.getSQL());
    }

    @Test
    public void clear() {
        QEmployee emp1 = new QEmployee("emp1");
        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1);
        update.set(emp1.id, 1);
        update.addBatch();
        assertEquals(1, update.getBatchCount());
        update.clear();
        assertEquals(0, update.getBatchCount());
    }

}

@@ -124,16 +124,61 @@
 quals;%0A%0A
+import com.querydsl.core.QueryFlag.Position;%0A
 import o
@@ -2940,32 +2940,1099 @@
 tSQL());%0A    %7D%0A%0A
+    @Test%0A    public void testBeforeFiltersFlag() %7B%0A        QEmployee emp1 = new QEmployee(%22emp1%22);%0A        QEmployee emp2 = new QEmployee(%22emp2%22);%0A        SQLUpdateClause update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1)%0A          .set(emp1.superiorId, emp2.id)%0A          .addFlag(Position.BEFORE_FILTERS, String.format(%22%5Cnfrom %25s %25s%22, emp2.getTableName(), emp2))%0A          .where(emp2.id.eq(emp1.id));%0A%0A        SQLBindings sql = update.getSQL().get(0);%0A        assertEquals(%22update EMPLOYEE%5Cn%22 +%0A                %22set SUPERIOR_ID = emp2.ID%5Cn%22 +%0A                %22from EMPLOYEE emp2%5Cn%22 +%0A                %22where emp2.ID = EMPLOYEE.ID%22, sql.getSQL());%0A%0A        update = new SQLUpdateClause(null, SQLTemplates.DEFAULT, emp1)%0A          .set(emp1.superiorId, emp2.id)%0A          .addFlag(Position.BEFORE_FILTERS, %22 THE_FLAG%22)%0A          .where(emp2.id.eq(emp1.id));%0A%0A        sql = update.getSQL().get(0);%0A        assertEquals(%22update EMPLOYEE%5Cn%22 +%0A          %22set SUPERIOR_ID = emp2.ID THE_FLAG%5Cn%22 +%0A          %22where emp2.ID = EMPLOYEE.ID%22, sql.getSQL());%0A    %7D%0A%0A
     @Test%0A    pu

add test for SQLUpdateClause BeforeFilters Flag