SqlKeyword.java 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
S
smallchill 已提交
16
package org.springblade.core.toolbox.support;
Z
zhuangqian 已提交
17

S
smallchill 已提交
18 19 20
import org.springblade.core.toolbox.Func;
import org.springblade.core.toolbox.kit.JsonKit;
import org.springblade.core.toolbox.kit.StrKit;
Z
zhuangqian 已提交
21

S
bug fix  
smallchill 已提交
22 23 24
import java.util.HashMap;
import java.util.Map;

Z
zhuangqian 已提交
25 26
/**
 * 定义常用的 sql关键字
27
 * @author zhuangqian
Z
zhuangqian 已提交
28 29
 */
public class SqlKeyword {
30
    public static final String SKIP = "_skip_true";
Z
zhuangqian 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
	private static final String EQUAL = "_equal";
	private static final String NOT_EQUAL = "_notequal";
	private static final String LIKE = "_like";
	private static final String NOT_LIKE = "_notlike";
	private static final String GT = "_gt";
	private static final String LT = "_lt";
	private static final String DATE_GT = "_dategt";
	private static final String DATE_LT = "_datelt";
	private static final String OR = "or_";
	private static final String AND = "and_";
	private static final String SECOND = "_2nd";
	private static final String IS_NULL = "_null";
	private static final String NOT_NULL = "_notnull";
44 45 46
	public static final String TOINT = "toint_";
	public static final String IT = "it_";
	public static final String F_IT = "f_it_";
Z
zhuangqian 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	private static HashMap<String, String> keyWord = new HashMap<String, String>();
	static {
		keyWord.put(EQUAL, " = ? ");
		keyWord.put(NOT_EQUAL, " <> ? ");
		keyWord.put(LIKE, " like ? ");
		keyWord.put(NOT_LIKE, " not like ? ");
		keyWord.put(GT, " > ? ");
		keyWord.put(LT, " < ? ");
		keyWord.put(DATE_GT, " > ? ");
		keyWord.put(DATE_LT, " < ? ");
		keyWord.put(IS_NULL, " is null ");
		keyWord.put(NOT_NULL, " is not null ");
	}

	/**
	 * 根据前台json格式转化成map进行遍历
	 * 
	 * @param w
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public static String getWhere(String w) {
		try {
			StringBuilder where = new StringBuilder(" where 1=1 ");
			StringBuilder or = new StringBuilder();
			StringBuilder and = new StringBuilder();
			if (StrKit.notBlank(w)) {
				w = Func.decodeUrl(w);
				Map<String, String> mm = JsonKit.parse(w, HashMap.class);
				for (String m : mm.keySet()) {
77 78 79
                    if (m.endsWith(SKIP)) {
                        continue;
                    }
Z
zhuangqian 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
					String col = clearKeyWord(m);
					String k = "";
					for (String key : keyWord.keySet()) {
						if (m.indexOf(key) >= 0) {
							k = key;
							break;
						}
					}
					if (StrKit.isBlank(k)) {
						k = LIKE;
					}
					String filter = col + getKeyWord(k, m);
					if (m.indexOf(OR) == -1) {
						and.append(" and (" + filter + ")");
					} else {
						or.append(" or (" + filter + ")");
					}
				}
				where.append(and.toString()).append(or.toString());
			}
			return where.toString();
		} catch (Exception ex) {
			return " where 1=1 ";
		}
	}

	public static String clearKeyWord(String key) {
		for (String k : keyWord.keySet()) {
			key = key.replace(k, "");
		}
110
		key = key.replace(OR, "").replace(AND, "").replace(SECOND, "").replace(TOINT, "");
Z
zhuangqian 已提交
111 112 113 114 115
		return key;
	}

	public static String getKeyWord(String key, String value) {
		String _keyWord = keyWord.get(key);
Z
update  
zhuangqian 已提交
116
		if (key.equals(DATE_GT) || key.equals(DATE_LT)) {
Z
zhuangqian 已提交
117 118
			if (Func.isOracle()) {
				value = "to_date(#{" + value + "},'yyyy-mm-dd hh24:mi:ss')" + (key.equals(DATE_LT) ? "-1" : "");
S
bug fix  
smallchill 已提交
119 120
                return _keyWord.replace("?", value);
            }
Z
update  
zhuangqian 已提交
121
		}
122 123
		String like = "like";
		if(key.indexOf(like) > 0){
Z
zhuangqian 已提交
124 125 126 127 128 129
			return _keyWord.replace("?", " CONCAT(CONCAT('%', #{" + value + "}),'%')  ");
		}
		return _keyWord.replace("?", "#{" + value + "}");
	}
	
}