Search in sources :

Example 26 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.

the class JdbcTemplateAutoConfigurationTests method testJdbcTemplateExistsWithCustomDataSource.

@Test
public void testJdbcTemplateExistsWithCustomDataSource() throws Exception {
    this.context.register(TestDataSourceConfiguration.class, DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
    assertThat(jdbcTemplate).isNotNull();
    assertThat(jdbcTemplate.getDataSource() instanceof BasicDataSource).isTrue();
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Test(org.junit.Test)

Example 27 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project otter by alibaba.

the class DataSourceChecker method checkNamespaceTables.

public String checkNamespaceTables(final String namespace, final String name, final Long dataSourceId) {
    DataSource dataSource = null;
    try {
        DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
        DbMediaSource dbMediaSource = (DbMediaSource) source;
        dataSource = dataSourceCreator.createDataSource(dbMediaSource);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<String> schemaList;
        {
            ModeValue mode = ConfigHelper.parseMode(namespace);
            String schemaPattern = ConfigHelper.makeSQLPattern(mode, namespace);
            final ModeValueFilter modeValueFilter = ConfigHelper.makeModeValueFilter(mode, namespace);
            if (source.getType().isOracle()) {
                schemaList = Arrays.asList(namespace);
            } else {
                schemaList = DdlUtils.findSchemas(jdbcTemplate, schemaPattern, new DdlSchemaFilter() {

                    @Override
                    public boolean accept(String schemaName) {
                        return modeValueFilter.accept(schemaName);
                    }
                });
            }
        }
        final List<String> matchSchemaTables = new ArrayList<String>();
        matchSchemaTables.add("Find schema and tables:");
        if (schemaList != null) {
            ModeValue mode = ConfigHelper.parseMode(name);
            String tableNamePattern = ConfigHelper.makeSQLPattern(mode, name);
            final ModeValueFilter modeValueFilter = ConfigHelper.makeModeValueFilter(mode, name);
            for (String schema : schemaList) {
                DdlUtils.findTables(jdbcTemplate, schema, schema, tableNamePattern, null, new DdlTableNameFilter() {

                    @Override
                    public boolean accept(String catalogName, String schemaName, String tableName) {
                        if (modeValueFilter.accept(tableName)) {
                            matchSchemaTables.add(schemaName + "." + tableName);
                        }
                        return false;
                    }
                });
            }
        }
        if (matchSchemaTables.size() == 1) {
            return TABLE_FAIL;
        }
        return StringUtils.join(matchSchemaTables, "<br>\n");
    } catch (Exception e) {
        logger.error("check error!", e);
        return TABLE_FAIL;
    } finally {
        dataSourceCreator.destroyDataSource(dataSource);
    }
}
Also used : ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) ModeValueFilter(com.alibaba.otter.shared.common.model.config.ModeValueFilter) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) DdlTableNameFilter(com.alibaba.otter.shared.common.utils.meta.DdlTableNameFilter) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) DdlSchemaFilter(com.alibaba.otter.shared.common.utils.meta.DdlSchemaFilter)

Example 28 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project otter by alibaba.

the class DataMediaServiceImpl method queryColumnByMedia.

@Override
public List<String> queryColumnByMedia(DataMedia dataMedia) {
    List<String> columnResult = new ArrayList<String>();
    if (dataMedia.getSource().getType().isNapoli()) {
        return columnResult;
    }
    DataSource dataSource = dataSourceCreator.createDataSource(dataMedia.getSource());
    // 针对multi表,直接获取第一个匹配的表结构
    String schemaName = dataMedia.getNamespaceMode().getSingleValue();
    String tableName = dataMedia.getNameMode().getSingleValue();
    try {
        Table table = DdlUtils.findTable(new JdbcTemplate(dataSource), schemaName, schemaName, tableName);
        for (Column column : table.getColumns()) {
            columnResult.add(column.getName());
        }
    } catch (Exception e) {
        logger.error("ERROR ## DdlUtils find table happen error!", e);
    }
    return columnResult;
}
Also used : Table(org.apache.ddlutils.model.Table) Column(org.apache.ddlutils.model.Column) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ManagerException(com.alibaba.otter.manager.biz.common.exceptions.ManagerException) RepeatConfigureException(com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException) DataSource(javax.sql.DataSource)

Example 29 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project otter by alibaba.

the class DbDialectTest method test_mysql.

