Java Interface (deprecated)
Version 0.2.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 three 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.
- Every method of the Java interface must be implemented.
This interface version is deprecated. Please see Java Interface for the current definitions.
OddgenGenerator
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 OddgenGenerator.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 |
/** * 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; import java.sql.Connection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; /** * Generators need to implement this interface to be shown in the Generators * window of oddgen for SQL Developer. */ public interface OddgenGenerator { public final static String[] BOOLEAN_TRUE = { "true", "yes", "ja", "oui", "si", "1" }; public final static String[] BOOLEAN_FALSE = { "false", "no", "nein", "non", "no", "0" }; /** * get 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 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 list of valid object types called by oddgen after opening a generator * node * * @param conn * active connection in the Generators window * @return the list of valid object types for this generator */ public abstract List<String> getObjectTypes(final Connection conn); /** * get list of valid object names called by oddgen after opening a object * type node * * @param conn * active connection in the Generators window * @param objectType * object type to filter object names * @return the list of object names valid for this objectType */ public abstract List<String> getObjectNames(final Connection conn, final String objectType); /** * get list of parameters with their default values called by oddgen while * generating code with default parameters and while initializing the * Generate dialog * * @param conn * active connection in the Generators window * @param objectType * object type to determine default parameter values * @param objectName * object name to determine default parameter values * @return the list of parameters with their default values */ public abstract LinkedHashMap<String, String> getParams(final Connection conn, final String objectType, final String objectName); /** * get 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 objectType * object type to determine list of values * @param objectName * object name to determine list of values * @param params * parameters with active values to determine list of values * @return the list of values per parameter */ public abstract HashMap<String, List<String>> getLov(final Connection conn, final String objectType, final String objectName, final LinkedHashMap<String, String> params); /** * get 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 objectType * object type to determine parameter state * @param objectName * object name to determine parameter state * @param params * parameters with active values to determine parameter state * @return the list states per parameter, might be a subset of the * parameters according getParams */ public abstract HashMap<String, Boolean> getParamStates(final Connection conn, final String objectType, final String objectName, final LinkedHashMap<String, String> params); /** * generate the result called by oddgen to generate code * * @param conn * active connection in the Generators window * @param objectType * object type to generate code for * @param objectName * object name to generate code for * @param params * parameters to customize the code generation * @return the generated code */ public abstract String generate(final Connection conn, final String objectType, final String objectName, final LinkedHashMap<String, String> params); } |
Example
Here’s an example of a simple oddgen Java interface implementation, based on HelloWorldClientGenerator.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 |
/** * 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.plugin.examples; import java.sql.Connection; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import org.eclipse.xtend2.lib.StringConcatenation; import org.oddgen.sqldev.generators.OddgenGenerator; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.SingleConnectionDataSource; public class HelloWorldClientGenerator implements OddgenGenerator { @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> getObjectTypes(final Connection conn) { final ArrayList<String> objectTypes = new ArrayList<String>(); objectTypes.add("TABLE"); objectTypes.add("VIEW"); return objectTypes; } @Override public List<String> getObjectNames(final Connection conn, final String objectType) { StringConcatenation _builder = new StringConcatenation(); _builder.append("SELECT object_name"); _builder.newLine(); _builder.append(" "); _builder.append("FROM user_objects"); _builder.newLine(); _builder.append(" "); _builder.append("WHERE object_type = ?"); _builder.newLine(); _builder.append(" "); _builder.append("AND generated = \'N\'"); _builder.newLine(); _builder.append("ORDER BY object_name"); _builder.newLine(); final String sql = _builder.toString(); SingleConnectionDataSource _singleConnectionDataSource = new SingleConnectionDataSource(conn, true); final JdbcTemplate jdbcTemplate = new JdbcTemplate(_singleConnectionDataSource); final List<String> objectNames = jdbcTemplate.<String> queryForList(sql, String.class, objectType); return objectNames; } @Override public LinkedHashMap<String, String> getParams(final Connection conn, final String objectType, final String objectName) { return new LinkedHashMap<String, String>(); } @Override public HashMap<String, List<String>> getLov(final Connection conn, final String objectType, final String objectName, final LinkedHashMap<String, String> params) { return new HashMap<String, List<String>>(); } @Override public HashMap<String, Boolean> getParamStates(final Connection conn, final String objectType, final String objectName, final LinkedHashMap<String, String> params) { return new HashMap<String, Boolean>(); } @Override public String generate(final Connection conn, final String objectType, final String objectName, final LinkedHashMap<String, String> params) { StringConcatenation _builder = new StringConcatenation(); _builder.append("BEGIN"); _builder.newLine(); _builder.append(" "); _builder.append("sys.dbms_output.put_line(\'Hello "); _builder.append(objectType, " "); _builder.append(" "); _builder.append(objectName, " "); _builder.append("!\');"); _builder.newLineIfNotEmpty(); _builder.append("END;"); _builder.newLine(); _builder.append("/"); _builder.newLine(); final String result = _builder.toString(); return result; } } |