Parcourir la source

Fixing database connection

Zoey76 il y a 5 ans
Parent
commit
dc373f723e

+ 41 - 9
pom.xml

@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>com.l2jserver</groupId>
 	<artifactId>l2j-server-cli</artifactId>
-	<version>1.0.0-SNAPSHOT</version>
+	<version>1.0.1</version>
 	<name>L2J Server Command Line</name>
 	<properties>
 		<maven.compiler.source>11</maven.compiler.source>
@@ -14,7 +14,13 @@
 		<jgit.version>5.2.1.201812262042-r</jgit.version>
 		<slf4j.version>1.7.25</slf4j.version>
 		<log4j.version>2.11.1</log4j.version>
-		<mysql-connector-java.version>8.0.15</mysql-connector-java.version>
+		<mysql-connector-java.version>8.0.17</mysql-connector-java.version>
+		<mariadb-java-client.version>2.4.3</mariadb-java-client.version>
+		<mssql-jdbc.version>7.4.1.jre11</mssql-jdbc.version>
+		<postgresql.version>42.2.6</postgresql.version>
+		<hsqldb.version>2.5.0</hsqldb.version>
+		<h2.version>1.4.199</h2.version>
+		<derbyclient.version>10.15.1.3</derbyclient.version>
 		<asciitable.version>0.3.2</asciitable.version>
 		<owner.version>1.0.10</owner.version>
 		<l2j-server-commons.version>2.6.1.1</l2j-server-commons.version>
@@ -55,11 +61,43 @@
 			<artifactId>log4j-core</artifactId>
 			<version>${log4j.version}</version>
 		</dependency>
+		<!-- Database -->
 		<dependency>
 			<groupId>mysql</groupId>
 			<artifactId>mysql-connector-java</artifactId>
 			<version>${mysql-connector-java.version}</version>
 		</dependency>
