src/org/sonews/util/DatabaseSetup.java
author cli
Sun, 29 Aug 2010 18:17:37 +0200
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 44 5d7d1adf387f
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.util;
    20 
    21 import java.io.BufferedReader;
    22 import java.io.InputStreamReader;
    23 import java.sql.Connection;
    24 import java.sql.DriverManager;
    25 import java.sql.Statement;
    26 import java.util.HashMap;
    27 import java.util.Map;
    28 import org.sonews.util.io.Resource;
    29 
    30 /**
    31  * Database setup utility class.
    32  * @author Christian Lins
    33  * @since sonews/0.5.0
    34  */
    35 public final class DatabaseSetup
    36 {
    37 
    38 	private static final Map<String, String> templateMap = new HashMap<String, String>();
    39 	private static final Map<String, StringTemplate> urlMap = new HashMap<String, StringTemplate>();
    40 	private static final Map<String, String> driverMap = new HashMap<String, String>();
    41 
    42 	static {
    43 		templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
    44 		templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
    45 
    46 		urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
    47 		urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
    48 
    49 		driverMap.put("1", "com.mysql.jdbc.Driver");
    50 		driverMap.put("2", "org.postgresql.Driver");
    51 	}
    52 
    53 	public static void main(String[] args)
    54 		throws Exception
    55 	{
    56 		System.out.println("sonews Database setup helper");
    57 		System.out.println("This program will create a initial database table structure");
    58 		System.out.println("for the sonews Newsserver.");
    59 		System.out.println("You need to create a database and a db user manually before!");
    60 
    61 		System.out.println("Select DBMS type:");
    62 		System.out.println("[1] MySQL 5.x or higher");
    63 		System.out.println("[2] PostgreSQL 8.x or higher");
    64 		System.out.print("Your choice: ");
    65 
    66 		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    67 		String dbmsType = in.readLine();
    68 		String tmplName = templateMap.get(dbmsType);
    69 		if (tmplName == null) {
    70 			System.err.println("Invalid choice. Try again you fool!");
    71 			main(args);
    72 			return;
    73 		}
    74 
    75 		// Load JDBC Driver class
    76 		Class.forName(driverMap.get(dbmsType));
    77 
    78 		String tmpl = Resource.getAsString(tmplName, true);
    79 
    80 		System.out.print("Database server hostname (e.g. localhost): ");
    81 		String dbHostname = in.readLine();
    82 
    83 		System.out.print("Database name: ");
    84 		String dbName = in.readLine();
    85 
    86 		System.out.print("Give name of DB user that can create tables: ");
    87 		String dbUser = in.readLine();
    88 
    89 		System.out.print("Password: ");
    90 		String dbPassword = in.readLine();
    91 
    92 		String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
    93 
    94 		Connection conn =
    95 			DriverManager.getConnection(url, dbUser, dbPassword);
    96 		conn.setAutoCommit(false);
    97 
    98 		String[] tmplChunks = tmpl.split(";");
    99 
   100 		for (String chunk : tmplChunks) {
   101 			if (chunk.trim().equals("")) {
   102 				continue;
   103 			}
   104 
   105 			Statement stmt = conn.createStatement();
   106 			stmt.execute(chunk);
   107 		}
   108 
   109 		conn.commit();
   110 		conn.setAutoCommit(true);
   111 
   112 		// Create config file
   113 
   114 		System.out.println("Ok");
   115 	}
   116 }