From 9cd0a2ed9837ed973706058d21c45b705de523e2 Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Sun, 19 Mar 2017 12:45:41 +0100 Subject: [PATCH] Fixed #24 - SQL create statements, Fixed #19 - prefill table category --- settings.properties | 6 +- .../main/DatabaseCreator.java | 229 ++++++++++++++++++ .../main/DatabaseHandler.java | 1 + .../budgetmasterserver/main/Settings.java | 6 + .../budgetmasterserver/main/Utils.java | 2 +- 5 files changed, 240 insertions(+), 4 deletions(-) create mode 100644 src/de/deadlocker8/budgetmasterserver/main/DatabaseCreator.java diff --git a/settings.properties b/settings.properties index 04a92f8bc..1da1d4e8e 100644 --- a/settings.properties +++ b/settings.properties @@ -1,8 +1,8 @@ { "databaseUrl": "jdbc:mysql://localhost:3306/", - "databaseName": "budgetmaster", + "databaseName": "budgetmastersave", "databaseUsername": "root", "databasePassword": "", - "serverPort": 9000, - "serverSecret": "geheim" + "port": 9000, + "secret": "geheim" } \ No newline at end of file diff --git a/src/de/deadlocker8/budgetmasterserver/main/DatabaseCreator.java b/src/de/deadlocker8/budgetmasterserver/main/DatabaseCreator.java new file mode 100644 index 000000000..b60867ca2 --- /dev/null +++ b/src/de/deadlocker8/budgetmasterserver/main/DatabaseCreator.java @@ -0,0 +1,229 @@ +package de.deadlocker8.budgetmasterserver.main; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +import logger.Logger; + +public class DatabaseCreator +{ + private Connection connection; + private Settings settings; + + public DatabaseCreator(Connection connection, Settings settings) + { + this.connection = connection; + this.settings = settings; + Logger.info("Checking tables..."); + createTables(getExistingTables()); + Logger.info("Successfully initialized database"); + } + + private ArrayList<String> getExistingTables() + { + ArrayList<String> tables = new ArrayList<>(); + try + { + DatabaseMetaData meta = connection.getMetaData(); + ResultSet res = meta.getTables(settings.getDatabaseName(), null, "", new String[] { "TABLE" }); + while(res.next()) + { + tables.add(res.getString("TABLE_NAME")); + } + } + catch(Exception e) + { + Logger.error(e); + } + return tables; + } + + private void createTables(ArrayList<String> existingTables) + { + if(!existingTables.contains("category")) + { + createTableCategory(); + } + + if(!existingTables.contains("payment")) + { + createTablePayment(); + } + + if(!existingTables.contains("repeating_payment")) + { + createTableRepeatingPayment(); + } + + if(!existingTables.contains("repeating_entry")) + { + createTableRepeatingEntry(); + } + } + + private void createTableCategory() + { + Statement stmt = null; + String query = "CREATE TABLE `category` (`ID` int(11) NOT NULL COMMENT 'ID'," + + " `Name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'category name'," + + "`Color` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'color hexcode'" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; + String query2 = "INSERT INTO `category` (`ID`, `Name`, `Color`) VALUES(1, 'NONE', '#FFFFFF'),(2, 'Übertrag', '#FFFF00');"; + String query3 = "ALTER TABLE `category` ADD PRIMARY KEY (`ID`);"; + String query4 = "ALTER TABLE `category` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', AUTO_INCREMENT=3;"; + + try + { + stmt = connection.createStatement(); + stmt.execute(query); + stmt.execute(query2); + stmt.execute(query3); + stmt.execute(query4); + Logger.info("Successfully created table category"); + } + catch(SQLException e) + { + Logger.error(e); + } + finally + { + if(stmt != null) + { + try + { + stmt.close(); + } + catch(SQLException e) + { + } + } + } + } + + private void createTablePayment() + { + Statement stmt = null; + String query = "CREATE TABLE `payment` (" + + "`ID` int(11) NOT NULL COMMENT 'ID'," + + "`Name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'payment name (description)'," + + "`CategoryID` int(11) DEFAULT NULL COMMENT 'category ID'," + + "`Amount` int(11) DEFAULT NULL COMMENT 'amount in cents'," + + "`Date` date DEFAULT NULL COMMENT 'payment date'" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; + String query2 = "ALTER TABLE `payment` ADD PRIMARY KEY (`ID`);"; + String query3 = "ALTER TABLE `payment` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID';"; + + try + { + stmt = connection.createStatement(); + stmt.execute(query); + stmt.execute(query2); + stmt.execute(query3); + Logger.info("Successfully created table payment"); + } + catch(SQLException e) + { + Logger.error(e); + } + finally + { + if(stmt != null) + { + try + { + stmt.close(); + } + catch(SQLException e) + { + } + } + } + } + + private void createTableRepeatingEntry() + { + Statement stmt = null; + String query = "CREATE TABLE `repeating_entry` (" + + "`ID` int(11) NOT NULL," + + "`RepeatingPaymentID` int(11) NOT NULL," + + "`Date` date NOT NULL" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; + String query2 = "ALTER TABLE `repeating_entry` ADD PRIMARY KEY (`ID`), ADD KEY `RepeatingPaymentID` (`RepeatingPaymentID`);"; + String query3 = "ALTER TABLE `repeating_entry` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID';"; + String query4 = "ALTER TABLE `repeating_entry` ADD CONSTRAINT `constraint_1` FOREIGN KEY (`RepeatingPaymentID`) REFERENCES `repeating_payment` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;"; + + try + { + stmt = connection.createStatement(); + stmt.execute(query); + stmt.execute(query2); + stmt.execute(query3); + stmt.execute(query4); + Logger.info("Successfully created table repeating_entry"); + } + catch(SQLException e) + { + Logger.error(e); + } + finally + { + if(stmt != null) + { + try + { + stmt.close(); + } + catch(SQLException e) + { + } + } + } + } + + private void createTableRepeatingPayment() + { + Statement stmt = null; + String query = "CREATE TABLE `repeating_payment` (" + + "`ID` int(11) NOT NULL COMMENT 'ID'," + + "`Name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'payment name (description)'," + + "`CategoryID` int(11) DEFAULT NULL COMMENT 'category ID'," + + "`Amount` int(11) DEFAULT NULL COMMENT 'amount in cents'," + + "`Date` date DEFAULT NULL COMMENT 'payment date'," + + "`RepeatInterval` int(11) DEFAULT NULL COMMENT 'repeat interval in days'," + + "`RepeatEndDate` date DEFAULT NULL COMMENT 'repeat end date'," + + "`RepeatMonthDay` int(11) DEFAULT NULL COMMENT 'day in month on which payment repeats'" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; + String query2 = "ALTER TABLE `repeating_payment` ADD PRIMARY KEY (`ID`);"; + String query3 = "ALTER TABLE `repeating_payment` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID';"; + + try + { + stmt = connection.createStatement(); + stmt.execute(query); + stmt.execute(query2); + stmt.execute(query3); + Logger.info("Successfully created table repeating_payment"); + } + catch(SQLException e) + { + Logger.error(e); + } + finally + { + if(stmt != null) + { + try + { + stmt.close(); + } + catch(SQLException e) + { + } + } + } + } +} \ No newline at end of file diff --git a/src/de/deadlocker8/budgetmasterserver/main/DatabaseHandler.java b/src/de/deadlocker8/budgetmasterserver/main/DatabaseHandler.java index 1edd1a699..ce93c39f2 100644 --- a/src/de/deadlocker8/budgetmasterserver/main/DatabaseHandler.java +++ b/src/de/deadlocker8/budgetmasterserver/main/DatabaseHandler.java @@ -31,6 +31,7 @@ public class DatabaseHandler try { this.connection = DriverManager.getConnection(settings.getDatabaseUrl() + settings.getDatabaseName() + "?useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin", settings.getDatabaseUsername(), settings.getDatabasePassword()); + new DatabaseCreator(connection, settings); } catch(Exception e) { diff --git a/src/de/deadlocker8/budgetmasterserver/main/Settings.java b/src/de/deadlocker8/budgetmasterserver/main/Settings.java index 30eac6753..f981d9ded 100644 --- a/src/de/deadlocker8/budgetmasterserver/main/Settings.java +++ b/src/de/deadlocker8/budgetmasterserver/main/Settings.java @@ -43,4 +43,10 @@ public class Settings { return serverSecret; } + + @Override + public String toString() + { + return "Settings [databaseUrl=" + databaseUrl + ", databaseName=" + databaseName + ", databaseUsername=" + databaseUsername + ", databasePassword=" + databasePassword + ", serverPort=" + serverPort + ", serverSecret=" + serverSecret + "]"; + } } \ No newline at end of file diff --git a/src/de/deadlocker8/budgetmasterserver/main/Utils.java b/src/de/deadlocker8/budgetmasterserver/main/Utils.java index a6d3340fc..04772c127 100644 --- a/src/de/deadlocker8/budgetmasterserver/main/Utils.java +++ b/src/de/deadlocker8/budgetmasterserver/main/Utils.java @@ -14,7 +14,7 @@ public class Utils Settings settings; Gson gson = new Gson(); - settingsJSON = new String(Files.readAllBytes(Paths.get("settings.properties"))); + settingsJSON = new String(Files.readAllBytes(Paths.get("settings.properties"))); settings = gson.fromJson(settingsJSON, Settings.class); return settings; } -- GitLab