Search in sources :

Example 6 with StoredProcedureMetadata

use of io.syndesis.connector.sql.common.stored.StoredProcedureMetadata in project syndesis by syndesisio.

the class SqlStoredProcedureTest method listAllStoredProcedures.

@Test
public void listAllStoredProcedures() {
    Map<String, Object> parameters = new HashMap<>();
    for (final String name : db.properties.stringPropertyNames()) {
        parameters.put(name.substring(name.indexOf('.') + 1), db.properties.getProperty(name));
    }
    Map<String, StoredProcedureMetadata> storedProcedures = SqlSupport.getStoredProcedures(parameters);
    assertThat(storedProcedures.isEmpty()).isFalse();
    // Find 'demo_add'
    assertThat(storedProcedures.keySet().contains("DEMO_ADD")).isTrue();
    for (String storedProcedureName : storedProcedures.keySet()) {
        StoredProcedureMetadata md = storedProcedures.get(storedProcedureName);
        LOGGER.info("{}:{}", storedProcedureName, md.getTemplate());
    }
    // Inspect demo_add
    StoredProcedureMetadata metaData = storedProcedures.get("DEMO_ADD");
    assertThat(metaData.getTemplate()).isEqualTo("DEMO_ADD(INTEGER ${body[A]}, INTEGER ${body[B]}, OUT INTEGER C)");
}
Also used : HashMap(java.util.HashMap) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata) Test(org.junit.Test)

Example 7 with StoredProcedureMetadata

use of io.syndesis.connector.sql.common.stored.StoredProcedureMetadata in project syndesis by syndesisio.

the class SqlMetadataRetrieval method adaptForStoredSql.

@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.StdCyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity" })
public SyndesisMetadata adaptForStoredSql(final String actionId, final Map<String, Object> properties, final MetaData metadata) {
    final Map<String, List<PropertyPair>> enrichedProperties = new HashMap<>();
    if (isPresentAndNonNull(properties, PROCEDURE_NAME)) {
        // fetch metadata for the named procedure
        final List<PropertyPair> ppList = new ArrayList<>();
        @SuppressWarnings("unchecked") final Map<String, StoredProcedureMetadata> procedureMap = (Map<String, StoredProcedureMetadata>) metadata.getPayload();
        final String procedureName = (String) properties.get(PROCEDURE_NAME);
        final StoredProcedureMetadata storedProcedure = procedureMap.get(procedureName);
        ppList.add(new PropertyPair(storedProcedure.getTemplate(), PROCEDURE_TEMPLATE));
        enrichedProperties.put(PROCEDURE_TEMPLATE, ppList);
        // build the input and output schemas
        final ObjectSchema builderIn = new ObjectSchema();
        builderIn.set$schema("http://json-schema.org/schema#");
        builderIn.setTitle(procedureName + "_IN");
        final ObjectSchema builderOut = new ObjectSchema();
        builderOut.setTitle(procedureName + "_OUT");
        builderOut.set$schema("http://json-schema.org/schema#");
        if (storedProcedure.getColumnList() != null && !storedProcedure.getColumnList().isEmpty()) {
            for (final StoredProcedureColumn column : storedProcedure.getColumnList()) {
                if (column.getMode().equals(ColumnMode.IN) || column.getMode().equals(ColumnMode.INOUT)) {
                    builderIn.putProperty(column.getName(), schemaFor(column.getJdbcType()));
                }
                if (column.getMode().equals(ColumnMode.OUT) || column.getMode().equals(ColumnMode.INOUT)) {
                    builderOut.putProperty(column.getName(), schemaFor(column.getJdbcType()));
                }
            }
        }
        try {
            DataShape.Builder inDataShapeBuilder = new DataShape.Builder().type(builderIn.getTitle());
            if (builderIn.getProperties().isEmpty()) {
                inDataShapeBuilder.kind(DataShapeKinds.NONE);
            } else {
                inDataShapeBuilder.kind(DataShapeKinds.JSON_SCHEMA).name(procedureName + " Parameter").description(String.format("Parameters of Stored Procedure '%s'", procedureName)).specification(Json.writer().writeValueAsString(builderIn));
            }
            DataShape.Builder outDataShapeBuilder = new DataShape.Builder().type(builderOut.getTitle());
            if (builderOut.getProperties().isEmpty()) {
                outDataShapeBuilder.kind(DataShapeKinds.NONE);
            } else {
                outDataShapeBuilder.kind(DataShapeKinds.JSON_SCHEMA).name(procedureName + " Return").description(String.format("Return value of Stored Procedure '%s'", procedureName)).specification(Json.writer().writeValueAsString(builderOut));
            }
            return new SyndesisMetadata(enrichedProperties, inDataShapeBuilder.build(), outDataShapeBuilder.build());
        } catch (JsonProcessingException e) {
            throw new IllegalStateException(e);
        }
    }
    // return list of stored procedures in the database
    @SuppressWarnings("unchecked") final Map<String, StoredProcedureMetadata> procedureMap = (Map<String, StoredProcedureMetadata>) metadata.getPayload();
    if (isPresentAndNonNull(properties, PATTERN) && FROM_PATTERN.equalsIgnoreCase(String.valueOf(properties.get(PATTERN)))) {
        enrichedProperties.put(PROCEDURE_NAME, obtainFromProcedureList(procedureMap));
    } else {
        enrichedProperties.put(PROCEDURE_NAME, obtainToProcedureList(procedureMap));
    }
    return new SyndesisMetadata(enrichedProperties, null, null);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PropertyPair(io.syndesis.connector.support.verifier.api.PropertyPair) StoredProcedureColumn(io.syndesis.connector.sql.common.stored.StoredProcedureColumn) DataShape(io.syndesis.common.model.DataShape) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata) SyndesisMetadata(io.syndesis.connector.support.verifier.api.SyndesisMetadata) ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 8 with StoredProcedureMetadata

