improved autocreate of table structure

This commit is contained in:
Lukas Heiligenbrunner 2020-04-17 18:00:47 +02:00
parent a633ba42e9
commit f4fbce7977
3 changed files with 112 additions and 303 deletions

150
db.sql
View File

@ -1,150 +0,0 @@
-- phpMyAdmin SQL Dump
-- version 4.6.6deb4
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 17. Apr 2020 um 09:07
-- Server-Version: 10.1.44-MariaDB-0+deb9u1
-- PHP-Version: 7.3.13-1+0~20191218.50+debian9~1.gbp23c2da
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Datenbank: `ingproject`
--
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `cities`
--
CREATE TABLE `cities` (
`id` int(11) NOT NULL,
`userid` int(11) NOT NULL,
`name` varchar(256) NOT NULL,
`wastetype` varchar(64) NOT NULL,
`zone` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `devices`
--
CREATE TABLE `devices` (
`DeviceID` int(11) NOT NULL,
`CityID` int(11) NOT NULL DEFAULT '-1',
`DeviceName` varchar(15) DEFAULT NULL,
`DeviceLocation` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `device_city`
--
CREATE TABLE `device_city` (
`DeviceID` int(11) NOT NULL,
`CityID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `pickupdates`
--
CREATE TABLE `pickupdates` (
`id` int(11) NOT NULL,
`citywastezoneid` int(11) NOT NULL,
`pickupdate` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `user`
--
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(150) NOT NULL,
`firstName` varchar(32) NOT NULL,
`secondName` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`permission` int(11) NOT NULL DEFAULT '0',
`email` varchar(64) NOT NULL,
`logindate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indizes der exportierten Tabellen
--
--
-- Indizes für die Tabelle `cities`
--
ALTER TABLE `cities`
ADD PRIMARY KEY (`id`);
--
-- Indizes für die Tabelle `devices`
--
ALTER TABLE `devices`
ADD PRIMARY KEY (`DeviceID`);
--
-- Indizes für die Tabelle `pickupdates`
--
ALTER TABLE `pickupdates`
ADD PRIMARY KEY (`id`),
ADD KEY `citywastezoneid` (`citywastezoneid`);
--
-- Indizes für die Tabelle `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT für exportierte Tabellen
--
--
-- AUTO_INCREMENT für Tabelle `cities`
--
ALTER TABLE `cities`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=143;
--
-- AUTO_INCREMENT für Tabelle `pickupdates`
--
ALTER TABLE `pickupdates`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67;
--
-- AUTO_INCREMENT für Tabelle `user`
--
ALTER TABLE `user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;
--
-- Constraints der exportierten Tabellen
--
--
-- Constraints der Tabelle `pickupdates`
--
ALTER TABLE `pickupdates`
ADD CONSTRAINT `pickupdates_ibfk_1` FOREIGN KEY (`citywastezoneid`) REFERENCES `cities` (`id`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

View File

@ -4,6 +4,7 @@ import com.wasteinformationserver.basicutils.Log;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
/**
* basic connection class to a Database
@ -151,160 +152,31 @@ public class JDBC {
*/
public void checkDBStructure() {
try {
ResultSet set = conn.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
set.last();
if (set.getRow() != 5) {
ResultSet seti = executeQuery("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" + dbnamec + "'");
seti.last();
Log.Log.debug("found " + seti.getInt(1) + " tables in db");
if (seti.getInt(1) != 5) {
// structure not valid
executeUpdate("-- phpMyAdmin SQL Dump\n" +
"-- version 4.6.6deb4\n" +
"-- https://www.phpmyadmin.net/\n" +
"--\n" +
"-- Host: localhost:3306\n" +
"-- Erstellungszeit: 17. Apr 2020 um 09:07\n" +
"-- Server-Version: 10.1.44-MariaDB-0+deb9u1\n" +
"-- PHP-Version: 7.3.13-1+0~20191218.50+debian9~1.gbp23c2da\n" +
"\n" +
"SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n" +
"SET time_zone = \"+00:00\";\n" +
"\n" +
"\n" +
"/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n" +
"/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n" +
"/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n" +
"/*!40101 SET NAMES utf8mb4 */;\n" +
"\n" +
"--\n" +
"-- Datenbank: `ingproject`\n" +
"--\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `cities`\n" +
"--\n" +
"\n" +
"CREATE TABLE `cities` (\n" +
" `id` int(11) NOT NULL,\n" +
" `userid` int(11) NOT NULL,\n" +
" `name` varchar(256) NOT NULL,\n" +
" `wastetype` varchar(64) NOT NULL,\n" +
" `zone` int(11) NOT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `devices`\n" +
"--\n" +
"\n" +
"CREATE TABLE `devices` (\n" +
" `DeviceID` int(11) NOT NULL,\n" +
" `CityID` int(11) NOT NULL DEFAULT '-1',\n" +
" `DeviceName` varchar(15) DEFAULT NULL,\n" +
" `DeviceLocation` varchar(15) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `device_city`\n" +
"--\n" +
"\n" +
"CREATE TABLE `device_city` (\n" +
" `DeviceID` int(11) NOT NULL,\n" +
" `CityID` int(11) NOT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `pickupdates`\n" +
"--\n" +
"\n" +
"CREATE TABLE `pickupdates` (\n" +
" `id` int(11) NOT NULL,\n" +
" `citywastezoneid` int(11) NOT NULL,\n" +
" `pickupdate` date NOT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
"\n" +
"-- --------------------------------------------------------\n" +
"\n" +
"--\n" +
"-- Tabellenstruktur für Tabelle `user`\n" +
"--\n" +
"\n" +
"CREATE TABLE `user` (\n" +
" `id` int(11) NOT NULL,\n" +
" `username` varchar(150) NOT NULL,\n" +
" `firstName` varchar(32) NOT NULL,\n" +
" `secondName` varchar(32) NOT NULL,\n" +
" `password` varchar(32) NOT NULL,\n" +
" `permission` int(11) NOT NULL DEFAULT '0',\n" +
" `email` varchar(64) NOT NULL,\n" +
" `logindate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
"\n" +
"--\n" +
"-- Indizes der exportierten Tabellen\n" +
"--\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `cities`\n" +
"--\n" +
"ALTER TABLE `cities`\n" +
" ADD PRIMARY KEY (`id`);\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `devices`\n" +
"--\n" +
"ALTER TABLE `devices`\n" +
" ADD PRIMARY KEY (`DeviceID`);\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `pickupdates`\n" +
"--\n" +
"ALTER TABLE `pickupdates`\n" +
" ADD PRIMARY KEY (`id`),\n" +
" ADD KEY `citywastezoneid` (`citywastezoneid`);\n" +
"\n" +
"--\n" +
"-- Indizes für die Tabelle `user`\n" +
"--\n" +
"ALTER TABLE `user`\n" +
" ADD PRIMARY KEY (`id`);\n" +
"\n" +
"--\n" +
"-- AUTO_INCREMENT für exportierte Tabellen\n" +
"--\n" +
"\n" +
"--\n" +
"-- AUTO_INCREMENT für Tabelle `cities`\n" +
"--\n" +
"ALTER TABLE `cities`\n" +
" MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=143;\n" +
"--\n" +
"-- AUTO_INCREMENT für Tabelle `pickupdates`\n" +
"--\n" +
"ALTER TABLE `pickupdates`\n" +
" MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67;\n" +
"--\n" +
"-- AUTO_INCREMENT für Tabelle `user`\n" +
"--\n" +
"ALTER TABLE `user`\n" +
" MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;\n" +
"--\n" +
"-- Constraints der exportierten Tabellen\n" +
"--\n" +
"\n" +
"--\n" +
"-- Constraints der Tabelle `pickupdates`\n" +
"--\n" +
"ALTER TABLE `pickupdates`\n" +
" ADD CONSTRAINT `pickupdates_ibfk_1` FOREIGN KEY (`citywastezoneid`) REFERENCES `cities` (`id`);\n" +
"\n" +
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n" +
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n" +
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
Scanner s = new Scanner(getClass().getResourceAsStream("/db.sql"));
s.useDelimiter("(;(\r)?\n)|(--\n)");
Statement st = null;
try {
st = conn.createStatement();
while (s.hasNext()) {
String line = s.next();
if (line.startsWith("/*!") && line.endsWith("*/")) {
int i = line.indexOf(' ');
line = line.substring(i + 1, line.length() - " */".length());
}
if (line.trim().length() > 0) {
executeUpdate(line);
}
}
} finally {
if (st != null) st.close();
}
} else {
Log.Log.message("found valid database structure!");
}

87
src/resources/db.sql Normal file
View File

@ -0,0 +1,87 @@
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
CREATE TABLE cities
(
id int(11) NOT NULL,
userid int(11) NOT NULL,
name varchar(256) NOT NULL,
wastetype varchar(64) NOT NULL,
zone int(11) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE devices
(
DeviceID int(11) NOT NULL,
CityID int(11) NOT NULL DEFAULT '-1',
DeviceName varchar(15) DEFAULT NULL,
DeviceLocation varchar(15) DEFAULT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE device_city
(
DeviceID int(11) NOT NULL,
CityID int(11) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE pickupdates
(
id int(11) NOT NULL,
citywastezoneid int(11) NOT NULL,
pickupdate date NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE `user`
(
id int(11) NOT NULL,
username varchar(150) NOT NULL,
firstName varchar(32) NOT NULL,
secondName varchar(32) NOT NULL,
password varchar(32) NOT NULL,
permission int(11) NOT NULL DEFAULT '0',
email varchar(64) NOT NULL,
logindate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
ALTER TABLE cities
ADD PRIMARY KEY (id);
ALTER TABLE devices
ADD PRIMARY KEY (DeviceID);
ALTER TABLE pickupdates
ADD PRIMARY KEY (id),
ADD KEY citywastezoneid (citywastezoneid);
ALTER TABLE `user`
ADD PRIMARY KEY (id);
ALTER TABLE cities
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 143;
ALTER TABLE pickupdates
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 67;
ALTER TABLE `user`
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 17;
ALTER TABLE pickupdates
ADD CONSTRAINT pickupdates_ibfk_1 FOREIGN KEY (citywastezoneid) REFERENCES cities (id);
/*!40101 SET CHARACTER_SET_CLIENT = @OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS = @OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION = @OLD_COLLATION_CONNECTION */;