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