Search in sources :

Example 1 with SQLServerBulkCopy

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

the class MSSqlServerDialect method flushEdgeCache.

@Override
public void flushEdgeCache(SqlgGraph sqlgGraph, Map<MetaEdge, Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>>> edgeCache) {
    Connection connection = sqlgGraph.tx().getConnection();
    try {
        for (MetaEdge metaEdge : edgeCache.keySet()) {
            SchemaTable schemaTable = metaEdge.getSchemaTable();
            Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>> triples = edgeCache.get(metaEdge);
            try {
                SQLServerConnection sqlServerConnection = connection.unwrap(SQLServerConnection.class);
                try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(sqlServerConnection)) {
                    bulkCopy.setDestinationTableName(sqlgGraph.getSqlDialect().maybeWrapInQoutes(schemaTable.getSchema()) + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(EDGE_PREFIX + schemaTable.getTable()));
                    bulkCopy.writeToServer(new SQLServerEdgeCacheBulkRecord(bulkCopy, sqlgGraph, metaEdge, schemaTable, triples));
                }
                int numberInserted = triples.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() + "." + EDGE_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 (SqlgEdge sqlgEdge : triples.getRight().keySet()) {
                        sqlgEdge.setInternalPrimaryKey(RecordId.from(schemaTable, id++));
                    }
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    } catch (Exception 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)

Example 2 with SQLServerBulkCopy

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

the class MSSqlServerDialect method flushVertexGlobalUniqueIndexes.

@Override
public void flushVertexGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> vertexCache) {
    for (SchemaTable schemaTable : vertexCache.keySet()) {
        Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices = vertexCache.get(schemaTable);
        Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(schemaTable.withPrefix(VERTEX_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 SQLServerVertexGlobalUniqueIndexBulkRecord(bulkCopy, sqlgGraph, vertices, propertyColumn));
                    }
                } 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)

Example 3 with SQLServerBulkCopy

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

the class BulkCopyAllTypes method testBulkCopyResultSet.

private void testBulkCopyResultSet(boolean setSelectMethod, Integer resultSetType, Integer resultSetConcurrency) throws SQLException {
    setupVariation();
    try (Connection connnection = DriverManager.getConnection(connectionString + (setSelectMethod ? ";selectMethod=cursor;" : ""));
        Statement statement = (null != resultSetType || null != resultSetConcurrency) ? connnection.createStatement(resultSetType, resultSetConcurrency) : connnection.createStatement()) {
        ResultSet rs = statement.executeQuery("select * from " + tableSrc.getEscapedTableName());
        SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(connection);
        bcOperation.setDestinationTableName(tableDest.getEscapedTableName());
        bcOperation.writeToServer(rs);
        bcOperation.close();
        ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc, tableDest);
    }
    terminateVariation();
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) ResultSet(java.sql.ResultSet) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)

Example 4 with SQLServerBulkCopy

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

the class BulkCopyWithSqlVariantTest method bulkCopyTestReadGUID.

/**
 * Read GUID stored in SqlVariant
 *
 * @throws SQLException
 */
@Test
public void bulkCopyTestReadGUID() throws SQLException {
    String col1Value = "1AE740A2-2272-4B0F-8086-3DDAC595BC11";
    beforeEachSetup("uniqueidentifier", "'" + col1Value + "'");
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
    SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
    bulkCopy.setDestinationTableName(destTableName);
    bulkCopy.writeToServer(rs);
    bulkCopy.close();
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
    while (rs.next()) {
        assertEquals("" + rs.getUniqueIdentifier(1), col1Value);
    }
}
Also used : SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 5 with SQLServerBulkCopy

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

the class BulkCopyWithSqlVariantTest method bulkCopyTestBit.

/**
 * test bit value
 *
 * @throws SQLException
 */
@Test
public void bulkCopyTestBit() throws SQLException {
    int col1Value = 5000;
    beforeEachSetup("bit", col1Value);
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
    SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
    bulkCopy.setDestinationTableName(destTableName);
    bulkCopy.writeToServer(rs);
    bulkCopy.close();
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
    while (rs.next()) {
        assertEquals(rs.getBoolean(1), true);
    }
}
Also used : SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)43 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)34 Test (org.junit.jupiter.api.Test)34 SQLException (java.sql.SQLException)8 ResultSet (java.sql.ResultSet)6 SQLServerException (com.microsoft.sqlserver.jdbc.SQLServerException)5 IOException (java.io.IOException)5 Connection (java.sql.Connection)5 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)4 Statement (java.sql.Statement)4 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)3 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)3 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)3 BigDecimal (java.math.BigDecimal)3 ColumnMap (com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap)1 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)1 DBTable (com.microsoft.sqlserver.testframework.DBTable)1 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1