java/sql-dk/src/main/java/info/globalcode/sql/dk/batch/BatchDecoder.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 148 java/sql-dk/src/info/globalcode/sql/dk/batch/BatchDecoder.java@1b2f40cd432b
child 250 aae5009bd0af
permissions -rw-r--r--
mavenized: sql-dk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
146
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.batch;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import info.globalcode.sql.dk.Parameter;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import info.globalcode.sql.dk.SQLCommand;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import info.globalcode.sql.dk.SQLCommandNumbered;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.io.DataInputStream;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.io.InputStream;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import static info.globalcode.sql.dk.batch.BatchConstants.*;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import static info.globalcode.sql.dk.Functions.toHex;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
import info.globalcode.sql.dk.SQLType;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
import java.io.IOException;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
import java.util.ArrayList;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
import java.util.Arrays;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
import java.util.List;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
/**
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 *
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
 * @author Ing. František Kučera (frantovo.cz)
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
 */
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
public class BatchDecoder {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	public Batch decode(InputStream in) throws BatchException {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		return new BatchFromStream(new DataInputStream(in));
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	private class BatchFromStream implements Batch {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		private DataInputStream in;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		private boolean hasNext;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		public BatchFromStream(DataInputStream in) throws BatchException {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			this.in = in;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
			hasNext = verifyHeader();
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		@Override
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		public boolean hasNext() throws BatchException {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
			return hasNext;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		@Override
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		public SQLCommand next() throws BatchException {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			try {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
				String sql = readNextString();
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
				int paramCount = in.readInt();
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
				List<Parameter> parameters = new ArrayList<>(paramCount);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
				for (int i = 0; i < paramCount; i++) {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
					SQLType type = SQLType.valueOf(in.readInt());
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
					String value = readNextString();
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
					parameters.add(new Parameter(value, type));
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
				}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				hasNext = verifyHeader();
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				SQLCommand sqlCommand = new SQLCommandNumbered(sql, parameters);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				return sqlCommand;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
			} catch (IOException e) {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				throw new BatchException("Unable to read batch", e);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		private String readNextString() throws IOException {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
			byte[] buffer = new byte[in.readInt()];
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			in.read(buffer);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			return new String(buffer, CHARSET);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		/**
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		 * @return true if correct batch header was found | false if EOF was found
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		 * @throws BatchException if unexpected data was found (not batch header nor EOF)
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		 */
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		private boolean verifyHeader() throws BatchException {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
			try {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
				byte[] buffer = new byte[BATCH_HEADER.length];
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
				int bytesRead = in.read(buffer);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
				if (bytesRead == BATCH_HEADER.length && Arrays.equals(buffer, BATCH_HEADER)) {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
					return true;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
				} else if (bytesRead == -1) {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
					return false;
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
				} else {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
					throw new BatchException("This is not SQL-DK batch: " + toHex(buffer));
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
				}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
			} catch (IOException e) {
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
				throw new BatchException("Unable to read batch header", e);
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
			}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
}