Search in sources :

Example 1 with ArrayHandler

use of org.apache.commons.dbutils.handlers.ArrayHandler in project swift by luastar.

the class H2Utils method savePubConfig.

/**
 * 保存配置信息
 *
 * @param pubConfig
 * @return
 */
public static boolean savePubConfig(PubConfig pubConfig) {
    try {
        if (pubConfig == null || ObjUtils.isEmpty(pubConfig.getConfig_code())) {
            logger.warn("需要保存的常用配置编码为空,不保存!");
            return false;
        }
        String querySQL = "SELECT * FROM PUB_CONFIG WHERE CONFIG_CODE=? LIMIT 1";
        PubConfig dbPubConfig = getInstance().getQueryRunner().query(querySQL, new BeanHandler<>(PubConfig.class), pubConfig.getConfig_code());
        if (dbPubConfig == null) {
            // 设置默认值
            pubConfig.setData_sort(ObjUtils.ifNull(pubConfig.getData_sort(), System.currentTimeMillis()));
            pubConfig.setData_status(ObjUtils.ifNull(pubConfig.getData_status(), ConfigStatus.ENABLE.getStatus()));
            pubConfig.setCreate_time(new Date());
            String insertSQL = "INSERT INTO PUB_CONFIG(GROUP_CODE, GROUP_NAME, CONFIG_CODE, CONFIG_NAME, DATA_ID, DATA_TYPE, DATA_CONTENT, DATA_SORT, DATA_STATUS, DATA_P1, DATA_P2, DATA_P3, DATA_P4, DATA_P5, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            Object[] rsAry = getInstance().getQueryRunner().insert(insertSQL, new ArrayHandler(), pubConfig.getGroup_code(), pubConfig.getGroup_name(), pubConfig.getConfig_code(), pubConfig.getConfig_name(), pubConfig.getData_id(), pubConfig.getData_type(), pubConfig.getData_content(), pubConfig.getData_sort(), pubConfig.getData_status(), pubConfig.getData_p1(), pubConfig.getData_p2(), pubConfig.getData_p3(), pubConfig.getData_p4(), pubConfig.getData_p5(), pubConfig.getCreate_time());
            Integer id = ObjUtils.toInteger(rsAry[0], 0);
            pubConfig.setId(id);
            return id > 0;
        } else {
            // 如果值没有设置,则取数据库中的值
            pubConfig.setGroup_code(ObjUtils.ifNull(pubConfig.getGroup_code(), dbPubConfig.getGroup_code()));
            pubConfig.setGroup_name(ObjUtils.ifNull(pubConfig.getGroup_name(), dbPubConfig.getGroup_name()));
            pubConfig.setConfig_name(ObjUtils.ifNull(pubConfig.getConfig_name(), dbPubConfig.getConfig_name()));
            pubConfig.setData_id(ObjUtils.ifNull(pubConfig.getData_id(), dbPubConfig.getData_id()));
            pubConfig.setData_type(ObjUtils.ifNull(pubConfig.getData_type(), dbPubConfig.getData_type()));
            pubConfig.setData_content(ObjUtils.ifNull(pubConfig.getData_content(), dbPubConfig.getData_content()));
            pubConfig.setData_sort(ObjUtils.ifNull(pubConfig.getData_sort(), dbPubConfig.getData_sort()));
            pubConfig.setData_status(ObjUtils.ifNull(pubConfig.getData_status(), dbPubConfig.getData_status()));
            pubConfig.setData_p1(ObjUtils.ifNull(pubConfig.getData_p1(), dbPubConfig.getData_p1()));
            pubConfig.setData_p2(ObjUtils.ifNull(pubConfig.getData_p2(), dbPubConfig.getData_p2()));
            pubConfig.setData_p3(ObjUtils.ifNull(pubConfig.getData_p3(), dbPubConfig.getData_p3()));
            pubConfig.setData_p4(ObjUtils.ifNull(pubConfig.getData_p4(), dbPubConfig.getData_p4()));
            pubConfig.setData_p5(ObjUtils.ifNull(pubConfig.getData_p5(), dbPubConfig.getData_p5()));
            pubConfig.setUpdate_time(new Date());
            String updateSQL = "UPDATE PUB_CONFIG SET GROUP_CODE=?, GROUP_NAME=?, CONFIG_NAME=?, DATA_ID=?, DATA_TYPE=?, DATA_CONTENT=?, DATA_SORT=?, DATA_STATUS=?, DATA_P1=?, DATA_P2=?, DATA_P3=?, DATA_P4=?, DATA_P5=? WHERE CONFIG_CODE=?";
            return getInstance().getQueryRunner().update(updateSQL, pubConfig.getGroup_code(), pubConfig.getGroup_name(), pubConfig.getConfig_name(), pubConfig.getData_id(), pubConfig.getData_type(), pubConfig.getData_content(), pubConfig.getData_sort(), pubConfig.getData_status(), pubConfig.getData_p1(), pubConfig.getData_p2(), pubConfig.getData_p3(), pubConfig.getData_p4(), pubConfig.getData_p5(), pubConfig.getConfig_code()) > 0;
        }
    } catch (Exception e) {
        logger.error("获取常用配置异常:" + e.getMessage(), e);
        return false;
    }
}
Also used : ArrayHandler(org.apache.commons.dbutils.handlers.ArrayHandler) PubConfig(com.luastar.swift.tools.model.gui.PubConfig) Date(java.util.Date) SQLException(java.sql.SQLException)

