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();
}
}
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 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 ServerInfo method applicationStopped.
@Override
public void applicationStopped() {
try {
infoUpdateTimer.cancel();
infoUpdateTimer.purge();
log.trace("Updating server information in the database");
DbTypeConverter types = persistence.getDbTypeConverter();
Object tsObj = types.getSqlObject(timeSource.currentTimestamp());
int tsType = types.getSqlType(Date.class);
Object falseObj = types.getSqlObject(Boolean.FALSE);
int boolType = types.getSqlType(Boolean.class);
QueryRunner runner = new QueryRunner(persistence.getDataSource());
runner.update("update SYS_SERVER set UPDATE_TS = ?, IS_RUNNING = ? where NAME = ?", new Object[] { tsObj, falseObj, getServerId() }, new int[] { tsType, boolType, Types.VARCHAR });
} catch (Exception e) {
log.error("Unable to update SYS_SERVER: {}", e);
}
}
use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.
the class DbUpdaterEngine method createChangelogTable.
protected void createChangelogTable() {
log.trace("Creating SYS_DB_CHANGELOG table");
String timeStampType = DbmsSpecificFactory.getDbmsFeatures().getTimeStampType();
QueryRunner runner = new QueryRunner(getDataSource());
try {
int pkLength = "mysql".equals(dbmsType) ? 255 : 300;
runner.update("create table SYS_DB_CHANGELOG(" + "SCRIPT_NAME varchar(" + pkLength + ") not null primary key, " + "CREATE_TS " + timeStampType + " default current_timestamp, " + "IS_INIT integer default 0)");
} catch (SQLException e) {
throw new RuntimeException(ERROR + "Error creating changelog table", e);
}
}
Aggregations