Commit b5b2baef authored by liaozan's avatar liaozan 🏀

Update starrocks

parent 2765b561
package com.schbrain.framework.autoconfigure.starrocks; package com.schbrain.framework.autoconfigure.starrocks;
import com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties; import com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import static com.schbrain.framework.autoconfigure.starrocks.constants.StarrocksConstants.JDBC_URL_TEMPLATE;
/** /**
* @author liaozan * @author liaozan
...@@ -12,4 +18,15 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties ...@@ -12,4 +18,15 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
@EnableConfigurationProperties(StarrocksProperties.class) @EnableConfigurationProperties(StarrocksProperties.class)
public class StarrocksAutoConfiguration { public class StarrocksAutoConfiguration {
@Bean("starrocksJdbcTemplate")
public NamedParameterJdbcTemplate starrocksJdbcTemplate(StarrocksProperties config) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setPoolName(config.getDatabase());
hikariConfig.setDriverClassName(config.getDriverClassName());
hikariConfig.setJdbcUrl(String.format(JDBC_URL_TEMPLATE, config.getHost(), config.getPort(), config.getDatabase()));
hikariConfig.setUsername(config.getUsername());
hikariConfig.setPassword(config.getPassword());
return new NamedParameterJdbcTemplate(new HikariDataSource(hikariConfig));
}
} }
package com.schbrain.framework.autoconfigure.starrocks.operation; package com.schbrain.framework.autoconfigure.starrocks.operation;
import org.springframework.jdbc.core.PreparedStatementCreator;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author liaozan * @author liaozan
...@@ -16,17 +15,17 @@ public interface StarrocksService<T> { ...@@ -16,17 +15,17 @@ public interface StarrocksService<T> {
void upsert(T entity); void upsert(T entity);
/** /**
* 单个保存/更新,传入 columns 只会处理响应的 column * 批量保存/更新
*/ */
void upsert(T entity, List<String> columns); void upsertBatch(List<T> entityList);
/** /**
* 批量保存/更新 * 单个保存/更新,传入 columns 只会处理相应的 columns
*/ */
void upsertBatch(List<T> entityList); void upsert(T entity, List<String> columns);
/** /**
* 批量保存/更新,传入 columns 只会处理响应的 column * 批量保存/更新,传入 columns 只会处理相应的 columns
*/ */
void upsertBatch(List<T> entityList, List<String> columns); void upsertBatch(List<T> entityList, List<String> columns);
...@@ -43,6 +42,6 @@ public interface StarrocksService<T> { ...@@ -43,6 +42,6 @@ public interface StarrocksService<T> {
/** /**
* 根据 sql 查询 * 根据 sql 查询
*/ */
List<T> search(PreparedStatementCreator creator); List<T> search(String sql, Map<String, Object> params);
} }
...@@ -3,18 +3,16 @@ package com.schbrain.framework.autoconfigure.starrocks.operation; ...@@ -3,18 +3,16 @@ package com.schbrain.framework.autoconfigure.starrocks.operation;
import com.schbrain.common.util.ValidateUtils; import com.schbrain.common.util.ValidateUtils;
import com.schbrain.framework.autoconfigure.starrocks.annotation.StarrocksTable; import com.schbrain.framework.autoconfigure.starrocks.annotation.StarrocksTable;
import com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties; import com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.jdbc.core.*; import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.*;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.schbrain.framework.autoconfigure.starrocks.constants.StarrocksConstants.JDBC_URL_TEMPLATE;
import static com.schbrain.framework.autoconfigure.starrocks.constants.StarrocksConstants.STREAM_LOAD_TEMPLATE; import static com.schbrain.framework.autoconfigure.starrocks.constants.StarrocksConstants.STREAM_LOAD_TEMPLATE;
/** /**
...@@ -28,8 +26,10 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin ...@@ -28,8 +26,10 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
@Autowired @Autowired
protected StarrocksProperties config; protected StarrocksProperties config;
@Autowired
@Qualifier("starrocksJdbcTemplate")
protected NamedParameterJdbcTemplate jdbcTemplate;
protected JdbcTemplate jdbcTemplate;
protected StarrocksStreamLoadHandler handler; protected StarrocksStreamLoadHandler handler;
@SuppressWarnings({"unchecked", "DataFlowIssue"}) @SuppressWarnings({"unchecked", "DataFlowIssue"})
...@@ -44,13 +44,13 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin ...@@ -44,13 +44,13 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
} }
@Override @Override
public void upsert(T entity, List<String> columns) { public void upsertBatch(List<T> entityList) {
upsertBatch(List.of(ValidateUtils.notNull(entity, "entity不能为空")), columns); upsertBatch(entityList, Collections.emptyList());
} }
@Override @Override
public void upsertBatch(List<T> entityList) { public void upsert(T entity, List<String> columns) {
upsertBatch(entityList, Collections.emptyList()); upsertBatch(List.of(ValidateUtils.notNull(entity, "entity不能为空")), columns);
} }
@Override @Override
...@@ -69,15 +69,21 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin ...@@ -69,15 +69,21 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
} }
@Override @Override
public List<T> search(PreparedStatementCreator callback) { public List<T> search(String sql, Map<String, Object> params) {
return jdbcTemplate.queryForStream(callback, rowMapper).collect(Collectors.toList()); SqlParameterSource parameterSource;
if (params.isEmpty()) {
parameterSource = EmptySqlParameterSource.INSTANCE;
} else {
parameterSource = new MapSqlParameterSource(params);
}
// noinspection SqlSourceToSinkFlow 关掉idea sql注入检查
return jdbcTemplate.queryForStream(sql, parameterSource, rowMapper).collect(Collectors.toList());
} }
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
StarrocksTable annotation = ValidateUtils.notNull(entityClass.getAnnotation(StarrocksTable.class), StarrocksTable.class.getName() + "不能为空"); StarrocksTable annotation = ValidateUtils.notNull(entityClass.getAnnotation(StarrocksTable.class), StarrocksTable.class.getName() + "不能为空");
this.handler = createHandler(annotation.value()); this.handler = createHandler(annotation.value());
this.jdbcTemplate = createJdbcTemplate(annotation.value());
} }
protected StarrocksStreamLoadHandler createHandler(String tableName) { protected StarrocksStreamLoadHandler createHandler(String tableName) {
...@@ -85,14 +91,4 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin ...@@ -85,14 +91,4 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
return new StarrocksStreamLoadHandler(streamLoadUrl, config.getUsername(), config.getPassword()); return new StarrocksStreamLoadHandler(streamLoadUrl, config.getUsername(), config.getPassword());
} }
protected JdbcTemplate createJdbcTemplate(String tableName) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setPoolName(String.format("%s:%s", config.getDatabase(), tableName));
hikariConfig.setDriverClassName(config.getDriverClassName());
hikariConfig.setJdbcUrl(String.format(JDBC_URL_TEMPLATE, config.getHost(), config.getPort(), config.getDatabase()));
hikariConfig.setUsername(config.getUsername());
hikariConfig.setPassword(config.getPassword());
return new JdbcTemplate(new HikariDataSource(hikariConfig));
}
} }
...@@ -3,7 +3,6 @@ package com.schbrain.framework.dao.util; ...@@ -3,7 +3,6 @@ package com.schbrain.framework.dao.util;
import com.schbrain.common.util.StreamUtils; import com.schbrain.common.util.StreamUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ClassUtils;
import java.util.*; import java.util.*;
...@@ -14,28 +13,33 @@ import java.util.*; ...@@ -14,28 +13,33 @@ import java.util.*;
*/ */
public class SQLUtil { public class SQLUtil {
private static final Set<Class<?>> NUMBER_TYPES = Set.of(Integer.class, Long.class, Short.class, Byte.class, Double.class, Float.class);
public static <T> String buildInClause(String columnName, Class<T> valueType, List<T> values) { public static <T> String buildInClause(String columnName, Class<T> valueType, List<T> values) {
Set<T> valueList = StreamUtils.filterToSet(values, Objects::nonNull); Set<T> valueList = StreamUtils.filterToSet(values, Objects::nonNull);
if (CollectionUtils.isEmpty(valueList)) { if (CollectionUtils.isEmpty(valueList)) {
throw new IllegalArgumentException("Value list can not be empty."); throw new IllegalArgumentException("Value list can not be empty.");
} }
StringBuilder builder = new StringBuilder(" "); StringBuilder builder = new StringBuilder(columnName).append(" in (");
builder.append(columnName).append(" in ("); if (isNumberType(valueType)) {
if (ClassUtils.isPrimitiveWrapper(valueType)) { builder.append(StreamUtils.join(valueList));
builder.append(StreamUtils.join(valueList)).append(")");
} else { } else {
builder.append(StreamUtils.join(valueList, SQLUtil::escapeSql)).append(")"); builder.append(StreamUtils.join(valueList, SQLUtil::escapeSql));
} }
builder.append(" "); builder.append(")");
return builder.toString(); return StringUtils.wrap(builder.toString(), " ");
} }
private static String escapeSql(Object value) { private static String escapeSql(Object value) {
if (value instanceof String) { if (value instanceof String) {
return StringUtils.replace((String) value, "'", "''"); return StringUtils.wrap((String) value, "'");
} else { } else {
return String.valueOf(value); return String.valueOf(value);
} }
} }
private static boolean isNumberType(Class<?> valueType) {
return NUMBER_TYPES.contains(valueType);
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment