]> WPIA git - gigi.git/blob - util/club/wpia/gigi/util/DatabaseManager.java
a6dc2f7153fb29abd258af12475fb89ee0a31747
[gigi.git] / util / club / wpia / gigi / util / DatabaseManager.java
1 package club.wpia.gigi.util;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.io.Reader;
9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.SQLException;
12 import java.sql.Statement;
13 import java.util.Properties;
14
15 import club.wpia.gigi.database.DatabaseConnection;
16 import club.wpia.gigi.database.SQLFileManager;
17 import club.wpia.gigi.database.SQLFileManager.ImportType;
18
19 public class DatabaseManager {
20
21     public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
22         boolean test = false;
23         boolean stdin = false;
24         if (args.length >= 1 && args[0].equals("--test")) {
25             test = true;
26             String[] ne = new String[args.length - 1];
27             System.arraycopy(args, 1, ne, 0, ne.length);
28             args = ne;
29         }
30         if (args.length >= 1 && args[0].equals("--stdin")) {
31             stdin = true;
32             String[] ne = new String[args.length - 1];
33             System.arraycopy(args, 1, ne, 0, ne.length);
34             args = ne;
35         }
36         if (args.length == 0) {
37             Properties p = new Properties();
38             try (Reader reader = new InputStreamReader(new FileInputStream("config/gigi.properties"), "UTF-8")) {
39                 p.load(reader);
40             }
41             args = new String[] {
42                     p.getProperty("sql.driver"), p.getProperty("sql.url"), p.getProperty("sql.user"), p.getProperty("sql.password")
43             };
44         }
45         if (args.length < 4) {
46             System.err.println("Usage: org.postgresql.Driver jdbc:postgresql://localhost/gigi user password");
47             return;
48         }
49         run(args, test ? ImportType.TEST : ImportType.PRODUCTION, stdin);
50     }
51
52     public static void run(String[] args, ImportType truncate, boolean stdin) throws ClassNotFoundException, SQLException, IOException {
53         Class.forName(args[0]);
54         final Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
55         try {
56             conn.setAutoCommit(false);
57             Statement stmt = conn.createStatement();
58             try {
59                 try (InputStream structure = stdin ? System.in : DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
60                     SQLFileManager.addFile(stmt, structure, truncate);
61                 }
62                 File localData = new File("doc/sampleData.sql");
63                 if (localData.exists()) {
64                     try (FileInputStream f = new FileInputStream(localData)) {
65                         SQLFileManager.addFile(stmt, f, ImportType.SAMPLE_DATA);
66                     }
67                 }
68                 stmt.executeBatch();
69                 conn.commit();
70             } finally {
71                 stmt.close();
72             }
73         } catch (SQLException e) {
74             e.getNextException().printStackTrace();
75             throw e;
76         } finally {
77             conn.close();
78         }
79     }
80 }