java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java
branchv_0
changeset 144 d273d7c6dc0c
child 145 5f90decd3b59
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java	Wed Jan 08 14:33:51 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.batch;
    1.22 +
    1.23 +import info.globalcode.sql.dk.Parameter;
    1.24 +import info.globalcode.sql.dk.SQLCommand;
    1.25 +import info.globalcode.sql.dk.SQLCommandNamed;
    1.26 +import java.io.DataOutputStream;
    1.27 +import java.io.IOException;
    1.28 +import java.io.OutputStream;
    1.29 +import static info.globalcode.sql.dk.batch.BatchConstants.*;
    1.30 +import java.io.ByteArrayOutputStream;
    1.31 +import java.sql.SQLException;
    1.32 +import java.util.List;
    1.33 +
    1.34 +/**
    1.35 + *
    1.36 + * @author Ing. František Kučera (frantovo.cz)
    1.37 + */
    1.38 +public class BatchEncoder {
    1.39 +
    1.40 +	public int encode(SQLCommand sqlCommand, OutputStream out) throws BatchException {
    1.41 +		try {
    1.42 +			ByteArrayOutputStream bufferAOS = new ByteArrayOutputStream();
    1.43 +			DataOutputStream buffer = new DataOutputStream(bufferAOS);
    1.44 +
    1.45 +			buffer.write(BATCH_START);
    1.46 +
    1.47 +			byte[] sqlBytes = toBytes(sqlCommand.getQuery());
    1.48 +			buffer.writeInt(sqlBytes.length);
    1.49 +			buffer.write(sqlBytes);
    1.50 +
    1.51 +			if (sqlCommand instanceof SQLCommandNamed) {
    1.52 +				sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();
    1.53 +			}
    1.54 +
    1.55 +			List<? extends Parameter> parameters = sqlCommand.getParameters();
    1.56 +
    1.57 +			buffer.writeInt(parameters.size());
    1.58 +
    1.59 +			for (Parameter p : parameters) {
    1.60 +				buffer.writeInt(p.getType().getCode());
    1.61 +				byte[] value = toBytes((String) p.getValue()); // parameters are encoded before any preprocessing
    1.62 +				buffer.writeInt(value.length);
    1.63 +				buffer.write(value);
    1.64 +			}
    1.65 +
    1.66 +			buffer.flush();
    1.67 +			bufferAOS.writeTo(out);
    1.68 +			out.flush();
    1.69 +			return bufferAOS.size();
    1.70 +		} catch (IOException e) {
    1.71 +			throw new BatchException("Unable to write SQL command: " + sqlCommand, e);
    1.72 +		} catch (SQLException e) {
    1.73 +			throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e);
    1.74 +		}
    1.75 +	}
    1.76 +
    1.77 +	private static byte[] toBytes(String s) {
    1.78 +		if (s == null) {
    1.79 +			return new byte[]{};
    1.80 +		} else {
    1.81 +			return s.getBytes(CHARSET);
    1.82 +		}
    1.83 +	}
    1.84 +}