Search in sources :

Example 11 with JSON

use of com.alibaba.fastjson2.JSON in project druid by alibaba.

the class Oracle_param_2 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD') FROM dual;";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement statement = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.ORACLE);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    /*visitor.setPrettyFormat(false);*/
    statement.accept(visitor);
    /* JSONArray array = new JSONArray();
        for(String table : visitor.getTables()){
            array.add(table.replaceAll("`",""));
        }*/
    String psql = out.toString();
    assertEquals("SELECT TO_DATE(?, 'YYYY-MM-DD')\n" + "FROM dual;", psql);
    String params_json = JSON.toJSONString(parameters, JSONWriter.Feature.WriteClassName);
    System.out.println(params_json);
    JSONArray jsonArray = JSON.parseArray(params_json);
    String json = JSON.toJSONString(jsonArray, JSONWriter.Feature.WriteClassName);
    assertEquals("[\"2013-02-11\"]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD')\n" + "FROM dual;", rsql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson2.JSONArray) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 12 with JSON

use of com.alibaba.fastjson2.JSON in project druid by alibaba.

the class MonitorStatService method service.

@Override
public String service(String url) {
    Map<String, String> parameters = getParameters(url);
    if (url.endsWith("serviceList.json")) {
        return JSON.toJSONString(monitorProperties.getApplications());
    }
    if (url.equals("/datasource.json")) {
        String serviceName = StringUtils.subString(url, "serviceName=", "&sql-");
        Integer id = StringUtils.subStringToInteger(url, "datasource-", ".");
        return getDataSourceStatData();
    }
    if (url.startsWith("/datasource-")) {
        String serviceName = StringUtils.subString(url, "serviceName=", "&sql-");
        Integer id = StringUtils.subStringToInteger(url, "datasource-", ".");
        Object result = getDataSourceStatData();
        return returnJSONResult(result == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, result);
    }
    // 活跃连接数查看
    if (url.startsWith("/connectionInfo-") && url.endsWith(".json")) {
        String serviceId = StringUtils.subString(url, "&serviceId=", ".json");
        Integer id = StringUtils.subStringToInteger(url, "connectionInfo-", "&");
        return getPoolingConnectionInfoByDataSourceId(id, serviceId);
    }
    // SQL监控列表
    if (url.startsWith("/sql.json")) {
        return getSqlStatDataList(parameters);
    }
    // SQL防火墙
    if (url.startsWith("/wall.json")) {
        return getWallStatMap(parameters);
    }
    // SQL详情
    if (url.startsWith("/serviceId") && url.indexOf(".json") > 0) {
        String serviceId = StringUtils.subString(url, "serviceId=", "&");
        Integer id = StringUtils.subStringToInteger(url, "sql-", ".json");
        return getSqlStat(id, serviceId);
    }
    if (url.startsWith("/weburi.json")) {
        return getWebURIStatDataList(parameters);
    }
    if (url.startsWith("/weburi-") && url.indexOf(".json") > 0) {
        String uri = StringUtils.subString(url, "weburi-", ".json", true);
        return returnJSONResult(RESULT_CODE_SUCCESS, getWebURIStatData(uri));
    }
    if (url.startsWith("/webapp.json")) {
        return returnJSONResult(RESULT_CODE_SUCCESS, getWebAppStatDataList(parameters));
    }
    if (url.startsWith("/websession.json")) {
        return returnJSONResult(RESULT_CODE_SUCCESS, getWebSessionStatDataList(parameters));
    }
    if (url.startsWith("/websession-") && url.indexOf(".json") > 0) {
        String id = StringUtils.subString(url, "websession-", ".json");
        return returnJSONResult(RESULT_CODE_SUCCESS, getWebSessionStatData(id));
    }
    if (url.startsWith("/spring.json")) {
        return returnJSONResult(RESULT_CODE_SUCCESS, getSpringStatDataList(parameters));
    }
    if (url.startsWith("/spring-detail.json")) {
        String clazz = parameters.get("class");
        String method = parameters.get("method");
        return returnJSONResult(RESULT_CODE_SUCCESS, getSpringMethodStatData(clazz, method));
    }
    return returnJSONResult(RESULT_CODE_ERROR, "Do not support this request, please contact with administrator.");
}
Also used : JSONObject(com.alibaba.fastjson2.JSONObject)

Example 13 with JSON

use of com.alibaba.fastjson2.JSON in project RuoYi-Vue by yangzongzhuan.

the class AddressUtils method getRealAddressByIP.

public static String getRealAddressByIP(String ip) {
    // 内网不查询
    if (IpUtils.internalIp(ip)) {
        return "内网IP";
    }
    if (RuoYiConfig.isAddressEnabled()) {
        try {
            String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
            if (StringUtils.isEmpty(rspStr)) {
                log.error("获取地理位置异常 {}", ip);
                return UNKNOWN;
            }
            JSONObject obj = JSON.parseObject(rspStr);
            String region = obj.getString("pro");
            String city = obj.getString("city");
            return String.format("%s %s", region, city);
        } catch (Exception e) {
            log.error("获取地理位置异常 {}", ip);
        }
    }
    return UNKNOWN;
}
Also used : JSONObject(com.alibaba.fastjson2.JSONObject)

Aggregations

JSONArray (com.alibaba.fastjson2.JSONArray)8 DbType (com.alibaba.druid.DbType)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)7 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)7 ArrayList (java.util.ArrayList)7 JSONObject (com.alibaba.fastjson2.JSONObject)4 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ChatDao (cn.wzpmc.dao.ChatDao)1 Message (cn.wzpmc.pojo.Message)1 JSONException (com.alibaba.fastjson2.JSONException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JSON (org.brapi.client.v2.JSON)1