src/org/sonews/util/DatabaseSetup.java
changeset 48 b78e77619152
parent 37 74139325d305
     1.1 --- a/src/org/sonews/util/DatabaseSetup.java	Sun Aug 29 18:17:37 2010 +0200
     1.2 +++ b/src/org/sonews/util/DatabaseSetup.java	Sun Sep 11 15:05:04 2011 +0200
     1.3 @@ -15,13 +15,13 @@
     1.4   *   You should have received a copy of the GNU General Public License
     1.5   *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.6   */
     1.7 -
     1.8  package org.sonews.util;
     1.9  
    1.10  import java.io.BufferedReader;
    1.11  import java.io.InputStreamReader;
    1.12  import java.sql.Connection;
    1.13  import java.sql.DriverManager;
    1.14 +import java.sql.SQLException;
    1.15  import java.sql.Statement;
    1.16  import java.util.HashMap;
    1.17  import java.util.Map;
    1.18 @@ -32,8 +32,7 @@
    1.19   * @author Christian Lins
    1.20   * @since sonews/0.5.0
    1.21   */
    1.22 -public final class DatabaseSetup
    1.23 -{
    1.24 +public final class DatabaseSetup {
    1.25  
    1.26  	private static final Map<String, String> templateMap = new HashMap<String, String>();
    1.27  	private static final Map<String, StringTemplate> urlMap = new HashMap<String, StringTemplate>();
    1.28 @@ -42,58 +41,79 @@
    1.29  	static {
    1.30  		templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
    1.31  		templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
    1.32 +		templateMap.put("3", "helpers/database_hsqldb_tmpl.sql");
    1.33  
    1.34  		urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
    1.35  		urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
    1.36  
    1.37  		driverMap.put("1", "com.mysql.jdbc.Driver");
    1.38  		driverMap.put("2", "org.postgresql.Driver");
    1.39 +		driverMap.put("3", "org.hsqldb.jdbcDriver");
    1.40  	}
    1.41  
    1.42  	public static void main(String[] args)
    1.43 -		throws Exception
    1.44 -	{
    1.45 -		System.out.println("sonews Database setup helper");
    1.46 -		System.out.println("This program will create a initial database table structure");
    1.47 -		System.out.println("for the sonews Newsserver.");
    1.48 -		System.out.println("You need to create a database and a db user manually before!");
    1.49 +			throws Exception {
    1.50 +		
    1.51 +		loadJDBCDriver();
    1.52  
    1.53 -		System.out.println("Select DBMS type:");
    1.54 -		System.out.println("[1] MySQL 5.x or higher");
    1.55 -		System.out.println("[2] PostgreSQL 8.x or higher");
    1.56 -		System.out.print("Your choice: ");
    1.57 +		if (args.length == 0) {
    1.58 +			System.out.println("sonews Database setup helper");
    1.59 +			System.out.println("This program will create a initial database table structure");
    1.60 +			System.out.println("for the sonews Newsserver.");
    1.61 +			System.out.println("You need to create a database and a db user manually before!");
    1.62  
    1.63 -		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    1.64 -		String dbmsType = in.readLine();
    1.65 -		String tmplName = templateMap.get(dbmsType);
    1.66 -		if (tmplName == null) {
    1.67 -			System.err.println("Invalid choice. Try again you fool!");
    1.68 -			main(args);
    1.69 -			return;
    1.70 +			System.out.println("Select DBMS type:");
    1.71 +			System.out.println("[1] MySQL 5.x or higher");
    1.72 +			System.out.println("[2] PostgreSQL 8.x or higher");
    1.73 +			System.out.print("Your choice: ");
    1.74 +
    1.75 +			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    1.76 +			String dbmsType = in.readLine();
    1.77 +			String tmplName = templateMap.get(dbmsType);
    1.78 +			if (tmplName == null) {
    1.79 +				System.err.println("Invalid choice. Try again you fool!");
    1.80 +				main(args);
    1.81 +				return;
    1.82 +			}
    1.83 +
    1.84 +			String tmpl = Resource.getAsString(tmplName, true);
    1.85 +
    1.86 +			System.out.print("Database server hostname (e.g. localhost): ");
    1.87 +			String dbHostname = in.readLine();
    1.88 +
    1.89 +			System.out.print("Database name: ");
    1.90 +			String dbName = in.readLine();
    1.91 +
    1.92 +			System.out.print("Give name of DB user that can create tables: ");
    1.93 +			String dbUser = in.readLine();
    1.94 +
    1.95 +			System.out.print("Password: ");
    1.96 +			String dbPassword = in.readLine();
    1.97 +
    1.98 +			String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
    1.99 +			createTables(tmpl, url, dbUser, dbPassword);
   1.100 +
   1.101 +			// TODO: Create config file
   1.102 +
   1.103 +		} else if(args.length == 4) {
   1.104 +			String tmplName = args[0];
   1.105 +			String url = args[1];
   1.106 +			String dbUser = args[2];
   1.107 +			String dbPassword = args[3];
   1.108 +
   1.109 +			String tmpl = Resource.getAsString(tmplName, true);
   1.110 +			createTables(tmpl, url, dbUser, dbPassword);
   1.111 +		} else {
   1.112 +			System.out.println("Wrong number of parameters!");
   1.113  		}
   1.114  
   1.115 -		// Load JDBC Driver class
   1.116 -		Class.forName(driverMap.get(dbmsType));
   1.117 +		System.out.println("Ok");
   1.118 +	}
   1.119  
   1.120 -		String tmpl = Resource.getAsString(tmplName, true);
   1.121 -
   1.122 -		System.out.print("Database server hostname (e.g. localhost): ");
   1.123 -		String dbHostname = in.readLine();
   1.124 -
   1.125 -		System.out.print("Database name: ");
   1.126 -		String dbName = in.readLine();
   1.127 -
   1.128 -		System.out.print("Give name of DB user that can create tables: ");
   1.129 -		String dbUser = in.readLine();
   1.130 -
   1.131 -		System.out.print("Password: ");
   1.132 -		String dbPassword = in.readLine();
   1.133 -
   1.134 -		String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
   1.135 -
   1.136 +	public static void createTables(String tmpl, String url, String dbUser, String dbPassword)
   1.137 +			throws SQLException {
   1.138  		Connection conn =
   1.139 -			DriverManager.getConnection(url, dbUser, dbPassword);
   1.140 -		conn.setAutoCommit(false);
   1.141 +				DriverManager.getConnection(url, dbUser, dbPassword);
   1.142  
   1.143  		String[] tmplChunks = tmpl.split(";");
   1.144  
   1.145 @@ -107,10 +127,16 @@
   1.146  		}
   1.147  
   1.148  		conn.commit();
   1.149 -		conn.setAutoCommit(true);
   1.150 +		conn.close();
   1.151 +	}
   1.152  
   1.153 -		// Create config file
   1.154 -
   1.155 -		System.out.println("Ok");
   1.156 +	public static void loadJDBCDriver() {
   1.157 +		for(String className : driverMap.values()) {
   1.158 +			try {
   1.159 +				Class.forName(className);
   1.160 +			} catch (ClassNotFoundException ex) {
   1.161 +				System.out.println("Could not load JDBC driver: " + className);
   1.162 +			}
   1.163 +		}
   1.164  	}
   1.165  }