@Test public void testUpdateWhereClause() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement analysis = analyze("update users set other_id=9 where name='Trillian'").nestedStatements().get(0); assertThat(analysis.whereClause().hasQuery(), is(true)); assertThat(analysis.whereClause().noMatch(), is(false)); }
@Test public void testUpdateSetExpression() throws Exception { UpdateAnalyzedStatement statement = analyze("update users set other_id=other_id+1"); UpdateAnalyzedStatement.NestedAnalyzedStatement statement1 = statement.nestedStatements().get(0); assertThat(statement1.assignments().size(), is(1)); Symbol value = statement1.assignments().entrySet().iterator().next().getValue(); assertThat(value, isFunction("add")); }
@Test public void testUpdateSetColumnToColumnValue() throws Exception { UpdateAnalyzedStatement statement = analyze("update users set name=name"); UpdateAnalyzedStatement.NestedAnalyzedStatement statement1 = statement.nestedStatements().get(0); assertThat(statement1.assignments().size(), is(1)); Symbol value = statement1.assignments().entrySet().iterator().next().getValue(); assertThat(value, isReference("name")); }
@Test public void testUpdateMuchAssignments() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement statement = analyze( "update users set other_id=9.9, name='Trillian', details=?, stuff=true, foo='bar'", new Object[] {new HashMap<String, Object>()}) .nestedStatements() .get(0); assertThat(statement.assignments().size(), is(5)); }
@Test public void testUpdateAssignmentConvertableType() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement statement = analyze("update users set other_id=9.9").nestedStatements().get(0); Reference ref = statement.assignments().keySet().iterator().next(); assertThat(ref, not(instanceOf(DynamicReference.class))); assertEquals(DataTypes.LONG, ref.info().type()); Symbol value = statement.assignments().entrySet().iterator().next().getValue(); assertThat(value, isLiteral(9L)); }
@Test public void testUpdateAssignmentNestedDynamicColumn() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement statement = analyze("update users set details['arms']=3").nestedStatements().get(0); assertThat(statement.assignments().size(), is(1)); Reference ref = statement.assignments().keySet().iterator().next(); assertThat(ref, instanceOf(DynamicReference.class)); Assert.assertEquals(DataTypes.LONG, ref.info().type()); assertThat(ref.info().ident().columnIdent().isColumn(), is(false)); assertThat(ref.info().ident().columnIdent().fqn(), is("details.arms")); }
@Test public void testUpdateWithEmptyObjectArray() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement analysis = analyze("update users set friends=? where other_id=0", new Object[] {new Map[0], 0}) .nestedStatements() .get(0); Literal friendsLiteral = (Literal) analysis .assignments() .get(new Reference(USER_TABLE_INFO.getReferenceInfo(new ColumnIdent("friends")))); assertThat(friendsLiteral.valueType().id(), is(ArrayType.ID)); assertEquals(DataTypes.OBJECT, ((ArrayType) friendsLiteral.valueType()).innerType()); assertThat(((Object[]) friendsLiteral.value()).length, is(0)); }
@Test public void testUpdateAssignments() throws Exception { UpdateAnalyzedStatement statement = analyze("update users set name='Trillian'"); UpdateAnalyzedStatement.NestedAnalyzedStatement statement1 = statement.nestedStatements().get(0); assertThat(statement1.assignments().size(), is(1)); assertThat( ((DocTableRelation) statement.sourceRelation()).tableInfo().ident(), is(new TableIdent(Schemas.DEFAULT_SCHEMA_NAME, "users"))); Reference ref = statement1.assignments().keySet().iterator().next(); assertThat(ref.info().ident().tableIdent().name(), is("users")); assertThat(ref.info().ident().columnIdent().name(), is("name")); assertTrue(statement1.assignments().containsKey(ref)); Symbol value = statement1.assignments().entrySet().iterator().next().getValue(); assertThat(value, isLiteral("Trillian")); }
@Test public void testUpdateTableAlias() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement expectedAnalysis = analyze("update users set awesome=true where awesome=false").nestedStatements().get(0); UpdateAnalyzedStatement.NestedAnalyzedStatement actualAnalysis = analyze("update users as u set awesome=true where awesome=false").nestedStatements().get(0); assertEquals(expectedAnalysis.assignments(), actualAnalysis.assignments()); assertEquals( ((Function) expectedAnalysis.whereClause().query()).arguments().get(0), ((Function) actualAnalysis.whereClause().query()).arguments().get(0)); }
@Test public void testUpdateWithParameter() throws Exception { Map[] friends = new Map[] { new HashMap<String, Object>() { { put("name", "Slartibartfast"); } }, new HashMap<String, Object>() { { put("name", "Marvin"); } } }; UpdateAnalyzedStatement.NestedAnalyzedStatement analysis = analyze( "update users set name=?, other_id=?, friends=? where id=?", new Object[] {"Jeltz", 0, friends, "9"}) .nestedStatements() .get(0); assertThat(analysis.assignments().size(), is(3)); assertThat( analysis .assignments() .get(new Reference(USER_TABLE_INFO.getReferenceInfo(new ColumnIdent("name")))), isLiteral("Jeltz")); assertThat( analysis .assignments() .get(new Reference(USER_TABLE_INFO.getReferenceInfo(new ColumnIdent("friends")))), isLiteral(friends, new ArrayType(DataTypes.OBJECT))); assertThat( analysis .assignments() .get(new Reference(USER_TABLE_INFO.getReferenceInfo(new ColumnIdent("other_id")))), isLiteral(0L)); assertThat(((Function) analysis.whereClause().query()).arguments().get(1), isLiteral(9L)); }
@Test public void testNoMatchWhereClause() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement analysis = analyze("update users set other_id=9 where true=false").nestedStatements().get(0); assertThat(analysis.whereClause().noMatch(), is(true)); }
@Test public void testNoWhereClause() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement analysis = analyze("update users set other_id=9").nestedStatements().get(0); assertThat(analysis.whereClause(), is(WhereClause.MATCH_ALL)); }