修改Elasticsearch JDBC兼容问题

This commit is contained in:
datagear 2019-10-11 15:18:57 +08:00
parent cd6d9bdb8d
commit 1bce008153
3 changed files with 115 additions and 1 deletions

View File

@ -831,7 +831,12 @@ public class SelectPersistenceOperation extends AbstractModelPersistenceOperatio
if (isEmptySqlBuilder(tableFieldCondition))
fromSql.sql(tableNameQuote);
else
fromSql.sql("(SELECT * FROM ").sql(tableNameQuote).sql(" WHERE ").sql(tableFieldCondition).sql(")");
{
// 这里使用显示列名查询SQL语句避免SELECT *
// FROM结果集列名与model不一致的情况比如Elasticsearch JDBC
String explicitColSelect = buildExplicitColumnSelectSql(dialect, model, tableNameQuote);
fromSql.sql("(").sql(explicitColSelect).sql(" WHERE ").sql(tableFieldCondition).sql(")");
}
fromSql.sql(" ").sql(tableAliasQuote);
}
@ -1545,6 +1550,73 @@ public class SelectPersistenceOperation extends AbstractModelPersistenceOperatio
return str;
}
/**
* 构建显示指定列名的表SELECT查询语句
*
* @param dialect
* @param model
* @param tableNameQuote
* @return
*/
protected String buildExplicitColumnSelectSql(Dialect dialect, Model model, String tableNameQuote)
{
StringBuilder sql = new StringBuilder();
sql.append("SELECT ");
Property[] properties = model.getProperties();
int appendCount = 0;
for (int i = 0; i < properties.length; i++)
{
Property property = properties[i];
if (property.hasFeature(NotReadable.class))
continue;
Mapper mapper = getMapper(model, property);
if (MapperUtil.isModelTableMapper(mapper))
{
ModelTableMapper mtm = MapperUtil.castModelTableMapper(mapper);
if (mtm.isPrimitivePropertyMapper())
{
String columnNameQuote = toQuoteName(dialect, mtm.getPrimitiveColumnName());
if (appendCount > 0)
sql.append(", ");
else
sql.append(" ");
sql.append(columnNameQuote);
appendCount++;
}
else
{
String[] pkeyColumnNamesQuote = toQuoteNames(dialect, mtm.getPropertyKeyColumnNames());
for (int j = 0; j < pkeyColumnNamesQuote.length; j++)
{
if (appendCount > 0)
sql.append(", ");
else
sql.append(" ");
sql.append(pkeyColumnNamesQuote[j]);
}
appendCount += pkeyColumnNamesQuote.length;
}
}
}
sql.append(" FROM ").append(tableNameQuote);
return sql.toString();
}
/**
* {@linkplain Query}构建查询条件SQL
*

View File

@ -57,4 +57,24 @@
port : 5000,
name : ""
}
},
{
dbType : "Hive",
template : "jdbc:hive2://{host}:{port}/{name}",
defaultValue :
{
host : "",
port : 10000,
name : "default"
}
},
{
dbType : "Elasticsearch",
template : "jdbc:es://http://{host}:{port}",
defaultValue :
{
host : "",
port : 9200,
name : ""
}
}

View File

@ -0,0 +1,22 @@
curl -XPUT "http://192.168.56.101:9200/t_account"
curl -XPUT "http://192.168.56.101:9200/t_address"
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/1" -H 'Content-Type: application/json' -d '{"name": "a","age" : "1"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/2" -H 'Content-Type: application/json' -d '{"name": "b","age" : "2"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/3" -H 'Content-Type: application/json' -d '{"name": "c","age" : "3"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/4" -H 'Content-Type: application/json' -d '{"name": "d","age" : "4"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/5" -H 'Content-Type: application/json' -d '{"name": "e","age" : "5"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/6" -H 'Content-Type: application/json' -d '{"name": "f","age" : "6"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/7" -H 'Content-Type: application/json' -d '{"name": "g","age" : "7"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/8" -H 'Content-Type: application/json' -d '{"name": "h","age" : "8"}'
curl -XPUT "http://192.168.56.101:9200/t_account/_doc/9" -H 'Content-Type: application/json' -d '{"name": "i","age" : "9"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/1' -H 'Content-Type: application/json' -d '{"name": "a","home" : "1", "work" : "w1"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/2' -H 'Content-Type: application/json' -d '{"name": "b","home" : "2", "work" : "w2"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/3' -H 'Content-Type: application/json' -d '{"name": "c","home" : "3", "work" : "w3"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/4' -H 'Content-Type: application/json' -d '{"name": "d","home" : "4", "work" : "w4"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/5' -H 'Content-Type: application/json' -d '{"name": "e","home" : "5", "work" : "w5"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/6' -H 'Content-Type: application/json' -d '{"name": "f","home" : "6", "work" : "w6"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/7' -H 'Content-Type: application/json' -d '{"name": "g","home" : "7", "work" : "w7"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/8' -H 'Content-Type: application/json' -d '{"name": "h","home" : "8", "work" : "w8"}'
curl -XPUT 'http://192.168.56.101:9200/t_address/_doc/9' -H 'Content-Type: application/json' -d '{"name": "i","home" : "9", "work" : "w9"}'