1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java Mon Mar 04 17:06:42 2019 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,83 +0,0 @@
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_HEADER);
1.46 -
1.47 - if (sqlCommand instanceof SQLCommandNamed) {
1.48 - sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();
1.49 - }
1.50 -
1.51 - writeNextString(sqlCommand.getQuery(), buffer);
1.52 -
1.53 - List<? extends Parameter> parameters = sqlCommand.getParameters();
1.54 -
1.55 - buffer.writeInt(parameters.size());
1.56 -
1.57 - for (Parameter p : parameters) {
1.58 - buffer.writeInt(p.getType().getCode());
1.59 - writeNextString((String) p.getValue(), buffer); // parameters are encoded before any preprocessing
1.60 - }
1.61 -
1.62 - buffer.flush();
1.63 - bufferAOS.writeTo(out);
1.64 - out.flush();
1.65 - return bufferAOS.size();
1.66 - } catch (IOException e) {
1.67 - throw new BatchException("Unable to write SQL command: " + sqlCommand, e);
1.68 - } catch (SQLException e) {
1.69 - throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e);
1.70 - }
1.71 - }
1.72 -
1.73 - private void writeNextString(String s, DataOutputStream out) throws IOException {
1.74 - byte[] bytes = toBytes(s);
1.75 - out.writeInt(bytes.length);
1.76 - out.write(bytes);
1.77 - }
1.78 -
1.79 - private static byte[] toBytes(String s) {
1.80 - if (s == null) {
1.81 - return new byte[]{};
1.82 - } else {
1.83 - return s.getBytes(CHARSET);
1.84 - }
1.85 - }
1.86 -}