/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.ignite.internal.processors.cache; import java.util.ArrayList; import java.util.Arrays; import org.apache.ignite.IgniteCache; import org.apache.ignite.cache.query.SqlFieldsQuery; import static org.apache.ignite.internal.processors.cache.IgniteCacheUpdateSqlQuerySelfTest.AllTypes; /** * */ @SuppressWarnings("unchecked") public class IgniteCacheMergeSqlQuerySelfTest extends IgniteCacheAbstractInsertSqlQuerySelfTest { /** * */ public void testMergeWithExplicitKey() { IgniteCache<String, Person> p = ignite(0).cache("S2P").withKeepBinary(); p.query(new SqlFieldsQuery("merge into Person (_key, id, firstName) values ('s', ?, ?), " + "('a', 2, 'Alex')").setArgs(1, "Sergi")); assertEquals(createPerson(1, "Sergi"), p.get("s")); assertEquals(createPerson(2, "Alex"), p.get("a")); } /** * */ public void testMergeFromSubquery() { IgniteCache p = ignite(0).cache("S2P").withKeepBinary(); p.query(new SqlFieldsQuery("merge into String (_key, _val) values ('s', ?), " + "('a', ?)").setArgs("Sergi", "Alex").setLocal(true)); assertEquals("Sergi", p.get("s")); assertEquals("Alex", p.get("a")); p.query(new SqlFieldsQuery("merge into Person(_key, id, firstName) " + "(select substring(lower(_val), 0, 2), cast(length(_val) as int), _val from String)")); assertEquals(createPerson(5, "Sergi"), p.get("se")); assertEquals(createPerson(4, "Alex"), p.get("al")); } /** * */ public void testMergeWithExplicitPrimitiveKey() { IgniteCache<Integer, Person> p = ignite(0).cache("I2P").withKeepBinary(); p.query(new SqlFieldsQuery( "merge into Person (_key, id, firstName) values (cast(? as int), ?, ?), (2, (5 - 3), 'Alex')") .setArgs("1", 1, "Sergi")); assertEquals(createPerson(1, "Sergi"), p.get(1)); assertEquals(createPerson(2, "Alex"), p.get(2)); } /** * */ public void testMergeWithDynamicKeyInstantiation() { IgniteCache<Key, Person> p = ignite(0).cache("K2P").withKeepBinary(); p.query(new SqlFieldsQuery( "merge into Person (key, id, firstName) values (1, ?, ?), (2, 2, 'Alex')").setArgs(1, "Sergi")); assertEquals(createPerson(1, "Sergi"), p.get(new Key(1))); assertEquals(createPerson(2, "Alex"), p.get(new Key(2))); } /** * */ public void testFieldsCaseSensitivity() { IgniteCache<Key2, Person> p = ignite(0).cache("K22P").withKeepBinary(); p.query(new SqlFieldsQuery("merge into \"Person2\" (\"Id\", \"id\", \"firstName\", \"IntVal\") values (1, ?, ?, 5), " + "(2, 3, 'Alex', 6)").setArgs(4, "Sergi")); assertEquals(createPerson2(4, "Sergi", 5), p.get(new Key2(1))); assertEquals(createPerson2(3, "Alex", 6), p.get(new Key2(2))); } /** * */ public void testPrimitives() { IgniteCache<Integer, Integer> p = ignite(0).cache("I2I").withKeepBinary(); p.query(new SqlFieldsQuery("merge into Integer(_key, _val) values (1, ?), " + "(?, 4)").setArgs(2, 3)); assertEquals(2, (int)p.get(1)); assertEquals(4, (int)p.get(3)); } /** * */ public void testNestedFieldsHandling() { IgniteCache<Integer, AllTypes> p = ignite(0).cache("I2AT"); p.query(new SqlFieldsQuery("merge into AllTypes(_key, innerTypeCol, arrListCol, _val, innerStrCol) " + "values (1, ?, ?, ?, 'sss')") .setArgs(new AllTypes.InnerType(50L), new ArrayList<>(Arrays.asList(3L, 2L, 1L)), new AllTypes(1L))); AllTypes res = p.get(1); AllTypes.InnerType resInner = new AllTypes.InnerType(50L); resInner.innerStrCol = "sss"; resInner.arrListCol = new ArrayList<>(Arrays.asList(3L, 2L, 1L)); assertEquals(resInner, res.innerTypeCol); } }