Search in sources :

Example 6 with EntityListHandler

use of com.github.drinkjava2.jsqlbox.handler.EntityListHandler in project jSqlBox by drinkjava2.

the class ActiveRecordUtils method doGuess.

/**
 * Execute operation to access database, based on current method @Sql annotated
 * String or Text String and parameters, guess a best fit
 * query/update/delete/execute method to run
 *
 * @param ac
 * @param params
 * @return <T> T
 */
@SuppressWarnings("all")
protected static <T> T doGuess(ActiveRecord ac, Object... params) {
    // NOSONAR
    int callerPos = 0;
    StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
    for (StackTraceElement stack : stacks) {
        callerPos++;
        if ("com.github.drinkjava2.jsqlbox.ActiveRecord".equals(stack.getClassName()) && "guess".equals(stack.getMethodName()))
            break;
    }
    String callerClassName = stacks[callerPos].getClassName();
    String callerMethodName = stacks[callerPos].getMethodName();
    Class<?> callerClass = ClassCacheUtils.checkClassExist(callerClassName);
    if (callerClass == null)
        throw new SqlBoxException("Can not find class '" + callerClassName + "'");
    Method callerMethod = ClassCacheUtils.checkMethodExist(callerClass, callerMethodName);
    if (callerMethod == null)
        throw new SqlBoxException("Can not find method '" + callerMethodName + "' in '" + callerClassName + "'");
    PreparedSQL sp = getPreparedSQLNoParamsFromSrcCode(callerClassName, callerMethodName, callerMethod);
    String sql = sp.getSql().trim();
    Class<?>[] handlerClass = sp.getHandlerClasses();
    char dotype;
    if (StrUtils.startsWithIgnoreCase(sql, "select"))
        dotype = 's';
    else if (StrUtils.startsWithIgnoreCase(sql, "delete"))
        dotype = 'u';
    else if (StrUtils.startsWithIgnoreCase(sql, "update"))
        dotype = 'u';
    else if (StrUtils.startsWithIgnoreCase(sql, "insert"))
        dotype = 'u';
    else
        // execute
        dotype = 'e';
    boolean hasQuestionMark = sql.indexOf('?') > -1;
    boolean useTemplate = sql.indexOf(':') > -1 || sql.indexOf("#{") > -1;
    if (useTemplate && hasQuestionMark)
        throw new SqlBoxException("guess() method can not determine use template or normal style for SQL '" + sql + "'");
    boolean isEntityQuery = sql.indexOf(".**") > -1;
    if (isEntityQuery && 's' != dotype)
        throw new SqlBoxException("'.**' entity query style SQL can only start with 'select'");
    Map<String, Object> map = null;
    if (useTemplate)
        map = buildParamMap(callerClassName, callerMethodName, params);
    Object o = null;
    switch(dotype) {
        case 's':
            {
                ResultSetHandler<T> resultSetHandler = buildResultHandler(handlerClass);
                if (isEntityQuery) {
                    if (useTemplate)
                        return (T) ac.ctx().tQuery(new EntityListHandler(ac.getClass()), sql, map);
                    else
                        return (T) ac.ctx().nQuery(new EntityListHandler(ac.getClass()), sql, params);
                } else {
                    if (useTemplate)
                        return ac.ctx().tQuery(resultSetHandler, sql, map);
                    else
                        return ac.ctx().nQuery(resultSetHandler, sql, params);
                }
            }
        case 'u':
            {
                if (useTemplate)
                    o = ac.ctx().tUpdate(sql, map);
                else
                    o = ac.ctx().nUpdate(sql, params);
                return (T) o;
            }
        case 'e':
            {
                if (handlerClass == null) {
                    if (useTemplate)
                        o = ac.ctx().tExecute(sql, map);
                    else
                        o = ac.ctx().nExecute(sql, params);
                    return (T) o;
                }
                ResultSetHandler<T> resultSetHandler = buildResultHandler(handlerClass);
                if (useTemplate)
                    o = ac.ctx().tExecute(resultSetHandler, sql, map);
                else
                    o = ac.ctx().nExecute(resultSetHandler, sql, params);
                return (T) o;
            }
        default:
            return null;
    }
}
Also used : PreparedSQL(com.github.drinkjava2.jdbpro.inline.PreparedSQL) Method(java.lang.reflect.Method) EntityListHandler(com.github.drinkjava2.jsqlbox.handler.EntityListHandler) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler)

Aggregations

EntityListHandler (com.github.drinkjava2.jsqlbox.handler.EntityListHandler)6 Test (org.junit.Test)5 Wrap (com.github.drinkjava2.jdbpro.handler.Wrap)3 PaginHandler (com.github.drinkjava2.jsqlbox.handler.PaginHandler)2 User (com.github.drinkjava2.functionstest.entitynet.entities.User)1 PrintSqlHandler (com.github.drinkjava2.jdbpro.handler.PrintSqlHandler)1 SimpleCacheHandler (com.github.drinkjava2.jdbpro.handler.SimpleCacheHandler)1 PreparedSQL (com.github.drinkjava2.jdbpro.inline.PreparedSQL)1 EntitySqlMapListHandler (com.github.drinkjava2.jsqlbox.handler.EntitySqlMapListHandler)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 ResultSetHandler (org.apache.commons.dbutils.ResultSetHandler)1