<commit_before>/*
 * Copyright 2015 Blazebit.
 *
 * Licensed 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 com.blazebit.persistence;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

import javax.persistence.EntityTransaction;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.blazebit.persistence.entity.Document;
import com.blazebit.persistence.entity.IdHolderCTE;
import com.blazebit.persistence.entity.IntIdEntity;
import com.blazebit.persistence.entity.Person;
import com.blazebit.persistence.entity.PolymorphicBase;
import com.blazebit.persistence.entity.Version;
import com.blazebit.persistence.testsuite.base.category.NoDatanucleus;
import com.blazebit.persistence.testsuite.base.category.NoEclipselink;
import com.blazebit.persistence.testsuite.base.category.NoFirebird;
import com.blazebit.persistence.testsuite.base.category.NoH2;
import com.blazebit.persistence.testsuite.base.category.NoMySQL;
import com.blazebit.persistence.testsuite.base.category.NoOpenJPA;
import com.blazebit.persistence.testsuite.base.category.NoOracle;
import com.blazebit.persistence.testsuite.base.category.NoSQLite;
import com.blazebit.persistence.tx.TxVoidWork;

/**
 *
 * @author <NAME>
 * @since 1.1.0
 */
public class UpdateTest extends AbstractCoreTest {

    Document doc1;
    Document doc2;
    Document doc3;

    @Override
    protected Class<?>[] getEntityClasses() {
        return new Class<?>[] {
            Document.class,
            Version.class,
            IntIdEntity.class,
            Person.class, 
            IdHolderCTE.class,
            PolymorphicBase.class
        };
    }
    
