Search in sources :

Example 1 with SqlStatementMetaData

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

the class SqlConnectorMetaDataExtension method meta.

@Override
public Optional<MetaData> meta(final Map<String, Object> properties) {
    final String sqlStatement = (String) properties.get("query");
    MetaData metaData = EMPTY_METADATA;
    if (sqlStatement != null) {
        try (Connection connection = SqlSupport.createConnection(properties)) {
            final DatabaseMetaData meta = connection.getMetaData();
            final String defaultSchema = DatabaseMetaDataHelper.getDefaultSchema(meta.getDatabaseProductName(), String.valueOf(properties.get("user")));
            final String schemaPattern = (String) properties.getOrDefault("schema-pattern", defaultSchema);
            final SqlStatementParser parser = new SqlStatementParser(connection, schemaPattern, sqlStatement);
            final SqlStatementMetaData sqlStatementMetaData = parseStatement(parser);
            metaData = new DefaultMetaData(null, null, sqlStatementMetaData);
        } catch (final SQLException e) {
            throw new SyndesisServerException(e.getMessage(), e);
        }
    }
    return Optional.of(metaData);
}
Also used : SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) SQLException(java.sql.SQLException) DefaultMetaData(org.apache.camel.component.extension.metadata.DefaultMetaData) DatabaseMetaData(java.sql.DatabaseMetaData) SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Connection(java.sql.Connection) DefaultMetaData(org.apache.camel.component.extension.metadata.DefaultMetaData) SqlStatementParser(io.syndesis.connector.sql.common.SqlStatementParser) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 2 with SqlStatementMetaData

use of io.syndesis.connector.sql.common.SqlStatementMetaData 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 3 with SqlStatementMetaData

use of io.syndesis.connector.sql.common.SqlStatementMetaData 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

SqlStatementMetaData (io.syndesis.connector.sql.common.SqlStatementMetaData)3 SqlParam (io.syndesis.connector.sql.common.SqlParam)2 SqlStatementParser (io.syndesis.connector.sql.common.SqlStatementParser)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)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 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)1 PropertyPair (io.syndesis.connector.support.verifier.api.PropertyPair)1 SyndesisMetadata (io.syndesis.connector.support.verifier.api.SyndesisMetadata)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataSource (javax.sql.DataSource)1 DefaultMetaData (org.apache.camel.component.extension.metadata.DefaultMetaData)1