Search in sources :

Example 1 with SQLServerDataTable

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

the class DateAndTimeTypeTest method testQueryTimestampTVP.

/**
 * Test query with date TVP
 */
@Test
public void testQueryTimestampTVP() throws SQLException {
    SQLServerDataTable tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.TIMESTAMP);
    tvp.addRow(TIMESTAMP_TO_TEST);
    String sPrepStmt = "select * from dateandtime where my_timestamp IN (select * from ?)";
    pstmt = connection.prepareStatement(sPrepStmt);
    ((SQLServerPreparedStatement) pstmt).setStructured(1, "timestampTVP", 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 2 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testSmallInt.

/**
 * Test with small int value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testSmallInt() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    String[] numeric = createNumericValues();
    tvp.addRow(Short.valueOf(numeric[2]));
    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.getInt(1), numeric[2]);
    // System.out.println(rs.getShort(1)); //does not work says cannot cast integer to short cause it is written as int
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 3 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testInt.

/**
 * Test a previous failure regarding to numeric precision. Issue #211
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testInt() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    tvp.addRow(12);
    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.getInt(1), 12);
        assertEquals(rs.getString(1), "" + 12);
        assertEquals(rs.getObject(1), 12);
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 4 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testFloat.

/**
 * Test with float value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testFloat() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    String[] numeric = createNumericValues();
    tvp.addRow(Float.parseFloat(numeric[1]));
    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.getFloat(1), Float.parseFloat(numeric[1]));
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 5 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testBoolean.

/**
 * Test with boolean value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testBoolean() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    String[] numeric = createNumericValues();
    tvp.addRow(Boolean.parseBoolean(numeric[0]));
    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.getBoolean(1), Boolean.parseBoolean(numeric[0]));
    }
}
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