|
| 1 | +/* |
| 2 | + * Copyright 2018 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.utplsql.sqldev.oddgen |
| 17 | + |
| 18 | +import java.sql.Connection |
| 19 | +import java.util.ArrayList |
| 20 | +import java.util.HashMap |
| 21 | +import java.util.HashSet |
| 22 | +import java.util.LinkedHashMap |
| 23 | +import java.util.List |
| 24 | +import oracle.ide.config.Preferences |
| 25 | +import org.oddgen.sqldev.generators.OddgenGenerator2 |
| 26 | +import org.oddgen.sqldev.generators.model.Node |
| 27 | +import org.utplsql.sqldev.dal.UtplsqlDao |
| 28 | +import org.utplsql.sqldev.model.preference.PreferenceModel |
| 29 | +import org.utplsql.sqldev.resources.UtplsqlResources |
| 30 | + |
| 31 | +class RunGenerator implements OddgenGenerator2 { |
| 32 | + |
| 33 | + public static val YES = "Yes" |
| 34 | + public static val NO = "No" |
| 35 | + |
| 36 | + public static var RESET_PACKAGE = UtplsqlResources.getString("PREF_RESET_PACKAGE_LABEL") |
| 37 | + public static var CLEAR_SCREEN = UtplsqlResources.getString("PREF_CLEAR_SCREEN_LABEL") |
| 38 | + public static var INDENT_SPACES = UtplsqlResources.getString("PREF_INDENT_SPACES_LABEL") |
| 39 | + |
| 40 | + override isSupported(Connection conn) { |
| 41 | + var ret = false |
| 42 | + if (conn !== null) { |
| 43 | + if (conn.metaData.databaseProductName.startsWith("Oracle")) { |
| 44 | + if (conn.metaData.databaseMajorVersion == 11) { |
| 45 | + if (conn.metaData.databaseMinorVersion >= 2) { |
| 46 | + ret = true |
| 47 | + } |
| 48 | + } else if (conn.metaData.databaseMajorVersion > 11) { |
| 49 | + ret = true |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return ret |
| 54 | + } |
| 55 | + |
| 56 | + override getName(Connection conn) { |
| 57 | + return "Run test" |
| 58 | + } |
| 59 | + |
| 60 | + override getDescription(Connection conn) { |
| 61 | + return "Runs utPLSQL test packages in the current user." |
| 62 | + } |
| 63 | + |
| 64 | + override getFolders(Connection conn) { |
| 65 | + val preferences = PreferenceModel.getInstance(Preferences.preferences) |
| 66 | + val folders = new ArrayList<String> |
| 67 | + for (f : preferences.rootFolderInOddgenView.split(",").filter[!it.empty]) { |
| 68 | + folders.add(f.trim) |
| 69 | + } |
| 70 | + return folders |
| 71 | + } |
| 72 | + |
| 73 | + override getHelp(Connection conn) { |
| 74 | + return "<p>not yet available</p>" |
| 75 | + } |
| 76 | + |
| 77 | + override getNodes(Connection conn, String parentNodeId) { |
| 78 | + val preferences = PreferenceModel.getInstance(Preferences.preferences) |
| 79 | + val params = new LinkedHashMap<String, String>() |
| 80 | + params.put(RESET_PACKAGE, if (preferences.resetPackage) {YES} else {NO}) |
| 81 | + params.put(CLEAR_SCREEN, if (preferences.clearScreen) {YES} else {NO}) |
| 82 | + params.put(INDENT_SPACES, String.valueOf(preferences.indentSpaces)) |
| 83 | + val UtplsqlDao dao = new UtplsqlDao(conn) |
| 84 | + val nodes = dao.runnables |
| 85 | + for (node : nodes) { |
| 86 | + node.params = params |
| 87 | + } |
| 88 | + return nodes |
| 89 | + } |
| 90 | + |
| 91 | + override getLov(Connection conn, LinkedHashMap<String, String> params, List<Node> nodes) { |
| 92 | + val lov = new HashMap<String, List<String>>() |
| 93 | + lov.put(RESET_PACKAGE, #[YES, NO]) |
| 94 | + lov.put(CLEAR_SCREEN, #[YES, NO]) |
| 95 | + lov.put(INDENT_SPACES, #["1", "2", "3", "4", "5", "6", "7", "8"]) |
| 96 | + return lov |
| 97 | + } |
| 98 | + |
| 99 | + override getParamStates(Connection conn, LinkedHashMap<String, String> params, List<Node> nodes) { |
| 100 | + return new HashMap<String, Boolean> |
| 101 | + } |
| 102 | + |
| 103 | + def private getPath(Node node, Connection conn) { |
| 104 | + if (node.id == "SUITE" || node.id == "SUITEPATH") { |
| 105 | + return conn.metaData.userName |
| 106 | + } else { |
| 107 | + return node.id |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + def replaceTabsWithSpaces(CharSequence input, int indentSpaces) { |
| 112 | + val spaces = String.format("%1$"+indentSpaces+"s", "") |
| 113 | + return input.toString.replace("\t", spaces) |
| 114 | + } |
| 115 | + |
| 116 | + def dedup(List<Node> nodes) { |
| 117 | + val set = new HashSet<String> |
| 118 | + for (node : nodes) { |
| 119 | + set.add(node.id) |
| 120 | + } |
| 121 | + val ret = new ArrayList<Node> |
| 122 | + for (node : nodes) { |
| 123 | + if (!set.contains(node.parentId)) { |
| 124 | + ret.add(node) |
| 125 | + } |
| 126 | + } |
| 127 | + return ret |
| 128 | + } |
| 129 | + |
| 130 | + override generateProlog(Connection conn, List<Node> nodes) { |
| 131 | + val dedupNodes = nodes.dedup |
| 132 | + val params = dedupNodes.get(0).params |
| 133 | + val ret = ''' |
| 134 | + «IF params.get(RESET_PACKAGE) == YES» |
| 135 | + EXECUTE dbms_session.reset_package; |
| 136 | + «ENDIF» |
| 137 | + SET SERVEROUTPUT ON SIZE UNLIMITED |
| 138 | + «IF params.get(CLEAR_SCREEN) == YES» |
| 139 | + CLEAR SCREEN |
| 140 | + «ENDIF» |
| 141 | + «IF dedupNodes.size == 1» |
| 142 | + EXECUTE ut.run('«dedupNodes.get(0).getPath(conn)»'); |
| 143 | + «ELSE» |
| 144 | + BEGIN |
| 145 | + ut.run( |
| 146 | + ut_varchar2_list( |
| 147 | + «FOR node : dedupNodes SEPARATOR ","» |
| 148 | + '«node.getPath(conn)»' |
| 149 | + «ENDFOR» |
| 150 | + ) |
| 151 | + ); |
| 152 | + END; |
| 153 | + / |
| 154 | + «ENDIF» |
| 155 | + ''' |
| 156 | + return ret.replaceTabsWithSpaces(Integer.valueOf(params.get(INDENT_SPACES))) |
| 157 | + } |
| 158 | + |
| 159 | + override generateSeparator(Connection conn) { |
| 160 | + return "" |
| 161 | + } |
| 162 | + |
| 163 | + override generateEpilog(Connection conn, List<Node> nodes) { |
| 164 | + return "" |
| 165 | + } |
| 166 | + |
| 167 | + override generate(Connection conn, Node node) { |
| 168 | + return "" |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments