changeset 206:e6dc6196f470 embedded

Move batch processing definition in order to support it properly in a embedded connection
author Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
date Fri, 02 Mar 2018 13:17:25 +0100 (2018-03-02)
parents c23218fbc2e9
children 4106c9ed7b6a
files src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java src/main/java/nl/cwi/monetdb/mcl/connection/mapi/MapiConnection.java
diffstat 3 files changed, 79 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
@@ -200,13 +200,26 @@ public abstract class MonetConnection ex
 	/**
 	 * Sends a control command to the server.
 	 *
-	 * @param commandID the command identifier according to {@link ControlCommands} listing
+	 * @param commandID The command identifier according to {@link ControlCommands} listing
 	 * @param data The integer to send according to the control command
 	 * @throws SQLException if an IO exception or a database error occurs
 	 */
 	public abstract void sendControlCommand(int commandID, int data) throws SQLException;
 
 	/**
+	 * Execute a batch of SQL query statements.
+	 *
+	 * @param statement The original MonetStatement where the batch comes from
+	 * @param batch The list of queries to execute
+	 * @param counts The return of the update statement of each input query
+	 * @param e An exception to be thrown if an error occurs
+	 * @return If all queries in the batch executed successfully or not
+	 * @throws SQLException if an IO exception or a database error occurs
+	 */
+	protected abstract boolean executeNextQueryBatch(MonetStatement statement, List<String> batch, int[] counts,
+													 BatchUpdateException e) throws SQLException;
+
+	/**
 	 * Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be
 	 * automatically released. All Statements created from this Connection will be closed when this method is called.
 	 *
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
@@ -186,43 +186,9 @@ public class MonetStatement extends Mone
 				return new int[0];
 
 			int[] counts = new int[batch.size()];
-			int offset = 0;
-			boolean first = true;
-			boolean error = false;
-
 			BatchUpdateException e = new BatchUpdateException("Error(s) occurred while executing the batch, see next SQLExceptions for details", "22000", counts);
-			int builderSize = connection.initialStringBuilderSize();
-			StringBuilder tmpBatch = new StringBuilder(builderSize);
-			String sep = connection.getLanguage().getQueryTemplateIndex(2);
-			for (int i = 0; i < batch.size(); i++) {
-				String tmp = batch.get(i);
-				if (sep.length() + tmp.length() > builderSize) {
-					// The thing is too big. Way too big. Since it won't be optimal anyway, just add it to whatever we
-					// have and continue.
-					if (!first) {
-						tmpBatch.append(sep);
-					}
-					tmpBatch.append(tmp);
-					// send and receive
-					error |= internalBatch(tmpBatch.toString(), counts, offset, i + 1, e);
-					offset = i;
-					tmpBatch.delete(0, tmpBatch.length());
-					first = true;
-					continue;
-				}
-				if (tmpBatch.length() + sep.length() + tmp.length() >= builderSize) {
-					// send and receive
-					error |= internalBatch(tmpBatch.toString(), counts, offset, i + 1, e);
-					offset = i;
-					tmpBatch.delete(0, tmpBatch.length());
-					first = true;
-				}
-				if (!first) tmpBatch.append(sep);
-				first = false;
-				tmpBatch.append(tmp);
-			}
-			// send and receive
-			error |= internalBatch(tmpBatch.toString(), counts, offset, counts.length, e);
+			MonetConnection con = (MonetConnection) this.getConnection();
+			boolean error = con.executeNextQueryBatch(this, this.batch, counts, e);
 
 			// throw BatchUpdateException if it contains something
 			if (error)
@@ -234,7 +200,18 @@ public class MonetStatement extends Mone
 		}
 	}
 
-	private boolean internalBatch(String batch, int[] counts, int offset, int max, BatchUpdateException e)
+	/**
+	 * Please don't use this method!! It is just for internal use only!!!
+	 *
+	 * @param batch The next batch to execute
+	 * @param counts The array of results of each batch
+	 * @param offset The offset in the array of results
+	 * @param max The max capacity in the array of results
+	 * @param e An exception to be thrown if an error occurs
+	 * @return If all queries in the batch executed successfully or not
+	 * @throws BatchUpdateException if an IO exception or a database error occurs
+	 */
+	public boolean internalBatch(String batch, int[] counts, int offset, int max, BatchUpdateException e)
 			throws BatchUpdateException {
 		try {
 			boolean type = internalExecute(batch);
--- a/src/main/java/nl/cwi/monetdb/mcl/connection/mapi/MapiConnection.java
+++ b/src/main/java/nl/cwi/monetdb/mcl/connection/mapi/MapiConnection.java
@@ -9,6 +9,7 @@
 package nl.cwi.monetdb.mcl.connection.mapi;
 
 import nl.cwi.monetdb.jdbc.MonetConnection;
+import nl.cwi.monetdb.jdbc.MonetStatement;
 import nl.cwi.monetdb.mcl.connection.ControlCommands;
 import nl.cwi.monetdb.mcl.connection.MCLException;
 import nl.cwi.monetdb.mcl.connection.helpers.ChannelSecurity;
@@ -22,6 +23,7 @@ import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.ByteOrder;
+import java.sql.BatchUpdateException;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.SQLNonTransientConnectionException;
@@ -252,6 +254,55 @@ public class MapiConnection extends Mone
 	}
 
 	/**
+	 * Execute a batch query in a MAPI connection.
+	 *
+	 * @param statement The original MonetStatement where the batch comes from
+	 * @param batch The list of queries to execute
+	 * @param counts The return of the update statement of each input query
+	 * @param e An exception to be thrown if an error occurs
+	 * @return If all queries in the batch executed successfully or not
+	 * @throws SQLException if an IO exception or a database error occurs
+	 */
+	@Override
+	protected boolean executeNextQueryBatch(MonetStatement statement, List<String> batch, int[] counts, BatchUpdateException e) throws SQLException {
+		int offset = 0;
+		boolean first = true, error = false;
+		int builderSize = this.initialStringBuilderSize();
+		StringBuilder tmpBatch = new StringBuilder(builderSize);
+		String sep = this.getLanguage().getQueryTemplateIndex(2);
+		for (int i = 0; i < batch.size(); i++) {
+			String tmp = batch.get(i);
+			if (sep.length() + tmp.length() > builderSize) {
+				// The thing is too big. Way too big. Since it won't be optimal anyway, just add it to whatever we
+				// have and continue.
+				if (!first) {
+					tmpBatch.append(sep);
+				}
+				tmpBatch.append(tmp);
+				// send and receive
+				error |= statement.internalBatch(tmpBatch.toString(), counts, offset, i + 1, e);
+				offset = i;
+				tmpBatch.delete(0, tmpBatch.length());
+				first = true;
+				continue;
+			}
+			if (tmpBatch.length() + sep.length() + tmp.length() >= builderSize) {
+				// send and receive
+				error |= statement.internalBatch(tmpBatch.toString(), counts, offset, i + 1, e);
+				offset = i;
+				tmpBatch.delete(0, tmpBatch.length());
+				first = true;
+			}
+			if (!first) tmpBatch.append(sep);
+			first = false;
+			tmpBatch.append(tmp);
+		}
+		// send and receive
+		error |= statement.internalBatch(tmpBatch.toString(), counts, offset, counts.length, e);
+		return error;
+	}
+
+	/**
 	 * Connects to the given host and port, logging in as the given user. If followRedirect is false, a
 	 * RedirectionException is thrown when a redirect is encountered.
 	 *