Search in sources :

Example 26 with SQLServerDataTable

use of com.microsoft.sqlserver.jdbc.SQLServerDataTable in project mssql-jdbc by Microsoft.

the class TVPWithSqlVariantTest method testVarChar8000.

/**
 * Test with varchar8000
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testVarChar8000() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 8000; i++) {
        buffer.append("a");
    }
    String value = buffer.toString();
    tvp.addRow(value);
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    if (null != pstmt) {
        pstmt.close();
    }
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTable);
    while (rs.next()) {
        assertEquals(rs.getString(1), value);
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 27 with SQLServerDataTable

use of com.microsoft.sqlserver.jdbc.SQLServerDataTable in project mssql-jdbc by Microsoft.

the class TVPWithSqlVariantTest method testDuplicateColumn.

/**
 * Test for allowing duplicate columns
 *
 * @throws SQLException
 */
@Test
public void testDuplicateColumn() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    tvp.addColumnMetadata("c2", microsoft.sql.Types.SQL_VARIANT);
    try {
        tvp.addColumnMetadata("c2", microsoft.sql.Types.SQL_VARIANT);
    } catch (SQLException e) {
        assertEquals(e.getMessage(), "A column name c2 already belongs to this SQLServerDataTable.");
    }
}
Also used : SQLException(java.sql.SQLException) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 28 with SQLServerDataTable

use of com.microsoft.sqlserver.jdbc.SQLServerDataTable in project mssql-jdbc by Microsoft.

the class TVPWithSqlVariantTest method testDate.

/**
 * Test with date value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testDate() throws SQLException {
    SqlDate sqlDate = new SqlDate();
    Date date = (Date) sqlDate.createdata();
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    tvp.addRow(date);
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    if (null != pstmt) {
        pstmt.close();
    }
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTable);
    while (rs.next()) {
        // TODO: GetDate has issues
        assertEquals(rs.getString(1), "" + date);
    }
}
Also used : SqlDate(com.microsoft.sqlserver.testframework.sqlType.SqlDate) Date(java.sql.Date) SqlDate(com.microsoft.sqlserver.testframework.sqlType.SqlDate) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 29 with SQLServerDataTable

use of com.microsoft.sqlserver.jdbc.SQLServerDataTable in project mssql-jdbc by Microsoft.

the class TVPWithSqlVariantTest method testLongVarChar.

/**
 * Check that we throw proper error message when inserting more than 8000
 *
 * @throws SQLException
 */
@Test
public void testLongVarChar() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 8001; i++) {
        buffer.append("a");
    }
    String value = buffer.toString();
    tvp.addRow(value);
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    try {
        pstmt.execute();
    } catch (SQLException e) {
        assertTrue(e.getMessage().contains("SQL_VARIANT does not support string values of length greater than 8000."));
    } catch (Exception e) {
        fail("Test should have failed! mistakenly inserted string value of more than 8000 in sql-variant");
    } finally {
        if (null != pstmt) {
            pstmt.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 30 with SQLServerDataTable

use of com.microsoft.sqlserver.jdbc.SQLServerDataTable in project mssql-jdbc by Microsoft.

the class TVPWithSqlVariantTest method testMoney.

/**
 * Test with money value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testMoney() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    String[] numeric = createNumericValues();
    tvp.addRow(new BigDecimal(numeric[14]));
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    if (null != pstmt) {
        pstmt.close();
    }
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTable);
    while (rs.next()) {
        assertEquals(rs.getMoney(1), new BigDecimal(numeric[14]));
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) BigDecimal(java.math.BigDecimal) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Aggregations

SQLServerDataTable (com.microsoft.sqlserver.jdbc.SQLServerDataTable)34 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)33 Test (org.junit.jupiter.api.Test)33 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)13 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)7 SQLServerResultSet (com.microsoft.sqlserver.jdbc.SQLServerResultSet)4 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)3 SQLTimeoutException (java.sql.SQLTimeoutException)2 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)1 SqlDate (com.microsoft.sqlserver.testframework.sqlType.SqlDate)1 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)1 BigDecimal (java.math.BigDecimal)1 Date (java.sql.Date)1 Random (java.util.Random)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1