|
33 | 33 |
|
34 | 34 | import java.sql.ResultSet;
|
35 | 35 | import java.sql.SQLException;
|
| 36 | +import java.sql.Statement; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.HashMap; |
36 | 39 | import java.util.Map;
|
| 40 | +import java.util.Set; |
37 | 41 |
|
38 | 42 | public class AddressExporter extends DatabaseExporter {
|
| 43 | + private final Table address; |
| 44 | + private final Select select; |
39 | 45 |
|
40 | 46 | public AddressExporter(ExportHelper helper) throws SQLException {
|
41 | 47 | super(helper);
|
42 |
| - stmt = helper.getConnection().prepareStatement(getQuery().toSql()); |
| 48 | + address = tableHelper.getTable(org.citydb.database.schema.Table.ADDRESS); |
| 49 | + select = getBaseQuery(); |
| 50 | + stmt = helper.getConnection().prepareStatement(Select.of(select) |
| 51 | + .where(address.column("id").eq(Placeholder.empty())) |
| 52 | + .toSql()); |
43 | 53 | }
|
44 | 54 |
|
45 |
| - private Select getQuery() { |
46 |
| - Table address = tableHelper.getTable(org.citydb.database.schema.Table.ADDRESS); |
| 55 | + private Select getBaseQuery() { |
47 | 56 | return Select.newInstance()
|
48 |
| - .select(address.columns(Map.of("objectid", "address_objectid", "identifier", "address_identifier", |
49 |
| - "identifier_codespace", "address_identifier_codespace"))) |
50 |
| - .select(address.columns("street", "house_number", "po_box", "zip_code", "city", |
51 |
| - "state", "country", "free_text", "content", "content_mime_type")) |
| 57 | + .select(address.columns("id", "objectid", "identifier", "identifier_codespace", "street", |
| 58 | + "house_number", "po_box", "zip_code", "city", "state", "country", "free_text", "content", |
| 59 | + "content_mime_type")) |
52 | 60 | .select(helper.getTransformOperator(address.column("multi_point")))
|
53 |
| - .from(address) |
54 |
| - .where(address.column("id").eq(Placeholder.empty())); |
| 61 | + .from(address); |
| 62 | + } |
| 63 | + |
| 64 | + private Select getQuery(Set<Long> ids) { |
| 65 | + return Select.of(select) |
| 66 | + .where(operationHelper.in(address.column("id"), ids)); |
55 | 67 | }
|
56 | 68 |
|
57 | 69 | public Address doExport(long id) throws ExportException, SQLException {
|
58 | 70 | stmt.setLong(1, id);
|
59 | 71 | try (ResultSet rs = stmt.executeQuery()) {
|
60 |
| - if (rs.next()) { |
61 |
| - return doExport(id, rs); |
62 |
| - } |
| 72 | + return doExport(rs).get(id); |
63 | 73 | }
|
| 74 | + } |
64 | 75 |
|
65 |
| - return null; |
| 76 | + public Map<Long, Address> doExport(Set<Long> ids) throws ExportException, SQLException { |
| 77 | + if (ids.size() == 1) { |
| 78 | + stmt.setLong(1, ids.iterator().next()); |
| 79 | + try (ResultSet rs = stmt.executeQuery()) { |
| 80 | + return doExport(rs); |
| 81 | + } |
| 82 | + } else if (!ids.isEmpty()) { |
| 83 | + try (Statement stmt = helper.getConnection().createStatement(); |
| 84 | + ResultSet rs = stmt.executeQuery(getQuery(ids).toSql())) { |
| 85 | + return doExport(rs); |
| 86 | + } |
| 87 | + } else { |
| 88 | + return Collections.emptyMap(); |
| 89 | + } |
66 | 90 | }
|
67 | 91 |
|
68 |
| - public Address doExport(long addressId, ResultSet rs) throws ExportException, SQLException { |
69 |
| - return Address.newInstance() |
70 |
| - .setObjectId(rs.getString("address_objectid")) |
71 |
| - .setIdentifier(rs.getString("address_identifier")) |
72 |
| - .setIdentifierCodeSpace(rs.getString("address_identifier_codespace")) |
73 |
| - .setStreet(rs.getString("street")) |
74 |
| - .setHouseNumber(rs.getString("house_number")) |
75 |
| - .setPoBox(rs.getString("po_box")) |
76 |
| - .setZipCode(rs.getString("zip_code")) |
77 |
| - .setCity(rs.getString("city")) |
78 |
| - .setState(rs.getString("state")) |
79 |
| - .setCountry(rs.getString("country")) |
80 |
| - .setFreeText(getArrayValue(rs.getString("free_text"))) |
81 |
| - .setMultiPoint(getGeometry(rs.getObject("multi_point"), MultiPoint.class)) |
82 |
| - .setGenericContent(rs.getString("content")) |
83 |
| - .setGenericContentMimeType(rs.getString("content_mime_type")) |
84 |
| - .setDescriptor(AddressDescriptor.of(addressId)); |
| 92 | + private Map<Long, Address> doExport(ResultSet rs) throws ExportException, SQLException { |
| 93 | + Map<Long, Address> addresses = new HashMap<>(); |
| 94 | + while (rs.next()) { |
| 95 | + long id = rs.getLong("id"); |
| 96 | + addresses.put(id, Address.newInstance() |
| 97 | + .setObjectId(rs.getString("objectid")) |
| 98 | + .setIdentifier(rs.getString("identifier")) |
| 99 | + .setIdentifierCodeSpace(rs.getString("identifier_codespace")) |
| 100 | + .setStreet(rs.getString("street")) |
| 101 | + .setHouseNumber(rs.getString("house_number")) |
| 102 | + .setPoBox(rs.getString("po_box")) |
| 103 | + .setZipCode(rs.getString("zip_code")) |
| 104 | + .setCity(rs.getString("city")) |
| 105 | + .setState(rs.getString("state")) |
| 106 | + .setCountry(rs.getString("country")) |
| 107 | + .setFreeText(getArrayValue(rs.getString("free_text"))) |
| 108 | + .setMultiPoint(getGeometry(rs.getObject("multi_point"), MultiPoint.class)) |
| 109 | + .setGenericContent(rs.getString("content")) |
| 110 | + .setGenericContentMimeType(rs.getString("content_mime_type")) |
| 111 | + .setDescriptor(AddressDescriptor.of(id))); |
| 112 | + } |
| 113 | + |
| 114 | + return addresses; |
85 | 115 | }
|
86 | 116 | }
|
0 commit comments