Data Integration Connector Toolkit
- Data Integration Connector Toolkit
- All Products
//Sample code to check if custom query is enabled public String getCustomQuery(List<Option> options) { try{ for (Option opt : options) { int optionID = opt.getDescription().getEffectiveDefinition().getOptionID(); if (optionID == CCatalogImportOpts.QUERY) return opt.getValue().toString(); } //returns empty string or null if custom query is not enabled }
//Sample code to import data object by executing the query public boolean populateCatalogForCustomQuery(Connection connection, Catalog catalog, String customQuery){ Factory sdkFactory = catalog.getFactory(); FlatRecord record = null; Package pack = sdkFactory.newPackage(catalog); pack.setName("CUSTOM_CATALOG"); pack.setNativeName("CUSTOM_CATALOG"); catalog.addRootPackage(pack); record=sdkFactory.newFlatRecord(catalog); try { // sample code to execute the custom query Connection nativeConn = conn.getNativeConnection(); stmt = nativeConn.createStatement(); rs = stmt.executeQuery(customQuery); rsMetaData = rs.getMetaData(); String tableName = rsMetaData.getTableName(1); int numberOfColumns = rsMetaData.getColumnCount(); // Sample code to populate the object details record.setName(tableName); record.setNativeName(tableName); for (int i = 1; i <= numberOfColumns; i++) { int scale=rsMetaData.getScale(i); int precision=rsMetaData.getPrecision(i); String columnName = rsMetaData.getColumnLabel(i); String colType = rsMetaData.getColumnTypeName(i); Field field=null; field = sdkFactry.newField(catalog); field.setNativeName(columnName); field.setScale(scale); field.setDataType(colType); field.setPrecision(precision); record.addField(field); } } catch (SQLException e) { ExceptionManager.createNonNlsAdapterSDKException( "An error occured while executing custom query:[" + e.getMessage() + "]"); return false; } return true; }
@Override public boolean validate(Connection sdkConnection, List<Option> options) { String customQuery = getCustomQuery(options); java.sql.Connection mySQLConnection = ((MySQL_CloudConnection) sdkConnection).getMySQLConnection(); try (Statement ps = mySQLConnection.createStatement()) { String dbForCustomQuery=((MySQL_CloudConnection) sdkConnection).getCatalog(); ps.executeQuery("USE "+dbForCustomQuery); ps.executeQuery(customQuery); } catch (SQLException e) { // TODO Auto-generated catch block ExceptionManager.createNonNlsAdapterSDKException( "An error occured while executing custom query for validation:[" + e.getMessage() + "]"); return false; } return true; }