Search in sources :

Example 1 with SqlStatementParser

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

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

the class SqlConnectorInputParamTest method sqlConnectorTest.

// **************************
// Tests
// **************************
@Test
public void sqlConnectorTest() throws Exception {
    SqlStatementParser parser = new SqlStatementParser(db.connection, null, STATEMENT);
    parser.parse();
    Map<String, Object> values = new HashMap<>();
    values.put("CHARVALUE", SqlParam.SqlSampleValue.CHAR_VALUE);
    values.put("VARCHARVALUE", SqlParam.SqlSampleValue.STRING_VALUE);
    values.put("NUMERICVALUE", SqlParam.SqlSampleValue.DECIMAL_VALUE);
    values.put("DECIMALVALUE", SqlParam.SqlSampleValue.DECIMAL_VALUE);
    values.put("SMALLINTVALUE", SqlParam.SqlSampleValue.INTEGER_VALUE);
    String result = template.requestBody("direct:start", JSONBeanUtil.toJSONBean(values), String.class);
    Assertions.assertThat(result).isNotNull();
    try (Statement stmt = db.connection.createStatement()) {
        stmt.execute("SELECT * FROM ALLTYPES");
        ResultSet resultSet = stmt.getResultSet();
        resultSet.next();
        for (int i = 1; i < 6; i++) {
            System.out.print(resultSet.getString(i) + " ");
        }
        System.out.println(resultSet.getString(1));
        Assertions.assertThat(resultSet.getString(1)).isEqualTo(SqlParam.SqlSampleValue.CHAR_VALUE.toString());
        Assertions.assertThat(resultSet.getString(2)).isEqualTo(SqlParam.SqlSampleValue.STRING_VALUE);
        Assertions.assertThat(resultSet.getString(3)).isEqualTo(SqlParam.SqlSampleValue.DECIMAL_VALUE.toString());
        Assertions.assertThat(resultSet.getString(4)).isEqualTo(SqlParam.SqlSampleValue.DECIMAL_VALUE.toString());
        Assertions.assertThat(resultSet.getString(5)).isEqualTo(SqlParam.SqlSampleValue.INTEGER_VALUE.toString());
    }
}
Also used : HashMap(java.util.HashMap) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SqlStatementParser(io.syndesis.connector.sql.common.SqlStatementParser) Test(org.junit.Test)

Example 3 with SqlStatementParser

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

SqlStatementParser (io.syndesis.connector.sql.common.SqlStatementParser)3 SqlStatementMetaData (io.syndesis.connector.sql.common.SqlStatementMetaData)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)1 SqlParam (io.syndesis.connector.sql.common.SqlParam)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 DataSource (javax.sql.DataSource)1 DefaultMetaData (org.apache.camel.component.extension.metadata.DefaultMetaData)1 Test (org.junit.Test)1