Search in sources :

Example 1 with DbTypeConverter

use of com.haulmont.cuba.core.sys.persistence.DbTypeConverter 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());
    }
}
Also used : DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter) QueryRunner(com.haulmont.bali.db.QueryRunner) IOException(java.io.IOException)

Example 2 with DbTypeConverter

use of com.haulmont.cuba.core.sys.persistence.DbTypeConverter in project cuba by cuba-platform.

the class QueryResultsManager method deleteForCurrentSession.

@Override
public void deleteForCurrentSession() {
    QueryRunner runner = new QueryRunner(persistence.getDataSource());
    try {
        DbTypeConverter converter = persistence.getDbTypeConverter();
        UUID userSessionId = userSessionSource.getUserSession().getId();
        String userSessionIdStr = converter.getSqlObject(userSessionId).toString();
        runner.update("delete from SYS_QUERY_RESULT where SESSION_ID = '" + userSessionIdStr + "'");
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) QueryRunner(com.haulmont.bali.db.QueryRunner) DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter)

Example 3 with DbTypeConverter

use of com.haulmont.cuba.core.sys.persistence.DbTypeConverter 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();
    }
}
Also used : SQLException(java.sql.SQLException) DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter) QueryRunner(com.haulmont.bali.db.QueryRunner) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 4 with DbTypeConverter

use of com.haulmont.cuba.core.sys.persistence.DbTypeConverter 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);
}
Also used : SQLException(java.sql.SQLException) DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter) QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 5 with DbTypeConverter

use of com.haulmont.cuba.core.sys.persistence.DbTypeConverter 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);
    }
}
Also used : SQLException(java.sql.SQLException) UserSession(com.haulmont.cuba.security.global.UserSession) DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter) QueryRunner(com.haulmont.bali.db.QueryRunner)

Aggregations

QueryRunner (com.haulmont.bali.db.QueryRunner)6 DbTypeConverter (com.haulmont.cuba.core.sys.persistence.DbTypeConverter)6 SQLException (java.sql.SQLException)4 IOException (java.io.IOException)2 EntityManager (com.haulmont.cuba.core.EntityManager)1 Transaction (com.haulmont.cuba.core.Transaction)1 UserSession (com.haulmont.cuba.security.global.UserSession)1