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;
}
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();
}
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;
}
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;
}
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;
}
}
Aggregations