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