<commit_before>/*
 * Copyright (c) 2010 Mysema Ltd.
 * All rights reserved.
 *
 */
package com.mysema.query;

import static com.mysema.query.Constants.survey;
import static com.mysema.query.Constants.survey2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mysema.query.sql.SQLSubQuery;
import com.mysema.query.sql.dml.SQLInsertClause;
import com.mysema.query.sql.domain.QEmployee;
import com.mysema.query.sql.domain.QSurvey;
import com.mysema.testutil.ExcludeIn;

public abstract class InsertBaseTest extends AbstractBaseTest{

    private void reset() throws SQLException{
        delete(survey).execute();
        insert(survey).values(1, "Hello World").execute();
    }

    @Before
    public void setUp() throws SQLException{
        reset();
    }

    @After
    public void tearDown() throws SQLException{
        reset();
    }

    @Test
    public void Insert_With_Columns() {
        assertEquals(1, insert(survey)
                .columns(survey.id, survey.name)
                .values(3, "Hello").execute());
    }
    
    @Test
    public void Insert_Without_Columns() {
        assertEquals(1, insert(survey)
                .values(4, "Hello").execute());
            
    }
    
    @Test
    public void Insert_With_SubQuery() {
        int count = (int)query().from(survey).count();
        assertEquals(count, insert(survey)
            .columns(survey.id, survey.name)
            .select(sq().from(survey2).list(survey2.id.add(20), survey2.name))
            .execute());
    }
    
    @Test
    public void Insert_With_SubQuery_Via_Constructor() {
        int count = (int)query().from(survey).count();
        SQLInsertClause insert = insert(survey, sq().from(survey2));
        insert.set(survey.id, survey2.id.add(20));
        insert.set(survey.name, survey2.name);
        assertEquals(count, insert.execute());
    }    
    
    @Test
    public void Insert_With_SubQuery_Without_Columns() {
        int count = (int)query().from(survey).count();
        assertEquals(count, insert(survey)
            .select(sq().from(survey2).list(survey2.id.add(10), survey2.name))
            .execute());
        
    }
        
    @Test
    public void Insert_Batch(){
        SQLInsertClause insert = insert(survey)
            .set(survey.id, 5)
            .set(survey.name, "55")
            .addBatch();
        
        insert.set(survey.id, 6)
            .set(survey.name, "66")
            .addBatch();
     
        assertEquals(2, insert.execute());
        
        assertEquals(1l, query().from(survey).where(survey.name.eq("55")).count());
        assertEquals(1l, query().from(survey).where(survey.name.eq("66")).count());
    }
    
    @Test
    public void Like_with_Escape(){
        SQLInsertClause insert = insert(survey);
        insert.set(survey.id, 5).set(survey.name, "aaa").addBatch();
        insert.set(survey.id, 6).set(survey.name, "a_").addBatch();    
        insert.set(survey.id, 7).set(survey.name, "a%").addBatch();
        assertEquals(3, insert.execute());
        
        assertEquals(1l, query().from(survey).where(survey.name.like("a\\%")).count());
        assertEquals(1l, query().from(survey).where(survey.name.like("a\\_")).count());
        assertEquals(3l, query().from(survey).where(survey.name.like("a%")).count());
        assertEquals(2l, query().from(survey).where(survey.name.like("a_")).count());
        
        assertEquals(1l, query().from(survey).where(survey.name.startsWith("a_")).count());
        assertEquals(1l, query().from(survey).where(survey.name.startsWith("a%")).count());
    }
    
    @Test
    public void InsertBatch_with_Subquery(){
        SQLInsertClause insert = insert(survey)
            .columns(survey.id, survey.name)
            .select(sq().from(survey2).list(survey2.id.add(20), survey2.name))
            .addBatch();
        
        insert(survey)
            .columns(survey.id, survey.name)
            .select(sq().from(survey2).list(survey2.id.add(40), survey2.name))
            .addBatch();
        
        insert.execute();
//        assertEquals(1, insert.execute());
    }

    @Test
    @ExcludeIn({Target.HSQLDB, Target.DERBY, Target.POSTGRES})
    public void Insert_With_Keys() throws SQLException{
        ResultSet rs = insert(survey).set(survey.name, "Hello World").executeWithKeys();
        assertTrue(rs.next());
        assertTrue(rs.getObject(1) != null);
        rs.close();
    }
    