	@Before
	public void setUp() {
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			doc1 = new Document("D1");
			doc2 = new Document("D2");
			doc3 = new Document("D3");

			Person o1 = new Person("P1");

			doc1.setOwner(o1);
			doc2.setOwner(o1);
			doc3.setOwner(o1);

			em.persist(o1);

			em.persist(doc1);
			em.persist(doc2);
			em.persist(doc3);

			em.flush();
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new RuntimeException(e);
		}
	}

	@Test
	public void testSimple() {
		final UpdateCriteriaBuilder<Document> cb = cbf.update(em, Document.class, "d");
		cb.set("name", "NewD1");
		cb.where("name").eq("D1");
		String expected = "UPDATE Document d SET name = :param_0 WHERE d.name = :param_1";

		assertEquals(expected, cb.getQueryString());

        transactional(new TxVoidWork() {
            @Override
            public void work() {
    			int updateCount = cb.executeUpdate();
    			assertEquals(1, updateCount);
            }
        });
	}

    @Test
    public void testParameterExpression() {
        final UpdateCriteriaBuilder<Document> cb = cbf.update(em, Document.class, "d");
        cb.setExpression("name", ":newName");
        cb.where("name").eq("D1");
        cb.setParameter("newName", "NewD1");
        String expected = "UPDATE Document d SET name = :newName WHERE d.name = :param_0";

        assertEquals(expected, cb.getQueryString());

        transactional(new TxVoidWork() {
            @Override
            public void work() {
                int updateCount = cb.executeUpdate();
                assertEquals(1, updateCount);
            }
        });
    }

    // NOTE: MySQL does not like subqueries in the set
    @Test
    @Category({ NoMySQL.class })
    public void testSubquery() {
        final UpdateCriteriaBuilder<Document> cb = cbf.update(em, Document.class, "d");
        cb.set("name")
            .from(Document.class, "subD")
            .select("CONCAT('New', subD.name)")
            .where("subD.id").eqExpression("d.id")
        .end();
        cb.where("name").eq("D1");
        String expected = "UPDATE Document d SET name = (SELECT CONCAT('New',subD.name) FROM Document subD WHERE subD.id = d.id) WHERE d.name = :param_0";<commit_msg>Disable subquery update set for datanucleus<commit_after>/*
 * Copyright 2015 Blazebit.
 *
 * Licensed 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 com.blazebit.persistence;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

import javax.persistence.EntityTransaction;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.blazebit.persistence.entity.Document;
import com.blazebit.persistence.entity.IdHolderCTE;
import com.blazebit.persistence.entity.IntIdEntity;
import com.blazebit.persistence.entity.Person;
import com.blazebit.persistence.entity.PolymorphicBase;
import com.blazebit.persistence.entity.Version;
import com.blazebit.persistence.testsuite.base.category.NoDatanucleus;
import com.blazebit.persistence.testsuite.base.category.NoEclipselink;
import com.blazebit.persistence.testsuite.base.category.NoFirebird;
import com.blazebit.persistence.testsuite.base.category.NoH2;
import com.blazebit.persistence.testsuite.base.category.NoMySQL;
import com.blazebit.persistence.testsuite.base.category.NoOpenJPA;
import com.blazebit.persistence.testsuite.base.category.NoOracle;
import com.blazebit.persistence.testsuite.base.category.NoSQLite;
import com.blazebit.persistence.tx.TxVoidWork;

/**
 *
 * @author <NAME>
 * @since 1.1.0
 */
public class UpdateTest extends AbstractCoreTest {

    Document doc1;
    Document doc2;
    Document doc3;

    @Override
    protected Class<?>[] getEntityClasses() {
        return new Class<?>[] {
            Document.class,
            Version.class,
            IntIdEntity.class,
            Person.class, 
            IdHolderCTE.class,
            PolymorphicBase.class
        };
    }
    
	@Before
	public void setUp() {
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			doc1 = new Document("D1");
			doc2 = new Document("D2");
			doc3 = new Document("D3");

			Person o1 = new Person("P1");

			doc1.setOwner(o1);
			doc2.setOwner(o1);
			doc3.setOwner(o1);

			em.persist(o1);

			em.persist(doc1);
			em.persist(doc2);
			em.persist(doc3);

			em.flush();
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new RuntimeException(e);
		}
	}

	@Test
	public void testSimple() {
		final UpdateCriteriaBuilder<Document> cb = cbf.update(em, Document.class, "d");
		cb.set("name", "NewD1");
		cb.where("name").eq("D1");
		String expected = "UPDATE Document d SET name = :param_0 WHERE d.name = :param_1";

		assertEquals(expected, cb.getQueryString());

        transactional(new TxVoidWork() {
            @Override
            public void work() {
    			int updateCount = cb.executeUpdate();
    			assertEquals(1, updateCount);
            }
        });
	}

    @Test
    public void testParameterExpression() {
        final UpdateCriteriaBuilder<Document> cb = cbf.update(em, Document.class, "d");
        cb.setExpression("name", ":newName");
        cb.where("name").eq("D1");
        cb.setParameter("newName", "NewD1");
        String expected = "UPDATE Document d SET name = :newName WHERE d.name = :param_0";

        assertEquals(expected, cb.getQueryString());

        transactional(new TxVoidWork() {
            @Override
            public void work() {
                int updateCount = cb.executeUpdate();
                assertEquals(1, updateCount);
            }
        });
    }

    // NOTE: MySQL does not like subqueries in the set, Datanucleus fails with a parser exception currently
    @Test
    @Category({ NoMySQL.class, NoDatanucleus.class })
    public void testSubquery() {
        final UpdateCriteriaBuilder<Document> cb = cbf.update(em, Document.class, "d");
        cb.set("name")
            .from(Document.class, "subD")
            .select("CONCAT('New', subD.name)")
            .where("subD.id").eqExpression("d.id")
        .end();
        cb.where("name").eq("D1");
        String expected = "UPDATE Document d SET name = (SELECT CONCAT('New',subD.name) FROM Document subD WHERE subD.id = d.id) WHERE d.name = :param_0";
{'fraction_non_alphanumeric': 0.12129409218783813, 'fraction_numerical': 0.009521748539277213, 'mean_word_length': 3.1653898152320865, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5044986', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2844, 'n_words': 662}