Search in sources :

Example 21 with SQLServerDataTable

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

the class TVPTypesTest method testImage.

/**
 * Test text support
 *
 * @throws SQLException
 */
@Test
public void testImage() throws SQLException {
    createTables("varbinary(max)");
    createTVPS("varbinary(max)");
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 10000; i++) buffer.append("a");
    value = buffer.toString();
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY);
    tvp.addRow(value.getBytes());
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + table + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    Connection con = DriverManager.getConnection(connectionString);
    ResultSet rs = con.createStatement().executeQuery("select * from " + table);
    while (rs.next()) assertTrue(parseByte(rs.getBytes(1), value.getBytes()));
    if (null != pstmt) {
        pstmt.close();
    }
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) SQLServerResultSet(com.microsoft.sqlserver.jdbc.SQLServerResultSet) ResultSet(java.sql.ResultSet) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 22 with SQLServerDataTable

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

the class TVPTypesTest method testTVPImageStoredProcedure.

/**
 * Image with StoredProcedure acts the same as varbinary(max)
 *
 * @throws SQLException
 */
@Test
public void testTVPImageStoredProcedure() throws SQLException {
    createTables("image");
    createTVPS("image");
    createPreocedure();
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 9000; i++) buffer.append("a");
    value = buffer.toString();
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY);
    tvp.addRow(value.getBytes());
    final String sql = "{call " + procedureName + "(?)}";
    SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql);
    P_C_statement.setStructured(1, tvpName, tvp);
    P_C_statement.execute();
    rs = stmt.executeQuery("select * from " + table);
    while (rs.next()) assertTrue(parseByte(rs.getBytes(1), value.getBytes()));
    if (null != P_C_statement) {
        P_C_statement.close();
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 23 with SQLServerDataTable

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

the class DateAndTimeTypeTest method testQueryDateTVP.

/**
 * Test query with date TVP
 */
@Test
public void testQueryDateTVP() throws SQLException {
    SQLServerDataTable tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.DATE);
    tvp.addRow(DATE_TO_TEST);
    String sPrepStmt = "select * from dateandtime where my_date IN (select * from ?)";
    pstmt = connection.prepareStatement(sPrepStmt);
    ((SQLServerPreparedStatement) pstmt).setStructured(1, "dateTVP", tvp);
    rs = pstmt.executeQuery();
    rs.next();
    assertTrue(rs.getInt(1) == 42, "did not find correct timestamp");
    rs.close();
    pstmt.close();
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 24 with SQLServerDataTable

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

the class DateAndTimeTypeTest method testQueryTimeTVP.

/**
 * Test query with date TVP
 */
@Test
public void testQueryTimeTVP() throws SQLException {
    SQLServerDataTable tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.TIME);
    tvp.addRow(TIME_TO_TEST);
    String sPrepStmt = "select * from dateandtime where my_time IN (select * from ?)";
    pstmt = connection.prepareStatement(sPrepStmt);
    ((SQLServerPreparedStatement) pstmt).setStructured(1, "timeTVP", tvp);
    rs = pstmt.executeQuery();
    rs.next();
    assertTrue(rs.getInt(1) == 42, "did not find correct timestamp");
    rs.close();
    pstmt.close();
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 25 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testNvarChar.

/**
 * Test with nvarchar
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testNvarChar() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    String colValue = "س";
    tvp.addRow(colValue);
    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), colValue);
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) 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