Search in sources :

Example 1 with DbProRuntimeException

use of com.github.drinkjava2.jdbpro.DbProRuntimeException in project jSqlBox by drinkjava2.

the class ImprovedQueryRunner method batchFlush.

// === Batch execute methods======
/**
 * Force flush cached SQLs
 */
public void batchFlush() throws SQLException {
    List<Object[]> sqlCacheList = sqlBatchCache.get();
    if (sqlCacheList.isEmpty())
        return;
    // first row
    Object[] f = sqlCacheList.get(0);
    if (f.length != 6)
        throw new DbProRuntimeException("Unexpected batch cached SQL format");
    int paramLenth = 0;
    if ("i1".equals(f[0]) || "i3".equals(f[0]) || "u1".equals(f[0]) || "u4".equals(f[0]))
        paramLenth = 0;
    if ("u2".equals(f[0]) || "u5".equals(f[0]))
        paramLenth = 1;
    else
        paramLenth = ((Object[]) sqlCacheList.get(0)[5]).length;
    Object[][] allParams = new Object[sqlCacheList.size()][paramLenth];
    int i = 0;
    for (Object[] c : sqlCacheList) {
        // cached parameters
        Object param = c[2];
        Object[] params = (Object[]) c[5];
        if ("i1".equals(f[0]) || "i3".equals(f[0]) || "u1".equals(f[0]) || "u4".equals(f[0]))
            allParams[i] = new Object[0];
        if ("u2".equals(f[0]) || "u5".equals(f[0]))
            allParams[i] = new Object[] { param };
        else
            allParams[i] = params;
        i++;
    }
    String sql = (String) f[3];
    Connection conn = (Connection) f[4];
    ResultSetHandler rsh = (ResultSetHandler) f[1];
    if (this.getAllowShowSQL()) {
        logger.info("Batch execute " + sqlCacheList.size() + " SQLs");
        logger.info(formatSqlForLoggerOutput(sql));
        logger.info("First row " + formatParametersForLoggerOutput(allParams[0]));
        logger.info("Last row " + formatParametersForLoggerOutput(allParams[allParams.length - 1]));
    }
    if ("e1".equals(f[0]) || "i1".equals(f[0]) || "u1".equals(f[0]) || "u2".equals(f[0]) || "u3".equals(f[0]))
        super.batch(conn, sql, allParams);
    else if ("e3".equals(f[0]) || "i3".equals(f[0]) || "u4".equals(f[0]) || "u5".equals(f[0]) || "u6".equals(f[0]))
        super.batch(sql, allParams);
    else if ("e2".equals(f[0]) || "i2".equals(f[0]))
        super.insertBatch(conn, sql, rsh, allParams);
    else if ("e4".equals(f[0]) || "i4".equals(f[0]))
        super.insertBatch(sql, rsh, allParams);
    else
        throw new DbProRuntimeException("unknow batch sql operation type +'" + f[0] + "'");
    sqlBatchCache.get().clear();
}
Also used : DbProRuntimeException(com.github.drinkjava2.jdbpro.DbProRuntimeException) Connection(java.sql.Connection) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler)

Example 2 with DbProRuntimeException

use of com.github.drinkjava2.jdbpro.DbProRuntimeException in project jSqlBox by drinkjava2.

the class InlineQueryRunner method inline.

/**
 * Usually used to translate a Bean to "field1,field2,...fieldx" format or
 * "field1=?,field2=?.... fieldx=?" format inLineSQL String piece, and save bean
 * property parameters in ThreadLocal
 *
 * @param bean
 *            The Bean will be transfer to SQL piece
 * @param conditionStr
 *            The condition String
 * @param separatorStr
 *            The separator String
 * @return a SQL piece and store all bean properties as parameters in
 *         ThreadLocaled
 */
public static String inline(Object bean, String conditionStr, String separatorStr) {
    DbProRuntimeException.assertNotNull(bean, "DbProBeanUtils bean can not be null");
    Class<?> beanClass = bean.getClass();
    BeanInfo beanInfo = null;
    PropertyDescriptor[] pds = null;
    try {
        beanInfo = Introspector.getBeanInfo(beanClass);
        pds = beanInfo.getPropertyDescriptors();
    } catch (Exception e) {
        throw new DbProRuntimeException("DbProBeanUtils  fail to get bean Properties.", e);
    }
    if (pds == null || pds.length < 1)
        return "";
    StringBuilder sb = new StringBuilder();
    Object[] params = new Object[pds.length - 1];
    int i = 0;
    for (PropertyDescriptor pd : pds) {
        String fieldName = pd.getName();
        if (!"class".equals(fieldName)) {
            Method md = pd.getReadMethod();
            try {
                Object value = md.invoke(bean);
                sb.append(fieldName).append(conditionStr).append(separatorStr);
                params[i++] = value;
            } catch (Exception e) {
                throw new DbProRuntimeException("DbProBeanUtils fail to get bean Properties.", e);
            }
        }
    }
    sb.setLength(sb.length() - separatorStr.length());
    for (Object param : params) {
        DbPro.param(param);
    }
    return sb.toString();
}
Also used : DbProRuntimeException(com.github.drinkjava2.jdbpro.DbProRuntimeException) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) DbProRuntimeException(com.github.drinkjava2.jdbpro.DbProRuntimeException) SQLException(java.sql.SQLException)

Aggregations

DbProRuntimeException (com.github.drinkjava2.jdbpro.DbProRuntimeException)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ResultSetHandler (org.apache.commons.dbutils.ResultSetHandler)1