java/sql-dk/src/info/globalcode/sql/dk/batch/BatchEncoder.java
branchv_0
changeset 144 d273d7c6dc0c
child 145 5f90decd3b59
equal deleted inserted replaced
143:1336bb9a4499 144:d273d7c6dc0c
       
     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_START);
       
    43 
       
    44 			byte[] sqlBytes = toBytes(sqlCommand.getQuery());
       
    45 			buffer.writeInt(sqlBytes.length);
       
    46 			buffer.write(sqlBytes);
       
    47 
       
    48 			if (sqlCommand instanceof SQLCommandNamed) {
       
    49 				sqlCommand = ((SQLCommandNamed) sqlCommand).getSQLCommandNumbered();
       
    50 			}
       
    51 
       
    52 			List<? extends Parameter> parameters = sqlCommand.getParameters();
       
    53 
       
    54 			buffer.writeInt(parameters.size());
       
    55 
       
    56 			for (Parameter p : parameters) {
       
    57 				buffer.writeInt(p.getType().getCode());
       
    58 				byte[] value = toBytes((String) p.getValue()); // parameters are encoded before any preprocessing
       
    59 				buffer.writeInt(value.length);
       
    60 				buffer.write(value);
       
    61 			}
       
    62 
       
    63 			buffer.flush();
       
    64 			bufferAOS.writeTo(out);
       
    65 			out.flush();
       
    66 			return bufferAOS.size();
       
    67 		} catch (IOException e) {
       
    68 			throw new BatchException("Unable to write SQL command: " + sqlCommand, e);
       
    69 		} catch (SQLException e) {
       
    70 			throw new BatchException("Unable to converd named SQL command to numbered: " + sqlCommand, e);
       
    71 		}
       
    72 	}
       
    73 
       
    74 	private static byte[] toBytes(String s) {
       
    75 		if (s == null) {
       
    76 			return new byte[]{};
       
    77 		} else {
       
    78 			return s.getBytes(CHARSET);
       
    79 		}
       
    80 	}
       
    81 }