Search in sources :

Example 21 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) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 22 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 23 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 24 with QueryRunner

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

Example 25 with QueryRunner

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

Aggregations

QueryRunner (com.haulmont.bali.db.QueryRunner)38 SQLException (java.sql.SQLException)14 Before (org.junit.Before)10 DbTypeConverter (com.haulmont.cuba.core.sys.persistence.DbTypeConverter)6 User (com.haulmont.cuba.security.entity.User)4 EntityManager (com.haulmont.cuba.core.EntityManager)3 Transaction (com.haulmont.cuba.core.Transaction)3 After (org.junit.After)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 Entity (com.haulmont.cuba.core.entity.Entity)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 ArrayHandler (com.haulmont.bali.db.ArrayHandler)1 ResultSetHandler (com.haulmont.bali.db.ResultSetHandler)1 EntitySnapshot (com.haulmont.cuba.core.entity.EntitySnapshot)1 Server (com.haulmont.cuba.core.entity.Server)1 View (com.haulmont.cuba.core.global.View)1 EntityListenerManager (com.haulmont.cuba.core.sys.listener.EntityListenerManager)1 Group (com.haulmont.cuba.security.entity.Group)1