Batch: basic encoder and prepare batch v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 08 Jan 2014 14:33:51 +0100
branchv_0
changeset 144d273d7c6dc0c
parent 143 1336bb9a4499
child 145 5f90decd3b59
Batch: basic encoder and prepare batch
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java
java/sql-dk/src/info/globalcode/sql/dk/batch/BatchConstants.java
java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java
java/sql-dk/src/info/globalcode/sql/dk/batch/BatchException.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Wed Jan 08 12:44:18 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Wed Jan 08 14:33:51 2014 +0100
     1.3 @@ -19,6 +19,8 @@
     1.4  
     1.5  import info.globalcode.sql.dk.configuration.ConfigurationProvider;
     1.6  import info.globalcode.sql.dk.CLIOptions.MODE;
     1.7 +import info.globalcode.sql.dk.batch.BatchException;
     1.8 +import info.globalcode.sql.dk.batch.BatchEncoder;
     1.9  import info.globalcode.sql.dk.configuration.Configuration;
    1.10  import info.globalcode.sql.dk.configuration.ConfigurationException;
    1.11  import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    1.12 @@ -96,6 +98,9 @@
    1.13  		} catch (FormatterException e) {
    1.14  			log.log(Level.SEVERE, "Formatting problem", e);
    1.15  			exitCode = EXIT_FORMATTING_ERROR;
    1.16 +		} catch (BatchException e) {
    1.17 +			log.log(Level.SEVERE, "Batch problem", e);
    1.18 +			exitCode = EXIT_FORMATTING_ERROR;
    1.19  		}
    1.20  
    1.21  		System.exit(exitCode);
    1.22 @@ -105,7 +110,7 @@
    1.23  		this.options = options;
    1.24  	}
    1.25  
    1.26 -	private void process() throws ConfigurationException, SQLException, FormatterException {
    1.27 +	private void process() throws ConfigurationException, SQLException, FormatterException, BatchException {
    1.28  		MODE mode = options.getMode();
    1.29  
    1.30  		/** Show info */
    1.31 @@ -147,7 +152,10 @@
    1.32  		}
    1.33  	}
    1.34  
    1.35 -	private void processPrepareBatch() {
    1.36 +	private void processPrepareBatch() throws BatchException {
    1.37 +		BatchEncoder enc = new BatchEncoder();
    1.38 +		int length = enc.encode(options.getSQLCommand(), options.getOutputStream());
    1.39 +		log.log(Level.FINE, "Prepared batch size: {0} bytes", length);
    1.40  	}
    1.41  
    1.42  	private void processExecuteBatch() {
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java	Wed Jan 08 12:44:18 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/Batch.java	Wed Jan 08 14:33:51 2014 +0100
     2.3 @@ -40,7 +40,6 @@
     2.4  
     2.5  	@Override
     2.6  	public void remove() {
     2.7 -		/** TODO: implement iterator */
     2.8 -		throw new UnsupportedOperationException("Not supported yet.");
     2.9 +		throw new UnsupportedOperationException("remove() is not supported");
    2.10  	}
    2.11  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchConstants.java	Wed Jan 08 14:33:51 2014 +0100
     3.3 @@ -0,0 +1,35 @@
     3.4 +/**
     3.5 + * SQL-DK
     3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, either version 3 of the License, or
    3.11 + * (at your option) any later version.
    3.12 + *
    3.13 + * This program is distributed in the hope that it will be useful,
    3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    3.16 + * GNU General Public License for more details.
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License
    3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    3.20 + */
    3.21 +package info.globalcode.sql.dk.batch;
    3.22 +
    3.23 +import java.nio.charset.Charset;
    3.24 +import java.nio.charset.StandardCharsets;
    3.25 +
    3.26 +/**
    3.27 + *
    3.28 + * @author Ing. František Kučera (frantovo.cz)
    3.29 + */
    3.30 +public class BatchConstants {
    3.31 +
    3.32 +	public static final Charset CHARSET = StandardCharsets.UTF_8;
    3.33 +	public static final byte VERSION = 0x01;
    3.34 +	public static final byte[] BATCH_START = {0x00, 0x53, 0x51, 0x4C, VERSION};
    3.35 +
    3.36 +	private BatchConstants() {
    3.37 +	}
    3.38 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java	Wed Jan 08 14:33:51 2014 +0100
     4.3 @@ -0,0 +1,81 @@
     4.4 +/**
     4.5 + * SQL-DK
     4.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, either version 3 of the License, or
    4.11 + * (at your option) any later version.
    4.12 + *
    4.13 + * This program is distributed in the hope that it will be useful,
    4.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    4.16 + * GNU General Public License for more details.
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License
    4.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    4.20 + */
    4.21 +package info.globalcode.sql.dk.batch;
    4.22 +
    4.23 +import info.globalcode.sql.dk.Parameter;
    4.24 +import info.globalcode.sql.dk.SQLCommand;
    4.25 +import info.globalcode.sql.dk.SQLCommandNamed;
    4.26 +import java.io.DataOutputStream;
    4.27 +import java.io.IOException;
    4.28 +import java.io.OutputStream;
    4.29 +import static info.globalcode.sql.dk.batch.BatchConstants.*;
    4.30 +import java.io.ByteArrayOutputStream;
    4.31 +import java.sql.SQLException;
    4.32 +import java.util.List;
    4.33 +
    4.34 +/**
    4.35 + *
    4.36 + * @author Ing. František Kučera (frantovo.cz)
    4.37 + */
    4.38 +public class BatchEncoder {
    4.39 +
    4.40 +	public int encode(SQLCommand sqlCommand, OutputStream out) throws BatchException {
    4.41 +		try {
    4.42 +			ByteArrayOutputStream bufferAOS = new ByteArrayOutputStream();
    4.43 +			DataOutputStream buffer = new DataOutputStream(bufferAOS);
    4.44 +
    4.45 +			buffer.write(BATCH_START);
    4.46 +
    4.47 +			byte[] sqlBytes = toBytes(sqlCommand.getQuery());
    4.48 +			buffer.writeInt(sqlBytes.length);
    4.49 +			buffer.write(sqlBytes);
    4.50 +
    4.51 +			if (sqlCommand instanceof SQLCommandNamed) {
    4.52 +				sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();
    4.53 +			}
    4.54 +
    4.55 +			List<? extends Parameter> parameters = sqlCommand.getParameters();
    4.56 +
    4.57 +			buffer.writeInt(parameters.size());
    4.58 +
    4.59 +			for (Parameter p : parameters) {
    4.60 +				buffer.writeInt(p.getType().getCode());
    4.61 +				byte[] value = toBytes((String) p.getValue()); // parameters are encoded before any preprocessing
    4.62 +				buffer.writeInt(value.length);
    4.63 +				buffer.write(value);
    4.64 +			}
    4.65 +
    4.66 +			buffer.flush();
    4.67 +			bufferAOS.writeTo(out);
    4.68 +			out.flush();
    4.69 +			return bufferAOS.size();
    4.70 +		} catch (IOException e) {
    4.71 +			throw new BatchException("Unable to write SQL command: " + sqlCommand, e);
    4.72 +		} catch (SQLException e) {
    4.73 +			throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e);
    4.74 +		}
    4.75 +	}
    4.76 +
    4.77 +	private static byte[] toBytes(String s) {
    4.78 +		if (s == null) {
    4.79 +			return new byte[]{};
    4.80 +		} else {
    4.81 +			return s.getBytes(CHARSET);
    4.82 +		}
    4.83 +	}
    4.84 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchException.java	Wed Jan 08 14:33:51 2014 +0100
     5.3 @@ -0,0 +1,42 @@
     5.4 +/**
     5.5 + * SQL-DK
     5.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, either version 3 of the License, or
    5.11 + * (at your option) any later version.
    5.12 + *
    5.13 + * This program is distributed in the hope that it will be useful,
    5.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    5.16 + * GNU General Public License for more details.
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License
    5.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    5.20 + */
    5.21 +package info.globalcode.sql.dk.batch;
    5.22 +
    5.23 +import info.globalcode.sql.dk.DKException;
    5.24 +
    5.25 +/**
    5.26 + *
    5.27 + * @author Ing. František Kučera (frantovo.cz)
    5.28 + */
    5.29 +public class BatchException extends DKException {
    5.30 +
    5.31 +	public BatchException() {
    5.32 +	}
    5.33 +
    5.34 +	public BatchException(String message) {
    5.35 +		super(message);
    5.36 +	}
    5.37 +
    5.38 +	public BatchException(Throwable cause) {
    5.39 +		super(cause);
    5.40 +	}
    5.41 +
    5.42 +	public BatchException(String message, Throwable cause) {
    5.43 +		super(message, cause);
    5.44 +	}
    5.45 +}