Search in sources :

Example 1 with StoredProcedureMetadata

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

the class SqlMetadataRetrieval method obtainFromProcedureList.

/**
 * Puts stored procedures in the list that have NO input parameters, which adheres to the `From`
 * pattern.
 *
 * @param procedureMap
 * @return list of property pairs containing the stored procedure names
 */
private List<PropertyPair> obtainFromProcedureList(Map<String, StoredProcedureMetadata> procedureMap) {
    final List<PropertyPair> ppList = new ArrayList<>();
    for (final StoredProcedureMetadata storedProcedure : procedureMap.values()) {
        if (!containsInputParams(storedProcedure)) {
            final PropertyPair pp = new PropertyPair(storedProcedure.getName(), storedProcedure.getName());
            ppList.add(pp);
        }
    }
    return ppList;
}
Also used : ArrayList(java.util.ArrayList) PropertyPair(io.syndesis.connector.support.verifier.api.PropertyPair) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata)

Example 2 with StoredProcedureMetadata

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

the class SqlSupport method getStoredProcedureMetadata.

public static StoredProcedureMetadata getStoredProcedureMetadata(final Connection connection, final String catalog, final String schema, final String procedureName) {
    final StoredProcedureMetadata storedProcedureMetadata = new StoredProcedureMetadata();
    storedProcedureMetadata.setName(procedureName);
    try {
        final DatabaseMetaData meta = connection.getMetaData();
        try (ResultSet columnSet = DatabaseMetaDataHelper.fetchProcedureColumns(meta, catalog, schema, procedureName)) {
            final List<StoredProcedureColumn> columnList = new ArrayList<>();
            while (columnSet.next()) {
                final ColumnMode mode = ColumnMode.valueOf(columnSet.getInt("COLUMN_TYPE"));
                if (ColumnMode.IN == mode || ColumnMode.OUT == mode || ColumnMode.INOUT == mode) {
                    final StoredProcedureColumn column = new StoredProcedureColumn();
                    column.setName(columnSet.getString("COLUMN_NAME"));
                    column.setMode(mode);
                    column.setJdbcType(JDBCType.valueOf(columnSet.getInt("DATA_TYPE")));
                    columnList.add(column);
                }
            }
            final String template = columnList.stream().map(StoredProcedureColumn::toProcedureParameterString).collect(Collectors.joining(", ", procedureName + "(", ")"));
            storedProcedureMetadata.setTemplate(template);
            storedProcedureMetadata.setColumnList(columnList);
            return storedProcedureMetadata;
        }
    } catch (final SQLException e) {
        throw new IllegalStateException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ColumnMode(io.syndesis.connector.sql.common.stored.ColumnMode) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DatabaseMetaData(java.sql.DatabaseMetaData) StoredProcedureColumn(io.syndesis.connector.sql.common.stored.StoredProcedureColumn) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata)

Example 3 with StoredProcedureMetadata

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

the class SqlStoredCommon method setupStoredProcedure.

public static void setupStoredProcedure(Connection connection, Properties properties) throws Exception {
    try {
        String databaseProductName = connection.getMetaData().getDatabaseProductName();
        Map<String, Object> parameters = new HashMap<>();
        for (final String name : properties.stringPropertyNames()) {
            parameters.put(name.substring(name.indexOf('.') + 1), properties.getProperty(name));
        }
        Map<String, StoredProcedureMetadata> storedProcedures = SqlSupport.getStoredProcedures(parameters);
        if (!storedProcedures.keySet().contains("DEMO_ADD") && databaseProductName.equalsIgnoreCase(DatabaseProduct.APACHE_DERBY.nameWithSpaces())) {
            try (Statement stmt = connection.createStatement()) {
                stmt.execute(SampleStoredProcedures.DERBY_DEMO_ADD_SQL);
                LOGGER.info("Created procedure {}", SampleStoredProcedures.DERBY_DEMO_ADD_SQL);
            } catch (SQLException e) {
                LOGGER.warn("", e);
                fail("Exception during Stored Procedure Creation.", e);
            }
        }
        if (!storedProcedures.keySet().contains("DEMO_OUT") && databaseProductName.equalsIgnoreCase(DatabaseProduct.APACHE_DERBY.nameWithSpaces())) {
            try (Statement stmt = connection.createStatement()) {
                // Create procedure
                stmt.execute(SampleStoredProcedures.DERBY_DEMO_OUT_SQL);
            } catch (Exception e) {
                fail("Exception during Stored Procedure Creation.", e);
            }
        }
    } catch (SQLException ex) {
        LOGGER.warn("", ex);
        fail("Exception during database startup.", ex);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) SQLException(java.sql.SQLException) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata)

Example 4 with StoredProcedureMetadata

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

the class SqlStoredConnectorMetaDataExtensionTest method shouldFetchStoredProcedureMetadataWithSingleParameter.

@Test
public void shouldFetchStoredProcedureMetadataWithSingleParameter() throws SQLException {
    final Connection connection = mock(Connection.class);
    final DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    when(connection.getMetaData()).thenReturn(databaseMetaData);
    when(databaseMetaData.getDatabaseProductName()).thenReturn("POSTGRESQL");
    final ResultSet result = mock(ResultSet.class);
    when(databaseMetaData.getFunctionColumns("catalog", "schema", "procedureName", null)).thenReturn(result);
    when(result.next()).thenReturn(true, false);
    when(result.getString("COLUMN_NAME")).thenReturn("A");
    when(result.getInt("COLUMN_TYPE")).thenReturn(ColumnMode.IN.ordinal());
    when(result.getInt("DATA_TYPE")).thenReturn(JDBCType.INTEGER.getVendorTypeNumber());
    final StoredProcedureMetadata metadata = SqlSupport.getStoredProcedureMetadata(connection, "catalog", "schema", "procedureName");
    final StoredProcedureColumn columnA = new StoredProcedureColumn();
    columnA.setJdbcType(JDBCType.INTEGER);
    columnA.setName("A");
    columnA.setOrdinal(0);
    columnA.setMode(ColumnMode.IN);
    assertThat(metadata.getName()).isEqualTo("procedureName");
    assertThat(metadata.getTemplate()).isEqualTo("procedureName(INTEGER ${body[A]})");
    final List<StoredProcedureColumn> columnList = metadata.getColumnList();
    assertThat(columnList.get(0)).isEqualToComparingFieldByField(columnA);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) StoredProcedureColumn(io.syndesis.connector.sql.common.stored.StoredProcedureColumn) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata) Test(org.junit.Test)

