java/sql-dk/src/main/java/info/globalcode/sql/dk/batch/BatchDecoder.java
branchv_0
changeset 238 4a1864c3e867
parent 148 1b2f40cd432b
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.SQLCommandNumbered;
       
    23 import java.io.DataInputStream;
       
    24 import java.io.InputStream;
       
    25 import static info.globalcode.sql.dk.batch.BatchConstants.*;
       
    26 import static info.globalcode.sql.dk.Functions.toHex;
       
    27 import info.globalcode.sql.dk.SQLType;
       
    28 import java.io.IOException;
       
    29 import java.util.ArrayList;
       
    30 import java.util.Arrays;
       
    31 import java.util.List;
       
    32 
       
    33 /**
       
    34  *
       
    35  * @author Ing. František Kučera (frantovo.cz)
       
    36  */
       
    37 public class BatchDecoder {
       
    38 
       
    39 	public Batch decode(InputStream in) throws BatchException {
       
    40 		return new BatchFromStream(new DataInputStream(in));
       
    41 	}
       
    42 
       
    43 	private class BatchFromStream implements Batch {
       
    44 
       
    45 		private DataInputStream in;
       
    46 		private boolean hasNext;
       
    47 
       
    48 		public BatchFromStream(DataInputStream in) throws BatchException {
       
    49 			this.in = in;
       
    50 			hasNext = verifyHeader();
       
    51 		}
       
    52 
       
    53 		@Override
       
    54 		public boolean hasNext() throws BatchException {
       
    55 			return hasNext;
       
    56 		}
       
    57 
       
    58 		@Override
       
    59 		public SQLCommand next() throws BatchException {
       
    60 			try {
       
    61 				String sql = readNextString();
       
    62 
       
    63 				int paramCount = in.readInt();
       
    64 				List<Parameter> parameters = new ArrayList<>(paramCount);
       
    65 
       
    66 				for (int i = 0; i < paramCount; i++) {
       
    67 					SQLType type = SQLType.valueOf(in.readInt());
       
    68 					String value = readNextString();
       
    69 					parameters.add(new Parameter(value, type));
       
    70 				}
       
    71 
       
    72 				hasNext = verifyHeader();
       
    73 
       
    74 				SQLCommand sqlCommand = new SQLCommandNumbered(sql, parameters);
       
    75 				return sqlCommand;
       
    76 			} catch (IOException e) {
       
    77 				throw new BatchException("Unable to read batch", e);
       
    78 			}
       
    79 		}
       
    80 
       
    81 		private String readNextString() throws IOException {
       
    82 			byte[] buffer = new byte[in.readInt()];
       
    83 			in.read(buffer);
       
    84 			return new String(buffer, CHARSET);
       
    85 		}
       
    86 
       
    87 		/**
       
    88 		 * @return true if correct batch header was found | false if EOF was found
       
    89 		 * @throws BatchException if unexpected data was found (not batch header nor EOF)
       
    90 		 */
       
    91 		private boolean verifyHeader() throws BatchException {
       
    92 			try {
       
    93 				byte[] buffer = new byte[BATCH_HEADER.length];
       
    94 				int bytesRead = in.read(buffer);
       
    95 
       
    96 				if (bytesRead == BATCH_HEADER.length && Arrays.equals(buffer, BATCH_HEADER)) {
       
    97 					return true;
       
    98 				} else if (bytesRead == -1) {
       
    99 					return false;
       
   100 				} else {
       
   101 					throw new BatchException("This is not SQL-DK batch: " + toHex(buffer));
       
   102 				}
       
   103 			} catch (IOException e) {
       
   104 				throw new BatchException("Unable to read batch header", e);
       
   105 			}
       
   106 		}
       
   107 	}
       
   108 }