    @Test
    @ExcludeIn({Target.HSQLDB, Target.DERBY, Target.POSTGRES})
    public void Insert_With_Keys_Projected() throws SQLException{
        assertNotNull(insert(survey).set(survey.name, "Hello you").executeWithKey(survey.id));
    }

    @Test
    public void Insert_Null_With_Columns() {
        assertEquals(1, insert(survey)
                .columns(survey.id, survey.name)
                .values(3, null).execute());    
    }

    @Test
    public void Insert_Null_Without_Columns() {
        assertEquals(1, insert(survey)
                .values(4, null).execute());        
    }
    
    @Test
    public void Insert_With_Set() {
        assertEquals(1, insert(survey)
                .set(survey.id, 5)
                .set(survey.name, (String)null)
                .execute());   
    }
    
    @Test
    public void Insert_Alternative_Syntax(){
        // with columns
        assertEquals(1, insert(survey)
            .set(survey.id, 3)
            .set(survey.name, "Hello")
            .execute());
    }

    @Test
    public void Complex1(){
        // related to #584795
        QSurvey survey = new QSurvey("survey");
        QEmployee emp1 = new QEmployee("emp1");
        QEmployee emp2 = new QEmployee("emp2");
        SQLInsertClause insert = insert(survey);
        insert.columns(survey.id, survey.name);
        insert.select(new SQLSubQuery().from(survey)
          .innerJoin(emp1)
           .on(survey.id.eq(emp1.id))
          .innerJoin(emp2)
           .on(emp1.superiorId.eq(emp2.superiorId), emp1.firstname.eq(emp2.firstname))
          .list(survey.id, emp2.firstname));
        
        insert.execute();
    }
    

}
<commit_msg>added test for executeWithKey<commit_after>/*
 * Copyright (c) 2010 Mysema Ltd.
 * All rights reserved.
 *
 */
package com.mysema.query;

import static com.mysema.query.Constants.survey;
import static com.mysema.query.Constants.survey2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mysema.query.sql.SQLSubQuery;
import com.mysema.query.sql.dml.SQLInsertClause;
import com.mysema.query.sql.domain.QEmployee;
import com.mysema.query.sql.domain.QSurvey;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathImpl;
import com.mysema.testutil.ExcludeIn;

public abstract class InsertBaseTest extends AbstractBaseTest{

    private void reset() throws SQLException{
        delete(survey).execute();
        insert(survey).values(1, "Hello World").execute();
    }

    @Before
    public void setUp() throws SQLException{
        reset();
    }

    @After
    public void tearDown() throws SQLException{
        reset();
    }

    @Test
    public void Insert_With_Columns() {
        assertEquals(1, insert(survey)
                .columns(survey.id, survey.name)
                .values(3, "Hello").execute());
    }
    
    @Test
    public void Insert_Without_Columns() {
        assertEquals(1, insert(survey)
                .values(4, "Hello").execute());
            
    }
    
    @Test
    public void Insert_With_SubQuery() {
        int count = (int)query().from(survey).count();
        assertEquals(count, insert(survey)
            .columns(survey.id, survey.name)
            .select(sq().from(survey2).list(survey2.id.add(20), survey2.name))
            .execute());
    }
    
    @Test
    public void Insert_With_SubQuery_Via_Constructor() {
        int count = (int)query().from(survey).count();
        SQLInsertClause insert = insert(survey, sq().from(survey2));
        insert.set(survey.id, survey2.id.add(20));
        insert.set(survey.name, survey2.name);
        assertEquals(count, insert.execute());
    }    
    
    @Test
    public void Insert_With_SubQuery_Without_Columns() {
        int count = (int)query().from(survey).count();
        assertEquals(count, insert(survey)
            .select(sq().from(survey2).list(survey2.id.add(10), survey2.name))
            .execute());
        
    }
        
    @Test
    public void Insert_Batch(){
        SQLInsertClause insert = insert(survey)
            .set(survey.id, 5)
            .set(survey.name, "55")
            .addBatch();
        
        insert.set(survey.id, 6)
            .set(survey.name, "66")
            .addBatch();
     
        assertEquals(2, insert.execute());
        
        assertEquals(1l, query().from(survey).where(survey.name.eq("55")).count());
        assertEquals(1l, query().from(survey).where(survey.name.eq("66")).count());
    }
    
