franta-hg@144: /** franta-hg@144: * SQL-DK franta-hg@144: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@144: * franta-hg@144: * This program is free software: you can redistribute it and/or modify franta-hg@144: * it under the terms of the GNU General Public License as published by franta-hg@250: * the Free Software Foundation, version 3 of the License. franta-hg@144: * franta-hg@144: * This program is distributed in the hope that it will be useful, franta-hg@144: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@144: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@144: * GNU General Public License for more details. franta-hg@144: * franta-hg@144: * You should have received a copy of the GNU General Public License franta-hg@144: * along with this program. If not, see . franta-hg@144: */ franta-hg@144: package info.globalcode.sql.dk.batch; franta-hg@144: franta-hg@144: import info.globalcode.sql.dk.Parameter; franta-hg@144: import info.globalcode.sql.dk.SQLCommand; franta-hg@144: import info.globalcode.sql.dk.SQLCommandNamed; franta-hg@144: import java.io.DataOutputStream; franta-hg@144: import java.io.IOException; franta-hg@144: import java.io.OutputStream; franta-hg@144: import static info.globalcode.sql.dk.batch.BatchConstants.*; franta-hg@144: import java.io.ByteArrayOutputStream; franta-hg@144: import java.sql.SQLException; franta-hg@144: import java.util.List; franta-hg@144: franta-hg@144: /** franta-hg@144: * franta-hg@144: * @author Ing. František Kučera (frantovo.cz) franta-hg@144: */ franta-hg@144: public class BatchEncoder { franta-hg@144: franta-hg@144: public int encode(SQLCommand sqlCommand, OutputStream out) throws BatchException { franta-hg@144: try { franta-hg@144: ByteArrayOutputStream bufferAOS = new ByteArrayOutputStream(); franta-hg@144: DataOutputStream buffer = new DataOutputStream(bufferAOS); franta-hg@144: franta-hg@146: buffer.write(BATCH_HEADER); franta-hg@144: franta-hg@145: if (sqlCommand instanceof SQLCommandNamed) { franta-hg@145: sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered(); franta-hg@145: } franta-hg@145: franta-hg@147: writeNextString(sqlCommand.getQuery(), buffer); franta-hg@144: franta-hg@144: List parameters = sqlCommand.getParameters(); franta-hg@144: franta-hg@144: buffer.writeInt(parameters.size()); franta-hg@144: franta-hg@144: for (Parameter p : parameters) { franta-hg@144: buffer.writeInt(p.getType().getCode()); franta-hg@147: writeNextString((String) p.getValue(), buffer); // parameters are encoded before any preprocessing franta-hg@144: } franta-hg@144: franta-hg@144: buffer.flush(); franta-hg@144: bufferAOS.writeTo(out); franta-hg@144: out.flush(); franta-hg@144: return bufferAOS.size(); franta-hg@144: } catch (IOException e) { franta-hg@144: throw new BatchException("Unable to write SQL command: " + sqlCommand, e); franta-hg@144: } catch (SQLException e) { franta-hg@144: throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e); franta-hg@144: } franta-hg@144: } franta-hg@144: franta-hg@147: private void writeNextString(String s, DataOutputStream out) throws IOException { franta-hg@147: byte[] bytes = toBytes(s); franta-hg@147: out.writeInt(bytes.length); franta-hg@147: out.write(bytes); franta-hg@147: } franta-hg@147: franta-hg@144: private static byte[] toBytes(String s) { franta-hg@144: if (s == null) { franta-hg@144: return new byte[]{}; franta-hg@144: } else { franta-hg@144: return s.getBytes(CHARSET); franta-hg@144: } franta-hg@144: } franta-hg@144: }