use of org.apache.commons.dbutils.ResultSetHandler in project metacat by Netflix.
the class MysqlUserMetadataService method searchByOwners.
@Override
public List<QualifiedName> searchByOwners(final Set<String> owners) {
final List<QualifiedName> result = Lists.newArrayList();
final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES);
final List<Object> paramList = Lists.newArrayList();
query.append(" where 1=0");
owners.forEach(s -> {
query.append(" or data like ?");
paramList.add("%\"userId\":\"" + s.trim() + "\"%");
});
final Object[] params = new Object[paramList.size()];
final Connection connection = DBUtil.getReadConnection(poolingDataSource);
try {
// Handler for reading the result set
final ResultSetHandler<Void> handler = rs -> {
while (rs.next()) {
final String definitionName = rs.getString("name");
result.add(QualifiedName.fromString(definitionName, false));
}
return null;
};
new QueryRunner().query(connection, query.toString(), handler, paramList.toArray(params));
} catch (SQLException e) {
log.error("Sql exception", e);
throw new UserMetadataServiceException("Failed to get definition data", e);
} finally {
DBUtil.closeReadConnection(connection);
}
return result;
}
use of org.apache.commons.dbutils.ResultSetHandler in project testcontainers-java by testcontainers.
the class JDBCDriverWithPoolTest method testMySQLWithConnectionPoolUsingSameContainer.
@Test
public void testMySQLWithConnectionPoolUsingSameContainer() throws SQLException, InterruptedException {
// Populate the database with some data in multiple threads, so that multiple connections from the pool will be used
for (int i = 0; i < 100; i++) {
executorService.submit(() -> {
try {
new QueryRunner(dataSource).insert("INSERT INTO my_counter (n) VALUES (5)", (ResultSetHandler<Object>) rs -> true);
} catch (SQLException e) {
e.printStackTrace();
}
});
}
// Complete population of the database
executorService.shutdown();
executorService.awaitTermination(5, TimeUnit.MINUTES);
// compare to expected results
int count = new QueryRunner(dataSource).query("SELECT COUNT(1) FROM my_counter", rs -> {
rs.next();
return rs.getInt(1);
});
assertEquals("Reuse of a datasource points to the same DB container", 100, count);
int sum = new QueryRunner(dataSource).query("SELECT SUM(n) FROM my_counter", rs -> {
rs.next();
return rs.getInt(1);
});
// 100 records * 5 = 500 expected
assertEquals("Reuse of a datasource points to the same DB container", 500, sum);
}
use of org.apache.commons.dbutils.ResultSetHandler 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;
}
}
use of org.apache.commons.dbutils.ResultSetHandler in project jSqlBox by drinkjava2.
the class ImprovedQueryRunner method readCache.
/**
* Explain SQL to cached result object, if have
*/
private Object[] readCache(ResultSetHandler<?> rsh, String sql, Object... params) {
Object[] result = new Object[2];
String key = null;
if (handlers != null)
for (ResultSetHandler handler : handlers) if (handler instanceof CacheSqlHandler) {
if (key == null)
key = createKey(sql, params);
result[0] = key;
Object value = ((CacheSqlHandler) handler).readFromCache(key);
if (value != null) {
result[1] = value;
return result;
}
}
for (ResultSetHandler handler : getThreadedHandlers()) if (handler instanceof CacheSqlHandler) {
if (key == null)
key = createKey(sql, params);
result[0] = key;
Object value = ((CacheSqlHandler) handler).readFromCache(key);
if (value != null) {
result[1] = value;
return result;
}
}
if (rsh != null && rsh instanceof CacheSqlHandler) {
if (key == null)
key = createKey(sql, params);
result[0] = key;
Object value = ((CacheSqlHandler) rsh).readFromCache(key);
if (value != null)
result[1] = value;
}
return result;
}
Aggregations