1 package club.wpia.gigi.util;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
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;
15 import club.wpia.gigi.database.DatabaseConnection;
16 import club.wpia.gigi.database.SQLFileManager;
17 import club.wpia.gigi.database.SQLFileManager.ImportType;
19 public class DatabaseManager {
21 public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
23 boolean stdin = false;
24 if (args.length >= 1 && args[0].equals("--test")) {
26 String[] ne = new String[args.length - 1];
27 System.arraycopy(args, 1, ne, 0, ne.length);
30 if (args.length >= 1 && args[0].equals("--stdin")) {
32 String[] ne = new String[args.length - 1];
33 System.arraycopy(args, 1, ne, 0, ne.length);
36 if (args.length == 0) {
37 Properties p = new Properties();
38 try (Reader reader = new InputStreamReader(new FileInputStream("config/gigi.properties"), "UTF-8")) {
42 p.getProperty("sql.driver"), p.getProperty("sql.url"), p.getProperty("sql.user"), p.getProperty("sql.password")
45 if (args.length < 4) {
46 System.err.println("Usage: org.postgresql.Driver jdbc:postgresql://localhost/gigi user password");
49 run(args, test ? ImportType.TEST : ImportType.PRODUCTION, stdin);
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]);
56 conn.setAutoCommit(false);
57 Statement stmt = conn.createStatement();
59 try (InputStream structure = stdin ? System.in : DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
60 SQLFileManager.addFile(stmt, structure, truncate);
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);
73 } catch (SQLException e) {
74 e.getNextException().printStackTrace();