Search in sources :

Example 1 with SqlParam

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

the class SqlMetadataRetrieval method adaptForSql.

@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.StdCyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity" })
public SyndesisMetadata adaptForSql(final String actionId, final Map<String, Object> properties, final MetaData metadata) {
    final Map<String, List<PropertyPair>> enrichedProperties = new HashMap<>();
    final List<PropertyPair> ppList = new ArrayList<>();
    @SuppressWarnings("unchecked") final SqlStatementMetaData sqlStatementMetaData = (SqlStatementMetaData) metadata.getPayload();
    if (sqlStatementMetaData != null) {
        ppList.add(new PropertyPair(sqlStatementMetaData.getSqlStatement(), QUERY));
        enrichedProperties.put(QUERY, ppList);
        // build the input and output schemas
        final ObjectSchema builderIn = new ObjectSchema();
        builderIn.set$schema("http://json-schema.org/schema#");
        builderIn.setTitle("SQL_PARAM_IN");
        for (SqlParam inParam : sqlStatementMetaData.getInParams()) {
            builderIn.putProperty(inParam.getName(), schemaFor(inParam.getJdbcType()));
        }
        final ObjectSchema builderOut = new ObjectSchema();
        builderOut.setTitle("SQL_PARAM_OUT");
        builderOut.set$schema("http://json-schema.org/schema#");
        for (SqlParam outParam : sqlStatementMetaData.getOutParams()) {
            builderOut.putProperty(outParam.getName(), schemaFor(outParam.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("SQL Parameter").description(String.format("Parameters of SQL [%s]", sqlStatementMetaData.getSqlStatement())).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("SQL Result").description(String.format("Result of SQL [%s]", sqlStatementMetaData.getSqlStatement())).specification(Json.writer().writeValueAsString(builderOut));
            }
            return new SyndesisMetadata(enrichedProperties, inDataShapeBuilder.build(), outDataShapeBuilder.build());
        } catch (JsonProcessingException e) {
            throw new IllegalStateException(e);
        }
    } else {
        return new SyndesisMetadata(enrichedProperties, null, null);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PropertyPair(io.syndesis.connector.support.verifier.api.PropertyPair) DataShape(io.syndesis.common.model.DataShape) SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) SyndesisMetadata(io.syndesis.connector.support.verifier.api.SyndesisMetadata) ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) ArrayList(java.util.ArrayList) List(java.util.List) SqlParam(io.syndesis.connector.sql.common.SqlParam) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with SqlParam

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

the class SqlConnectorCustomizer method initJdbcMap.

private void initJdbcMap() {
    final String sql = String.valueOf(options.get("query"));
    final DataSource dataSource = (DataSource) options.get("dataSource");
    final Map<String, Integer> tmpMap = new HashMap<>();
    try (Connection connection = dataSource.getConnection()) {
        SqlStatementMetaData md = new SqlStatementParser(connection, null, sql).parse();
        for (SqlParam sqlParam : md.getInParams()) {
            tmpMap.put(sqlParam.getName(), sqlParam.getJdbcType().getVendorTypeNumber());
        }
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
    }
    jdbcTypeMap = tmpMap;
}
Also used : SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlStatementParser(io.syndesis.connector.sql.common.SqlStatementParser) SqlParam(io.syndesis.connector.sql.common.SqlParam) DataSource(javax.sql.DataSource)

Aggregations

SqlParam (io.syndesis.connector.sql.common.SqlParam)2 SqlStatementMetaData (io.syndesis.connector.sql.common.SqlStatementMetaData)2 HashMap (java.util.HashMap)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectSchema (com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema)1 DataShape (io.syndesis.common.model.DataShape)1 SqlStatementParser (io.syndesis.connector.sql.common.SqlStatementParser)1 PropertyPair (io.syndesis.connector.support.verifier.api.PropertyPair)1 SyndesisMetadata (io.syndesis.connector.support.verifier.api.SyndesisMetadata)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataSource (javax.sql.DataSource)1