Эх сурвалжийг харах

Fixed database installer

Fixes #334
Reported by: maneco2
Zoey76 4 жил өмнө
parent
commit
e923d0d821

+ 1 - 1
pom.xml

@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>com.l2jserver</groupId>
 	<artifactId>l2j-server-cli</artifactId>
-	<version>1.0.5</version>
+	<version>1.0.6</version>
 	<name>L2J Server Command Line</name>
 	<properties>
 		<maven.compiler.source>14</maven.compiler.source>

+ 48 - 35
src/main/java/com/l2jserver/cli/command/database/DatabaseInstallCommand.java

@@ -18,7 +18,10 @@
  */
 package com.l2jserver.cli.command.database;
 
+import static com.l2jserver.cli.model.DatabaseInstallType.FULL;
+
 import java.io.File;
+import java.util.Scanner;
 
 import org.aeonbits.owner.Mutable;
 
@@ -29,46 +32,46 @@ import com.l2jserver.cli.dao.GameServerDatabaseDAO;
 import com.l2jserver.cli.dao.LoginServerDatabaseDAO;
 import com.l2jserver.cli.model.DatabaseInstallType;
 import com.l2jserver.cli.model.ServerType;
+import com.l2jserver.cli.util.CloseShieldInputStreamReader;
 
 import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 
 /**
  * Database install command.
- * 
  * @author Zoey76
  * @version 1.0.2
  */
 @Command(name = "install")
 public class DatabaseInstallCommand extends AbstractCommand {
-
+	
 	@Option(names = "-sql", required = true, description = "SQL Files location")
 	private String path;
-
+	
 	@Option(names = "-url", description = "Database URL")
 	private String url;
-
+	
 	@Option(names = "-db", description = "Database Name")
 	private String name;
-
+	
 	@Option(names = "-u", description = "Database User")
 	private String user;
-
+	
 	@Option(names = "-p", description = "Database Password")
 	private String password;
-
+	
 	@Option(names = "-m", required = true, description = "Database installation mode")
 	private DatabaseInstallType mode;
-
+	
 	@Option(names = "-t", required = true, description = "Server Type")
 	private ServerType serverType;
-
+	
 	@Option(names = "-c", description = "Custom Tables")
 	private boolean customs;
-
+	
 	@Option(names = "-mods", description = "Mods Tables")
 	private boolean mods;
-
+	
 	@Override
 	public void run() {
 		// Validate files exists
@@ -77,57 +80,67 @@ public class DatabaseInstallCommand extends AbstractCommand {
 			System.err.println("The path does not exist!");
 			return;
 		}
-
+		
 		final AbstractDatabaseDAO databaseDAO = databaseDAO();
-
-		if (mode == DatabaseInstallType.FULL) {
-			databaseDAO.createDatabase();
+		
+		try {
+			if (mode == FULL) {
+				databaseDAO.createDatabase();
+			}
+		} catch (Exception ex) {
+			System.out.print("Seems database already exists, do you want to continue installing? (y/N): ");
+			try (var reader = new Scanner(new CloseShieldInputStreamReader(System.in))) {
+				final var input = reader.next();
+				if (!"y".equalsIgnoreCase(input) && !"yes".equalsIgnoreCase(input)) {
+					return;
+				}
+			}
 		}
-
+		
 		databaseDAO.createDump();
-
+		
 		databaseDAO.updates(mode, sqlPath);
-
+		
 		databaseDAO.basic(sqlPath);
-
+		
 		if (customs) {
 			databaseDAO.custom(sqlPath);
 		}
-
+		
 		if (mods) {
 			databaseDAO.mods(sqlPath);
 		}
-
+		
 		System.out.println("Database installation complete.");
 	}
-
+	
 	private AbstractDatabaseDAO databaseDAO() {
 		switch (serverType) {
-		case GAME: {
-			overrideConfigs(Configuration.gameServer());
-			return new GameServerDatabaseDAO();
-		}
-		default:
-		case LOGIN: {
-			overrideConfigs(Configuration.loginServer());
-			return new LoginServerDatabaseDAO();
-		}
+			case GAME: {
+				overrideConfigs(Configuration.gameServer());
+				return new GameServerDatabaseDAO();
+			}
+			default:
+			case LOGIN: {
+				overrideConfigs(Configuration.loginServer());
+				return new LoginServerDatabaseDAO();
+			}
 		}
 	}
-
+	
 	private void overrideConfigs(Mutable databaseConfiguration) {
 		if (url != null) {
 			databaseConfiguration.setProperty("DatabaseURL", url);
 		}
-
+		
 		if (user != null) {
 			databaseConfiguration.setProperty("DatabaseUser", user);
 		}
-
+		
 		if (password != null) {
 			databaseConfiguration.setProperty("DatabasePassword", password);
 		}
-
+		
 		if (name != null) {
 			databaseConfiguration.setProperty("DatabaseName", name);
 		}

+ 1 - 1
src/main/java/com/l2jserver/cli/config/ServerConfiguration.java

@@ -7,7 +7,7 @@ import org.aeonbits.owner.Mutable;
 /**
  * ServerConfiguration.
  * @author Zoey76
- * @version 2.6.1.0
+ * @version 1.0.0
  */
 public interface ServerConfiguration extends Mutable {
 	

+ 1 - 3
src/main/java/com/l2jserver/cli/dao/AbstractDAO.java

@@ -69,12 +69,10 @@ class AbstractDAO {
 		return connectionFactory.getConnection();
 	}
 	
-	public void createDatabase() {
+	public void createDatabase() throws SQLException {
 		try (var con = connectionFactory.getPlainConnection(); //
 			var st = con.createStatement()) {
 			st.executeUpdate(CREATE_DATABASE + database);
-		} catch (SQLException ex) {
-			ex.printStackTrace();
 		}
 	}
 	

+ 39 - 0
src/main/java/com/l2jserver/cli/util/CloseShieldInputStreamReader.java

@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2019-2020 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.util;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * Close Shield InputStreamReader.
+ * @author Zoey76
+ * @version 1.0.6
+ */
+public class CloseShieldInputStreamReader extends InputStreamReader {
+	
+	public CloseShieldInputStreamReader(InputStream inputStream) {
+		super(inputStream);
+	}
+	
+	@Override
+	public void close() {
+		// Do nothing.
+	}
+}