Legend:
- Unmodified
- Added
- Removed
-
backend/GlobeGuru-backend/src/main/java/DatabaseUtil.java
r0a7426e rdf7f390 53 53 "price REAL," + 54 54 "FOREIGN KEY(optionId) REFERENCES options(id))" 55 ); 56 PreparedStatement stmt5 = conn.prepareStatement( 57 "CREATE TABLE IF NOT EXISTS changeLog(" + 58 "id INTEGER PRIMARY KEY AUTOINCREMENT," + 59 "detail_id INTEGER NOT NULL," + 60 "attribute TEXT NOT NULL," + 61 "oldValue TEXT," + 62 "newValue TEXT," + 63 "FOREIGN KEY(detail_id) REFERENCES optionDetails(id))" 55 64 )) { 56 65 … … 59 68 stmt3.executeUpdate(); 60 69 stmt4.executeUpdate(); 70 stmt5.executeUpdate(); 61 71 } 62 72 } … … 201 211 } 202 212 } 203 204 205 206 213 public static List<Option> poolOptionDetails(int id) throws SQLException{ 207 214 String sql = "SELECT * FROM optionDetails WHERE optionId = ?"; … … 283 290 } 284 291 public static int saveOptionToDatabase(Option option) { 285 String sql = "INSERT INTO options (link, imgSrc, hotelName, country, dateRange,numberOfPeople , isPriceChanged, newPrice) VALUES (?, ?,?, ?, ?, ?, ?, ?)";292 String sql = "INSERT INTO options (link, imgSrc, hotelName, country, dateRange,numberOfPeople) VALUES (?, ?, ?, ?, ?, ?)"; 286 293 int id = 0; 287 294 try (Connection conn = DriverManager.getConnection("jdbc:sqlite:globe_guru.db"); … … 293 300 stmt.setString(5, option.getDateRange()); 294 301 stmt.setInt(6,option.getNumPeople()); 295 stmt.setBoolean(7, option.isPriceChanged());296 stmt.setFloat(8, option.getNewPrice());297 302 stmt.executeUpdate(); 298 303 try(ResultSet genKey = stmt.getGeneratedKeys()){ … … 331 336 } 332 337 } 333 334 338 public static void updateOptionDetails(int detail_id, String type, String board, String amenities, float price) { 335 String sql = "UPDATE optionDetails SET "; 336 337 List<String> updates = new ArrayList<>(); 338 if (type != null && !type.isEmpty()) updates.add("type = '" + type + "'"); 339 if (board != null && !board.isEmpty()) updates.add("board = '" + board + "'"); 340 if (amenities != null && !amenities.isEmpty()) updates.add("amenities = '" + amenities + "'"); 341 updates.add("price = " + price); 342 343 sql += String.join(", ", updates); 344 sql += " WHERE id = " + detail_id; 345 346 try (Connection conn = getConnection(); 347 PreparedStatement stmt = conn.prepareStatement(sql)) { 348 stmt.executeUpdate(); 339 try (Connection conn = getConnection()) { 340 String selectSql = "SELECT * FROM optionDetails WHERE id = ?"; 341 PreparedStatement selectStmt = conn.prepareStatement(selectSql); 342 selectStmt.setInt(1, detail_id); 343 ResultSet rs = selectStmt.executeQuery(); 344 345 if (rs.next()) { 346 String oldType = rs.getString("type"); 347 String oldBoard = rs.getString("board"); 348 String oldAmenities = rs.getString("amenities"); 349 float oldPrice = rs.getFloat("price"); 350 351 insertChangeLog(detail_id, "type", oldType, type); 352 insertChangeLog(detail_id, "board", oldBoard, board); 353 insertChangeLog(detail_id, "amenities", oldAmenities, amenities); 354 insertChangeLog(detail_id, "price", String.valueOf(oldPrice), String.valueOf(price)); 355 } 356 357 String sql = "UPDATE optionDetails SET "; 358 List<String> updates = new ArrayList<>(); 359 if (type != null && !type.isEmpty()) updates.add("type = '" + type + "'"); 360 if (board != null && !board.isEmpty()) updates.add("board = '" + board + "'"); 361 if (amenities != null && !amenities.isEmpty()) updates.add("amenities = '" + amenities + "'"); 362 if (price > 0) updates.add("price = " + price); 363 364 sql += String.join(", ", updates); 365 sql += " WHERE id = " + detail_id; 366 367 try (PreparedStatement stmt = conn.prepareStatement(sql)) { 368 stmt.executeUpdate(); 369 } 349 370 } catch (SQLException e) { 350 371 e.printStackTrace(); 351 372 } 352 373 } 353 354 public static void updateOptionInDatabase(Option option) { 355 String sql = "UPDATE options SET link = ?, imgSrc = ?, hotelName = ?, country = ?, price = ?, dateRange = ?, isPriceChanged = ?, newPrice = ? WHERE id = ?"; 356 try (Connection conn = DriverManager.getConnection("jdbc:sqlite:globe_guru.db"); 357 PreparedStatement stmt = conn.prepareStatement(sql)) { 358 stmt.setString(1, option.getLink()); 359 stmt.setString(2, option.getImgSrc()); 360 stmt.setString(3, option.getHotelName()); 361 stmt.setString(4, option.getCountry()); 362 stmt.setFloat(5, option.getPrice()); 363 stmt.setString(6, option.getDateRange()); 364 stmt.setBoolean(7, option.isPriceChanged()); 365 stmt.setFloat(8, option.getNewPrice()); 366 stmt.setInt(9, option.getId()); 367 stmt.executeUpdate(); 368 } catch (SQLException e) { 369 e.printStackTrace(); 370 } 371 } 372 public static Option findOption(Option option) { 373 String sql = "SELECT * FROM options WHERE id = ?"; 374 try (Connection conn = DriverManager.getConnection("jdbc:sqlite:globe_guru.db"); 375 PreparedStatement stmt = conn.prepareStatement(sql)) { 376 stmt.setInt(1, option.getId()); 377 try (ResultSet rs = stmt.executeQuery()) { 378 if (rs.next()) { 379 Option existingOption = new Option(); 380 existingOption.setId(rs.getInt("id")); 381 existingOption.setLink(rs.getString("link")); 382 existingOption.setImgSrc(rs.getString("imgSrc")); 383 existingOption.setHotelName(rs.getString("hotelName")); 384 existingOption.setCountry(rs.getString("country")); 385 existingOption.setPrice(rs.getFloat("price")); 386 existingOption.setDateRange(rs.getString("dateRange")); 387 existingOption.setPriceChanged(rs.getBoolean("isPriceChanged")); 388 existingOption.setNewPrice(rs.getInt("newPrice")); 389 existingOption.setNumPeople(rs.getInt("numberOfPeople")); 390 return existingOption; 391 } 392 } 393 } catch (SQLException e) { 394 e.printStackTrace(); 395 } 396 return null; 397 } 398 399 374 private static void insertChangeLog(int detail_id, String attribute, String oldValue, String newValue) { 375 if (!oldValue.equals(newValue)) { 376 String sql = "INSERT INTO changeLog (detail_id, attribute, oldValue, newValue) VALUES (?, ?, ?, ?)"; 377 try (Connection conn = getConnection(); 378 PreparedStatement stmt = conn.prepareStatement(sql)) { 379 stmt.setInt(1, detail_id); 380 stmt.setString(2, attribute); 381 stmt.setString(3, oldValue); 382 stmt.setString(4, newValue); 383 stmt.executeUpdate(); 384 } catch (SQLException e) { 385 e.printStackTrace(); 386 } 387 } 388 } 400 389 public static int getCurrentOptionsCount() throws SQLException { 401 390 String sql = "SELECT COUNT(*) AS optionsCount FROM options";
Note:
See TracChangeset
for help on using the changeset viewer.