+		<dependency>
+			<groupId>org.mariadb.jdbc</groupId>
+			<artifactId>mariadb-java-client</artifactId>
+			<version>${mariadb-java-client.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.microsoft.sqlserver</groupId>
+			<artifactId>mssql-jdbc</artifactId>
+			<version>${mssql-jdbc.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.postgresql</groupId>
+			<artifactId>postgresql</artifactId>
+			<version>${postgresql.version}</version>
+		</dependency>
+		<!-- In-Memory Database -->
+		<dependency>
+			<groupId>org.hsqldb</groupId>
+			<artifactId>hsqldb</artifactId>
+			<version>${hsqldb.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.h2database</groupId>
+			<artifactId>h2</artifactId>
+			<version>${h2.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.derby</groupId>
+			<artifactId>derbyclient</artifactId>
+			<version>${derbyclient.version}</version>
+		</dependency>
 		<dependency>
 			<groupId>de.vandermeer</groupId>
 			<artifactId>asciitable</artifactId>
@@ -70,12 +108,6 @@
 			<artifactId>owner-java8</artifactId>
 			<version>${owner.version}</version>
 		</dependency>
-		<!-- L2J -->
-		<dependency>
-			<groupId>org.bitbucket.l2jserver</groupId>
-			<artifactId>l2j-server-commons</artifactId>
-			<version>${l2j-server-commons.version}</version>
-		</dependency>
 	</dependencies>
 	<build>
 		<plugins>
@@ -97,7 +129,7 @@
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-assembly-plugin</artifactId>
-				<version>${maven-assembly-plugin.version}</version>
+				<version>${maven-assembly-plugin.version}</version><!--$NO-MVN-MAN-VER$-->
 				<executions>
 					<execution>
 						<phase>package</phase>

+ 8 - 0
src/main/assembly/zip.xml

@@ -18,6 +18,14 @@
 			<source>src/main/resources/l2jcli.sh</source>
 			<outputDirectory>.</outputDirectory>
 		</file>
+		<file>
+			<source>src/main/resources/config/game-server.properties</source>
+			<outputDirectory>config</outputDirectory>
+		</file>
+		<file>
+			<source>src/main/resources/config/login-server.properties</source>
+			<outputDirectory>config</outputDirectory>
+		</file>
 	</files>
 	<dependencySets>
 		<dependencySet>

+ 12 - 20
src/main/java/com/l2jserver/cli/command/database/DatabaseInstallCommand.java

@@ -48,18 +48,18 @@ public class DatabaseInstallCommand extends AbstractCommand {
 	}, required = true, description = "Files location")
 	private String path;
 	
-	@Option(names = {
-		"-dv",
-		"--database-driver"
-	}, description = "Database Driver")
-	private String driver;
-	
 	@Option(names = {
 		"-url",
 		"--database-url"
 	}, description = "Database URL")
 	private String url;
 	
+	@Option(names = {
+		"-db",
+		"--database-name"
+	}, description = "Database Name")
+	private String name;
+	
 	@Option(names = {
 		"-u",
 		"--database-user"
@@ -67,17 +67,11 @@ public class DatabaseInstallCommand extends AbstractCommand {
 	private String user;
 	
 	@Option(names = {
-		"-pw",
+		"-p",
 		"--database-password"
-	}, description = "Database Password")
+	}, interactive = true, description = "Database Password")
 	private String password;
 	
-	@Option(names = {
-		"-pool",
-		"--connection-pool"
-	}, description = "Connection Pool")
-	private String connectionPool;
-	
 	@Option(names = {
 		"-m",
 		"--mode"
@@ -101,6 +95,8 @@ public class DatabaseInstallCommand extends AbstractCommand {
 		
 		final AbstractDatabaseDAO databaseDAO = databaseDAO();
 		
+		databaseDAO.createDatabase();
+		
 		databaseDAO.createDump();
 		
 		databaseDAO.updates(mode, sqlPath);
@@ -139,10 +135,6 @@ public class DatabaseInstallCommand extends AbstractCommand {
 	}
 	
 	private void overrideConfigs(Mutable databaseConfiguration) {
-		if (driver != null) {
-			databaseConfiguration.setProperty("DatabaseDriver", driver);
-		}
-		
 		if (url != null) {
 			databaseConfiguration.setProperty("DatabaseURL", url);
 		}
@@ -155,8 +147,8 @@ public class DatabaseInstallCommand extends AbstractCommand {
 			databaseConfiguration.setProperty("DatabasePassword", password);
 		}
 		
-		if (connectionPool != null) {
-			databaseConfiguration.setProperty("DatabaseConnectionPool", connectionPool);
+		if (name != null) {
+			databaseConfiguration.setProperty("DatabaseName", name);
 		}
 	}
 }

+ 18 - 7
src/main/java/com/l2jserver/cli/dao/AbstractDAO.java

@@ -21,6 +21,7 @@ package com.l2jserver.cli.dao;
 import java.io.File;
 import java.io.FileWriter;
 import java.sql.Connection;
+import java.sql.SQLException;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
@@ -30,9 +31,9 @@ import java.util.List;
 import java.util.Scanner;
 
 import com.l2jserver.cli.config.ServerConfiguration;
+import com.l2jserver.cli.database.ConnectionFactory;
 import com.l2jserver.cli.util.FileWriterStdout;
 import com.l2jserver.cli.util.SQLFilter;
-import com.l2jserver.commons.database.ConnectionFactory;
 
 /**
  * Abstract DAO.
@@ -41,21 +42,22 @@ import com.l2jserver.commons.database.ConnectionFactory;
  */
 class AbstractDAO {
 	
+	private static final String CREATE_DATABASE = "CREATE DATABASE ";
+	
 	private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
 	
+	private ConnectionFactory connectionFactory;
+	
 	private String database;
 	
 	AbstractDAO(ServerConfiguration server) {
 		this.database = server.getDatabaseName();
 		
-		ConnectionFactory.builder() //
-			.withDriver(server.getDatabaseDriver()) //
+		this.connectionFactory = ConnectionFactory.builder() //
+			.withDatabaseName(server.getDatabaseName()) //
 			.withUrl(server.getDatabaseURL()) //
 			.withUser(server.getDatabaseUser()) //
 			.withPassword(server.getDatabasePassword()) //
-			.withConnectionPool(server.getDatabaseConnectionPool()) //
-			.withMaxPoolSize(server.getDatabaseMaximumPoolSize()) //
-			.withMaxIdleTime(server.getDatabaseMaximumIdleTime()) //
 			.build();
 	}
 	
@@ -64,7 +66,16 @@ class AbstractDAO {
 	}
 	
 	public Connection getConnection() {
-		return ConnectionFactory.getInstance().getConnection();
+		return connectionFactory.getConnection();
+	}
+	
+	public void createDatabase() {
+		try (var con = connectionFactory.getPlainConnection(); //
+			var st = con.createStatement()) {
+			st.executeUpdate(CREATE_DATABASE + database);
+		} catch (SQLException ex) {
+			ex.printStackTrace();
+		}
 	}
 	
 	public void executeSQLScript(File file) {

+ 104 - 0
src/main/java/com/l2jserver/cli/database/ConnectionFactory.java

@@ -0,0 +1,104 @@
+/*
+ * Copyright © 2019 L2J Server
+ *
+ * This file is part of L2J Server.
+ *
+ * L2J Server is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * L2J Server is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.cli.database;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * Connection Factory.
+ * @author Zoey76
+ * @version 1.0.1
+ */
+public class ConnectionFactory {
+	
+	private String url;
+	
+	private String databaseName;
+	
+	private final Properties properties = new Properties();
+	
+	private ConnectionFactory(Builder builder) {
+		this.url = builder.url;
+		this.databaseName = builder.databaseName;
+		this.properties.setProperty("user", builder.user);
+		this.properties.setProperty("password", builder.password);
+		this.properties.putAll(DatabaseType.getType(builder.url).getParameters());
+	}
+	
+	public Connection getPlainConnection() {
+		try {
+			return DriverManager.getConnection(url, properties);
+		} catch (SQLException ex) {
+			ex.printStackTrace();
+		}
+		return null;
+	}
+	
+	public Connection getConnection() {
+		try {
+			final var con = DriverManager.getConnection(url + "/" + databaseName, properties);
+			con.setCatalog(databaseName);
+			return con;
+		} catch (SQLException ex) {
+			ex.printStackTrace();
+		}
+		return null;
+	}
+	
+	public static Builder builder() {
+		return new Builder();
+	}
+	
+	public static final class Builder {
+		private String url;
+		private String user;
+		private String password;
+		private String databaseName;
+		
+		private Builder() {
+		}
+		
+		public Builder withUrl(String url) {
+			this.url = url;
+			return this;
+		}
+		
+		public Builder withUser(String user) {
+			this.user = user;
+			return this;
+		}
+		
+		public Builder withPassword(String password) {
+			this.password = password;
+			return this;
+		}
+		
+		public Builder withDatabaseName(String databaseName) {
+			this.databaseName = databaseName;
+			return this;
+		}
+		
+		public ConnectionFactory build() {
+			return new ConnectionFactory(this);
+		}
+	}
+}

+ 74 - 0
src/main/java/com/l2jserver/cli/database/DatabaseType.java

@@ -0,0 +1,74 @@
+/*
+ * Copyright © 2019 L2J Server
+ *
+ * This file is part of L2J Server.
+ *
+ * L2J Server is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * L2J Server is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.cli.database;
+
+import java.util.Properties;
+
+/**
+ * DatabaseType.
+ * @author Zoey76
+ * @version 1.0.1
+ */
+public enum DatabaseType {
+	MARIADB("MariaDB", "mariadb", true),
+	MYSQL("MySQL", "mysql", true) {
+		@Override
+		public Properties getParameters() {
+			properties.setProperty("serverTimezone", "UTC");
+			// For MySQL versions previous to 8.0.17:
+			properties.setProperty("useSSL", "false");
+			properties.setProperty("allowPublicKeyRetrieval", "true");
+			return properties;
+		}
+	},
+	SQLSERVER("Microsoft SQL Server", "sqlserver", false),
+	HSQLDB("HyperSQL DataBase", "hsqldb", false),
+	H2("H2 Database Engine", "h2", false),
+	POSTGRESQL("PostgreSQL", "postgresql", false),
+	ORACLE("Oracle Database", "oracle", false),
+	DERBY("Apache Derby", "derby", false);
+	
+	String name;
+	
+	String jdbcPrefix;
+	
+	boolean supported;
+	
+	Properties properties;
+	
+	private DatabaseType(String name, String jdbcPrefix, boolean supported) {
+		this.name = name;
+		this.jdbcPrefix = jdbcPrefix;
+		this.supported = supported;
+		this.properties = new Properties();
+	}
+	
+	public Properties getParameters() {
+		return properties;
+	}
+	
+	public static DatabaseType getType(String url) {
+		for (DatabaseType databaseType : DatabaseType.values()) {
+			if (url != null && url.startsWith("jdbc:" + databaseType.jdbcPrefix)) {
+				return databaseType;
+			}
+		}
+		return null;
+	}
+}

+ 6 - 21
src/main/resources/config/game-server.properties

@@ -1,27 +1,12 @@
-# Database Driver
-# DatabaseDriver=com.mysql.cj.jdbc.Driver
-# DatabaseDriver=org.hsqldb.jdbcDriver
-# DatabaseDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver
-# DatabaseDriver=org.mariadb.jdbc.Driver
-DatabaseDriver=org.mariadb.jdbc.Driver
 # Database URL
-# DatabaseURL=jdbc:mysql://localhost/l2jgs?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
-# DatabaseURL=jdbc:hsqldb:hsql://localhost/l2jgs
-# DatabaseURL=jdbc:sqlserver://localhost/database=l2jgs/user=sa/password=sa
-# DatabaseURL=jdbc:mariadb://localhost/l2jgs?createDatabaseIfNotExist=true
-DatabaseURL=jdbc:mariadb://localhost/l2jgs?createDatabaseIfNotExist=true
+# DatabaseURL=jdbc:mysql://localhost
+# DatabaseURL=jdbc:hsqldb:hsql://localhost
+# DatabaseURL=jdbc:sqlserver://localhost
+# DatabaseURL=jdbc:mariadb://localhost
+DatabaseURL=jdbc:mariadb://localhost
 # Database Name
 DatabaseName=l2jgs
 # Database User
 DatabaseUser=l2j
 # Database Password
-DatabasePassword=l2jserver2019
-# Connection Pool
-# DatabaseConnectionPool=BoneCP
-# DatabaseConnectionPool=C3P0
-# DatabaseConnectionPool=HikariCP
-DatabaseConnectionPool=HikariCP
-# Maximum Pool Size
-DatabaseMaximumPoolSize=100
-# Maximum Idle Time
-DatabaseMaximumIdleTime=0
+DatabasePassword=l2jserver2019

+ 6 - 21
src/main/resources/config/login-server.properties

@@ -1,27 +1,12 @@
-# Database Driver
-# DatabaseDriver=com.mysql.cj.jdbc.Driver
-# DatabaseDriver=org.hsqldb.jdbcDriver
-# DatabaseDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver
-# DatabaseDriver=org.mariadb.jdbc.Driver
-DatabaseDriver=org.mariadb.jdbc.Driver
 # Database URL
-# DatabaseURL=jdbc:mysql://localhost/l2jls?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
-# DatabaseURL=jdbc:hsqldb:hsql://localhost/l2jls
-# DatabaseURL=jdbc:sqlserver://localhost/database=l2jls/user=sa/password=sa
-# DatabaseURL=jdbc:mariadb://localhost/l2jls?createDatabaseIfNotExist=true
-DatabaseURL=jdbc:mariadb://localhost/l2jls?createDatabaseIfNotExist=true
+# DatabaseURL=jdbc:mysql://localhost
+# DatabaseURL=jdbc:hsqldb:hsql://localhost
+# DatabaseURL=jdbc:sqlserver://localhost
+# DatabaseURL=jdbc:mariadb://localhost
+DatabaseURL=jdbc:mariadb://localhost
 # Database Name
 DatabaseName=l2jls
 # Database User
 DatabaseUser=l2j
 # Database Password
-DatabasePassword=l2jserver2019
-# Connection Pool
-# DatabaseConnectionPool=BoneCP
-# DatabaseConnectionPool=C3P0
-# DatabaseConnectionPool=HikariCP
-DatabaseConnectionPool=HikariCP
-# Maximum Pool Size
-DatabaseMaximumPoolSize=100
-# Maximum Idle Time
-DatabaseMaximumIdleTime=0
+DatabasePassword=l2jserver2019