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);
}
}
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);
}
}
}
}
}
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();
}
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);
}
}
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);
}
}
Aggregations