Java Interface
Version 0.3.0
To make a generator visible within SQL Developer, implement the oddgen Java interface in a JVM language of your choice such as Java, Scala, Groovy, Clojure, JRuby, Jython or Xtend. oddgen looks for classes implementing the OddgenGenerator interface in JAR files in the SQL Developer extension directory. This allows you to distribute your client generators as a SQL Developer extension or an ordinary JAR file. See Tutorial 3 – Extended 1:1 View Generator (Xtend) for an example how to create a client generator.
The Java interface is very similar to the PL/SQL interface. Basically there are four differences:
- Every method of the Java interface has a parameter named conn to access the active database connection.
- The method getParams returns a LinkedHashMap, this means the parameters are ordered by entry. Hence no equivalent for the PL/SQL method get_orderd_params is necessary.
- The additional method isSupported determines the executability under the current connection. No equivalent exist for PL/SQL, since it is being expected that all installed and valid PL/SQL packages are executable.
- Every method of the Java interface must be implemented.
oddgen for SQL Developer is backward compatible, hence the deprecated Java interface v0.2.0 ist still fully functional. In fact it is possible to implement both interfaces making the generator work in older and current installations. Just the newest version of an interface is used, if a class implements more than one oddgen interface.
OddgenGenerator2
oddgen for SQL Developer is completely developed in Xtend, mainly because of its excellent template expression support. However, for the interface itself the language does not really matter.
Here’s the interface in Java, based on OddgenGenerator2.xtend:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
/** * Copyright 2016 Philipp Salvisberg <philipp.salvisberg@trivadis.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.oddgen.sqldev.generators; import java.sql.Connection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import org.oddgen.sqldev.generators.model.Node; /** * Generators need to implement this interface to be shown in the Generators * window of oddgen for SQL Developer. * * @since v0.3.0 */ @SuppressWarnings("all") public interface OddgenGenerator2 { public final static String[] BOOLEAN_TRUE = { "true", "yes", "ja", "oui", "si", "1" }; public final static String[] BOOLEAN_FALSE = { "false", "no", "nein", "non", "no", "0" }; /** * Checks if the current connection is supported by the generator. * Called by oddgen before populating GUI components. * @param conn active connection in the Generators window * @return true if the generator supports the database vendor and version */ public abstract boolean isSupported(final Connection conn); /** * Get the name of the generator. * Called by oddgen after establishing/refreshing a connection. * @param conn active connection in the Generators window * @return the name of the generator */ public abstract String getName(final Connection conn); /** * Get the description of the generator. * Called by oddgen after establishing/refreshing a connection. * @param conn active connection in the Generators window * @return the description of the generator */ public abstract String getDescription(final Connection conn); /** * Get the list of folder names. The first entry in the list is the folder * under 'All Generators', the second one is the subfolder under the * first one and so on. The generator will be visible in the last folder * of the list. * @param conn active connection in the Generators window * @return the list of folders under 'All Generators' */ public abstract List<String> getFolders(final Connection conn); /** * Get the help of the generator. * Called by oddgen after pressing the help button in the Generator dialog. * @param conn active connection in the Generators window * @return the help of the generator */ public abstract String getHelp(final Connection conn); /** * Get the list of nodes shown to be shown in the SQL Developer navigator tree. * The implementation decides if nodes are returned eagerly oder lazily. * Called by oddgen after opening a generator node and after opening other * nodes, if no children have been fetched for this node before and if this * node is not a leaf. * @param conn active connection in the Generators window * @param parentNodeId root node to get children for * @return a list of nodes in a hierarchical structure */ public abstract List<Node> getNodes(final Connection conn, final String parentNodeId); /** * Get the list of values per parameter. * Called by oddgen while initializing the Generate dialog and after * change of a parameter value. * @param conn active connection in the Generators window * @param params parameters with active values to determine list of values * @param nodes list of selected nodes to be generated with default parameter values * @return the list of values per parameter */ public abstract HashMap<String, List<String>> getLov(final Connection conn, final LinkedHashMap<String, String> params, final List<Node> nodes); /** * Get the list of parameter states (enabled/disabled). * Called by oddgen while initializing the Generate dialog and after change * of a parameter value. * @param conn active connection in the Generators window * @param params parameters with active values to determine parameter state * @param nodes list of selected nodes to be generated with default parameter values * @return the list states per parameter, might be a subset of the parameters */ public abstract HashMap<String, Boolean> getParamStates(final Connection conn, final LinkedHashMap<String, String> params, final List<Node> nodes); /** * Generate the prolog. * Called by oddgen once for all selected nodes at the very beginning of the processing. * @param conn active connection in the Generators window * @param nodes list of selected nodes to be generated * @return the generated prolog */ public abstract String generateProlog(final Connection conn, final List<Node> nodes); /** * Generates the separator between generate calls. * Called by oddgen once for all selected nodes, but applied between generator calls. * @param conn active connection in the Generators window * @return the generator separator */ public abstract String generateSeparator(final Connection conn); /** * Generate the epilog. * Called by oddgen once for all selected nodes at the very end of the processing. * @param conn active connection in the Generators window * @param nodes list of selected nodes to be generated * @return the generated epilog */ public abstract String generateEpilog(final Connection conn, final List<Node> nodes); /** * Generates the result. * Called for every selected and relevant node, including its children. * @param conn active connection in the Generators window * @param node node to be generated * @return the generated code */ public abstract String generate(final Connection conn, final Node node); } |
Node
The interface references in several methods a Node type. Here is the Java equivalent of the Node definition in Node.xtend:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
/** * Copyright 2015-2016 Philipp Salvisberg <philipp.salvisberg@trivadis.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.oddgen.sqldev.generators.model; import java.util.LinkedHashMap; import org.eclipse.xtend.lib.annotations.Accessors; import org.eclipse.xtext.xbase.lib.Pure; import org.oddgen.sqldev.model.AbstractModel; @Accessors @SuppressWarnings("all") public class Node extends AbstractModel { /** * node identifier, case-sensitive, e.g. EMP */ private String id; /** * parent node identifier, NULL for root nodes, e.g. TABLE */ private String parentId; /** * name of the node, e.g. Emp */ private String name; /** * description of the node, e.g. Table Emp */ private String description; /** * existing icon name, e.g. TABLE_ICON, VIEW_ICON */ private String iconName; /** * Base64 encoded icon, size: 16x16 pixels, format: PNG, GIF or JPEG */ private String iconBase64; /** * array of parameters, e.g. View suffix=_V, Instead-of-trigger suffix=_TRG */ private LinkedHashMap<String, String> params; /** * Is this a leaf node? true|false, default false */ private Boolean leaf; /** * Is the node with all its children generatable? true|false, default leaf */ private Boolean generatable; /** * May this node be part of a multiselection? true|false, default leaf */ private Boolean multiselectable; /** * Pass node to the generator? true|false, default leaf */ private Boolean relevant; @Pure public String getId() { return this.id; } public void setId(final String id) { this.id = id; } @Pure public String getParentId() { return this.parentId; } public void setParentId(final String parentId) { this.parentId = parentId; } @Pure public String getName() { return this.name; } public void setName(final String name) { this.name = name; } @Pure public String getDescription() { return this.description; } public void setDescription(final String description) { this.description = description; } @Pure public String getIconName() { return this.iconName; } public void setIconName(final String iconName) { this.iconName = iconName; } @Pure public String getIconBase64() { return this.iconBase64; } public void setIconBase64(final String iconBase64) { this.iconBase64 = iconBase64; } @Pure public LinkedHashMap<String, String> getParams() { return this.params; } public void setParams(final LinkedHashMap<String, String> params) { this.params = params; } @Pure public Boolean getLeaf() { return this.leaf; } public void setLeaf(final Boolean leaf) { this.leaf = leaf; } @Pure public Boolean getGeneratable() { return this.generatable; } public void setGeneratable(final Boolean generatable) { this.generatable = generatable; } @Pure public Boolean getMultiselectable() { return this.multiselectable; } public void setMultiselectable(final Boolean multiselectable) { this.multiselectable = multiselectable; } @Pure public Boolean getRelevant() { return this.relevant; } public void setRelevant(final Boolean relevant) { this.relevant = relevant; } } |
Example
Here’s an example of a simple oddgen Java interface implementation, based on HelloWorldClientGenerator.xtend . Please note that this generator is supported for every JDBC connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
/** * Copyright 2015 Philipp Salvisberg <philipp.salvisberg@trivadis.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.oddgen.sqldev.plugin.examples; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import org.eclipse.xtend2.lib.StringConcatenation; import org.eclipse.xtext.xbase.lib.CollectionLiterals; import org.eclipse.xtext.xbase.lib.Exceptions; import org.eclipse.xtext.xbase.lib.Extension; import org.oddgen.sqldev.dal.DalTools; import org.oddgen.sqldev.generators.OddgenGenerator2; import org.oddgen.sqldev.generators.model.Node; import org.oddgen.sqldev.generators.model.NodeTools; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.SingleConnectionDataSource; @SuppressWarnings("all") public class HelloWorldClientGenerator implements OddgenGenerator2 { @Extension private NodeTools nodeTools = new NodeTools(); private double startTimeNanos = System.nanoTime(); @Override public boolean isSupported(final Connection conn) { return true; } @Override public String getName(final Connection conn) { return "Hello World"; } @Override public String getDescription(final Connection conn) { return "Hello World example generator"; } @Override public List<String> getFolders(final Connection conn) { return Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("Examples", "Xtend")); } @Override public String getHelp(final Connection conn) { return "<p>Hello World example generator</p>"; } @Override public List<Node> getNodes(final Connection conn, final String parentNodeId) { try { final DalTools dalTools = new DalTools(conn); boolean _isAtLeastOracle = dalTools.isAtLeastOracle(9, 2); if (_isAtLeastOracle) { StringConcatenation _builder = new StringConcatenation(); _builder.append("SELECT \'TABLE\' AS id,"); _builder.newLine(); _builder.append(" "); _builder.append("NULL AS parent_id,"); _builder.newLine(); _builder.append(" "); _builder.append("0 AS leaf,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS generatable,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS multiselectable"); _builder.newLine(); _builder.append(" "); _builder.append("FROM dual"); _builder.newLine(); _builder.append("UNION ALL"); _builder.newLine(); _builder.append("SELECT \'VIEW\' AS id,"); _builder.newLine(); _builder.append(" "); _builder.append("NULL AS parent_id,"); _builder.newLine(); _builder.append(" "); _builder.append("0 AS leaf,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS generatable,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS multiselectable"); _builder.newLine(); _builder.append(" "); _builder.append("FROM dual"); _builder.newLine(); _builder.append("UNION ALL"); _builder.newLine(); _builder.append("SELECT object_type || \'.\' || object_name AS id,"); _builder.newLine(); _builder.append(" "); _builder.append("object_type AS parent_id,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS leaf,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS generatable,"); _builder.newLine(); _builder.append(" "); _builder.append("1 AS multiselectable"); _builder.newLine(); _builder.append(" "); _builder.append("FROM user_objects"); _builder.newLine(); _builder.append(" "); _builder.append("WHERE object_type IN (\'TABLE\', \'VIEW\')"); _builder.newLine(); _builder.append(" "); _builder.append("AND generated = \'N\'"); _builder.newLine(); final String sql = _builder.toString(); SingleConnectionDataSource _singleConnectionDataSource = new SingleConnectionDataSource(conn, true); final JdbcTemplate jdbcTemplate = new JdbcTemplate(_singleConnectionDataSource); BeanPropertyRowMapper<Node> _beanPropertyRowMapper = new BeanPropertyRowMapper<Node>(Node.class); final List<Node> nodes = jdbcTemplate.<Node>query(sql, _beanPropertyRowMapper); return nodes; } else { if (((parentNodeId == null) || parentNodeId.isEmpty())) { final Node tableNode = new Node(); tableNode.setId("TABLE"); tableNode.setLeaf(Boolean.valueOf(false)); tableNode.setGeneratable(Boolean.valueOf(true)); tableNode.setMultiselectable(Boolean.valueOf(true)); final Node viewNode = new Node(); viewNode.setId("VIEW"); viewNode.setLeaf(Boolean.valueOf(false)); viewNode.setGeneratable(Boolean.valueOf(true)); viewNode.setMultiselectable(Boolean.valueOf(true)); return Collections.<Node>unmodifiableList(CollectionLiterals.<Node>newArrayList(tableNode, viewNode)); } else { final ArrayList<Node> nodes_1 = new ArrayList<Node>(); final ResultSet resultSet = conn.getMetaData().getTables(null, null, null, new String[] { parentNodeId }); while (resultSet.next()) { { final Node node = new Node(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append(parentNodeId); _builder_1.append("."); String _string = resultSet.getString("TABLE_NAME"); _builder_1.append(_string); node.setId(_builder_1.toString()); node.setParentId(parentNodeId); node.setLeaf(Boolean.valueOf(true)); node.setGeneratable(Boolean.valueOf(true)); node.setMultiselectable(Boolean.valueOf(true)); nodes_1.add(node); } } resultSet.close(); return nodes_1; } } } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } } @Override public HashMap<String, List<String>> getLov(final Connection conn, final LinkedHashMap<String, String> params, final List<Node> nodes) { return new HashMap<String, List<String>>(); } @Override public HashMap<String, Boolean> getParamStates(final Connection conn, final LinkedHashMap<String, String> params, final List<Node> nodes) { return new HashMap<String, Boolean>(); } @Override public String generateProlog(final Connection conn, final List<Node> nodes) { this.startTimeNanos = System.nanoTime(); StringConcatenation _builder = new StringConcatenation(); _builder.append("BEGIN"); _builder.newLine(); _builder.append(" "); _builder.append("-- "); int _size = nodes.size(); _builder.append(_size, " "); _builder.append(" nodes selected."); _builder.newLineIfNotEmpty(); { int _size_1 = nodes.size(); boolean _equals = (_size_1 == 0); if (_equals) { _builder.append(" "); _builder.append("NULL;"); _builder.newLine(); } } final String result = _builder.toString(); return result; } @Override public String generateSeparator(final Connection conn) { return ""; } @Override public String generateEpilog(final Connection conn, final List<Node> nodes) { StringConcatenation _builder = new StringConcatenation(); _builder.append(" "); _builder.append("-- "); int _size = nodes.size(); _builder.append(_size); _builder.append(" nodes generated in "); long _nanoTime = System.nanoTime(); double _minus = (_nanoTime - this.startTimeNanos); double _divide = (_minus / 1000000); String _format = String.format("%.3f", Double.valueOf(_divide)); _builder.append(_format); _builder.append(" ms."); _builder.newLineIfNotEmpty(); _builder.append("END;"); _builder.newLine(); _builder.append("/"); _builder.newLine(); final String result = _builder.toString(); return result; } @Override public String generate(final Connection conn, final Node node) { StringConcatenation _builder = new StringConcatenation(); _builder.append(" "); _builder.append("sys.dbms_output.put_line(\'Hello "); String _objectType = this.nodeTools.toObjectType(node); _builder.append(_objectType); _builder.append(" "); String _objectName = this.nodeTools.toObjectName(node); _builder.append(_objectName); _builder.append("!\');"); _builder.newLineIfNotEmpty(); final String result = _builder.toString(); return result; } } |