use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.
the class QueryResultsManager method deleteForInactiveSessions.
@Override
public void deleteForInactiveSessions() {
if (!AppContext.isStarted() || !clusterManager.isMaster() || !configuration.getConfig(GlobalConfig.class).getAllowQueryFromSelected())
return;
log.debug("Delete query results for inactive user sessions");
StringBuilder sb = new StringBuilder("delete from SYS_QUERY_RESULT");
Collection<UserSession> userSessionEntities = userSessions.getUserSessionsStream().collect(Collectors.toList());
DbTypeConverter converter = persistence.getDbTypeConverter();
if (!userSessionEntities.isEmpty()) {
sb.append(" where SESSION_ID not in (");
for (Iterator<UserSession> it = userSessionEntities.iterator(); it.hasNext(); ) {
UserSession userSession = it.next();
UUID userSessionId = userSession.getId();
String userSessionIdStr = converter.getSqlObject(userSessionId).toString();
sb.append("'").append(userSessionIdStr).append("'");
if (it.hasNext())
sb.append(",");
}
sb.append(")");
}
QueryRunner runner = new QueryRunner(persistence.getDataSource());
try {
runner.update(sb.toString());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.
the class QueryResultsManager method delete.
@Override
public void delete(int queryKey) {
DbTypeConverter converter = persistence.getDbTypeConverter();
UUID userSessionId = userSessionSource.getUserSession().getId();
String userSessionIdStr = converter.getSqlObject(userSessionId).toString();
long start = System.currentTimeMillis();
String logMsg = "Delete query results for " + userSessionId + " / " + queryKey;
log.debug(logMsg);
String sql = "delete from SYS_QUERY_RESULT where SESSION_ID = '" + userSessionIdStr + "' and QUERY_KEY = " + queryKey;
QueryRunner runner = new QueryRunner(persistence.getDataSource());
try {
runner.update(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
log.debug("Done in " + (System.currentTimeMillis() - start) + "ms : " + logMsg);
}
use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.
the class ServerInfo method updateCurrentServer.
protected void updateCurrentServer() {
try {
log.trace("Updating server information in the database");
String serverId = getServerId();
DbTypeConverter types = persistence.getDbTypeConverter();
Object tsObj = types.getSqlObject(timeSource.currentTimestamp());
int tsType = types.getSqlType(Date.class);
Object trueObj = types.getSqlObject(Boolean.TRUE);
int boolType = types.getSqlType(Boolean.class);
QueryRunner runner = new QueryRunner(persistence.getDataSource());
int updated = runner.update("update SYS_SERVER set UPDATE_TS = ?, IS_RUNNING = ? where NAME = ?", new Object[] { tsObj, trueObj, serverId }, new int[] { tsType, boolType, Types.VARCHAR });
if (updated == 0) {
Object id = types.getSqlObject(uuidSource.createUuid());
int idType = types.getSqlType(UUID.class);
runner.update("insert into SYS_SERVER (ID, CREATE_TS, UPDATE_TS, NAME, IS_RUNNING) " + "values (?, ?, ?, ?, ?)", new Object[] { id, tsObj, tsObj, serverId, trueObj }, new int[] { idType, tsType, tsType, Types.VARCHAR, boolType });
}
} catch (Exception e) {
log.error("Unable to update SYS_SERVER: {}", e.toString());
}
}
use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.
the class QueryResultsManager method delete.
protected void delete(List<Long> ids) {
log.debug("Deleting " + ids.size() + " records");
String str = ids.stream().map(String::valueOf).collect(Collectors.joining(","));
QueryRunner runner = new QueryRunner(persistence.getDataSource());
try {
runner.update("delete from SYS_QUERY_RESULT where ID in (" + str + ")");
} catch (SQLException e) {
throw new RuntimeException("Error deleting query result records", e);
}
}
use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.
the class QueryResultsManager method insert.
@Override
public void insert(int queryKey, List idList) {
if (idList.isEmpty())
return;
UUID userSessionId = userSessionSource.getUserSession().getId();
long start = System.currentTimeMillis();
String logMsg = "Insert " + idList.size() + " query results for " + userSessionId + " / " + queryKey;
log.debug(logMsg);
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
DbTypeConverter converter = persistence.getDbTypeConverter();
Object idFromList = idList.get(0);
String columnName = null;
if (idFromList instanceof String) {
columnName = "STRING_ENTITY_ID";
} else if (idFromList instanceof Long) {
columnName = "LONG_ENTITY_ID";
} else if (idFromList instanceof Integer) {
columnName = "INT_ENTITY_ID";
} else {
columnName = "ENTITY_ID";
}
QueryRunner runner = new QueryRunner();
try {
// assuming that UUID can be passed to query as string in all databases
String userSessionIdStr = converter.getSqlObject(userSessionId).toString();
String sql = String.format("insert into SYS_QUERY_RESULT (SESSION_ID, QUERY_KEY, %s) values ('%s', %s, ?)", columnName, userSessionIdStr, queryKey);
int[] paramTypes = new int[] { converter.getSqlType(idFromList.getClass()) };
for (int i = 0; i < idList.size(); i += BATCH_SIZE) {
List<UUID> sublist = idList.subList(i, Math.min(i + BATCH_SIZE, idList.size()));
Object[][] params = new Object[sublist.size()][1];
for (int j = 0; j < sublist.size(); j++) {
params[j][0] = converter.getSqlObject(sublist.get(j));
}
runner.batch(em.getConnection(), sql, params, paramTypes);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
log.debug("Done in " + (System.currentTimeMillis() - start) + "ms: " + logMsg);
tx.commit();
} finally {
tx.end();
}
}
Aggregations