chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.util; chris@1: chris@1: import java.io.BufferedReader; chris@1: import java.io.InputStreamReader; chris@1: import java.sql.Connection; chris@1: import java.sql.DriverManager; chris@1: import java.sql.Statement; chris@1: import java.util.HashMap; chris@1: import java.util.Map; chris@1: import org.sonews.util.io.Resource; chris@1: chris@1: /** chris@1: * Database setup utility class. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ cli@37: public final class DatabaseSetup chris@1: { chris@1: cli@37: private static final Map templateMap = new HashMap(); cli@37: private static final Map urlMap = new HashMap(); cli@37: private static final Map driverMap = new HashMap(); chris@1: cli@37: static { cli@37: templateMap.put("1", "helpers/database_mysql5_tmpl.sql"); cli@37: templateMap.put("2", "helpers/database_postgresql8_tmpl.sql"); chris@1: cli@37: urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB")); cli@37: urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB")); cli@37: cli@37: driverMap.put("1", "com.mysql.jdbc.Driver"); cli@37: driverMap.put("2", "org.postgresql.Driver"); cli@37: } cli@37: cli@37: public static void main(String[] args) cli@37: throws Exception cli@37: { cli@37: System.out.println("sonews Database setup helper"); cli@37: System.out.println("This program will create a initial database table structure"); cli@37: System.out.println("for the sonews Newsserver."); cli@37: System.out.println("You need to create a database and a db user manually before!"); cli@37: cli@37: System.out.println("Select DBMS type:"); cli@37: System.out.println("[1] MySQL 5.x or higher"); cli@37: System.out.println("[2] PostgreSQL 8.x or higher"); cli@37: System.out.print("Your choice: "); cli@37: cli@37: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); cli@37: String dbmsType = in.readLine(); cli@37: String tmplName = templateMap.get(dbmsType); cli@37: if (tmplName == null) { cli@37: System.err.println("Invalid choice. Try again you fool!"); cli@37: main(args); cli@37: return; cli@37: } cli@37: cli@37: // Load JDBC Driver class cli@37: Class.forName(driverMap.get(dbmsType)); cli@37: cli@37: String tmpl = Resource.getAsString(tmplName, true); cli@37: cli@37: System.out.print("Database server hostname (e.g. localhost): "); cli@37: String dbHostname = in.readLine(); cli@37: cli@37: System.out.print("Database name: "); cli@37: String dbName = in.readLine(); cli@37: cli@37: System.out.print("Give name of DB user that can create tables: "); cli@37: String dbUser = in.readLine(); cli@37: cli@37: System.out.print("Password: "); cli@37: String dbPassword = in.readLine(); cli@37: cli@37: String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString(); cli@37: cli@37: Connection conn = cli@37: DriverManager.getConnection(url, dbUser, dbPassword); cli@37: conn.setAutoCommit(false); cli@37: cli@37: String[] tmplChunks = tmpl.split(";"); cli@37: cli@37: for (String chunk : tmplChunks) { cli@37: if (chunk.trim().equals("")) { cli@37: continue; cli@37: } cli@37: cli@37: Statement stmt = conn.createStatement(); cli@37: stmt.execute(chunk); cli@37: } cli@37: cli@37: conn.commit(); cli@37: conn.setAutoCommit(true); cli@37: cli@37: // Create config file cli@37: cli@37: System.out.println("Ok"); cli@37: } chris@1: }