--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Wed Jan 08 12:44:18 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Wed Jan 08 14:33:51 2014 +0100
@@ -19,6 +19,8 @@
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
import info.globalcode.sql.dk.CLIOptions.MODE;
+import info.globalcode.sql.dk.batch.BatchException;
+import info.globalcode.sql.dk.batch.BatchEncoder;
import info.globalcode.sql.dk.configuration.Configuration;
import info.globalcode.sql.dk.configuration.ConfigurationException;
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
@@ -96,6 +98,9 @@
} catch (FormatterException e) {
log.log(Level.SEVERE, "Formatting problem", e);
exitCode = EXIT_FORMATTING_ERROR;
+ } catch (BatchException e) {
+ log.log(Level.SEVERE, "Batch problem", e);
+ exitCode = EXIT_FORMATTING_ERROR;
}
System.exit(exitCode);
@@ -105,7 +110,7 @@
this.options = options;
}
- private void process() throws ConfigurationException, SQLException, FormatterException {
+ private void process() throws ConfigurationException, SQLException, FormatterException, BatchException {
MODE mode = options.getMode();
/** Show info */
@@ -147,7 +152,10 @@
}
}
- private void processPrepareBatch() {
+ private void processPrepareBatch() throws BatchException {
+ BatchEncoder enc = new BatchEncoder();
+ int length = enc.encode(options.getSQLCommand(), options.getOutputStream());
+ log.log(Level.FINE, "Prepared batch size: {0} bytes", length);
}
private void processExecuteBatch() {
--- a/java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java Wed Jan 08 12:44:18 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java Wed Jan 08 14:33:51 2014 +0100
@@ -40,7 +40,6 @@
@Override
public void remove() {
- /** TODO: implement iterator */
- throw new UnsupportedOperationException("Not supported yet.");
+ throw new UnsupportedOperationException("remove() is not supported");
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchConstants.java Wed Jan 08 14:33:51 2014 +0100
@@ -0,0 +1,35 @@
+/**
+ * SQL-DK
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.batch;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class BatchConstants {
+
+ public static final Charset CHARSET = StandardCharsets.UTF_8;
+ public static final byte VERSION = 0x01;
+ public static final byte[] BATCH_START = {0x00, 0x53, 0x51, 0x4C, VERSION};
+
+ private BatchConstants() {
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java Wed Jan 08 14:33:51 2014 +0100
@@ -0,0 +1,81 @@
+/**
+ * SQL-DK
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.batch;
+
+import info.globalcode.sql.dk.Parameter;
+import info.globalcode.sql.dk.SQLCommand;
+import info.globalcode.sql.dk.SQLCommandNamed;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import static info.globalcode.sql.dk.batch.BatchConstants.*;
+import java.io.ByteArrayOutputStream;
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class BatchEncoder {
+
+ public int encode(SQLCommand sqlCommand, OutputStream out) throws BatchException {
+ try {
+ ByteArrayOutputStream bufferAOS = new ByteArrayOutputStream();
+ DataOutputStream buffer = new DataOutputStream(bufferAOS);
+
+ buffer.write(BATCH_START);
+
+ byte[] sqlBytes = toBytes(sqlCommand.getQuery());
+ buffer.writeInt(sqlBytes.length);
+ buffer.write(sqlBytes);
+
+ if (sqlCommand instanceof SQLCommandNamed) {
+ sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();
+ }
+
+ List<? extends Parameter> parameters = sqlCommand.getParameters();
+
+ buffer.writeInt(parameters.size());
+
+ for (Parameter p : parameters) {
+ buffer.writeInt(p.getType().getCode());
+ byte[] value = toBytes((String) p.getValue()); // parameters are encoded before any preprocessing
+ buffer.writeInt(value.length);
+ buffer.write(value);
+ }
+
+ buffer.flush();
+ bufferAOS.writeTo(out);
+ out.flush();
+ return bufferAOS.size();
+ } catch (IOException e) {
+ throw new BatchException("Unable to write SQL command: " + sqlCommand, e);
+ } catch (SQLException e) {
+ throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e);
+ }
+ }
+
+ private static byte[] toBytes(String s) {
+ if (s == null) {
+ return new byte[]{};
+ } else {
+ return s.getBytes(CHARSET);
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchException.java Wed Jan 08 14:33:51 2014 +0100
@@ -0,0 +1,42 @@
+/**
+ * SQL-DK
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.batch;
+
+import info.globalcode.sql.dk.DKException;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class BatchException extends DKException {
+
+ public BatchException() {
+ }
+
+ public BatchException(String message) {
+ super(message);
+ }
+
+ public BatchException(Throwable cause) {
+ super(cause);
+ }
+
+ public BatchException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}