<commit_before>/*
 * Microsoft JDBC Driver for SQL Server
 * 
 * Copyright(c) Microsoft Corporation All rights reserved.
 * 
 * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
 */
package com.microsoft.sqlserver.jdbc.tvp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBTable;
import com.microsoft.sqlserver.testframework.Utils;

@RunWith(JUnitPlatform.class)
public class TVPAllTypes extends AbstractTest {
    private static Connection conn = null;
    static Statement stmt = null;

    private static String tvpName = "TVPAllTypesTable_char_TVP";
    private static String tableNameSrc;
    private static String tableNameDest;

    /**
     * Test TVP with result set
     * 
     * @throws SQLException
     */
    @Test
    public void testTVP_RS() throws SQLException {
        testTVP_RS(false, null, null);
        testTVP_RS(true, null, null);
        testTVP_RS(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        testTVP_RS(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        testTVP_RS(false, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        testTVP_RS(false, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    }

    private void testTVP_RS(boolean setSelectMethod,
            Integer resultSetType,
            Integer resultSetConcurrency) throws SQLException {
        setupVariation();

        Connection connnection = null;
        if (setSelectMethod) {
            connnection = DriverManager.getConnection(connectionString + ";selectMethod=cursor;");
        }
        else {
            connnection = DriverManager.getConnection(connectionString);
        }

        Statement stmtement = null;
        if (null != resultSetType || null != resultSetConcurrency) {
            stmtement = connnection.createStatement(resultSetType, resultSetConcurrency);
        }
        else {
            stmtement = connnection.createStatement();
        }

        ResultSet rs = stmtement.executeQuery("select * from " + tableNameSrc);

        SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connnection
                .prepareStatement("INSERT INTO " + tableNameDest + " select * from ? ;");
        pstmt.setStructured(1, tvpName, rs);
        pstmt.execute();

        terminateVariation();
    }

    private static void dropTVPS(String tvpName) throws SQLException {
        stmt.executeUpdate("IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = '" + tvpName + "') " + " drop type " + tvpName);
    }

    private static void createTVPS(String TVPName,
            String TVPDefinition) throws SQLException {
        String TVPCreateCmd = "CREATE TYPE " + TVPName + " as table (" + TVPDefinition + ");";
        stmt.executeUpdate(TVPCreateCmd);
    }

    private void setupVariation() throws SQLException {
        conn = DriverManager.getConnection(connectionString);
        stmt = conn.createStatement();

        dropTVPS(tvpName);

        DBConnection dbConnection = new DBConnection(connectionString);
        DBStatement dbStmt = dbConnection.createStatement();

        DBTable tableSrc = new DBTable(true);
        DBTable tableDest = tableSrc.cloneSchema();
        dbStmt.createTable(tableSrc);
        dbStmt.createTable(tableDest);

        createTVPS(tvpName, tableSrc.getDefinitionOfColumns());

        dbStmt.populateTable(tableSrc);

        tableNameSrc = tableSrc.getEscapedTableName();
        tableNameDest = tableDest.getEscapedTableName();
    }

    private void terminateVariation() throws SQLException {
        conn = DriverManager.getConnection(connectionString);
        stmt = conn.createStatement();

        Utils.dropTableIfExists(tableNameSrc, stmt);
        Utils.dropTableIfExists(tableNameDest, stmt);
        dropTVPS(tvpName);
    }
}<commit_msg>added tests for stored procedure and result set<commit_after>/*
 * Microsoft JDBC Driver for SQL Server
 * 
 * Copyright(c) Microsoft Corporation All rights reserved.
 * 
 * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
 */
package com.microsoft.sqlserver.jdbc.tvp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBTable;
import com.microsoft.sqlserver.testframework.Utils;

@RunWith(JUnitPlatform.class)
public class TVPAllTypes extends AbstractTest {
    private static Connection conn = null;
    static Statement stmt = null;

    private static String tvpName = "TVPAllTypesTable_char_TVP";
    private static String procedureName = "TVPAllTypesTable_char_SP";
    private static String tableNameSrc;
    private static String tableNameDest;

    /**
     * Test TVP with result set
     * 
     * @throws SQLException
     */
    @Test
    public void testTVP_ResultSet() throws SQLException {
        testTVP_ResultSet(false, null, null);
        testTVP_ResultSet(true, null, null);
        testTVP_ResultSet(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        testTVP_ResultSet(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        testTVP_ResultSet(false, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        testTVP_ResultSet(false, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    }

    private void testTVP_ResultSet(boolean setSelectMethod,
            Integer resultSetType,
            Integer resultSetConcurrency) throws SQLException {
        setupVariation();

        Connection connnection = null;
        if (setSelectMethod) {
            connnection = DriverManager.getConnection(connectionString + ";selectMethod=cursor;");
        }
        else {
            connnection = DriverManager.getConnection(connectionString);
        }

        Statement stmtement = null;
        if (null != resultSetType || null != resultSetConcurrency) {
            stmtement = connnection.createStatement(resultSetType, resultSetConcurrency);
        }
        else {
            stmtement = connnection.createStatement();
        }

        ResultSet rs = stmtement.executeQuery("select * from " + tableNameSrc);

        SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connnection
                .prepareStatement("INSERT INTO " + tableNameDest + " select * from ? ;");
        pstmt.setStructured(1, tvpName, rs);
        pstmt.execute();

        terminateVariation();
    }

    /**
     * Test TVP with stored procedure and result set
     * 
     * @throws SQLException
     */
    @Test
    public void testTVP_StoredProcedure_ResultSet() throws SQLException {
        testTVP_StoredProcedure_ResultSet(false, null, null);
        testTVP_StoredProcedure_ResultSet(true, null, null);
        testTVP_StoredProcedure_ResultSet(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        testTVP_StoredProcedure_ResultSet(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        testTVP_StoredProcedure_ResultSet(false, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        testTVP_StoredProcedure_ResultSet(false, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    }

    private void testTVP_StoredProcedure_ResultSet(boolean setSelectMethod,
            Integer resultSetType,
            Integer resultSetConcurrency) throws SQLException {
        setupVariation();

        Connection connnection = null;
        if (setSelectMethod) {
            connnection = DriverManager.getConnection(connectionString + ";selectMethod=cursor;");
        }
        else {
            connnection = DriverManager.getConnection(connectionString);
        }

        Statement stmtement = null;
        if (null != resultSetType || null != resultSetConcurrency) {
            stmtement = connnection.createStatement(resultSetType, resultSetConcurrency);
        }
        else {
            stmtement = connnection.createStatement();
        }

        ResultSet rs = stmtement.executeQuery("select * from " + tableNameSrc);

        String sql = "{call " + procedureName + "(?)}";
        SQLServerCallableStatement Cstmt = (SQLServerCallableStatement) connnection.prepareCall(sql);
        Cstmt.setStructured(1, tvpName, rs);
        Cstmt.execute();

        terminateVariation();
    }

    private static void createPreocedure(String procedureName,
            String destTable) throws SQLException {
        String sql = "CREATE PROCEDURE " + procedureName + " @InputData " + tvpName + " READONLY " + " AS " + " BEGIN " + " INSERT INTO " + destTable
                + " SELECT * FROM @InputData" + " END";

        stmt.execute(sql);
    }

    private static void dropTVPS(String tvpName) throws SQLException {
        stmt.executeUpdate("IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = '" + tvpName + "') " + " drop type " + tvpName);
    }

    private static void createTVPS(String TVPName,
            String TVPDefinition) throws SQLException {
        String TVPCreateCmd = "CREATE TYPE " + TVPName + " as table (" + TVPDefinition + ");";
        stmt.executeUpdate(TVPCreateCmd);
    }

    private void setupVariation() throws SQLException {
        conn = DriverManager.getConnection(connectionString);
        stmt = conn.createStatement();

        Utils.dropProcedureIfExists(procedureName, stmt);
        dropTVPS(tvpName);

        DBConnection dbConnection = new DBConnection(connectionString);
        DBStatement dbStmt = dbConnection.createStatement();

        DBTable tableSrc = new DBTable(true);
        DBTable tableDest = tableSrc.cloneSchema();
        dbStmt.createTable(tableSrc);
        dbStmt.createTable(tableDest);

        createTVPS(tvpName, tableSrc.getDefinitionOfColumns());
        createPreocedure(procedureName, tableDest.getEscapedTableName());

        dbStmt.populateTable(tableSrc);

        tableNameSrc = tableSrc.getEscapedTableName();
        tableNameDest = tableDest.getEscapedTableName();
    }

    private void terminateVariation() throws SQLException {
        conn = DriverManager.getConnection(connectionString);
        stmt = conn.createStatement();

        Utils.dropProcedureIfExists(procedureName, stmt);
        Utils.dropTableIfExists(tableNameSrc, stmt);
        Utils.dropTableIfExists(tableNameDest, stmt);
        dropTVPS(tvpName);
    }
}
{'fraction_non_alphanumeric': 0.08644778204680405, 'fraction_numerical': 0.0004366049598323437, 'mean_word_length': 3.323518308795772, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7249102', 'n_tokens_mistral': 3264, 'n_tokens_neox': 3149, 'n_words': 667}