    @Test
    public void Like_with_Escape(){
        SQLInsertClause insert = insert(survey);
        insert.set(survey.id, 5).set(survey.name, "aaa").addBatch();
        insert.set(survey.id, 6).set(survey.name, "a_").addBatch();    
        insert.set(survey.id, 7).set(survey.name, "a%").addBatch();
        assertEquals(3, insert.execute());
        
        assertEquals(1l, query().from(survey).where(survey.name.like("a\\%")).count());
        assertEquals(1l, query().from(survey).where(survey.name.like("a\\_")).count());
        assertEquals(3l, query().from(survey).where(survey.name.like("a%")).count());
        assertEquals(2l, query().from(survey).where(survey.name.like("a_")).count());
        
        assertEquals(1l, query().from(survey).where(survey.name.startsWith("a_")).count());
        assertEquals(1l, query().from(survey).where(survey.name.startsWith("a%")).count());
    }
    
    @Test
    public void InsertBatch_with_Subquery(){
        SQLInsertClause insert = insert(survey)
            .columns(survey.id, survey.name)
            .select(sq().from(survey2).list(survey2.id.add(20), survey2.name))
            .addBatch();
        
        insert(survey)
            .columns(survey.id, survey.name)
            .select(sq().from(survey2).list(survey2.id.add(40), survey2.name))
            .addBatch();
        
        insert.execute();
//        assertEquals(1, insert.execute());
    }

    @Test
    @ExcludeIn({Target.HSQLDB, Target.DERBY, Target.POSTGRES})
    public void Insert_With_Keys() throws SQLException{
        ResultSet rs = insert(survey).set(survey.name, "Hello World").executeWithKeys();
        assertTrue(rs.next());
        assertTrue(rs.getObject(1) != null);
        rs.close();
    }
    
    @Test
    @ExcludeIn({Target.HSQLDB, Target.DERBY, Target.POSTGRES})
    public void Insert_With_Keys_Projected() throws SQLException{
        assertNotNull(insert(survey).set(survey.name, "Hello you").executeWithKey(survey.id));
    }
    
    @Test
    @ExcludeIn({Target.HSQLDB, Target.DERBY, Target.POSTGRES})
    public void Insert_With_Keys_Projected2() throws SQLException{
        Path<Object> idPath = new PathImpl<Object>(Object.class, "id");
        Object id = insert(survey).set(survey.name, "<NAME>").executeWithKey(idPath);
        assertNotNull(id);
    }

    @Test
    public void Insert_Null_With_Columns() {
        assertEquals(1, insert(survey)
                .columns(survey.id, survey.name)
                .values(3, null).execute());    
    }

    @Test
    public void Insert_Null_Without_Columns() {
        assertEquals(1, insert(survey)
                .values(4, null).execute());        
    }
    
    @Test
    public void Insert_With_Set() {
        assertEquals(1, insert(survey)
                .set(survey.id, 5)
                .set(survey.name, (String)null)
                .execute());   
    }
    
    @Test
    public void Insert_Alternative_Syntax(){
        // with columns
        assertEquals(1, insert(survey)
            .set(survey.id, 3)
            .set(survey.name, "Hello")
            .execute());
    }

    @Test
    public void Complex1(){
        // related to #584795
        QSurvey survey = new QSurvey("survey");
        QEmployee emp1 = new QEmployee("emp1");
        QEmployee emp2 = new QEmployee("emp2");
        SQLInsertClause insert = insert(survey);
        insert.columns(survey.id, survey.name);
        insert.select(new SQLSubQuery().from(survey)
          .innerJoin(emp1)
           .on(survey.id.eq(emp1.id))
          .innerJoin(emp2)
           .on(emp1.superiorId.eq(emp2.superiorId), emp1.firstname.eq(emp2.firstname))
          .list(survey.id, emp2.firstname));
        
        insert.execute();
    }
    

}

{'fraction_non_alphanumeric': 0.1465648854961832, 'fraction_numerical': 0.013358778625954198, 'mean_word_length': 2.417057902973396, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7659374', 'n_tokens_mistral': 4199, 'n_tokens_neox': 4126, 'n_words': 658}