use of io.syndesis.connector.sql.common.stored.StoredProcedureMetadata in project syndesis by syndesisio.

the class SqlSupport method getStoredProcedures.

public static Map<String, StoredProcedureMetadata> getStoredProcedures(final Map<String, Object> parameters) {
    final Map<String, StoredProcedureMetadata> storedProcedures = new HashMap<>();
    try (Connection connection = DriverManager.getConnection(String.valueOf(parameters.get("url")), String.valueOf(parameters.get("user")), String.valueOf(parameters.get("password")))) {
        final DatabaseMetaData meta = connection.getMetaData();
        final String catalog = (String) parameters.getOrDefault("catalog", null);
        final String defaultSchema = DatabaseMetaDataHelper.getDefaultSchema(meta.getDatabaseProductName(), String.valueOf(parameters.get("user")));
        final String schemaPattern = (String) parameters.getOrDefault("schema-pattern", defaultSchema);
        final String procedurePattern = (String) parameters.getOrDefault("procedure-pattern", null);
        try (ResultSet procedureSet = DatabaseMetaDataHelper.fetchProcedures(meta, catalog, schemaPattern, procedurePattern)) {
            while (procedureSet.next()) {
                final String name = procedureSet.getString("PROCEDURE_NAME");
                final StoredProcedureMetadata storedProcedureMetadata = getStoredProcedureMetadata(connection, catalog, schemaPattern, name);
                storedProcedureMetadata.setName(procedureSet.getString("PROCEDURE_NAME"));
                storedProcedureMetadata.setType(procedureSet.getString("PROCEDURE_TYPE"));
                storedProcedureMetadata.setRemark(procedureSet.getString("REMARKS"));
                storedProcedures.put(storedProcedureMetadata.getName(), storedProcedureMetadata);
            }
        }
        return storedProcedures;
    } catch (final SQLException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata)

Example 9 with StoredProcedureMetadata

use of io.syndesis.connector.sql.common.stored.StoredProcedureMetadata in project syndesis by syndesisio.

the class SqlStoredConnectorMetaDataExtension method meta.

@Override
public Optional<MetaData> meta(final Map<String, Object> properties) {
    final Map<String, StoredProcedureMetadata> list = SqlSupport.getStoredProcedures(properties);
    final MetaData metaData = new DefaultMetaData(null, null, list);
    return Optional.of(metaData);
}
Also used : DefaultMetaData(org.apache.camel.component.extension.metadata.DefaultMetaData) DefaultMetaData(org.apache.camel.component.extension.metadata.DefaultMetaData) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata)

Aggregations

StoredProcedureMetadata (io.syndesis.connector.sql.common.stored.StoredProcedureMetadata)9 StoredProcedureColumn (io.syndesis.connector.sql.common.stored.StoredProcedureColumn)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 ResultSet (java.sql.ResultSet)4 HashMap (java.util.HashMap)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PropertyPair (io.syndesis.connector.support.verifier.api.PropertyPair)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectSchema (com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema)1 DataShape (io.syndesis.common.model.DataShape)1 ColumnMode (io.syndesis.connector.sql.common.stored.ColumnMode)1 SyndesisMetadata (io.syndesis.connector.support.verifier.api.SyndesisMetadata)1 Statement (java.sql.Statement)1 List (java.util.List)1 Map (java.util.Map)1 DefaultMetaData (org.apache.camel.component.extension.metadata.DefaultMetaData)1