# HG changeset patch # User František Kučera # Date 1389205132 -3600 # Node ID 4f4f515df807ce7c1fde2b7052b1eb5dd13d8058 # Parent 5f90decd3b593babee4dd2b76bed2a19270d5c59 BatchDecoder: basic decoder diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Wed Jan 08 19:18:52 2014 +0100 @@ -22,6 +22,7 @@ import info.globalcode.sql.dk.InfoLister.InfoType; import info.globalcode.sql.dk.configuration.Properties; import info.globalcode.sql.dk.configuration.Property; +import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; @@ -250,4 +251,8 @@ public OutputStream getOutputStream() { return System.out; } + + public InputStream getInputStream() { + return System.in; + } } diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Wed Jan 08 19:18:52 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.Batch; +import info.globalcode.sql.dk.batch.BatchDecoder; import info.globalcode.sql.dk.batch.BatchException; import info.globalcode.sql.dk.batch.BatchEncoder; import info.globalcode.sql.dk.configuration.Configuration; @@ -158,7 +160,18 @@ log.log(Level.FINE, "Prepared batch size: {0} bytes", length); } - private void processExecuteBatch() { + private void processExecuteBatch() throws ConfigurationException, SQLException, FormatterException, BatchException { + BatchDecoder dec = new BatchDecoder(); + Batch b = dec.decode(options.getInputStream()); + + DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName()); + FormatterDefinition fd = configuration.getFormatter(options.getFormatterName()); + try (DatabaseConnection c = dd.connect(options.getDatabaseProperties())) { + log.log(Level.FINE, "Database connected"); + try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream(), options.getFormatterProperties()))) { + c.executeBatch(b, f); + } + } } @Override diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java --- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java Wed Jan 08 19:18:52 2014 +0100 @@ -18,6 +18,7 @@ package info.globalcode.sql.dk; import info.globalcode.sql.dk.batch.Batch; +import info.globalcode.sql.dk.batch.BatchException; import info.globalcode.sql.dk.configuration.DatabaseDefinition; import info.globalcode.sql.dk.configuration.Properties; import info.globalcode.sql.dk.configuration.Property; @@ -75,7 +76,7 @@ formatter.writeEndBatch(); } - public void executeBatch(Batch batch, Formatter formatter) throws SQLException { + public void executeBatch(Batch batch, Formatter formatter) throws SQLException, BatchException { formatter.writeStartBatch(); formatter.writeStartDatabase(databaseDefinition); while (batch.hasNext()) { diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/SQLType.java --- a/java/sql-dk/src/info/globalcode/sql/dk/SQLType.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/SQLType.java Wed Jan 08 19:18:52 2014 +0100 @@ -83,7 +83,7 @@ * @return found SQLType * @throws IllegalArgumentException if no data type has given code */ - public SQLType valueOf(int code) { + public static SQLType valueOf(int code) { for (SQLType t : values()) { if (t.code == code) { return t; diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java --- a/java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java Wed Jan 08 19:18:52 2014 +0100 @@ -18,28 +18,14 @@ package info.globalcode.sql.dk.batch; import info.globalcode.sql.dk.SQLCommand; -import java.util.Iterator; /** * * @author Ing. František Kučera (frantovo.cz) */ -public class Batch implements Iterator { +public interface Batch { - @Override - public boolean hasNext() { - /** TODO: implement iterator */ - throw new UnsupportedOperationException("Not supported yet."); - } + public boolean hasNext() throws BatchException; - @Override - public SQLCommand next() { - /** TODO: implement iterator */ - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("remove() is not supported"); - } + public SQLCommand next() throws BatchException; } diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/batch/BatchConstants.java --- a/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchConstants.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchConstants.java Wed Jan 08 19:18:52 2014 +0100 @@ -28,7 +28,7 @@ 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}; + public static final byte[] BATCH_HEADER = {0x00, 0x53, 0x51, 0x4C, VERSION}; private BatchConstants() { } diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/batch/BatchDecoder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchDecoder.java Wed Jan 08 19:18:52 2014 +0100 @@ -0,0 +1,110 @@ +/** + * 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 . + */ +package info.globalcode.sql.dk.batch; + +import info.globalcode.sql.dk.Parameter; +import info.globalcode.sql.dk.SQLCommand; +import info.globalcode.sql.dk.SQLCommandNumbered; +import java.io.DataInputStream; +import java.io.InputStream; +import static info.globalcode.sql.dk.batch.BatchConstants.*; +import static info.globalcode.sql.dk.Functions.toHex; +import info.globalcode.sql.dk.SQLType; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class BatchDecoder { + + public Batch decode(InputStream in) throws BatchException { + return new BatchFromStream(new DataInputStream(in)); + + } + + private class BatchFromStream implements Batch { + + private DataInputStream in; + private boolean hasNext; + + public BatchFromStream(DataInputStream in) throws BatchException { + this.in = in; + hasNext = verifyHeader(); + } + + @Override + public boolean hasNext() throws BatchException { + return hasNext; + } + + @Override + public SQLCommand next() throws BatchException { + try { + String sql = readNextString(); + + int paramCount = in.readInt(); + List parameters = new ArrayList<>(paramCount); + + for (int i = 0; i < paramCount; i++) { + SQLType type = SQLType.valueOf(in.readInt()); + String value = readNextString(); + parameters.add(new Parameter(value, type)); + } + + hasNext = verifyHeader(); + + SQLCommand sqlCommand = new SQLCommandNumbered(sql, parameters); + return sqlCommand; + } catch (IOException e) { + throw new BatchException("Unable to read batch", e); + } + } + + private String readNextString() throws IOException { + byte[] buffer = new byte[in.readInt()]; + in.read(buffer); + return new String(buffer, CHARSET); + } + + /** + * @return true if correct batch header was found | false if EOF was found + * @throws BatchException if unexpected data was found (not batch header nor EOF) + */ + private boolean verifyHeader() throws BatchException { + try { + byte[] buffer = new byte[BATCH_HEADER.length]; + int bytesRead = in.read(buffer); + + if (bytesRead == BATCH_HEADER.length && Arrays.equals(buffer, BATCH_HEADER)) { + return true; + } else if (bytesRead == -1) { + return false; + } else { + throw new BatchException("This is not SQL-DK batch: " + toHex(buffer)); + } + } catch (IOException e) { + throw new BatchException("Unable to read batch header", e); + } + + } + } +} diff -r 5f90decd3b59 -r 4f4f515df807 java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java --- a/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java Wed Jan 08 14:50:26 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java Wed Jan 08 19:18:52 2014 +0100 @@ -39,7 +39,7 @@ ByteArrayOutputStream bufferAOS = new ByteArrayOutputStream(); DataOutputStream buffer = new DataOutputStream(bufferAOS); - buffer.write(BATCH_START); + buffer.write(BATCH_HEADER); if (sqlCommand instanceof SQLCommandNamed) { sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();