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