From e4943d846cf4033748b9a7d1c2994bc1b84dbff7 Mon Sep 17 00:00:00 2001 From: laurimaaninka Date: Fri, 29 May 2026 23:30:14 +0300 Subject: [PATCH] =?UTF-8?q?Lis=C3=A4tty=20kuljetustietokantaan=20tyyppi=20?= =?UTF-8?q?sarake,=20sek=C3=A4=20siistitty=20tietokannan=20alustamista?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laurimaaninka --- .../java/fi/lpam/dataluokat/Tietokanta.java | 27 +++++++------------ src/main/resources/tietokanta.sql | 9 ++++++- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/fi/lpam/dataluokat/Tietokanta.java b/src/main/java/fi/lpam/dataluokat/Tietokanta.java index d41f4ed..589d26a 100644 --- a/src/main/java/fi/lpam/dataluokat/Tietokanta.java +++ b/src/main/java/fi/lpam/dataluokat/Tietokanta.java @@ -24,26 +24,19 @@ public class Tietokanta { osoite = "jdbc:sqlite:" + tietokantatiedosto.getAbsolutePath(); - - try (Connection conn = haeYhteys()) { - InputStream is = Objects.requireNonNull(Main.class.getClassLoader().getResourceAsStream("tietokanta.sql")); - BufferedReader br = new BufferedReader(new InputStreamReader(is)); - String sql = br.readAllAsString(); - - for (String query : sql.split(";")) { - conn.createStatement().execute(query); - } - } - catch (SQLException | IOException e) { - handleException(e); + String sql = ""; + try (BufferedReader br = new BufferedReader( + new InputStreamReader( + Objects.requireNonNull(Main.class.getClassLoader() + .getResourceAsStream("tietokanta.sql"))))) { + sql = br.readAllAsString(); + } catch (IOException e) { + throw new RuntimeException(e); } - //Lisää kuljetuspäivät asiakassarakkeeseen (v2.0.2->3.x.x) - String[] sarakkeet = {"lauantaiKpl", "sunnuntaiKpl"}; - for (String sarake : sarakkeet) { + for (String query : sql.split(";")) { try (Connection conn = haeYhteys()) { - PreparedStatement stmt = conn.prepareStatement("alter table asiakkaat add column " + sarake + " int not null default 0"); - stmt.execute(); + conn.createStatement().execute(query); } catch (SQLException e) { if (!e.getMessage().contains("duplicate column name")) { handleException(e); diff --git a/src/main/resources/tietokanta.sql b/src/main/resources/tietokanta.sql index 9cdf382..eb708ef 100644 --- a/src/main/resources/tietokanta.sql +++ b/src/main/resources/tietokanta.sql @@ -40,4 +40,11 @@ create table if not exists kuljetukset ( lisätieto varchar(255) ); -create index if not exists idx_kuljetuspvm on kuljetukset(kuljetusPäivämäärä); \ No newline at end of file +create index if not exists idx_kuljetuspvm on kuljetukset(kuljetusPäivämäärä); + +--2.0.2->3.x.x +alter table asiakkaat add lauantaiKpl int not null default 0; +alter table asiakkaat add sunnuntaiKpl int not null default 0; + +--3.x.x->4.0.0 +alter table kuljetukset add tyyppi varchar(50); \ No newline at end of file