]> WPIA git - gigi.git/blobdiff - util/club/wpia/gigi/util/DatabaseManager.java
add: support for resetting DB from stdin
[gigi.git] / util / club / wpia / gigi / util / DatabaseManager.java
index 54797224f9d7d8e47eae01a7580628f706bd83ea..a6dc2f7153fb29abd258af12475fb89ee0a31747 100644 (file)
@@ -20,12 +20,19 @@ public class DatabaseManager {
 
     public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
         boolean test = false;
+        boolean stdin = false;
         if (args.length >= 1 && args[0].equals("--test")) {
             test = true;
             String[] ne = new String[args.length - 1];
             System.arraycopy(args, 1, ne, 0, ne.length);
             args = ne;
         }
+        if (args.length >= 1 && args[0].equals("--stdin")) {
+            stdin = true;
+            String[] ne = new String[args.length - 1];
+            System.arraycopy(args, 1, ne, 0, ne.length);
+            args = ne;
+        }
         if (args.length == 0) {
             Properties p = new Properties();
             try (Reader reader = new InputStreamReader(new FileInputStream("config/gigi.properties"), "UTF-8")) {
@@ -39,17 +46,17 @@ public class DatabaseManager {
             System.err.println("Usage: org.postgresql.Driver jdbc:postgresql://localhost/gigi user password");
             return;
         }
-        run(args, test ? ImportType.TEST : ImportType.PRODUCTION);
+        run(args, test ? ImportType.TEST : ImportType.PRODUCTION, stdin);
     }
 
-    public static void run(String[] args, ImportType truncate) throws ClassNotFoundException, SQLException, IOException {
+    public static void run(String[] args, ImportType truncate, boolean stdin) throws ClassNotFoundException, SQLException, IOException {
         Class.forName(args[0]);
         final Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
         try {
             conn.setAutoCommit(false);
             Statement stmt = conn.createStatement();
             try {
-                try (InputStream structure = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
+                try (InputStream structure = stdin ? System.in : DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
                     SQLFileManager.addFile(stmt, structure, truncate);
                 }
                 File localData = new File("doc/sampleData.sql");