1
2
3
4 package com.webstersmalley.mediacollection.database;
5
6 import java.io.IOException;
7 import java.sql.SQLException;
8
9 import org.apache.log4j.Logger;
10 import org.apache.log4j.PropertyConfigurator;
11
12 public class Installer
13 {
14 private static Logger log = Logger.getLogger(Installer.class);
15 static
16 {
17 PropertyConfigurator.configure("log4j.properties");
18 }
19
20 private DatabaseConnection dbconn;
21 private String databaseName;
22
23 public Installer(String databaseName)
24 {
25 this.databaseName = databaseName;
26 }
27
28 private void drop() throws IOException
29 {
30 DatabaseConnection.drop(databaseName);
31 }
32
33 private void runDDL() throws IOException, SQLException
34 {
35 dbconn.runScript("db/create_tables.sql");
36 dbconn.runScript("db/create_constraints.sql");
37 dbconn.runScript("db/create_views.sql");
38 }
39
40 private void insertStatic() throws IOException, SQLException
41 {
42 dbconn.runScript("db/insert_static.sql");
43 }
44
45 private void recreate() throws ClassNotFoundException, IOException, SQLException
46 {
47 runDDL();
48 insertStatic();
49 }
50
51 private void shutdown() throws ClassNotFoundException, SQLException
52 {
53 DatabaseConnection.shutdown(databaseName);
54 }
55
56 public void go()
57 {
58 try
59 {
60 log.info("Dropping current database");
61 drop();
62 log.info("Recreating new database");
63 dbconn = new DatabaseConnection(databaseName, true);
64 recreate();
65 log.info("Creation complete - shutting down");
66 shutdown();
67 }
68 catch (Exception e)
69 {
70 log.error("Error creating database", e);
71 }
72 }
73
74 public static void main(String[] args)
75 {
76 log.info("Installation process started");
77 Installer install = new Installer("mcdb");
78 install.go();
79 }
80 }