use of com.github.drinkjava2.jsqlbox.annotation.Sql 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();
}
use of com.github.drinkjava2.jsqlbox.annotation.Sql 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();
}
use of com.github.drinkjava2.jsqlbox.annotation.Sql in project jSqlBox by drinkjava2.
the class EntitySqlMapListHandler method replaceStarStarToColumn.
/**
* Replace .** to all fields, replace .## to all PKey and Fkey fields only,
* for example:
*
* <pre>
* u.** ==> u.id as u_id, u.userName as u_userName, u.address as u_address...
* u.## ==> u.id as u_id
* </pre>
*/
private static String replaceStarStarToColumn(String sql, String alias, String tableName, TableModel[] models) {
String result = sql;
if (sql.contains(alias + ".**")) {
StringBuilder sb = new StringBuilder();
if (models != null && models.length > 0) {
for (TableModel tb : models) {
if (tableName.equalsIgnoreCase(tb.getTableName())) {
if (StrUtils.isEmpty(tb.getAlias()))
tb.setAlias(alias);
else {
if (!alias.equalsIgnoreCase(tb.getAlias()))
throw new SqlBoxException("Alias '" + alias + "' not same as tableModel's alias");
}
for (ColumnModel col : tb.getColumns()) {
if (!col.getTransientable())
sb.append(alias).append(".").append(col.getColumnName()).append(" as ").append(alias).append("_").append(col.getColumnName()).append(", ");
}
break;
}
}
}
if (sb.length() == 0)
throw new SqlBoxException("In SQL '" + sql + "', Can not find columns in table '" + tableName + "'");
sb.setLength(sb.length() - 2);
result = StrUtils.replaceFirst(sql, alias + ".**", sb.toString());
return result;
}
if (sql.contains(alias + ".##")) {
// Pkey and Fkey only
StringBuilder sb = new StringBuilder();
if (models != null && models.length > 0) {
for (TableModel tb : models) {
if (tableName.equalsIgnoreCase(tb.getTableName())) {
if (StrUtils.isEmpty(tb.getAlias()))
tb.setAlias(alias);
else {
if (!alias.equalsIgnoreCase(tb.getAlias()))
throw new SqlBoxException("Alias '" + alias + "' not same as tableModel's alias");
}
for (ColumnModel col : tb.getColumns()) {
boolean found = false;
if (!col.getTransientable()) {
if (col.getPkey())
found = true;
else {
for (FKeyModel tableModel : tb.getFkeyConstraints()) {
if (tableModel.getColumnNames().contains(col.getColumnName())) {
found = true;
break;
}
}
}
}
if (found)
sb.append(alias).append(".").append(col.getColumnName()).append(" as ").append(alias).append("_").append(col.getColumnName()).append(", ");
}
break;
}
}
}
if (sb.length() == 0)
throw new SqlBoxException("In SQL '" + sql + "', Can not find key columns in table '" + tableName + "'");
sb.setLength(sb.length() - 2);
result = StrUtils.replaceFirst(result, alias + ".##", sb.toString());
}
return result;
}
use of com.github.drinkjava2.jsqlbox.annotation.Sql 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.jsqlbox.annotation.Sql in project jDialects by drinkjava2.
the class IdentityIdGenerator method getNextID.
@Override
public Object getNextID(NormalJdbcTool jdbc, Dialect dialect, Type dataType) {
if (!dialect.ddlFeatures.getSupportsIdentityColumns())
throw new DialectException("Dialect '" + dialect + "' does not support identity type");
String sql = null;
if (Type.BIGINT.equals(dataType))
sql = dialect.ddlFeatures.getIdentitySelectStringBigINT();
else
sql = dialect.ddlFeatures.getIdentitySelectString();
if (StrUtils.isEmpty(sql) || DDLFeatures.NOT_SUPPORT.equals(sql))
throw new DialectException("Dialect '" + dialect + "' does not support identity type");
sql = StrUtils.replaceFirst(sql, "_table__col", new StringBuilder(table).append("_").append(column).toString());
return jdbc.jdbcQueryForObject(sql);
}
Aggregations