Search in sources :

Example 1 with QueryRunner

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);
    }
}
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)

Example 2 with QueryRunner

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

Example 3 with QueryRunner

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

Example 4 with QueryRunner

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);
    }
}
Also used : SQLException(java.sql.SQLException) QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 5 with QueryRunner

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

Aggregations

QueryRunner (com.haulmont.bali.db.QueryRunner)45 SQLException (java.sql.SQLException)17 BeforeEach (org.junit.jupiter.api.BeforeEach)13 DbTypeConverter (com.haulmont.cuba.core.sys.persistence.DbTypeConverter)6 User (com.haulmont.cuba.security.entity.User)5 AfterEach (org.junit.jupiter.api.AfterEach)4 ArrayHandler (com.haulmont.bali.db.ArrayHandler)3 EntityManager (com.haulmont.cuba.core.EntityManager)3 Transaction (com.haulmont.cuba.core.Transaction)3 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 Entity (com.haulmont.cuba.core.entity.Entity)2 Stores.storeNameToString (com.haulmont.cuba.core.global.Stores.storeNameToString)2 View (com.haulmont.cuba.core.global.View)2 PersistenceImpl (com.haulmont.cuba.core.sys.PersistenceImpl)2 Group (com.haulmont.cuba.security.entity.Group)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 Gson (com.google.gson.Gson)1