]> gitweb.ps.run Git - l21s-case-study/commitdiff
add vim fold marker
authorpatrick-scho <patrick.schoenberger@posteo.de>
Sun, 23 Nov 2025 23:20:04 +0000 (23:20 +0000)
committerpatrick-scho <patrick.schoenberger@posteo.de>
Sun, 23 Nov 2025 23:20:04 +0000 (23:20 +0000)
src/DbMigration.java

index bcf22d901eb6688f5110c7778a92df8785de205f..9357811ad90a2d791bf77018a9e03491b1005bcc 100644 (file)
@@ -4,7 +4,7 @@ import java.sql.*;
 import java.util.*;
 
 class DbMigration {
-    // constants
+    // constants {{{
     static final boolean CONFIG_PRINT_DB = false;
     static final String CSV_SEPARATOR = ",";
     static final String DB_URL = "jdbc:postgresql:users";
@@ -20,20 +20,23 @@ class DbMigration {
                 CanDelete = EXCLUDED.CanDelete
               ;
             """;
+    // }}}
 
-    // user
+    // user {{{
     public record User(int id, String mail, Role role) {
         public String toString() {
             return this.id + " " + this.mail + " " + this.role;
         }
     }
+    // }}}
 
-    // role
+    // role {{{
     public record Role(boolean read, boolean write, boolean delete) {
         static final Role Default = new Role(false, false, false);
     }
+    // }}}
 
-    // csv
+    // csv {{{
     public static String[][] LoadCSV(String filename) throws IOException {
         var path = Paths.get(filename);
         return Files.lines(path)
@@ -41,8 +44,9 @@ class DbMigration {
                 .map(line -> line.split(CSV_SEPARATOR))
                 .toArray(String[][]::new);
     }
+    // }}}
 
-    // db
+    // db {{{
     public static void insertUser(PreparedStatement stmt, User user) throws SQLException {
         stmt.setInt(1, user.id);
         stmt.setString(2, user.mail);
@@ -69,18 +73,20 @@ class DbMigration {
 
         return users.toArray(User[]::new);
     }
+    // }}}
 
-    // main
+    // main {{{
     public static void main(String[] args) {
-        // filenames from command line args
+        // filenames from command line args {{{
         if (args.length != 2) {
             System.out.printf("Usage: java src/DbMigrations.java <users.csv> <roles.csv>\n");
             return;
         }
         String filenameUsers = args[0];
         String filenameRoles = args[1];
+        // }}}
 
-        // read csvs
+        // read csvs {{{
         String[][] csvUsers;
         String[][] csvRoles;
         try {
@@ -90,8 +96,9 @@ class DbMigration {
             System.out.printf("Expection occured while trying to load CSV: %s\n", e);
             return;
         }
+        // }}}
 
-        // process roles
+        // process roles {{{
         var roles = new HashMap<Integer, Role>();
         for (var row : csvRoles) {
             // get id and single role
@@ -111,8 +118,9 @@ class DbMigration {
                         return role;
                     });
         }
+        // }}}
 
-        // OPTIONAL: open db connection for reading
+        // OPTIONAL: open db connection for reading {{{
         if (CONFIG_PRINT_DB) {
             try (var conn = DriverManager.getConnection(DB_URL)) {
 
@@ -127,8 +135,9 @@ class DbMigration {
                 return;
             }
         }
+        // }}}
 
-        // open db connection for updating
+        // open db connection for updating {{{
         try (var conn = DriverManager.getConnection(DB_URL);
                 var stmt = conn.prepareStatement(SQL_UPSERT_USER); ) {
             // batching
@@ -151,7 +160,9 @@ class DbMigration {
             System.out.printf("Expection occured while accessing database: %s\n", e);
             return;
         }
+        // }}}
 
         System.out.println("Updated successfully");
     }
+    // }}}
 }