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);
}
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);
}
}
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;
}
Aggregations