View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical.jdbc;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Shared
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  import java.sql.CallableStatement;
24  import java.sql.Connection;
25  import java.sql.SQLException;
26  
27  public class OracleStatements {
28  
29      private static final String DISABLE_ALL_CONSTRAINTS_SQL = "BEGIN\n"+
30              "  FOR c IN\n"+
31              "  (SELECT c.owner, c.table_name, c.constraint_name\n"+
32              "   FROM user_constraints c, user_tables t\n"+
33              "   WHERE c.table_name = t.table_name\n"+
34              "   AND c.status = 'ENABLED'\n" +
35              "   AND c.constraint_type IN ('P', 'R')\n"+
36              "   AND NOT (t.iot_type IS NOT NULL AND c.constraint_type = 'P')\n"+
37              "   ORDER BY c.constraint_type DESC)\n"+
38              "  LOOP\n"+
39              "    dbms_utility.exec_ddl_statement('alter table \"' || c.owner || '\".\"' || c.table_name || '\" disable constraint ' || c.constraint_name);\n"+
40              "  END LOOP;\n"+
41              "END;";
42  
43      private static final String ENABLE_ALL_CONSTRAINTS_SQL = "BEGIN\n" +
44              "  FOR c IN\n" +
45              "  (SELECT c.owner, c.table_name, c.constraint_name\n" +
46              "   FROM user_constraints c, user_tables t\n" +
47              "   WHERE c.table_name = t.table_name\n" +
48              "   AND c.status = 'DISABLED'\n" +
49              "   AND c.constraint_type IN ('P', 'R')\n" +
50              "   ORDER BY c.constraint_type)\n" +
51              "  LOOP\n" +
52              "    dbms_utility.exec_ddl_statement('alter table \"' || c.owner || '\".\"' || c.table_name || '\" enable constraint ' || c.constraint_name);\n" +
53              "  END LOOP;\n" +
54              "END;";
55  
56      //Disable all delete triggers (need to load database for tests
57      private static final String DISABLE_DELETE_TRIGGERS_SQL = "DECLARE\n" +
58              "  CURSOR cursorTriggerNames is\n" +
59              "    select\n" +
60              "      TRIGGER_NAME\n" +
61              "    from\n" +
62              "      user_triggers\n" +
63              "    where\n" +
64              "      trigger_name like 'TBD_%'\n" +
65              "      and STATUS = 'ENABLED';\n" +
66              "  cur integer;\n" +
67              "  rc  integer;\n" +
68              "  triggerName VARCHAR2(100);\n" +
69              "  sqlQuery VARCHAR2(2000);\n" +
70              "BEGIN\n" +
71              "  cur := DBMS_SQL.OPEN_CURSOR;\n" +
72              "  DBMS_OUTPUT.PUT_LINE('Disabling delete triggers...');\n" +
73              "  for cursorTriggerName in cursorTriggerNames loop\n" +
74              "      triggerName := cursorTriggerName.TRIGGER_NAME;\n" +
75              "      DBMS_OUTPUT.PUT_LINE('Disable trigger '''|| triggerName ||'''');\n" +
76              "      sqlQuery := 'ALTER TRIGGER '||triggerName||' DISABLE';\n" +
77              "      DBMS_SQL.PARSE(cur, sqlQuery, DBMS_SQL.NATIVE);\n" +
78              "      rc := DBMS_SQL.EXECUTE(cur);\n" +
79              "    end loop;\n" +
80              "  DBMS_OUTPUT.PUT_LINE('Disabling delete triggers... [OK]');\n" +
81              "  COMMIT;\n" +
82              "  DBMS_SQL.CLOSE_CURSOR(cur);\n" +
83              "END;";
84  
85      // Enable all delete triggers (need to load database for tests)
86      private static final String ENABLE_DELETE_TRIGGERS_SQL = "DECLARE\n" +
87              "  CURSOR cursorTriggerNames is\n" +
88              "    select\n" +
89              "      TRIGGER_NAME\n" +
90              "    from\n" +
91              "      user_triggers\n" +
92              "    where\n" +
93              "      trigger_name like 'TBD_%'\n" +
94              "      and STATUS = 'DISABLED';\n" +
95              "  cur integer;\n" +
96              "  rc  integer;\n" +
97              "  triggerName VARCHAR2(100);\n" +
98              "  sqlQuery VARCHAR2(2000);\n" +
99              "BEGIN\n" +
100             "  cur := DBMS_SQL.OPEN_CURSOR;\n" +
101             "  DBMS_OUTPUT.PUT_LINE('Enabling delete triggers...');\n" +
102             "  for cursorTriggerName in cursorTriggerNames loop\n" +
103             "      triggerName := cursorTriggerName.TRIGGER_NAME;\n" +
104             "      DBMS_OUTPUT.PUT_LINE('Enable trigger '''|| triggerName ||'''');\n" +
105             "      sqlQuery := 'ALTER TRIGGER '||triggerName||' ENABLE';\n" +
106             "      DBMS_SQL.PARSE(cur, sqlQuery, DBMS_SQL.NATIVE);\n" +
107             "      rc := DBMS_SQL.EXECUTE(cur);\n" +
108             "    end loop;\n" +
109             "  DBMS_OUTPUT.PUT_LINE('Enabling delete triggers... [OK]');\n" +
110             "  COMMIT;\n" +
111             "  DBMS_SQL.CLOSE_CURSOR(cur);\n" +
112             "END;";
113 
114     public static void setIntegrityConstraints(Connection connection, boolean enableIntegrityConstraints) throws SQLException{
115 
116         // Disable
117         if (!enableIntegrityConstraints) {
118             executeProcedure(connection, DISABLE_ALL_CONSTRAINTS_SQL);
119         }
120 
121         // Enable
122         else {
123             executeProcedure(connection, ENABLE_ALL_CONSTRAINTS_SQL);
124         }
125     }
126 
127     public static void setEnableDeleteTriggers(Connection connection, boolean enableDeleteTriggers) throws SQLException{
128 
129         // Disable
130         if (!enableDeleteTriggers) {
131             executeProcedure(connection, DISABLE_DELETE_TRIGGERS_SQL);
132         }
133 
134         // Enable
135         else {
136             executeProcedure(connection, ENABLE_DELETE_TRIGGERS_SQL);
137         }
138     }
139 
140     private static void executeProcedure(Connection connection, String procedure) throws SQLException {
141         CallableStatement stat = connection.prepareCall(procedure);
142         stat.executeUpdate();
143     }
144 }