]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/CACertificate.java
fix: SQL change database call pattern
[gigi.git] / src / org / cacert / gigi / dbObjects / CACertificate.java
index cc5bcd8935c43fdcf795a8649839503e71fcc0d9..41401b6ddab883b84e10180f766bf1492882d212 100644 (file)
@@ -13,7 +13,6 @@ import java.util.HashMap;
 
 import javax.security.auth.x500.X500Principal;
 
-import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.util.ServerConstants;
@@ -32,17 +31,19 @@ public class CACertificate implements IdCachable {
 
     private CACertificate(int id) {
         this.id = id;
-        GigiPreparedStatement conn = DatabaseConnection.getInstance().prepare("SELECT `keyname`, `parentRoot`, `link` FROM `cacerts` WHERE `id`=?");
-        conn.setInt(1, id);
-        GigiResultSet res = conn.executeQuery();
-        if ( !res.next()) {
-            throw new IllegalArgumentException();
-        }
-        keyname = res.getString("keyname");
-        link = res.getString("link");
-        int parentRoot = res.getInt("parentRoot");
-        if (res.next()) {
-            throw new RuntimeException("DB is broken");
+        int parentRoot;
+        try (GigiPreparedStatement conn = new GigiPreparedStatement("SELECT `keyname`, `parentRoot`, `link` FROM `cacerts` WHERE `id`=?")) {
+            conn.setInt(1, id);
+            GigiResultSet res = conn.executeQuery();
+            if ( !res.next()) {
+                throw new IllegalArgumentException();
+            }
+            keyname = res.getString("keyname");
+            link = res.getString("link");
+            parentRoot = res.getInt("parentRoot");
+            if (res.next()) {
+                throw new RuntimeException("DB is broken");
+            }
         }
         if (parentRoot == id) {
             parent = this;
@@ -110,39 +111,42 @@ public class CACertificate implements IdCachable {
 
                 X500Principal subj = toInsert.getSubjectX500Principal();
                 boolean self = toInsert.getIssuerX500Principal().equals(subj);
-                GigiPreparedStatement q = DatabaseConnection.getInstance().prepare("SELECT `id`, `parentRoot` FROM `cacerts` WHERE `keyname`=?");
-                q.setString(1, names.get(subj));
-                GigiResultSet res = q.executeQuery();
-                int id;
-                if (res.next()) {
-                    id = res.getInt("id");
-                    if (res.getInt("parentRoot") != (self ? id : inserted.get(toInsert.getIssuerX500Principal()))) {
-                        throw new Error("Invalid DB structure: " + subj + "->" + inserted.get(toInsert.getIssuerX500Principal()) + " vs " + res.getInt("parentRoot"));
-                    }
-                } else {
-                    String link;
-                    String keyname = names.get(subj);
-                    if ( !keyname.contains("_")) {
-                        link = "https://g2.crt." + ServerConstants.getSuffix() + "/g2/" + keyname + ".crt";
+                try (GigiPreparedStatement q = new GigiPreparedStatement("SELECT `id`, `parentRoot` FROM `cacerts` WHERE `keyname`=?")) {
+                    q.setString(1, names.get(subj));
+                    GigiResultSet res = q.executeQuery();
+                    int id;
+                    if (res.next()) {
+                        id = res.getInt("id");
+                        if (res.getInt("parentRoot") != (self ? id : inserted.get(toInsert.getIssuerX500Principal()))) {
+                            throw new Error("Invalid DB structure: " + subj + "->" + inserted.get(toInsert.getIssuerX500Principal()) + " vs " + res.getInt("parentRoot"));
+                        }
                     } else {
-                        String[] parts = keyname.split("_");
-                        link = "https://g2.crt." + ServerConstants.getSuffix() + "/g2/" + parts[1] + "/" + parts[0] + "-" + parts[2] + ".crt";
-
-                    }
-                    GigiPreparedStatement q2 = DatabaseConnection.getInstance().prepare("INSERT INTO `cacerts` SET `parentRoot`=?, `keyname`=?, `link`=?");
-                    q2.setInt(1, self ? 0 : inserted.get(toInsert.getIssuerX500Principal()));
-                    q2.setString(2, keyname);
-                    q2.setString(3, link);
-                    q2.execute();
-                    id = q2.lastInsertId();
-                    if (self) {
-                        GigiPreparedStatement q3 = DatabaseConnection.getInstance().prepare("UPDATE `cacerts` SET `parentRoot`=?, `id`=?");
-                        q3.setInt(1, id);
-                        q3.setInt(2, id);
-                        q3.execute();
+                        String link;
+                        String keyname = names.get(subj);
+                        if ( !keyname.contains("_")) {
+                            link = "https://g2.crt." + ServerConstants.getSuffix() + "/g2/" + keyname + ".crt";
+                        } else {
+                            String[] parts = keyname.split("_");
+                            link = "https://g2.crt." + ServerConstants.getSuffix() + "/g2/" + parts[1] + "/" + parts[0] + "-" + parts[2] + ".crt";
+
+                        }
+                        try (GigiPreparedStatement q2 = new GigiPreparedStatement("INSERT INTO `cacerts` SET `parentRoot`=?, `keyname`=?, `link`=?")) {
+                            q2.setInt(1, self ? 0 : inserted.get(toInsert.getIssuerX500Principal()));
+                            q2.setString(2, keyname);
+                            q2.setString(3, link);
+                            q2.execute();
+                            id = q2.lastInsertId();
+                        }
+                        if (self) {
+                            try (GigiPreparedStatement q3 = new GigiPreparedStatement("UPDATE `cacerts` SET `parentRoot`=?, `id`=?")) {
+                                q3.setInt(1, id);
+                                q3.setInt(2, id);
+                                q3.execute();
+                            }
+                        }
                     }
+                    inserted.put(subj, id);
                 }
-                inserted.put(subj, id);
             }
         }
     }