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

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!");
}