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