org/sonews/util/DatabaseSetup.java
changeset 35 ed84c8bdd87b
parent 34 9f0b95aafaa3
child 36 c404a87db5b7
     1.1 --- a/org/sonews/util/DatabaseSetup.java	Sun Aug 29 17:04:25 2010 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,127 +0,0 @@
     1.4 -/*
     1.5 - *   SONEWS News Server
     1.6 - *   see AUTHORS for the list of contributors
     1.7 - *
     1.8 - *   This program is free software: you can redistribute it and/or modify
     1.9 - *   it under the terms of the GNU General Public License as published by
    1.10 - *   the Free Software Foundation, either version 3 of the License, or
    1.11 - *   (at your option) any later version.
    1.12 - *
    1.13 - *   This program is distributed in the hope that it will be useful,
    1.14 - *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 - *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 - *   GNU General Public License for more details.
    1.17 - *
    1.18 - *   You should have received a copy of the GNU General Public License
    1.19 - *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 - */
    1.21 -
    1.22 -package org.sonews.util;
    1.23 -
    1.24 -import java.io.BufferedReader;
    1.25 -import java.io.InputStreamReader;
    1.26 -import java.sql.Connection;
    1.27 -import java.sql.DriverManager;
    1.28 -import java.sql.Statement;
    1.29 -import java.util.HashMap;
    1.30 -import java.util.Map;
    1.31 -import org.sonews.config.Config;
    1.32 -import org.sonews.util.io.Resource;
    1.33 -
    1.34 -/**
    1.35 - * Database setup utility class.
    1.36 - * @author Christian Lins
    1.37 - * @since sonews/0.5.0
    1.38 - */
    1.39 -public final class DatabaseSetup 
    1.40 -{
    1.41 -
    1.42 -  private static final Map<String, String> templateMap 
    1.43 -    = new HashMap<String, String>();
    1.44 -  private static final Map<String, StringTemplate> urlMap
    1.45 -    = new HashMap<String, StringTemplate>();
    1.46 -  private static final Map<String, String> driverMap
    1.47 -    = new HashMap<String, String>();
    1.48 -  
    1.49 -  static
    1.50 -  {
    1.51 -    templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
    1.52 -    templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
    1.53 -    
    1.54 -    urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
    1.55 -    urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
    1.56 -    
    1.57 -    driverMap.put("1", "com.mysql.jdbc.Driver");
    1.58 -    driverMap.put("2", "org.postgresql.Driver");
    1.59 -  }
    1.60 -  
    1.61 -  public static void main(String[] args)
    1.62 -    throws Exception
    1.63 -  {
    1.64 -    System.out.println("sonews Database setup helper");
    1.65 -    System.out.println("This program will create a initial database table structure");
    1.66 -    System.out.println("for the sonews Newsserver.");
    1.67 -    System.out.println("You need to create a database and a db user manually before!");
    1.68 -    
    1.69 -    System.out.println("Select DBMS type:");
    1.70 -    System.out.println("[1] MySQL 5.x or higher");
    1.71 -    System.out.println("[2] PostgreSQL 8.x or higher");
    1.72 -    System.out.print("Your choice: ");
    1.73 -    
    1.74 -    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    1.75 -    String dbmsType = in.readLine();
    1.76 -    String tmplName = templateMap.get(dbmsType);
    1.77 -    if(tmplName == null)
    1.78 -    {
    1.79 -      System.err.println("Invalid choice. Try again you fool!");
    1.80 -      main(args);
    1.81 -      return;
    1.82 -    }
    1.83 -    
    1.84 -    // Load JDBC Driver class
    1.85 -    Class.forName(driverMap.get(dbmsType));
    1.86 -    
    1.87 -    String tmpl = Resource.getAsString(tmplName, true);
    1.88 -    
    1.89 -    System.out.print("Database server hostname (e.g. localhost): ");
    1.90 -    String dbHostname = in.readLine();
    1.91 -    
    1.92 -    System.out.print("Database name: ");
    1.93 -    String dbName = in.readLine();
    1.94 -
    1.95 -    System.out.print("Give name of DB user that can create tables: ");
    1.96 -    String dbUser = in.readLine();
    1.97 -
    1.98 -    System.out.print("Password: ");
    1.99 -    String dbPassword = in.readLine();
   1.100 -    
   1.101 -    String url = urlMap.get(dbmsType)
   1.102 -      .set("HOSTNAME", dbHostname)
   1.103 -      .set("DB", dbName).toString();
   1.104 -    
   1.105 -    Connection conn = 
   1.106 -      DriverManager.getConnection(url, dbUser, dbPassword);
   1.107 -    conn.setAutoCommit(false);
   1.108 -    
   1.109 -    String[] tmplChunks = tmpl.split(";");
   1.110 -    
   1.111 -    for(String chunk : tmplChunks)
   1.112 -    {
   1.113 -      if(chunk.trim().equals(""))
   1.114 -      {
   1.115 -        continue;
   1.116 -      }
   1.117 -      
   1.118 -      Statement stmt = conn.createStatement();
   1.119 -      stmt.execute(chunk);
   1.120 -    }
   1.121 -    
   1.122 -    conn.commit();
   1.123 -    conn.setAutoCommit(true);
   1.124 -    
   1.125 -    // Create config file
   1.126 -    
   1.127 -    System.out.println("Ok");
   1.128 -  }
   1.129 -  
   1.130 -}