Search in sources :

Example 1 with PreparedSQL

use of com.github.drinkjava2.jdbpro.inline.PreparedSQL in project jSqlBox by drinkjava2.

the class ActiveRecordUtils method getPreparedSQLNoParamsFromSrcCode.

private static PreparedSQL getPreparedSQLNoParamsFromSrcCode(String callerClassName, String callerMethodName, Method callerMethod) {
    // key is only inside used by cache
    String key = callerClassName + "@#$^!" + callerMethodName;
    PreparedSQL result = methodSQLCache.get(key);
    if (result != null)
        return result;
    else
        result = new PreparedSQL();
    Annotation[] annos = callerMethod.getAnnotations();
    Sql sqlAnno = null;
    Class<?>[] handlerClasses = null;
    for (Annotation anno : annos) {
        if (Sql.class.equals(anno.annotationType()))
            sqlAnno = (Sql) anno;
        if (Handler.class.equals(anno.annotationType())) {
            handlerClasses = ((Handler) anno).value();
        }
    }
    String sql = null;
    if (sqlAnno != null)
        sql = sqlAnno.value()[0];
    else {
        String src = null;
        try {
            src = TextUtils.getJavaSourceCodeUTF8(callerClassName);
        } catch (Exception e) {
            throw new SqlBoxException("Method '" + callerMethodName + "' in '" + callerClassName + "' have no Sql annotation or text.");
        }
        sql = StrUtils.substringAfter(src, callerMethodName + "(");
        sql = StrUtils.substringBetween(sql, "/*-", "*/");
    }
    if (sql != null)
        sql = sql.trim();
    result.setSql(sql);
    result.setHandlerClasses(handlerClasses);
    methodSQLCache.put(key, result);
    return result;
}
Also used : PreparedSQL(com.github.drinkjava2.jdbpro.inline.PreparedSQL) Annotation(java.lang.annotation.Annotation) Sql(com.github.drinkjava2.jsqlbox.annotation.Sql)

Example 2 with PreparedSQL

use of com.github.drinkjava2.jdbpro.inline.PreparedSQL in project jSqlBox by drinkjava2.

the class ActiveRecordUtils method doGuessSQL.

protected static String doGuessSQL(ActiveRecord ac) {
    // NOSONAR
    int callerPos = 0;
    StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
    for (StackTraceElement stack : stacks) {
        callerPos++;
        if ("com.github.drinkjava2.jsqlbox.ActiveRecord".equals(stack.getClassName()) && "guessSQL".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);
    return sp.getSql();
}
Also used : PreparedSQL(com.github.drinkjava2.jdbpro.inline.PreparedSQL) Method(java.lang.reflect.Method)

Example 3 with PreparedSQL

use of com.github.drinkjava2.jdbpro.inline.PreparedSQL in project jSqlBox by drinkjava2.

the class BeetlSqlTempalte method doRender.

private static PreparedSQL doRender(SQLManager sm, String sqlId, Object paras) {
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("_root", paras);
    SQLScript script = sm.getScript(sqlId);
    SQLResult result = null;
    try {
        // run method in SQLScript is protected, have to make it accessible
        Method method = SQLScript.class.getDeclaredMethod("run", Map.class);
        ReflectionUtils.makeAccessible(method);
        result = (SQLResult) method.invoke(script, param);
    } catch (Exception e) {
        throw new SqlBoxException("Can not access method 'run' in class 'org.beetl.sql.core.SQLScript'");
    }
    PreparedSQL sp = new PreparedSQL();
    sp.setSql(result.jdbcSql);
    List<SQLParameter> sqlparam = result.jdbcPara;
    Object[] params = new Object[sqlparam.size()];
    for (int i = 0; i < sqlparam.size(); i++) {
        params[i] = sqlparam.get(i).value;
    }
    sp.setParams(params);
    return sp;
}
Also used : PreparedSQL(com.github.drinkjava2.jdbpro.inline.PreparedSQL) HashMap(java.util.HashMap) SqlBoxException(com.github.drinkjava2.jsqlbox.SqlBoxException) SQLParameter(org.beetl.sql.core.engine.SQLParameter) SQLScript(org.beetl.sql.core.SQLScript) Method(java.lang.reflect.Method) SqlBoxException(com.github.drinkjava2.jsqlbox.SqlBoxException) SQLResult(org.beetl.sql.core.SQLResult)

Example 4 with PreparedSQL

use of com.github.drinkjava2.jdbpro.inline.PreparedSQL in project jSqlBox by drinkjava2.

the class ActiveRecordUtils method doGuessPreparedSQL.

protected static PreparedSQL doGuessPreparedSQL(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()) && "guessPreparedSQL".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);
    sp.setParams(params);
    return sp;
}
Also used : PreparedSQL(com.github.drinkjava2.jdbpro.inline.PreparedSQL) Method(java.lang.reflect.Method)

Example 5 with PreparedSQL

use of com.github.drinkjava2.jdbpro.inline.PreparedSQL 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

PreparedSQL (com.github.drinkjava2.jdbpro.inline.PreparedSQL)6 Method (java.lang.reflect.Method)5 SqlBoxException (com.github.drinkjava2.jsqlbox.SqlBoxException)1 Sql (com.github.drinkjava2.jsqlbox.annotation.Sql)1 EntityListHandler (com.github.drinkjava2.jsqlbox.handler.EntityListHandler)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ResultSetHandler (org.apache.commons.dbutils.ResultSetHandler)1 SQLResult (org.beetl.sql.core.SQLResult)1 SQLScript (org.beetl.sql.core.SQLScript)1 SQLParameter (org.beetl.sql.core.engine.SQLParameter)1