Search in sources :

Example 1 with DruidRuntimeException

use of com.alibaba.druid.DruidRuntimeException in project druid by alibaba.

the class SQLEvalVisitorUtils method eval.

public static Object eval(String dbType, SQLObject sqlObject, List<Object> parameters, boolean throwError) {
    SQLEvalVisitor visitor = createEvalVisitor(dbType);
    visitor.setParameters(parameters);
    sqlObject.accept(visitor);
    Object value = getValue(sqlObject);
    if (value == null) {
        if (throwError && !sqlObject.getAttributes().containsKey(EVAL_VALUE)) {
            throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType));
        }
    }
    return value;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 2 with DruidRuntimeException

use of com.alibaba.druid.DruidRuntimeException in project druid by alibaba.

the class MonitorDaoJdbcImpl method loadHashValue.

private void loadHashValue(FieldInfo hashField, List<?> list, Map<String, Object> filters) {
    String domain = (String) filters.get("domain");
    if (StringUtils.isEmpty(domain)) {
        domain = "default";
    }
    String app = (String) filters.get("app");
    if (StringUtils.isEmpty(app)) {
        app = "default";
    }
    for (Object statValue : list) {
        try {
            Long hash = (Long) hashField.field.get(statValue);
            String value = cacheGet(hashField.getHashForType(), hash);
            if (value == null) {
                value = getConstValueFromDb(domain, app, hashField.getHashForType(), hash);
            }
            hashField.getHashFor().set(statValue, value);
        } catch (IllegalArgumentException e) {
            throw new DruidRuntimeException("set field error" + hashField.getField(), e);
        } catch (IllegalAccessException e) {
            throw new DruidRuntimeException("set field error" + hashField.getField(), e);
        }
    }
}
Also used : DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 3 with DruidRuntimeException

use of com.alibaba.druid.DruidRuntimeException in project druid by alibaba.

the class MonitorDaoJdbcImpl method setParameterForSqlStat.

protected //
void setParameterForSqlStat(//
BeanInfo beanInfo, //
MonitorContext ctx, //
PreparedStatement stmt, Object sqlStat) throws SQLException {
    int paramIndex = 1;
    setParam(stmt, paramIndex++, ctx.getDomain());
    setParam(stmt, paramIndex++, ctx.getApp());
    setParam(stmt, paramIndex++, ctx.getCluster());
    setParam(stmt, paramIndex++, ctx.getHost());
    setParam(stmt, paramIndex++, ctx.getPID());
    setParam(stmt, paramIndex++, ctx.getCollectTime());
    try {
        List<FieldInfo> fields = beanInfo.getFields();
        for (FieldInfo field : fields) {
            Class<?> fieldType = field.getFieldType();
            Object value = field.getField().get(sqlStat);
            if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
                setParam(stmt, paramIndex, (Integer) value);
            } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
                setParam(stmt, paramIndex, (Long) value);
            } else if (fieldType.equals(String.class)) {
                setParam(stmt, paramIndex, (String) value);
            } else if (fieldType.equals(Date.class)) {
                setParam(stmt, paramIndex, (Date) value);
            } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
                setParam(stmt, paramIndex, (Boolean) value);
            } else {
                throw new UnsupportedOperationException("not support type : " + fieldType);
            }
            paramIndex++;
        }
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new DruidRuntimeException("setParam error", ex);
    }
}
Also used : Date(java.util.Date) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 4 with DruidRuntimeException

use of com.alibaba.druid.DruidRuntimeException in project druid by alibaba.

the class MonitorDaoJdbcImpl method saveHash.

private void saveHash(FieldInfo hashField, MonitorContext ctx, List<?> list) {
    final String hashType = hashField.getHashForType();
    for (Object statValue : list) {
        try {
            Long hash = (Long) hashField.field.get(statValue);
            if (!cacheContains(hashField.getHashForType(), hash)) {
                String value = (String) hashField.getHashFor().get(statValue);
                final String sql = "insert into druid_const (domain, app, type, hash, value) values (?, ?, ?, ?, ?)";
                Connection conn = null;
                PreparedStatement stmt = null;
                try {
                    conn = dataSource.getConnection();
                    stmt = conn.prepareStatement(sql);
                    stmt.setString(1, ctx.getDomain());
                    stmt.setString(2, ctx.getApp());
                    stmt.setString(3, hashType);
                    stmt.setLong(4, hash);
                    stmt.setString(5, value);
                    stmt.execute();
                    stmt.close();
                } catch (SQLException ex) {
                // LOG.error("save const error error", ex);
                } finally {
                    JdbcUtils.close(stmt);
                    JdbcUtils.close(conn);
                }
                cachePut(hashField.getHashForType(), hash, value);
            }
        } catch (IllegalArgumentException e) {
            throw new DruidRuntimeException("set field error" + hashField.getField(), e);
        } catch (IllegalAccessException e) {
            throw new DruidRuntimeException("set field error" + hashField.getField(), e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 5 with DruidRuntimeException

use of com.alibaba.druid.DruidRuntimeException in project druid by alibaba.

the class MonitorDaoJdbcImpl method readFieldValue.

protected void readFieldValue(Object object, FieldInfo field, ResultSet rs, int paramIndex) throws SQLException {
    Class<?> fieldType = field.getFieldType();
    Object fieldValue = null;
    if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
        fieldValue = rs.getInt(paramIndex);
    } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
        fieldValue = rs.getLong(paramIndex);
    } else if (fieldType.equals(String.class)) {
        fieldValue = rs.getString(paramIndex);
    } else if (fieldType.equals(Date.class)) {
        Timestamp timestamp = rs.getTimestamp(paramIndex);
        if (timestamp != null) {
            fieldValue = new Date(timestamp.getTime());
        }
    } else {
        throw new UnsupportedOperationException();
    }
    try {
        field.getField().set(object, fieldValue);
    } catch (IllegalArgumentException e) {
        throw new DruidRuntimeException("set field error" + field.getField(), e);
    } catch (IllegalAccessException e) {
        throw new DruidRuntimeException("set field error" + field.getField(), e);
    }
}
Also used : Timestamp(java.sql.Timestamp) Date(java.util.Date) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Aggregations

DruidRuntimeException (com.alibaba.druid.DruidRuntimeException)5 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1