java/sql-dk/src/main/java/info/globalcode/sql/dk/batch/BatchEncoder.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 147 java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java@4a704c1669f4
child 250 aae5009bd0af
permissions -rw-r--r--
mavenized: sql-dk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
144
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
d273d7c6dc0c Batch: basic encoder and prepare batch
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
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.batch;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import info.globalcode.sql.dk.Parameter;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import info.globalcode.sql.dk.SQLCommand;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import info.globalcode.sql.dk.SQLCommandNamed;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.io.DataOutputStream;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.io.IOException;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.io.OutputStream;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import static info.globalcode.sql.dk.batch.BatchConstants.*;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
import java.io.ByteArrayOutputStream;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
import java.sql.SQLException;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
import java.util.List;
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
/**
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 *
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 * @author Ing. František Kučera (frantovo.cz)
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 */
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
public class BatchEncoder {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	public int encode(SQLCommand sqlCommand, OutputStream out) throws BatchException {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		try {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
			ByteArrayOutputStream bufferAOS = new ByteArrayOutputStream();
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
			DataOutputStream buffer = new DataOutputStream(bufferAOS);
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
146
4f4f515df807 BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents: 145
diff changeset
    42
			buffer.write(BATCH_HEADER);
144
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
145
5f90decd3b59 BatchEncoder: fix (encode query with numbered parameters)
František Kučera <franta-hg@frantovo.cz>
parents: 144
diff changeset
    44
			if (sqlCommand instanceof SQLCommandNamed) {
5f90decd3b59 BatchEncoder: fix (encode query with numbered parameters)
František Kučera <franta-hg@frantovo.cz>
parents: 144
diff changeset
    45
				sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();
5f90decd3b59 BatchEncoder: fix (encode query with numbered parameters)
František Kučera <franta-hg@frantovo.cz>
parents: 144
diff changeset
    46
			}
5f90decd3b59 BatchEncoder: fix (encode query with numbered parameters)
František Kučera <franta-hg@frantovo.cz>
parents: 144
diff changeset
    47
147
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    48
			writeNextString(sqlCommand.getQuery(), buffer);
144
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
			List<? extends Parameter> parameters = sqlCommand.getParameters();
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
			buffer.writeInt(parameters.size());
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			for (Parameter p : parameters) {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
				buffer.writeInt(p.getType().getCode());
147
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    56
				writeNextString((String) p.getValue(), buffer); // parameters are encoded before any preprocessing
144
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			}
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			buffer.flush();
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			bufferAOS.writeTo(out);
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			out.flush();
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			return bufferAOS.size();
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		} catch (IOException e) {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			throw new BatchException("Unable to write SQL command: " + sqlCommand, e);
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		} catch (SQLException e) {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e);
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		}
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
147
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    70
	private void writeNextString(String s, DataOutputStream out) throws IOException {
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    71
		byte[] bytes = toBytes(s);
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    72
		out.writeInt(bytes.length);
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    73
		out.write(bytes);
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    74
	}
4a704c1669f4 Improved BatchEncoder: writeNextString()
František Kučera <franta-hg@frantovo.cz>
parents: 146
diff changeset
    75
144
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	private static byte[] toBytes(String s) {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		if (s == null) {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			return new byte[]{};
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		} else {
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			return s.getBytes(CHARSET);
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		}
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	}
d273d7c6dc0c Batch: basic encoder and prepare batch
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}