1.1 --- a/src/org/sonews/util/DatabaseSetup.java Sun Aug 29 17:28:58 2010 +0200
1.2 +++ b/src/org/sonews/util/DatabaseSetup.java Mon Aug 30 00:20:06 2010 +0200
1.3 @@ -25,7 +25,6 @@
1.4 import java.sql.Statement;
1.5 import java.util.HashMap;
1.6 import java.util.Map;
1.7 -import org.sonews.config.Config;
1.8 import org.sonews.util.io.Resource;
1.9
1.10 /**
1.11 @@ -33,95 +32,85 @@
1.12 * @author Christian Lins
1.13 * @since sonews/0.5.0
1.14 */
1.15 -public final class DatabaseSetup
1.16 +public final class DatabaseSetup
1.17 {
1.18
1.19 - private static final Map<String, String> templateMap
1.20 - = new HashMap<String, String>();
1.21 - private static final Map<String, StringTemplate> urlMap
1.22 - = new HashMap<String, StringTemplate>();
1.23 - private static final Map<String, String> driverMap
1.24 - = new HashMap<String, String>();
1.25 -
1.26 - static
1.27 - {
1.28 - templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
1.29 - templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
1.30 -
1.31 - urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
1.32 - urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
1.33 -
1.34 - driverMap.put("1", "com.mysql.jdbc.Driver");
1.35 - driverMap.put("2", "org.postgresql.Driver");
1.36 - }
1.37 -
1.38 - public static void main(String[] args)
1.39 - throws Exception
1.40 - {
1.41 - System.out.println("sonews Database setup helper");
1.42 - System.out.println("This program will create a initial database table structure");
1.43 - System.out.println("for the sonews Newsserver.");
1.44 - System.out.println("You need to create a database and a db user manually before!");
1.45 -
1.46 - System.out.println("Select DBMS type:");
1.47 - System.out.println("[1] MySQL 5.x or higher");
1.48 - System.out.println("[2] PostgreSQL 8.x or higher");
1.49 - System.out.print("Your choice: ");
1.50 -
1.51 - BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
1.52 - String dbmsType = in.readLine();
1.53 - String tmplName = templateMap.get(dbmsType);
1.54 - if(tmplName == null)
1.55 - {
1.56 - System.err.println("Invalid choice. Try again you fool!");
1.57 - main(args);
1.58 - return;
1.59 - }
1.60 -
1.61 - // Load JDBC Driver class
1.62 - Class.forName(driverMap.get(dbmsType));
1.63 -
1.64 - String tmpl = Resource.getAsString(tmplName, true);
1.65 -
1.66 - System.out.print("Database server hostname (e.g. localhost): ");
1.67 - String dbHostname = in.readLine();
1.68 -
1.69 - System.out.print("Database name: ");
1.70 - String dbName = in.readLine();
1.71 + private static final Map<String, String> templateMap = new HashMap<String, String>();
1.72 + private static final Map<String, StringTemplate> urlMap = new HashMap<String, StringTemplate>();
1.73 + private static final Map<String, String> driverMap = new HashMap<String, String>();
1.74
1.75 - System.out.print("Give name of DB user that can create tables: ");
1.76 - String dbUser = in.readLine();
1.77 + static {
1.78 + templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
1.79 + templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
1.80
1.81 - System.out.print("Password: ");
1.82 - String dbPassword = in.readLine();
1.83 -
1.84 - String url = urlMap.get(dbmsType)
1.85 - .set("HOSTNAME", dbHostname)
1.86 - .set("DB", dbName).toString();
1.87 -
1.88 - Connection conn =
1.89 - DriverManager.getConnection(url, dbUser, dbPassword);
1.90 - conn.setAutoCommit(false);
1.91 -
1.92 - String[] tmplChunks = tmpl.split(";");
1.93 -
1.94 - for(String chunk : tmplChunks)
1.95 - {
1.96 - if(chunk.trim().equals(""))
1.97 - {
1.98 - continue;
1.99 - }
1.100 -
1.101 - Statement stmt = conn.createStatement();
1.102 - stmt.execute(chunk);
1.103 - }
1.104 -
1.105 - conn.commit();
1.106 - conn.setAutoCommit(true);
1.107 -
1.108 - // Create config file
1.109 -
1.110 - System.out.println("Ok");
1.111 - }
1.112 -
1.113 + urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
1.114 + urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
1.115 +
1.116 + driverMap.put("1", "com.mysql.jdbc.Driver");
1.117 + driverMap.put("2", "org.postgresql.Driver");
1.118 + }
1.119 +
1.120 + public static void main(String[] args)
1.121 + throws Exception
1.122 + {
1.123 + System.out.println("sonews Database setup helper");
1.124 + System.out.println("This program will create a initial database table structure");
1.125 + System.out.println("for the sonews Newsserver.");
1.126 + System.out.println("You need to create a database and a db user manually before!");
1.127 +
1.128 + System.out.println("Select DBMS type:");
1.129 + System.out.println("[1] MySQL 5.x or higher");
1.130 + System.out.println("[2] PostgreSQL 8.x or higher");
1.131 + System.out.print("Your choice: ");
1.132 +
1.133 + BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
1.134 + String dbmsType = in.readLine();
1.135 + String tmplName = templateMap.get(dbmsType);
1.136 + if (tmplName == null) {
1.137 + System.err.println("Invalid choice. Try again you fool!");
1.138 + main(args);
1.139 + return;
1.140 + }
1.141 +
1.142 + // Load JDBC Driver class
1.143 + Class.forName(driverMap.get(dbmsType));
1.144 +
1.145 + String tmpl = Resource.getAsString(tmplName, true);
1.146 +
1.147 + System.out.print("Database server hostname (e.g. localhost): ");
1.148 + String dbHostname = in.readLine();
1.149 +
1.150 + System.out.print("Database name: ");
1.151 + String dbName = in.readLine();
1.152 +
1.153 + System.out.print("Give name of DB user that can create tables: ");
1.154 + String dbUser = in.readLine();
1.155 +
1.156 + System.out.print("Password: ");
1.157 + String dbPassword = in.readLine();
1.158 +
1.159 + String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
1.160 +
1.161 + Connection conn =
1.162 + DriverManager.getConnection(url, dbUser, dbPassword);
1.163 + conn.setAutoCommit(false);
1.164 +
1.165 + String[] tmplChunks = tmpl.split(";");
1.166 +
1.167 + for (String chunk : tmplChunks) {
1.168 + if (chunk.trim().equals("")) {
1.169 + continue;
1.170 + }
1.171 +
1.172 + Statement stmt = conn.createStatement();
1.173 + stmt.execute(chunk);
1.174 + }
1.175 +
1.176 + conn.commit();
1.177 + conn.setAutoCommit(true);
1.178 +
1.179 + // Create config file
1.180 +
1.181 + System.out.println("Ok");
1.182 + }
1.183 }