1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package com.boman.system.common;
- /**
- * @author shiqian
- * @description
- * @date 2021年03月22日 11:42
- **/
- public class QueryUtils {
- public static String replaceVariables(String sql, User user) {
- if (user == null) {
- return sql;
- } else if (sql == null) {
- return null;
- } else {
- StringBuilder sb = new StringBuilder();
- int p = 0;
- while (p < sql.length()) {
- int p1 = sql.indexOf("$", p);
- if (p1 > -1) {
- int p2 = sql.indexOf("$", p1 + 1);
- if (p2 > -1) {
- String n = sql.substring(p1, p2 + 1);
- Object v = user.getAttribute(n);
- if (v != null) {
- sb.append(sql.substring(p, p1)).append(v);
- p = p2 + 1;
- } else {
- sb.append(sql.substring(p, p2));
- p = p2;
- }
- continue;
- }
- sb.append(sql.substring(p));
- break;
- }
- sb.append(sql.substring(p));
- break;
- }
- return sb.toString();
- }
- }
- }
|