Skip to content

Commit ed05ae3

Browse files
committed
Add:ORM's support for Hive
1 parent c496f01 commit ed05ae3

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
126126
DATABASE_LIST.add(DATABASE_ORACLE);
127127
DATABASE_LIST.add(DATABASE_DB2);
128128
DATABASE_LIST.add(DATABASE_CLICKHOUSE);
129+
DATABASE_LIST.add(DATABASE_HIVE);
129130

130131

131132
RAW_MAP = new LinkedHashMap<>(); // 保证顺序,避免配置冲突等意外情况
@@ -903,6 +904,13 @@ public boolean isClickHouse() {
903904
public static boolean isClickHouse(String db) {
904905
return DATABASE_CLICKHOUSE.equals(db);
905906
}
907+
@Override
908+
public boolean isHive() {
909+
return isHive(getSQLDatabase());
910+
}
911+
public static boolean isHive(String db) {
912+
return DATABASE_HIVE.equals(db);
913+
}
906914

907915
@Override
908916
public String getQuote() {
@@ -2727,6 +2735,9 @@ public String getRegExpString(String key, String value, boolean ignoreCase) {
27272735
if (isClickHouse()) {
27282736
return "match(" + (ignoreCase ? "lower(" : "") + getKey(key) + (ignoreCase ? ")" : "") + ", " + (ignoreCase ? "lower(" : "") + getValue(value) + (ignoreCase ? ")" : "") + ")";
27292737
}
2738+
if (isHive()) {
2739+
return (ignoreCase ? "lower(" : "") + getKey(key) + (ignoreCase ? ")" : "") + " REGEXP " + (ignoreCase ? "lower(" : "") + getValue(value) + (ignoreCase ? ")" : "");
2740+
}
27302741
return getKey(key) + " REGEXP " + (ignoreCase ? "" : "BINARY ") + getValue(value);
27312742
}
27322743
//~ regexp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map.Entry;
2828
import java.util.Objects;
2929
import java.util.Set;
30+
import java.util.regex.Pattern;
3031

3132
import com.alibaba.fastjson.JSON;
3233
import com.alibaba.fastjson.JSONObject;
@@ -36,6 +37,7 @@
3637
import apijson.NotNull;
3738
import apijson.RequestMethod;
3839
import apijson.StringUtil;
40+
import apijson.orm.AbstractSQLConfig;
3941

4042
/**executor for query(read) or update(write) MySQL database
4143
* @author Lemon
@@ -718,7 +720,15 @@ protected List<JSONObject> onPutTable(@NotNull SQLConfig config, @NotNull Result
718720

719721
protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd
720722
, final int tablePosition, @NotNull JSONObject table, final int columnIndex, Map<String, JSONObject> childMap) throws Exception {
721-
return rsmd.getColumnLabel(columnIndex); // dotIndex < 0 ? lable : lable.substring(dotIndex + 1);
723+
String key = rsmd.getColumnLabel(columnIndex);// dotIndex < 0 ? lable : lable.substring(dotIndex + 1);
724+
if (config.isHive()) {
725+
String table_name = config.getTable();
726+
if(AbstractSQLConfig.TABLE_KEY_MAP.containsKey(table_name)) table_name = AbstractSQLConfig.TABLE_KEY_MAP.get(table_name);
727+
String pattern = "^" + table_name + "\\." + "[a-zA-Z]+$";
728+
boolean isMatch = Pattern.matches(pattern, key);
729+
if(isMatch) key = key.split("\\.")[1];
730+
}
731+
return key;
722732
}
723733

724734
protected Object getValue(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd
@@ -974,6 +984,7 @@ public ResultSet executeQuery(@NotNull SQLConfig config) throws Exception {
974984
public int executeUpdate(@NotNull SQLConfig config) throws Exception {
975985
PreparedStatement s = getStatement(config);
976986
int count = s.executeUpdate(); //PreparedStatement 不用传 SQL
987+
if (config.isHive() && count==0) count = 1;
977988

978989
if (config.getMethod() == RequestMethod.POST && config.getId() == null) { //自增id
979990
ResultSet rs = s.getGeneratedKeys();

APIJSONORM/src/main/java/apijson/orm/SQLConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface SQLConfig {
2222
String DATABASE_ORACLE = "ORACLE";
2323
String DATABASE_DB2 = "DB2";
2424
String DATABASE_CLICKHOUSE = "CLICKHOUSE";
25+
String DATABASE_HIVE = "HIVE";
2526

2627
String SCHEMA_INFORMATION = "information_schema"; //MySQL, PostgreSQL, SQL Server 都有的系统模式
2728
String SCHEMA_SYS = "sys"; //SQL Server 系统模式
@@ -38,6 +39,7 @@ public interface SQLConfig {
3839
boolean isOracle();
3940
boolean isDb2();
4041
boolean isClickHouse();
42+
boolean isHive();
4143
//暂时只兼容以上 5 种
4244
// boolean isSQL();
4345
// boolean isTSQL();

0 commit comments

Comments
 (0)