use of org.pentaho.di.core.exception.KettlePluginException in project pentaho-kettle by pentaho.
the class AccessOutputMeta method getLayout.
public static final RowMetaInterface getLayout(Table table) throws SQLException, KettleStepException {
RowMetaInterface row = new RowMeta();
List<Column> columns = table.getColumns();
for (int i = 0; i < columns.size(); i++) {
Column column = columns.get(i);
int valtype = ValueMetaInterface.TYPE_STRING;
int length = -1;
int precision = -1;
int type = column.getType().getSQLType();
switch(type) {
case java.sql.Types.CHAR:
case java.sql.Types.VARCHAR:
case // Character Large Object
java.sql.Types.LONGVARCHAR:
valtype = ValueMetaInterface.TYPE_STRING;
length = column.getLength();
break;
case java.sql.Types.CLOB:
valtype = ValueMetaInterface.TYPE_STRING;
length = DatabaseMeta.CLOB_LENGTH;
break;
case java.sql.Types.BIGINT:
valtype = ValueMetaInterface.TYPE_INTEGER;
// Max 9.223.372.036.854.775.807
precision = 0;
length = 15;
break;
case java.sql.Types.INTEGER:
valtype = ValueMetaInterface.TYPE_INTEGER;
// Max 2.147.483.647
precision = 0;
length = 9;
break;
case java.sql.Types.SMALLINT:
valtype = ValueMetaInterface.TYPE_INTEGER;
// Max 32.767
precision = 0;
length = 4;
break;
case java.sql.Types.TINYINT:
valtype = ValueMetaInterface.TYPE_INTEGER;
// Max 127
precision = 0;
length = 2;
break;
case java.sql.Types.DECIMAL:
case java.sql.Types.DOUBLE:
case java.sql.Types.FLOAT:
case java.sql.Types.REAL:
case java.sql.Types.NUMERIC:
valtype = ValueMetaInterface.TYPE_NUMBER;
length = column.getLength();
precision = column.getPrecision();
if (length >= 126) {
length = -1;
}
if (precision >= 126) {
precision = -1;
}
if (type == java.sql.Types.DOUBLE || type == java.sql.Types.FLOAT || type == java.sql.Types.REAL) {
if (precision == 0) {
// precision is obviously incorrect if the type if Double/Float/Real
precision = -1;
}
} else {
if (precision == 0 && length < 18 && length > 0) {
// Among others Oracle is affected here.
valtype = ValueMetaInterface.TYPE_INTEGER;
}
}
if (length > 18 || precision > 18) {
valtype = ValueMetaInterface.TYPE_BIGNUMBER;
}
break;
case java.sql.Types.DATE:
case java.sql.Types.TIME:
case java.sql.Types.TIMESTAMP:
valtype = ValueMetaInterface.TYPE_DATE;
break;
case java.sql.Types.BOOLEAN:
case java.sql.Types.BIT:
valtype = ValueMetaInterface.TYPE_BOOLEAN;
break;
case java.sql.Types.BINARY:
case java.sql.Types.BLOB:
case java.sql.Types.VARBINARY:
case java.sql.Types.LONGVARBINARY:
valtype = ValueMetaInterface.TYPE_BINARY;
break;
default:
valtype = ValueMetaInterface.TYPE_STRING;
length = column.getLength();
break;
}
ValueMetaInterface v;
try {
v = ValueMetaFactory.createValueMeta(column.getName(), valtype);
} catch (KettlePluginException e) {
throw new KettleStepException(e);
}
v.setLength(length, precision);
row.addValueMeta(v);
}
return row;
}
use of org.pentaho.di.core.exception.KettlePluginException in project pentaho-kettle by pentaho.
the class PluginRegistryIT method testPluginRegistry.
public void testPluginRegistry() throws KettlePluginException {
PluginRegistry registry = PluginRegistry.getInstance();
assertNotNull("Registry singleton was not found!", registry);
// PluginRegistry.init() may have already been called, our test path will be different
// for each case. If it has not been called, try to register the type and plugin. If
// it has been called, simply verify the plugin type and plugin have been registered.
Class<? extends PluginTypeInterface> pluginTypeClass = StepPluginType.class;
Object pluginClass = new TableInputMeta();
List<PluginInterface> inputPluginsAtTestStart = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_INPUT_CATEGORY);
int numInputPluginsAtStart = inputPluginsAtTestStart.size();
List<PluginInterface> outputPluginsAtTestStart = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_OUTPUT_CATEGORY);
int numOutputPluginsAtStart = outputPluginsAtTestStart.size();
try {
registry.getPluginType(pluginTypeClass);
} catch (KettlePluginException kpe) {
// Register a new plugin type...
registry.registerPluginType(pluginTypeClass);
}
// See if the plugin is there...
try {
registry.getPluginType(pluginTypeClass);
} catch (KettlePluginException kpe) {
fail(pluginTypeClass.getName() + " expected in the PluginRegistry but was not found!");
}
PluginInterface plugin = registry.getPlugin(pluginTypeClass, pluginClass);
Map<Class<?>, String> classMap = new HashMap<Class<?>, String>();
PluginInterface tableInputPlugin = plugin;
int numInputPluginsRegistered = 0;
if (plugin == null) {
// Register a single step plugin
//
classMap.put(StepMetaInterface.class, "org.pentaho.di.trans.steps.tableinput.TableInputMeta");
tableInputPlugin = new Plugin(new String[] { TABLE_INPUT_PLUGIN_ID }, pluginTypeClass, StepMetaInterface.class, PLUGIN_INPUT_CATEGORY, TABLE_INPUT_PLUGIN_NAME, TABLE_INPUT_PLUGIN_DESCRIPTION, TABLE_INPUT_PLUGIN_IMAGE_FILE_NAME, false, true, classMap, new ArrayList<String>(), // No error help file
null, // pluginFolder
null, // documentation URL
null, // cases URL
null, // forum URL
null);
registry.registerPlugin(pluginTypeClass, tableInputPlugin);
numInputPluginsRegistered++;
}
// Verify the plugin has been registered
PluginInterface verify = registry.getPlugin(pluginTypeClass, TABLE_INPUT_PLUGIN_ID);
assertNotNull("A plugin was not found in the plugin registry", verify);
assertEquals("A different plugin then expected was retrieved from the plugin registry", verify, tableInputPlugin);
pluginClass = new TableOutputMeta();
plugin = registry.getPlugin(pluginTypeClass, pluginClass);
PluginInterface tableOutputPlugin = plugin;
int numOutputPluginsRegistered = 0;
if (plugin == null) {
// Register a second step plugin
//
classMap = new HashMap<Class<?>, String>();
classMap.put(StepMetaInterface.class, "org.pentaho.di.trans.steps.tableoutput.TableOutputMeta");
tableOutputPlugin = new Plugin(new String[] { TABLE_OUTPUT_PLUGIN_ID }, pluginTypeClass, StepMetaInterface.class, PLUGIN_OUTPUT_CATEGORY, TABLE_OUTPUT_PLUGIN_NAME, TABLE_OUTPUT_PLUGIN_DESCRIPTION, TABLE_OUTPUT_PLUGIN_IMAGE_FILE_NAME, false, true, classMap, new ArrayList<String>(), // No error help file
null, // pluginFolder
null, // documentation URL
null, // cases URL
null, // forum URL
null);
registry.registerPlugin(pluginTypeClass, tableOutputPlugin);
numOutputPluginsRegistered++;
}
// Verify the plugin has been registered
verify = registry.getPlugin(pluginTypeClass, TABLE_OUTPUT_PLUGIN_ID);
assertNotNull("A plugin was not found in the plugin registry", verify);
assertEquals("A different plugin then expected was retrieved from the plugin registry", verify, tableOutputPlugin);
// Get a list by category...
//
List<PluginInterface> inputPlugins = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_INPUT_CATEGORY);
assertEquals("Exactly one plugin expected in the step plugin input category", numInputPluginsAtStart + numInputPluginsRegistered, inputPlugins.size());
assertTrue("Input plugins list should contain the table input step", inputPlugins.contains(tableInputPlugin));
assertFalse("Input plugins list should not contain the table output step", inputPlugins.contains(tableOutputPlugin));
List<PluginInterface> outputPlugins = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_OUTPUT_CATEGORY);
assertEquals("Exactly one plugin expected in the step plugin output category", numOutputPluginsAtStart + numOutputPluginsRegistered, outputPlugins.size());
assertTrue("Output plugins list should contain the table output step", outputPlugins.contains(tableOutputPlugin));
assertFalse("Output plugins list should not contain the table input step", outputPlugins.contains(tableInputPlugin));
// List the categories...
//
List<String> categories = registry.getCategories(pluginTypeClass);
assertTrue("The input category was expected in the categories list", categories.contains(PLUGIN_INPUT_CATEGORY));
assertTrue("The output category was expected in the categories list", categories.contains(PLUGIN_OUTPUT_CATEGORY));
// Now have a little bit of class loading fun: load the main class of the plugin
//
Object object = registry.loadClass(tableInputPlugin, StepMetaInterface.class);
assertNotNull(object);
// The same but now explicitly asking for the main class
//
Object object2 = registry.loadClass(tableOutputPlugin, StepMetaInterface.class);
assertNotNull(object2);
try {
registry.loadClass(tableInputPlugin, String.class);
fail("A String class type can't be used when loading a step class");
} catch (Exception e) {
// OK!
}
}
Aggregations