9 |
import java.util.List; |
import java.util.List; |
10 |
|
|
11 |
|
|
12 |
|
|
13 |
import dk.daoas.adressevedligehold.beans.Address; |
import dk.daoas.adressevedligehold.beans.Address; |
14 |
import dk.daoas.adressevedligehold.beans.Address.AddressState; |
import dk.daoas.adressevedligehold.beans.Address.AddressState; |
15 |
|
import dk.daoas.adressevedligehold.tasks.TaskLogger; |
16 |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
17 |
import dk.daoas.adressevedligehold.util.TimingHelper; |
import dk.daoas.adressevedligehold.util.TimingHelper; |
18 |
|
|
19 |
/* |
/* |
|
* TODO: Batched skrivning af updates |
|
|
* TODO: Skrivning af nye adresser |
|
20 |
* |
* |
21 |
*/ |
*/ |
22 |
|
|
23 |
public class DatabaseLayerImplementation { |
public class DatabaseCoverageUpdate { |
24 |
|
|
25 |
//static boolean DEBUG = false; |
//static boolean DEBUG = false; |
26 |
|
|
27 |
|
private TaskLogger logger = TaskLogger.getInstance(); |
28 |
|
|
29 |
|
|
30 |
public List<Address> getAllAdresses() throws SQLException { |
public List<Address> getAllAdresses() throws SQLException { |
31 |
TimingHelper timing = new TimingHelper(); |
TimingHelper timing = new TimingHelper(); |
40 |
; |
; |
41 |
|
|
42 |
try ( Connection conn = DBConnection.getConnection(); |
try ( Connection conn = DBConnection.getConnection(); |
43 |
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); |
44 |
) { |
) { |
45 |
stmt.setFetchSize(Integer.MIN_VALUE); |
stmt.setFetchSize(Integer.MIN_VALUE); |
46 |
ResultSet res = stmt.executeQuery(sql); |
ResultSet res = stmt.executeQuery(sql); |
96 |
res.close(); |
res.close(); |
97 |
|
|
98 |
|
|
99 |
System.out.println("DB Loaded " + list.size() + " adresses in " + timing.getElapsed() + "ms"); |
logger.info("DB Loaded " + list.size() + " adresses in " + timing.getElapsed() + "ms"); |
100 |
|
|
101 |
return list; |
return list; |
102 |
} |
} |
103 |
} |
} |
104 |
|
|
105 |
public void saveNewAddresses(List<Address> addresses) { |
public void saveNewAddresses(List<Address> addresses) { |
106 |
System.out.println("DB::saveNewAddresses size:" + addresses.size() ); |
logger.info("DB::saveNewAddresses size:" + addresses.size() ); |
107 |
|
|
108 |
String sql = "INSERT IGNORE INTO fulddaekning.adressetabel " + |
String sql = "INSERT IGNORE INTO fulddaekning.adressetabel " + |
109 |
"(vejnavn,husnr,husnrbogstav,kommunekode,vejkode,postnr,gadeid,dbkbane,indlast,aendret,david,googlechecked," + |
"(vejnavn,husnr,husnrbogstav,kommunekode,vejkode,postnr,gadeid,dbkbane,indlast,aendret,david,googlechecked," + |
114 |
|
|
115 |
|
|
116 |
try (Connection con = DBConnection.getConnection()) { |
try (Connection con = DBConnection.getConnection()) { |
117 |
//con.setAutoCommit(false); |
|
118 |
|
|
119 |
try (PreparedStatement stmt = con.prepareStatement(sql)) { |
try (PreparedStatement stmt = con.prepareStatement(sql)) { |
120 |
for (Address addr : addresses) { |
for (Address addr : addresses) { |
149 |
|
|
150 |
stmt.execute(); |
stmt.execute(); |
151 |
if (stmt.getUpdateCount() != 1) { |
if (stmt.getUpdateCount() != 1) { |
152 |
System.out.println("Error executing insert: " + addr); |
logger.info("Error executing insert: " + addr); |
153 |
addr.state = AddressState.CREATE_FAIL; |
addr.state = AddressState.CREATE_FAIL; |
154 |
} |
} |
155 |
|
|
158 |
|
|
159 |
//con.commit(); |
//con.commit(); |
160 |
} catch (SQLException e) { |
} catch (SQLException e) { |
161 |
System.out.println("Error inserting addresses" + e.getMessage() ); |
logger.warning("Error inserting addresses", e ); |
162 |
} |
} |
163 |
} |
} |
164 |
|
|
165 |
public void updateAddresses(List<Address> addresses) { |
public void updateAddresses(List<Address> addresses) { |
166 |
System.out.println("DB::UpdateAddresses size:" + addresses.size() ); |
logger.info("DB::UpdateAddresses size:" + addresses.size() ); |
167 |
|
|
168 |
String sql = "UPDATE fulddaekning.adressetabel " + |
String sql = "UPDATE fulddaekning.adressetabel " + |
169 |
"SET " + |
"SET " + |
208 |
|
|
209 |
stmt.execute(); |
stmt.execute(); |
210 |
if (stmt.getUpdateCount() != 1) { |
if (stmt.getUpdateCount() != 1) { |
211 |
System.out.println("Error executing update"); |
logger.warning("Error executing update: " + addr); |
212 |
} |
} |
213 |
|
|
214 |
} |
} |
216 |
|
|
217 |
//con.commit(); |
//con.commit(); |
218 |
} catch (SQLException e) { |
} catch (SQLException e) { |
219 |
System.out.println("Error updating addresses" + e.getMessage() ); |
logger.warning("Error updating addresses", e ); |
220 |
} |
} |
221 |
} |
} |
222 |
|
|