|
@ -0,0 +1,317 @@
|
|
|
package com.yihu.ehr.framework.util.sql;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
|
|
import com.yihu.ehr.framework.constrant.Constants;
|
|
|
import com.yihu.ehr.framework.util.operator.CollectionUtil;
|
|
|
import com.yihu.ehr.framework.util.operator.StringUtil;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by lingfeng on 2015/6/23.
|
|
|
*/
|
|
|
public class SqlCreator {
|
|
|
|
|
|
private String tableName;
|
|
|
private List<String> itemList;
|
|
|
private List<String> itemValueList;
|
|
|
private List<String> conditionList;
|
|
|
private List<String> orderList;
|
|
|
private Map<String, Object> keyValueMap;
|
|
|
private BeanTransformer transformer;
|
|
|
public SqlCreator(Class tClass) {
|
|
|
keyValueMap = new HashMap<>();
|
|
|
conditionList = new ArrayList<>();
|
|
|
itemList = new ArrayList<>();
|
|
|
itemValueList = new ArrayList<>();
|
|
|
orderList = new ArrayList<>();
|
|
|
transformer = new BeanTransformer(tClass);
|
|
|
}
|
|
|
|
|
|
public String insertData() {
|
|
|
|
|
|
StringBuilder sqlBuffer = new StringBuilder();
|
|
|
sqlBuffer.append(Constants.INSERT_INTO + tableName + Constants.LEFT_BRACKET);
|
|
|
for (String item : itemList) {
|
|
|
sqlBuffer.append(item + Constants.COMMA);
|
|
|
}
|
|
|
sqlBuffer.deleteCharAt(sqlBuffer.length() - 1);
|
|
|
sqlBuffer.append(Constants.RIGHT_BRACKET + Constants.VALUES + Constants.LEFT_BRACKET);
|
|
|
for (String item : itemValueList) {
|
|
|
sqlBuffer.append(item + Constants.COMMA);
|
|
|
}
|
|
|
sqlBuffer.deleteCharAt(sqlBuffer.length() - 1);
|
|
|
sqlBuffer.append(Constants.RIGHT_BRACKET + Constants.SEMICOLON);
|
|
|
|
|
|
return sqlBuffer.toString();
|
|
|
}
|
|
|
|
|
|
public String insertData(String tabelName, JsonNode jsonNode) {
|
|
|
|
|
|
setTableName(tabelName);
|
|
|
for (Object key : transformer.columnToProperty.keySet()) {
|
|
|
String property = StringUtil.toString(transformer.columnToProperty.get(key));
|
|
|
JsonNode propertyJsonNode = jsonNode.get(property);
|
|
|
if (propertyJsonNode != null) {
|
|
|
itemList.add(StringUtil.toString(key));
|
|
|
itemValueList.add(Constants.COLON + property);
|
|
|
setKeyValueMapByType(property, propertyJsonNode, keyValueMap);
|
|
|
}
|
|
|
}
|
|
|
return insertData();
|
|
|
}
|
|
|
|
|
|
public void setKeyValueMapByType(String property, JsonNode propertyJsonNode, Map<String, Object> keyValueMap) {
|
|
|
JsonNodeType jsonNodeType = propertyJsonNode.getNodeType();
|
|
|
if (jsonNodeType.equals(JsonNodeType.STRING)) {
|
|
|
keyValueMap.put(property, propertyJsonNode.asText());
|
|
|
} else if (jsonNodeType.equals(JsonNodeType.NUMBER)) {
|
|
|
keyValueMap.put(property, propertyJsonNode.asInt());
|
|
|
} else {
|
|
|
keyValueMap.put(property, null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String countData(String tabelName) {
|
|
|
|
|
|
StringBuilder sqlBuffer = new StringBuilder();
|
|
|
setTableName(tabelName);
|
|
|
sqlBuffer.append(Constants.SELECT);
|
|
|
sqlBuffer.append(Constants.COUNT);
|
|
|
sqlBuffer.append(Constants.FROM + tableName);
|
|
|
if (!CollectionUtil.isEmpty(conditionList)) {
|
|
|
sqlBuffer.append(Constants.WHERE);
|
|
|
sqlBuffer.append("1=1" + Constants.AND);
|
|
|
for (String condition : conditionList) {
|
|
|
sqlBuffer.append(condition + Constants.AND);
|
|
|
}
|
|
|
sqlBuffer.delete(sqlBuffer.length() - 4, sqlBuffer.length() - 1);
|
|
|
}
|
|
|
return sqlBuffer.toString();
|
|
|
}
|
|
|
|
|
|
public String selectData(String tabelName) {
|
|
|
|
|
|
setTableName(tabelName);
|
|
|
for(Object key : transformer.columnToProperty.keySet()){
|
|
|
itemList.add(StringUtil.toString(key));
|
|
|
}
|
|
|
|
|
|
return selectData();
|
|
|
}
|
|
|
|
|
|
public String selectData() {
|
|
|
|
|
|
StringBuilder sqlBuffer = new StringBuilder();
|
|
|
sqlBuffer.append(Constants.SELECT);
|
|
|
for (String item : itemList) {
|
|
|
sqlBuffer.append(item + Constants.COMMA);
|
|
|
}
|
|
|
sqlBuffer.deleteCharAt(sqlBuffer.length() - 1);
|
|
|
sqlBuffer.append(Constants.FROM + tableName);
|
|
|
if (!CollectionUtil.isEmpty(conditionList)) {
|
|
|
sqlBuffer.append(Constants.WHERE);
|
|
|
sqlBuffer.append("1=1" + Constants.AND);
|
|
|
for (String condition : conditionList) {
|
|
|
sqlBuffer.append(condition + Constants.AND);
|
|
|
}
|
|
|
sqlBuffer.delete(sqlBuffer.length() - 4, sqlBuffer.length() - 1);
|
|
|
}
|
|
|
if (!CollectionUtil.isEmpty(orderList)) {
|
|
|
for (String order : orderList) {
|
|
|
sqlBuffer.append(order);
|
|
|
}
|
|
|
sqlBuffer.delete(sqlBuffer.length() - 1, sqlBuffer.length());
|
|
|
}
|
|
|
return sqlBuffer.toString();
|
|
|
}
|
|
|
|
|
|
public String updateDataByTableKey(String tabelName, JsonNode jsonNode) {
|
|
|
|
|
|
setTableName(tabelName);
|
|
|
StringBuilder sqlBuffer = new StringBuilder();
|
|
|
sqlBuffer.append(Constants.UPDATE + tableName + Constants.SET);
|
|
|
Map columnToProperty = transformer.columnToProperty;
|
|
|
for (Object key : columnToProperty.keySet()) {
|
|
|
String property = StringUtil.toString(columnToProperty.get(key));
|
|
|
if (!property.equals(Constants.TABLE_KEY)) {
|
|
|
sqlBuffer.append(key + Constants.EQUAL_MARK + Constants.COLON + property + Constants.COMMA);
|
|
|
setKeyValueMapByType(property, jsonNode.get(property), keyValueMap);
|
|
|
}
|
|
|
}
|
|
|
sqlBuffer.deleteCharAt(sqlBuffer.length() - 1);
|
|
|
Object tableKey = columnToProperty.get(Constants.TABLE_KEY.toUpperCase());
|
|
|
sqlBuffer.append(Constants.WHERE + Constants.TABLE_KEY + Constants.EQUAL_MARK + Constants.COLON + tableKey);
|
|
|
|
|
|
String property = StringUtil.toString(tableKey);
|
|
|
setKeyValueMapByType(property, jsonNode.get(property), keyValueMap);
|
|
|
return sqlBuffer.toString();
|
|
|
}
|
|
|
|
|
|
public String betweenCondition(String item, Object value1, Object value2) {
|
|
|
|
|
|
String condition = item + Constants.BETWEEN + Constants.BEGIN + item + Constants.AND + Constants.END + item;
|
|
|
keyValueMap.put(Constants.BEGIN + item, value1);
|
|
|
keyValueMap.put(Constants.END + item, value2);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String greaterAndEqualCondition(String item, Object value) {
|
|
|
String condition = item + Constants.GREATER_EQUAL_MARK + Constants.COLON + item;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String greaterCondition(String item, Object value) {
|
|
|
String condition = item + Constants.GREATER_MARK + Constants.COLON + item;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String lessAndEqualCondition(String item, Object value) {
|
|
|
String condition = item + Constants.LESS_EQUAL_MARK + Constants.COLON + item;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String lessCondition(String item, Object value) {
|
|
|
String condition = item + Constants.LESS_MARK + Constants.COLON + item;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String equalCondition(String item, Object value) {
|
|
|
String column = StringUtil.toString(transformer.propertyToColumn.get(item));
|
|
|
String condition = column + Constants.EQUAL_MARK + Constants.COLON + item;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String inCondition(String item, Object value) {
|
|
|
String column = StringUtil.toString(transformer.propertyToColumn.get(item));
|
|
|
String condition = column + Constants.IN + Constants.LEFT_BRACKET + Constants.COLON + item + Constants.RIGHT_BRACKET;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String equalConditionQue(String item, Object value) {
|
|
|
String column = StringUtil.toString(transformer.propertyToColumn.get(item));
|
|
|
String condition = column + Constants.EQUAL_MARK + Constants.QUESTION_MARK;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String likeOrCondition(String item1, String item2, Object value) {
|
|
|
if (!StringUtil.isEmpty(value)) {
|
|
|
String column1 = StringUtil.toString(transformer.propertyToColumn.get(item1));
|
|
|
String column2 = StringUtil.toString(transformer.propertyToColumn.get(item2));
|
|
|
String condition = Constants.LEFT_BRACKET + column1 + Constants.LIKE + Constants.COLON + item1
|
|
|
+ Constants.OR + column2 + Constants.LIKE + Constants.COLON + item2 + Constants.RIGHT_BRACKET;
|
|
|
value = Constants.PERCENT + value + Constants.PERCENT;
|
|
|
keyValueMap.put(item1, value);
|
|
|
keyValueMap.put(item2, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
} else {
|
|
|
return Constants.EMPTY;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public String likeCondition(String item, Object value) {
|
|
|
String column = StringUtil.toString(transformer.propertyToColumn.get(item));
|
|
|
String condition = column + Constants.LIKE + Constants.COLON + item;
|
|
|
value = Constants.PERCENT + value + Constants.PERCENT;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String neCondition(String item, Object value) {
|
|
|
String column = StringUtil.toString(transformer.propertyToColumn.get(item));
|
|
|
String condition = column + Constants.NE_MARK + Constants.COLON + item;
|
|
|
keyValueMap.put(item, value);
|
|
|
conditionList.add(condition);
|
|
|
return condition;
|
|
|
}
|
|
|
|
|
|
public String order(String item, Object value) {
|
|
|
String column = StringUtil.toString(transformer.propertyToColumn.get(item));
|
|
|
String order;
|
|
|
if (CollectionUtil.isEmpty(orderList)) {
|
|
|
order = Constants.ORDER_BY + column + Constants.BLANK + value + Constants.COMMA;
|
|
|
} else {
|
|
|
order = column + Constants.BLANK + value + Constants.COMMA;
|
|
|
}
|
|
|
orderList.add(order);
|
|
|
return order;
|
|
|
}
|
|
|
|
|
|
public String getTableName() {
|
|
|
return tableName;
|
|
|
}
|
|
|
|
|
|
public void setTableName(String tableName) {
|
|
|
this.tableName = tableName;
|
|
|
}
|
|
|
|
|
|
public List<String> getItemList() {
|
|
|
return itemList;
|
|
|
}
|
|
|
|
|
|
public void setItemList(List<String> itemList) {
|
|
|
this.itemList = itemList;
|
|
|
}
|
|
|
|
|
|
public List<String> getItemValueList() {
|
|
|
return itemValueList;
|
|
|
}
|
|
|
|
|
|
public void setItemValueList(List<String> itemValueList) {
|
|
|
this.itemValueList = itemValueList;
|
|
|
}
|
|
|
|
|
|
public List<String> getConditionList() {
|
|
|
return conditionList;
|
|
|
}
|
|
|
|
|
|
public void setConditionList(List<String> conditionList) {
|
|
|
this.conditionList = conditionList;
|
|
|
}
|
|
|
|
|
|
public Map<String, Object> getKeyValueMap() {
|
|
|
return keyValueMap;
|
|
|
}
|
|
|
|
|
|
public void setKeyValueMap(Map<String, Object> keyValueMap) {
|
|
|
this.keyValueMap = keyValueMap;
|
|
|
}
|
|
|
|
|
|
public List<String> getOrderList() {
|
|
|
return orderList;
|
|
|
}
|
|
|
|
|
|
public void setOrderList(List<String> orderList) {
|
|
|
this.orderList = orderList;
|
|
|
}
|
|
|
|
|
|
public BeanTransformer getTransformer() {
|
|
|
return transformer;
|
|
|
}
|
|
|
|
|
|
public void setTransformer(BeanTransformer transformer) {
|
|
|
this.transformer = transformer;
|
|
|
}
|
|
|
}
|