Example 5 with StoredProcedureMetadata

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

the class SqlStoredConnectorMetaDataExtensionTest method shouldFetchStoredProcedureMetadata.

@Test
public void shouldFetchStoredProcedureMetadata() throws SQLException {
    final Connection connection = mock(Connection.class);
    final DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    when(connection.getMetaData()).thenReturn(databaseMetaData);
    when(databaseMetaData.getDatabaseProductName()).thenReturn("POSTGRESQL");
    final ResultSet result = mock(ResultSet.class);
    when(databaseMetaData.getFunctionColumns("catalog", "schema", "procedureName", null)).thenReturn(result);
    when(result.next()).thenReturn(true, true, true, false);
    when(result.getString("COLUMN_NAME")).thenReturn("A", "B", "C");
    when(result.getInt("COLUMN_TYPE")).thenReturn(ColumnMode.IN.ordinal(), ColumnMode.IN.ordinal(), ColumnMode.OUT.ordinal());
    when(result.getInt("DATA_TYPE")).thenReturn(JDBCType.INTEGER.getVendorTypeNumber(), JDBCType.INTEGER.getVendorTypeNumber(), JDBCType.INTEGER.getVendorTypeNumber());
    final StoredProcedureMetadata metadata = SqlSupport.getStoredProcedureMetadata(connection, "catalog", "schema", "procedureName");
    final StoredProcedureColumn columnA = new StoredProcedureColumn();
    columnA.setJdbcType(JDBCType.INTEGER);
    columnA.setName("A");
    columnA.setOrdinal(0);
    columnA.setMode(ColumnMode.IN);
    final StoredProcedureColumn columnB = new StoredProcedureColumn();
    columnB.setJdbcType(JDBCType.INTEGER);
    columnB.setName("B");
    columnB.setOrdinal(0);
    columnB.setMode(ColumnMode.IN);
    final StoredProcedureColumn columnC = new StoredProcedureColumn();
    columnC.setJdbcType(JDBCType.INTEGER);
    columnC.setName("C");
    columnC.setOrdinal(0);
    columnC.setMode(ColumnMode.OUT);
    assertThat(metadata.getName()).isEqualTo("procedureName");
    assertThat(metadata.getTemplate()).isEqualTo("procedureName(INTEGER ${body[A]}, INTEGER ${body[B]}, OUT INTEGER C)");
    final List<StoredProcedureColumn> columnList = metadata.getColumnList();
    assertThat(columnList.get(0)).isEqualToComparingFieldByField(columnA);
    assertThat(columnList.get(1)).isEqualToComparingFieldByField(columnB);
    assertThat(columnList.get(2)).isEqualToComparingFieldByField(columnC);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) StoredProcedureColumn(io.syndesis.connector.sql.common.stored.StoredProcedureColumn) StoredProcedureMetadata(io.syndesis.connector.sql.common.stored.StoredProcedureMetadata) Test(org.junit.Test)

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