Example 2 with ArrayHandler

use of org.apache.commons.dbutils.handlers.ArrayHandler in project swift by luastar.

the class H2Utils method insertDbConfig.

/**
 * 新增数据库配置
 * 返回新增配置的ID
 *
 * @param dbConfig
 * @return
 */
public static Integer insertDbConfig(DbConfig dbConfig) {
    try {
        if (dbConfig == null) {
            return 0;
        }
        dbConfig.setCreate_time(new Date());
        String sql = "INSERT INTO DB_CONFIG(DB_TYPE, DB_NAME, JDBC_URL, USER_NAME, PASSWORD, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?)";
        Object[] rsAry = getInstance().getQueryRunner().insert(sql, new ArrayHandler(), dbConfig.getDb_type(), dbConfig.getDb_name(), dbConfig.getJdbc_url(), dbConfig.getUser_name(), dbConfig.getPassword(), dbConfig.getCreate_time());
        return ObjUtils.toInteger(rsAry[0]);
    } catch (Exception e) {
        logger.error("获取数据库配置异常:" + e.getMessage(), e);
        return 0;
    }
}
Also used : ArrayHandler(org.apache.commons.dbutils.handlers.ArrayHandler) Date(java.util.Date) SQLException(java.sql.SQLException)

Example 3 with ArrayHandler

use of org.apache.commons.dbutils.handlers.ArrayHandler in project jSqlBox by drinkjava2.

the class SqlBoxContextUtils method load.

@SuppressWarnings("unchecked")
public static <T> T load(SqlBoxContext ctx, Class<?> entityClass, Object pkeyValue) {
    SqlBoxException.assureNotNull(entityClass, "entityClass can not be null");
    SqlBoxException.assureNotNull(entityClass, "pkey can not be null");
    Map<String, Object> pkValueMap = null;
    if (pkeyValue instanceof Map)
        // NOSONAR
        pkValueMap = (Map<String, Object>) pkeyValue;
    else {
        pkValueMap = new HashMap<String, Object>();
        pkValueMap.put("ooxxooxx", pkeyValue);
    }
    Object entity = null;
    try {
        entity = entityClass.newInstance();
    } catch (Exception e) {
        throw new SqlBoxException(e);
    }
    SqlBox box = SqlBoxUtils.findAndBindSqlBox(ctx, entity);
    TableModel model = box.getTableModel();
    StringBuilder sb = new StringBuilder("select ");
    List<Object> pkParams = new ArrayList<Object>();
    List<ColumnModel> pkeyColumns = new ArrayList<ColumnModel>();
    List<ColumnModel> allColumns = new ArrayList<ColumnModel>();
    List<String> allFieldNames = new ArrayList<String>();
    Map<String, Method> writeMethods = ClassCacheUtils.getClassWriteMethods(entityClass);
    for (String fieldName : writeMethods.keySet()) {
        ColumnModel col = findMatchColumnForJavaField(fieldName, box);
        if (!col.getTransientable()) {
            allColumns.add(col);
            allFieldNames.add(fieldName);
            sb.append(col.getColumnName()).append(", ");
            if (col.getPkey()) {
                pkeyColumns.add(col);
                if (pkValueMap.size() == 1)
                    pkParams.add(pkValueMap.entrySet().iterator().next().getValue());
                else
                    pkParams.add(pkValueMap.get(fieldName));
            }
        }
    }
    if (pkeyColumns.isEmpty())
        throw new SqlBoxException("No primary key set for entityBean");
    // delete the last ", "
    sb.setLength(sb.length() - 2);
    if (pkParams.size() != pkValueMap.size())
        throw new SqlBoxException("Wrong number of primary key parameters: expected " + pkParams.size() + ", was given " + pkValueMap.size());
    sb.append(" from ").append(model.getTableName()).append(" where ");
    for (ColumnModel col : pkeyColumns) sb.append(col.getColumnName()).append("=? and ");
    // delete the last " and "
    sb.setLength(sb.length() - 5);
    try {
        Object[] values = ctx.nQuery(new ArrayHandler(), sb.toString(), pkParams.toArray(new Object[pkParams.size()]));
        for (int i = 0; i < values.length; i++) {
            Method writeMethod = writeMethods.get(allFieldNames.get(i));
            writeMethod.invoke(entity, values[i]);
        }
    } catch (Exception e) {
        throw new SqlBoxException(e);
    }
    return (T) entity;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) ArrayHandler(org.apache.commons.dbutils.handlers.ArrayHandler) ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) HashMap(java.util.HashMap) Map(java.util.Map) TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Aggregations

SQLException (java.sql.SQLException)3 ArrayHandler (org.apache.commons.dbutils.handlers.ArrayHandler)3 Date (java.util.Date)2 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 TableModel (com.github.drinkjava2.jdialects.model.TableModel)1 PubConfig (com.luastar.swift.tools.model.gui.PubConfig)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1