Search in sources :

Example 21 with SQLServerConnection

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

the class WarningTest method testWarnings.

@Test
public void testWarnings() throws SQLException {
    SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
    Properties info = conn.getClientInfo();
    conn.setClientInfo(info);
    SQLWarning warn = conn.getWarnings();
    assertEquals(null, warn, "Warnings found.");
    Properties info2 = new Properties();
    String[] infoArray = { "prp1", "prp2", "prp3", "prp4", "prp5" };
    for (int i = 0; i < 5; i++) {
        info2.put(infoArray[i], "");
    }
    conn.setClientInfo(info2);
    warn = conn.getWarnings();
    for (int i = 0; i < 5; i++) {
        boolean found = false;
        List<String> list = Arrays.asList(infoArray);
        for (String word : list) {
            if (warn.toString().contains(word)) {
                found = true;
                break;
            }
        }
        assertTrue(found, "warning : '" + warn.toString() + "' not found!");
        warn = warn.getNextWarning();
        found = false;
    }
    conn.clearWarnings();
    conn.setClientInfo("prop7", "");
    warn = conn.getWarnings();
    assertTrue(warn.toString().contains("prop7"), "Warnings not found!");
    warn = warn.getNextWarning();
    assertEquals(null, warn, "Warnings found!");
}
Also used : SQLWarning(java.sql.SQLWarning) SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 22 with SQLServerConnection

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

the class RegressionTest method testSelectIntoUpdateCount.

/**
 * Tests update count returned by SELECT INTO
 *
 * @throws SQLException
 */
@Test
public void testSelectIntoUpdateCount() throws SQLException {
    SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
    // Azure does not do SELECT INTO
    if (!DBConnection.isSqlAzure(con)) {
        final String tableName = "[#SourceTableForSelectInto]";
        Statement stmt = con.createStatement();
        stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 int primary key, col2 varchar(3), col3 varchar(128))");
        stmt.executeUpdate("INSERT INTO " + tableName + " VALUES (1, 'CAN', 'Canada')");
        stmt.executeUpdate("INSERT INTO " + tableName + " VALUES (2, 'USA', 'United States of America')");
        stmt.executeUpdate("INSERT INTO " + tableName + " VALUES (3, 'JPN', 'Japan')");
        // expected values
        int numRowsToCopy = 2;
        PreparedStatement ps = con.prepareStatement("SELECT * INTO #TMPTABLE FROM " + tableName + " WHERE col1 <= ?");
        ps.setInt(1, numRowsToCopy);
        int updateCount = ps.executeUpdate();
        assertEquals(numRowsToCopy, updateCount, "Incorrect update count");
        if (null != stmt)
            stmt.close();
    }
    if (null != con)
        con.close();
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) PreparedStatement(java.sql.PreparedStatement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 23 with SQLServerConnection

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

the class RegressionTest method terminate.

@AfterAll
public static void terminate() throws SQLException {
    SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
    Statement stmt = con.createStatement();
    Utils.dropTableIfExists(tableName, stmt);
    Utils.dropProcedureIfExists(procName, stmt);
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) AfterAll(org.junit.jupiter.api.AfterAll)

Example 24 with SQLServerConnection

use of com.microsoft.sqlserver.jdbc.SQLServerConnection in project sqlg by pietermartin.

the class MSSqlServerDialect method flushEdgeGlobalUniqueIndexes.

@Override
public void flushEdgeGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<MetaEdge, Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>>> edgeCache) {
    for (MetaEdge metaEdge : edgeCache.keySet()) {
        Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>> triples = edgeCache.get(metaEdge);
        Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> edgeMap = triples.getRight();
        Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(metaEdge.getSchemaTable().withPrefix(EDGE_PREFIX));
        for (Map.Entry<String, PropertyColumn> propertyColumnEntry : propertyColumnMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumnEntry.getValue();
            for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                try {
                    Connection connection = sqlgGraph.tx().getConnection();
                    SQLServerConnection sqlServerConnection = connection.unwrap(SQLServerConnection.class);
                    try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(sqlServerConnection)) {
                        bulkCopy.setDestinationTableName(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA) + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
                        bulkCopy.writeToServer(new SQLServerEdgeGlobalUniqueIndexBulkRecord(bulkCopy, sqlgGraph, edgeMap, propertyColumn));
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) Triple(org.apache.commons.lang3.tuple.Triple) SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)

Example 25 with SQLServerConnection

use of com.microsoft.sqlserver.jdbc.SQLServerConnection in project sqlg by pietermartin.

the class MSSqlServerDialect method flushVertexCache.

@Override
public void flushVertexCache(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> vertexCache) {
    Connection connection = sqlgGraph.tx().getConnection();
    for (Map.Entry<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> entry : vertexCache.entrySet()) {
        SchemaTable schemaTable = entry.getKey();
        Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices = entry.getValue();
        if (vertices.getLeft().isEmpty()) {
            Map<String, PropertyType> columns = new HashMap<>();
            columns.put("dummy", PropertyType.from(0));
            sqlgGraph.getTopology().ensureVertexLabelPropertiesExist(schemaTable.getSchema(), schemaTable.getTable(), columns);
        }
        try {
            SQLServerConnection sqlServerConnection = connection.unwrap(SQLServerConnection.class);
            try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(sqlServerConnection)) {
                if (schemaTable.isTemporary()) {
                    bulkCopy.setDestinationTableName(sqlgGraph.getSqlDialect().maybeWrapInQoutes(sqlgGraph.getSqlDialect().temporaryTablePrefix() + VERTEX_PREFIX + schemaTable.getTable()));
                } else {
                    bulkCopy.setDestinationTableName(sqlgGraph.getSqlDialect().maybeWrapInQoutes(schemaTable.getSchema()) + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + schemaTable.getTable()));
                }
                bulkCopy.writeToServer(new SQLServerVertexCacheBulkRecord(bulkCopy, sqlgGraph, schemaTable, vertices));
            }
            int numberInserted = vertices.getRight().size();
            if (!schemaTable.isTemporary() && numberInserted > 0) {
                long endHigh;
                // If multiple threads are bulk writing to the same label then the indexes might no be in sequence.
                try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT IDENT_CURRENT('" + schemaTable.getSchema() + "." + VERTEX_PREFIX + schemaTable.getTable() + "')")) {
                    ResultSet resultSet = preparedStatement.executeQuery();
                    resultSet.next();
                    endHigh = resultSet.getLong(1);
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                // set the id on the vertex
                long id = endHigh - numberInserted + 1;
                for (SqlgVertex sqlgVertex : vertices.getRight().keySet()) {
                    sqlgVertex.setInternalPrimaryKey(RecordId.from(schemaTable, id++));
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)25 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)19 Test (org.junit.jupiter.api.Test)19 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)8 SQLException (java.sql.SQLException)8 ISQLServerConnection (com.microsoft.sqlserver.jdbc.ISQLServerConnection)7 Statement (java.sql.Statement)5 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)3 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)2 BatchUpdateException (java.sql.BatchUpdateException)2 Properties (java.util.Properties)2 SQLServerColumnEncryptionJavaKeyStoreProvider (com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider)1 SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)1 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Connection (java.sql.Connection)1 JDBCType (java.sql.JDBCType)1