diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/mysql/MysqlConnectImpl.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/mysql/MysqlConnectImpl.java index 93387d249187dd93dc9b4466bd61afa88e1668fe..d0aecb4b7b4d8026a6b18327406833dc17a1cb8a 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/mysql/MysqlConnectImpl.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/mysql/MysqlConnectImpl.java @@ -20,7 +20,7 @@ import java.util.Date; @Resource public class MysqlConnectImpl implements MysqlConnect { - public MysqlConnectImpl() { + public MysqlConnectImpl() { //System.out.println("test"); } diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/MysqlGeneralConnect.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/MysqlGeneralConnect.java new file mode 100644 index 0000000000000000000000000000000000000000..4c94a9a565671e5f52c9e9814f992036d418527d --- /dev/null +++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/MysqlGeneralConnect.java @@ -0,0 +1,40 @@ +package unipotsdam.gf.core.database; + +import ch.vorburger.exec.ManagedProcessException; +import unipotsdam.gf.config.GFDatabaseConfig; +import unipotsdam.gf.mysql.MysqlConnectImpl; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MysqlGeneralConnect extends MysqlConnectImpl { + + @Override + public Connection getConnection() throws ManagedProcessException, SQLException { + try { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException ex) { + System.out.println(ex); //logger? + } + return DriverManager.getConnection(createConnectionString()); + + } catch (SQLException ex) { + System.out.println("SQLException: " + ex.getMessage()); + System.out.println("SQLState: " + ex.getSQLState()); + System.out.println("VendorError: " + ex.getErrorCode()); + throw new Error("could not connect to mysql"); + } + } + + private static String createConnectionString() { + + String connString = + "jdbc:mysql://" + "localhost" + "/" + "mysql" + "?user=" + GFDatabaseConfig.USER + "&password=" + + GFDatabaseConfig.PASS; + return String.format(connString, GFDatabaseConfig.DB_NAME); + } + + +} diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java new file mode 100644 index 0000000000000000000000000000000000000000..da6783d7ecb40a2db36ecdb09e7fd5513858e40e --- /dev/null +++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java @@ -0,0 +1,232 @@ +package unipotsdam.gf.core.database; + +import ch.vorburger.exec.ManagedProcessException; +import unipotsdam.gf.mysql.MysqlConnect; +import unipotsdam.gf.mysql.MysqlConnectImpl; + +import java.io.*; +import java.sql.*; + +public class UpdateDB { + + private static final String DEFAULT_DELIMITER = ";"; + + private Connection connection; + + private boolean stopOnError; + private boolean autoCommit; + + private PrintWriter logWriter = new PrintWriter(System.out); + private PrintWriter errorLogWriter = new PrintWriter(System.err); + + private String delimiter = DEFAULT_DELIMITER; + private boolean fullLineDelimiter = false; + + + public static void main(String[] args) throws SQLException, ManagedProcessException, IOException { + MysqlConnect mysqlConnect = new MysqlGeneralConnect(); + Connection connection = mysqlConnect.getConnection(); + + UpdateDB updateDB = new UpdateDB(connection, true, false); + System.out.println(new java.io.File( "." ).getCanonicalPath()); + updateDB.runScript(new FileReader("src/test/resources/database/fltrail.sql")); + } + + public UpdateDB(Connection connection, boolean stopOnError, boolean autoCommit) { + this.connection = connection; + this.stopOnError = stopOnError; + this.autoCommit = autoCommit; + } + + public void setDelimiter(String delimiter, boolean fullLineDelimiter) { + this.delimiter = delimiter; + this.fullLineDelimiter = fullLineDelimiter; + } + + /** + * Setter for logWriter property + * + * @param logWriter + * - the new value of the logWriter property + */ + public void setLogWriter(PrintWriter logWriter) { + this.logWriter = logWriter; + } + + /** + * Setter for errorLogWriter property + * + * @param errorLogWriter + * - the new value of the errorLogWriter property + */ + public void setErrorLogWriter(PrintWriter errorLogWriter) { + this.errorLogWriter = errorLogWriter; + } + + /** + * Runs an SQL script (read in using the Reader parameter) + * + * @param reader + * - the source of the script + */ + public void runScript(Reader reader) throws IOException, SQLException { + try { + boolean originalAutoCommit = connection.getAutoCommit(); + try { + if (originalAutoCommit != this.autoCommit) { + connection.setAutoCommit(this.autoCommit); + } + runScript(connection, reader); + } finally { + connection.setAutoCommit(originalAutoCommit); + } + } catch (IOException e) { + throw e; + } catch (SQLException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException("Error running script. Cause: " + e, e); + } + } + + /** + * Runs an SQL script (read in using the Reader parameter) using the + * connection passed in + * + * @param conn + * - the connection to use for the script + * @param reader + * - the source of the script + * @throws SQLException + * if any SQL errors occur + * @throws IOException + * if there is an error reading from the Reader + */ + private void runScript(Connection conn, Reader reader) throws IOException, + SQLException { + StringBuffer command = null; + try { + LineNumberReader lineReader = new LineNumberReader(reader); + String line = null; + while ((line = lineReader.readLine()) != null) { + if (command == null) { + command = new StringBuffer(); + } + String trimmedLine = line.trim(); + if (trimmedLine.startsWith("--")) { + println(trimmedLine); + } else if (trimmedLine.length() < 1 + || trimmedLine.startsWith("//")) { + // Do nothing + } else if (trimmedLine.length() < 1 + || trimmedLine.startsWith("--")) { + // Do nothing + } else if (!fullLineDelimiter + && trimmedLine.endsWith(getDelimiter()) + || fullLineDelimiter + && trimmedLine.equals(getDelimiter())) { + command.append(line.substring(0, line + .lastIndexOf(getDelimiter()))); + command.append(" "); + Statement statement = conn.createStatement(); + + println(command); + + boolean hasResults = false; + if (stopOnError) { + hasResults = statement.execute(command.toString()); + } else { + try { + statement.execute(command.toString()); + } catch (SQLException e) { + e.fillInStackTrace(); + printlnError("Error executing: " + command); + printlnError(e); + } + } + + if (autoCommit && !conn.getAutoCommit()) { + conn.commit(); + } + + ResultSet rs = statement.getResultSet(); + if (hasResults && rs != null) { + ResultSetMetaData md = rs.getMetaData(); + int cols = md.getColumnCount(); + for (int i = 0; i < cols; i++) { + String name = md.getColumnLabel(i); + print(name + "\t"); + } + println(""); + while (rs.next()) { + for (int i = 0; i < cols; i++) { + String value = rs.getString(i); + print(value + "\t"); + } + println(""); + } + } + + command = null; + try { + statement.close(); + } catch (Exception e) { + // Ignore to workaround a bug in Jakarta DBCP + } + Thread.yield(); + } else { + command.append(line); + command.append(" "); + } + } + if (!autoCommit) { + conn.commit(); + } + } catch (SQLException e) { + e.fillInStackTrace(); + printlnError("Error executing: " + command); + printlnError(e); + throw e; + } catch (IOException e) { + e.fillInStackTrace(); + printlnError("Error executing: " + command); + printlnError(e); + throw e; + } finally { + conn.rollback(); + flush(); + } + } + + private String getDelimiter() { + return delimiter; + } + + private void print(Object o) { + if (logWriter != null) { + System.out.print(o); + } + } + + private void println(Object o) { + if (logWriter != null) { + logWriter.println(o); + } + } + + private void printlnError(Object o) { + if (errorLogWriter != null) { + errorLogWriter.println(o); + } + } + + private void flush() { + if (logWriter != null) { + logWriter.flush(); + } + if (errorLogWriter != null) { + errorLogWriter.flush(); + } + } + +} diff --git a/gemeinsamforschen/src/test/resources/database/fltrail.sql b/gemeinsamforschen/src/test/resources/database/fltrail.sql index 67080c1848b94fd84139a8cf91a7c41c3b0a1e74..1de2fb40854f715fa388fc94430ea7c87cb97727 100644 --- a/gemeinsamforschen/src/test/resources/database/fltrail.sql +++ b/gemeinsamforschen/src/test/resources/database/fltrail.sql @@ -3,12 +3,9 @@ SET AUTOCOMMIT = 0; START TRANSACTION; 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 */; - +DROP DATABASE `fltrail`; CREATE DATABASE IF NOT EXISTS `fltrail` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; + USE `fltrail`; CREATE TABLE `annotations` ( @@ -215,6 +212,4 @@ COMMIT; CREATE UNIQUE INDEX fullsubmissions_user_projectName_uindex ON fullsubmissions (user, projectName); -/*!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 */; +