
org.neo4j.jdbc.bolt.BoltStatement Maven / Gradle / Ivy
Show all versions of neo4j-jdbc-bolt Show documentation
/*
* Copyright (c) 2016 LARUS Business Automation [http://www.larus-ba.it]
*
* This file is part of the "LARUS Integration Framework for Neo4j".
*
* The "LARUS Integration Framework for Neo4j" is licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on 19/02/16
*/
package org.neo4j.jdbc.bolt;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.summary.SummaryCounters;
import org.neo4j.jdbc.InstanceFactory;
import org.neo4j.jdbc.Loggable;
import org.neo4j.jdbc.Statement;
import java.sql.BatchUpdateException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author AgileLARUS
* @since 3.0.0
*/
public class BoltStatement extends Statement implements Loggable {
private int[] rsParams;
private List batchStatements;
private boolean loggable = false;
/**
* Default Constructor
*
* @param connection The connection used for sharing the transaction between statements
* @param rsParams The params (type, concurrency and holdability) used to create a new ResultSet
*/
public BoltStatement(BoltConnection connection, int... rsParams) {
super(connection);
this.rsParams = rsParams;
this.batchStatements = new ArrayList<>();
}
private StatementResult executeInternal(String sql) throws SQLException {
this.checkClosed();
StatementResult result;
if (this.getConnection().getAutoCommit()) {
try (Transaction t = ((BoltConnection) this.getConnection()).getSession().beginTransaction()) {
result = t.run(sql);
t.success();
}
} else {
result = ((BoltConnection) this.getConnection()).getTransaction().run(sql);
}
return result;
}
//Mustn't return null
@Override public ResultSet executeQuery(String sql) throws SQLException {
try {
StatementResult result = executeInternal(sql);
BoltResultSet resultSet = new BoltResultSet(this, result, this.rsParams);
this.currentResultSet = InstanceFactory.debug(BoltResultSet.class, resultSet, this.isLoggable());
this.currentUpdateCount = -1;
return this.currentResultSet;
} catch (ClientException e) {
throw wrapException(e);
}
}
private SQLException wrapException(ClientException e) {
return new SQLException(e.neo4jErrorCode()+": "+e.getMessage(),e);
}
@Override public int executeUpdate(String sql) throws SQLException {
try {
StatementResult result = executeInternal(sql);
SummaryCounters stats = result.consume().counters();
this.currentUpdateCount = stats.nodesCreated() + stats.nodesDeleted() + stats.relationshipsCreated() + stats.relationshipsDeleted();
this.currentResultSet = null;
return this.currentUpdateCount;
} catch (ClientException e) {
throw wrapException(e);
}
}
@Override public boolean execute(String sql) throws SQLException {
try {
boolean result = false;
if (sql.contains("DELETE") || sql.contains("MERGE") || sql.contains("CREATE") || sql.contains("delete") || sql.contains("merge") || sql
.contains("create")) {
this.executeUpdate(sql);
} else {
this.executeQuery(sql);
result = true;
}
return result;
} catch (ClientException e) {
throw wrapException(e);
}
}
@Override public int getResultSetConcurrency() throws SQLException {
this.checkClosed();
if (currentResultSet != null) {
return currentResultSet.getConcurrency();
}
if (this.rsParams.length > 1) {
return this.rsParams[1];
}
return BoltResultSet.DEFAULT_CONCURRENCY;
}
@Override public int getResultSetType() throws SQLException {
this.checkClosed();
if (currentResultSet != null) {
return currentResultSet.getType();
}
if (this.rsParams.length > 0) {
return this.rsParams[0];
}
return BoltResultSet.DEFAULT_TYPE;
}
@Override public int getResultSetHoldability() throws SQLException {
this.checkClosed();
if (currentResultSet != null) {
return currentResultSet.getHoldability();
}
if (this.rsParams.length > 2) {
return this.rsParams[2];
}
return BoltResultSet.DEFAULT_HOLDABILITY;
}
/*-------------------*/
/* Batch */
/*-------------------*/
@Override public void addBatch(String sql) throws SQLException {
this.checkClosed();
this.batchStatements.add(sql);
}
@Override public void clearBatch() throws SQLException {
this.checkClosed();
this.batchStatements.clear();
}
@Override public int[] executeBatch() throws SQLException {
this.checkClosed();
int[] result = new int[0];
try {
for (String query : this.batchStatements) {
StatementResult res;
if (this.connection.getAutoCommit()) {
res = ((BoltConnection) connection).getSession().run(query);
} else {
res = ((BoltConnection) connection).getTransaction().run(query);
}
SummaryCounters count = res.consume().counters();
result = Arrays.copyOf(result, result.length + 1);
result[result.length - 1] = count.nodesCreated() + count.nodesDeleted();
}
} catch (Exception e) {
throw new BatchUpdateException(result, e);
}
return result;
}
/*--------------------*/
/* Logger */
/*--------------------*/
@Override public boolean isLoggable() {
return this.loggable;
}
@Override public void setLoggable(boolean loggable) {
this.loggable = loggable;
}
}