1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/batch/BatchDecoder.java Mon Mar 04 20:15:24 2019 +0100
1.3 @@ -0,0 +1,108 @@
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.SQLCommandNumbered;
1.26 +import java.io.DataInputStream;
1.27 +import java.io.InputStream;
1.28 +import static info.globalcode.sql.dk.batch.BatchConstants.*;
1.29 +import static info.globalcode.sql.dk.Functions.toHex;
1.30 +import info.globalcode.sql.dk.SQLType;
1.31 +import java.io.IOException;
1.32 +import java.util.ArrayList;
1.33 +import java.util.Arrays;
1.34 +import java.util.List;
1.35 +
1.36 +/**
1.37 + *
1.38 + * @author Ing. František Kučera (frantovo.cz)
1.39 + */
1.40 +public class BatchDecoder {
1.41 +
1.42 + public Batch decode(InputStream in) throws BatchException {
1.43 + return new BatchFromStream(new DataInputStream(in));
1.44 + }
1.45 +
1.46 + private class BatchFromStream implements Batch {
1.47 +
1.48 + private DataInputStream in;
1.49 + private boolean hasNext;
1.50 +
1.51 + public BatchFromStream(DataInputStream in) throws BatchException {
1.52 + this.in = in;
1.53 + hasNext = verifyHeader();
1.54 + }
1.55 +
1.56 + @Override
1.57 + public boolean hasNext() throws BatchException {
1.58 + return hasNext;
1.59 + }
1.60 +
1.61 + @Override
1.62 + public SQLCommand next() throws BatchException {
1.63 + try {
1.64 + String sql = readNextString();
1.65 +
1.66 + int paramCount = in.readInt();
1.67 + List<Parameter> parameters = new ArrayList<>(paramCount);
1.68 +
1.69 + for (int i = 0; i < paramCount; i++) {
1.70 + SQLType type = SQLType.valueOf(in.readInt());
1.71 + String value = readNextString();
1.72 + parameters.add(new Parameter(value, type));
1.73 + }
1.74 +
1.75 + hasNext = verifyHeader();
1.76 +
1.77 + SQLCommand sqlCommand = new SQLCommandNumbered(sql, parameters);
1.78 + return sqlCommand;
1.79 + } catch (IOException e) {
1.80 + throw new BatchException("Unable to read batch", e);
1.81 + }
1.82 + }
1.83 +
1.84 + private String readNextString() throws IOException {
1.85 + byte[] buffer = new byte[in.readInt()];
1.86 + in.read(buffer);
1.87 + return new String(buffer, CHARSET);
1.88 + }
1.89 +
1.90 + /**
1.91 + * @return true if correct batch header was found | false if EOF was found
1.92 + * @throws BatchException if unexpected data was found (not batch header nor EOF)
1.93 + */
1.94 + private boolean verifyHeader() throws BatchException {
1.95 + try {
1.96 + byte[] buffer = new byte[BATCH_HEADER.length];
1.97 + int bytesRead = in.read(buffer);
1.98 +
1.99 + if (bytesRead == BATCH_HEADER.length && Arrays.equals(buffer, BATCH_HEADER)) {
1.100 + return true;
1.101 + } else if (bytesRead == -1) {
1.102 + return false;
1.103 + } else {
1.104 + throw new BatchException("This is not SQL-DK batch: " + toHex(buffer));
1.105 + }
1.106 + } catch (IOException e) {
1.107 + throw new BatchException("Unable to read batch header", e);
1.108 + }
1.109 + }
1.110 + }
1.111 +}