@Test(expectedExceptions = RuntimeException.class)
public void test_mysql() {
    DbDataMedia media = getMysqlMedia();
    final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, media.getSource());
    want.object(dbDialect).clazIs(MysqlDialect.class);
    final SqlTemplate sqlTemplate = dbDialect.getSqlTemplate();
    final JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
    final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();
    final int[] pkColumnTypes = { Types.INTEGER, Types.VARCHAR };
    final int[] columnTypes = { Types.CHAR, Types.DECIMAL, Types.BLOB, Types.CLOB, Types.DATE, Types.TIMESTAMP, Types.TIMESTAMP };
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            int affect = 0;
            String sql = null;
            // 执行insert
            sql = sqlTemplate.getInsertSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            // 执行update
            sql = sqlTemplate.getUpdateSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            // 执行deleate
            sql = sqlTemplate.getDeleteSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(pkColumnTypes), toValues(pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            // 执行merge
            sql = sqlTemplate.getMergeSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns, columns, null, true);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            throw new RuntimeException("rollback");
        }
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) PreparedStatement(java.sql.PreparedStatement) SqlTemplate(com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DbDataMedia(com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia) Test(org.testng.annotations.Test) BaseDbTest(com.alibaba.otter.node.etl.BaseDbTest)

Example 30 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project camel by apache.

the class SqlComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    DataSource target = null;
    // endpoint options overrule component configured datasource
    DataSource ds = resolveAndRemoveReferenceParameter(parameters, "dataSource", DataSource.class);
    if (ds != null) {
        target = ds;
    }
    String dataSourceRef = getAndRemoveParameter(parameters, "dataSourceRef", String.class);
    if (target == null && dataSourceRef != null) {
        target = CamelContextHelper.mandatoryLookup(getCamelContext(), dataSourceRef, DataSource.class);
    }
    if (target == null) {
        // fallback and use component
        target = dataSource;
    }
    if (target == null) {
        // check if the registry contains a single instance of DataSource
        Set<DataSource> dataSources = getCamelContext().getRegistry().findByType(DataSource.class);
        if (dataSources.size() > 1) {
            throw new IllegalArgumentException("Multiple DataSources found in the registry and no explicit configuration provided");
        } else if (dataSources.size() == 1) {
            target = dataSources.stream().findFirst().orElse(null);
        }
    }
    if (target == null) {
        throw new IllegalArgumentException("DataSource must be configured");
    }
    String parameterPlaceholderSubstitute = getAndRemoveParameter(parameters, "placeholder", String.class, "#");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(target);
    Map<String, Object> templateOptions = IntrospectionSupport.extractProperties(parameters, "template.");
    IntrospectionSupport.setProperties(jdbcTemplate, templateOptions);
    String query = remaining;
    if (usePlaceholder) {
        query = query.replaceAll(parameterPlaceholderSubstitute, "?");
    }
    String onConsume = getAndRemoveParameter(parameters, "consumer.onConsume", String.class);
    if (onConsume == null) {
        onConsume = getAndRemoveParameter(parameters, "onConsume", String.class);
    }
    if (onConsume != null && usePlaceholder) {
        onConsume = onConsume.replaceAll(parameterPlaceholderSubstitute, "?");
    }
    String onConsumeFailed = getAndRemoveParameter(parameters, "consumer.onConsumeFailed", String.class);
    if (onConsumeFailed == null) {
        onConsumeFailed = getAndRemoveParameter(parameters, "onConsumeFailed", String.class);
    }
    if (onConsumeFailed != null && usePlaceholder) {
        onConsumeFailed = onConsumeFailed.replaceAll(parameterPlaceholderSubstitute, "?");
    }
    String onConsumeBatchComplete = getAndRemoveParameter(parameters, "consumer.onConsumeBatchComplete", String.class);
    if (onConsumeBatchComplete == null) {
        onConsumeBatchComplete = getAndRemoveParameter(parameters, "onConsumeBatchComplete", String.class);
    }
    if (onConsumeBatchComplete != null && usePlaceholder) {
        onConsumeBatchComplete = onConsumeBatchComplete.replaceAll(parameterPlaceholderSubstitute, "?");
    }
    SqlEndpoint endpoint = new SqlEndpoint(uri, this, jdbcTemplate, query);
    endpoint.setPlaceholder(parameterPlaceholderSubstitute);
    endpoint.setUsePlaceholder(isUsePlaceholder());
    endpoint.setOnConsume(onConsume);
    endpoint.setOnConsumeFailed(onConsumeFailed);
    endpoint.setOnConsumeBatchComplete(onConsumeBatchComplete);
    endpoint.setDataSource(ds);
    endpoint.setDataSourceRef(dataSourceRef);
    endpoint.setTemplateOptions(templateOptions);
    return endpoint;
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)121 Test (org.junit.Test)45 DataSource (javax.sql.DataSource)36 Before (org.junit.Before)18 SQLException (java.sql.SQLException)11 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)11 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)6 Connection (java.sql.Connection)6 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 Test (org.testng.annotations.Test)6 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 PreparedStatement (java.sql.PreparedStatement)5 Table (org.apache.ddlutils.model.Table)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 DataAccessException (org.springframework.dao.DataAccessException)5 AbstractDriverBasedDataSource (org.springframework.jdbc.datasource.AbstractDriverBasedDataSource)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5