package net.sf.jsqlparser.test.update;
import java.io.StringReader;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.update.Update;
import static net.sf.jsqlparser.test.TestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class UpdateTest {
static CCJSqlParserManager parserManager = new CCJSqlParserManager();
@Test
public void testUpdate() throws JSQLParserException {
String statement = "UPDATE mytable set col1='as', col2=?, col3=565 Where o >= 3";
Update update = (Update) parserManager.parse(new StringReader(statement));
assertEquals("mytable", update.getTables().get(0).getName());
assertEquals(3, update.getColumns().size());
assertEquals("col1", ((Column) update.getColumns().get(0)).getColumnName());
assertEquals("col2", ((Column) update.getColumns().get(1)).getColumnName());
assertEquals("col3", ((Column) update.getColumns().get(2)).getColumnName());
assertEquals("as", ((StringValue) update.getExpressions().get(0)).getValue());
assertTrue(update.getExpressions().get(1) instanceof JdbcParameter);
assertEquals(565, ((LongValue) update.getExpressions().get(2)).getValue());
assertTrue(update.getWhere() instanceof GreaterThanEquals);
}
@Test
public void testUpdateWAlias() throws JSQLParserException {
String statement = "UPDATE table1 A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'";
Update update = (Update) parserManager.parse(new StringReader(statement));
}
@Test
public void testUpdateWithDeparser() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE table1 AS A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'");
}
@Test
public void testUpdateWithFrom() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE table1 SET columna = 5 FROM table1 LEFT JOIN table2 ON col1 = col2");
}
@Test
public void testUpdateMultiTable() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE T1, T2 SET T1.C2 = T2.C2, T2.C3 = 'UPDATED' WHERE T1.C1 = T2.C1 AND T1.C2 < 10");
}
@Test
public void testUpdateWithSelect() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE NATION SET (N_NATIONKEY) = (SELECT ? FROM SYSIBM.SYSDUMMY1)");
}
@Test
public void testUpdateWithSelect2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET (col1, col2, col3) = (SELECT a, b, c FROM mytable2)");
}
}