提交 aa3c45d6 编写于 作者: xuhui961310148's avatar xuhui961310148

update:表同步支持写入pgsql数据库

上级 043a97a1
......@@ -48,4 +48,15 @@ public interface DbEntityMapper extends BaseMapper<EntityObj>{
"</script>"})
boolean saveDm(@Param("model") EntityModel model, @Param("entity") EntityObj entity);
@Insert({"<script> "+
" INSERT INTO ${model.tableName} " +
" (${model.postGresqlColumnSegment}) \n" +
" VALUES \n" +
"<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\",\">\n" +
" (${model.postGresqlMergeColumnSegment})\n" +
"</foreach>\n" +
" ${model.postGresqlMergeSegment}" +
"</script>"})
boolean saveBatchPGSql(@Param("model") EntityModel model, @Param("list") List<EntityObj> list);
}
\ No newline at end of file
......@@ -357,6 +357,88 @@ public class EntityModel {
return sql;
}
/**
* pgsql批量保存,数据值读取列拼接
* @return
*/
@JsonIgnore
@JSONField(serialize = false)
public String getPostGresqlMergeColumnSegment(){
String sql="";
boolean first = true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first = false;
else
sql += " , ";
sql+=("#{item."+fieldModel.getColumnName().toUpperCase()+"}");
}
return sql;
}
/**
* pgsql批量保存,数据插入表字段列拼接
* @return
*/
@JsonIgnore
@JSONField(serialize = false)
public String getPostGresqlColumnSegment(){
String sql="";
boolean first=true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first = false;
else
sql += " , ";
sql += fieldModel.getColumnName().toUpperCase();
}
return sql;
}
/**
* pgsql批量保存,更新sql表字段拼接
* @return
*/
@JsonIgnore
@JSONField(serialize = false)
public String getPostGresqlMergeSegment(){
String sql=" ON CONFLICT(";
boolean first=true;
for(FieldModel fieldModel:this.getKeyFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first = false;
else
sql += " , ";
sql += (fieldModel.getColumnName().toUpperCase());
}
sql += ") DO UPDATE SET ";
first = true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(fieldModel.isKeyField())
continue;
if(fieldModel.isUnionKeyField())
continue;
if(first)
first = false;
else
sql += ",";
sql += fieldModel.getColumnName().toUpperCase() + " = excluded."+fieldModel.getColumnName().toUpperCase();
}
return sql;
}
@JsonIgnore
@JSONField(serialize = false)
public String getDsName()
......
......@@ -29,6 +29,7 @@ import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Timestamp;
import java.util.*;
......@@ -268,6 +269,45 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl
else
baseMapper.saveBatchDm(entityModel, list);
}
else if ("postgresql".equalsIgnoreCase(dstype))
{
batchsize = 100;
List<FieldModel> fields = entityModel.getFields();
List<String> clobField = new ArrayList<>();
for (FieldModel fieldModel : fields) {
if(!fieldModel.isPhysicalField())
continue;
if(fieldModel.getField() != null && fieldModel.getField().getDataType() != null && fieldModel.getField().getDataType().equalsIgnoreCase("CLOB")){
clobField.add(fieldModel.getColumnName().toUpperCase());
}
}
if (clobField.size() > 0){
for (EntityObj entityObj : list) {
for (String field : clobField) {
Object objValue = entityObj.get(field);
if (objValue instanceof Clob){
String value = ClobToString((Clob) objValue);
if (value == null){
entityObj.set(field,objValue);
}else {
entityObj.set(field,value);
}
}
}
}
}
if(list.size()>batchsize)
{
List<List<EntityObj>> splist = LiteDataService.splitList(list, batchsize);
splist.forEach(array->{
baseMapper.saveBatchPGSql(entityModel, array);
});
}
else
{
baseMapper.saveBatchPGSql(entityModel, list);
}
}
else
{
if(list.size()>batchsize)
......@@ -293,6 +333,31 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl
}
}
/**
* CLOB转String
* @param clob
* @return
*/
public String ClobToString(final Clob clob) {
if (clob == null) {
return null;
}
StringBuffer sb = new StringBuffer();
try {
Reader is = clob.getCharacterStream();
BufferedReader br = new BufferedReader(is);
String str = br.readLine(); // 读取第一行
while (str != null) { // 如果没有到达流的末尾,则继续读取下一行
sb.append(str);
str = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return sb.toString();
}
@Override
public boolean save(String dsName, EntityModel entityModel, EntityObj entityObj) {
try {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册