Search in sources :

Example 1 with DbOomQuery

use of jodd.db.oom.DbOomQuery in project jodd by oblac.

the class DbPager method page.

/**
	 * Pages given page.
	 *
	 * @param sql sql query that lists <b>all</b> items
	 * @param params map of SQL parameters
	 * @param page current page to show
	 * @param pageSize number of items to show
	 * @param sortColumnName name of sorting column, <code>null</code> for no sorting
	 * @param ascending <code>true</code> for ascending order
	 * @param target db entities for mapping (sa usual in DbOom)
	 */
protected <T> PageData<T> page(String sql, Map params, int page, int pageSize, String sortColumnName, boolean ascending, Class[] target) {
    if (sortColumnName != null) {
        sql = buildOrderSql(sql, sortColumnName, ascending);
    }
    int from = (page - 1) * pageSize;
    String pageSql = buildPageSql(sql, from, pageSize);
    DbSqlBuilder dbsql = sql(pageSql);
    DbOomQuery query = query(dbsql);
    query.setMaxRows(pageSize);
    query.setFetchSize(pageSize);
    query.setMap(params);
    List<T> list = query.list(pageSize, target);
    query.close();
    String countSql = buildCountSql(sql);
    dbsql = sql(countSql);
    query = query(dbsql);
    query.setMap(params);
    long count = query.executeCount();
    query.close();
    return new PageData<>(page, (int) count, pageSize, list);
}
Also used : DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) DbOomQuery(jodd.db.oom.DbOomQuery)

Example 2 with DbOomQuery

use of jodd.db.oom.DbOomQuery in project jodd by oblac.

the class DbQueryBuilder method createAndPopulateDbQuery.

/**
	 * Prepares <code>DbQuery</code>.
	 */
public DbOomQuery createAndPopulateDbQuery(ProxyTargetInfo proxyTargetInfo, String query) {
    if (query == null) {
        query = resolveQuery(proxyTargetInfo);
        if (query == null) {
            throw new DbOomException("Query not resolved.");
        }
    }
    // sql generator
    DbSqlGenerator dbSqlGenerator = parsedSqlMap.get(query);
    if (dbSqlGenerator == null) {
        ParsedSql parsedSql = sql(query).parse();
        parsedSqlMap.put(query, parsedSql);
        dbSqlGenerator = parsedSql;
    }
    // db oom query
    DbOomQuery dbOomQuery = query(dbSqlGenerator);
    // parameter names
    String keyName = proxyTargetInfo.targetClass.getName() + "." + proxyTargetInfo.targetMethodName;
    String[] paramNames = methodParamNames.get(keyName);
    if (paramNames == null) {
        paramNames = resolveMethodParameterNames(proxyTargetInfo);
        methodParamNames.put(keyName, paramNames);
    }
    for (int i = 0; i < paramNames.length; i++) {
        String paramName = paramNames[i];
        dbOomQuery.setObject(paramName, proxyTargetInfo.arguments[i]);
    }
    return dbOomQuery;
}
Also used : DbOomException(jodd.db.oom.DbOomException) DbSqlGenerator(jodd.db.oom.DbSqlGenerator) DbOomQuery(jodd.db.oom.DbOomQuery) ParsedSql(jodd.db.oom.sqlgen.ParsedSql)

Example 3 with DbOomQuery

use of jodd.db.oom.DbOomQuery in project jodd by oblac.

the class DbIdGenerator method nextId.

/**
	 * Returns next ID for given entity type.
	 * On the first call, it finds the max value of all IDs and stores it.
	 * On later calls, stored id is incremented and returned.
	 */
public synchronized long nextId(Class entityType) {
    MutableLong lastId = entityIdsMap.get(entityType);
    if (lastId == null) {
        DbOomManager dbOomManager = DbOomManager.getInstance();
        DbEntityDescriptor ded = dbOomManager.lookupType(entityType);
        String tableName = ded.getTableName();
        String idColumn = ded.getIdColumnName();
        DbOomQuery dbOomQuery = query("select max(" + idColumn + ") from " + tableName);
        long lastLong = dbOomQuery.autoClose().executeCount();
        if (log.isDebugEnabled()) {
            log.debug("Last id for " + entityType.getName() + " is " + lastLong);
        }
        lastId = new MutableLong(lastLong);
        entityIdsMap.put(entityType, lastId);
    }
    lastId.value++;
    return lastId.value;
}
Also used : MutableLong(jodd.mutable.MutableLong) DbOomQuery(jodd.db.oom.DbOomQuery) DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Aggregations

DbOomQuery (jodd.db.oom.DbOomQuery)3 DbEntityDescriptor (jodd.db.oom.DbEntityDescriptor)1 DbOomException (jodd.db.oom.DbOomException)1 DbOomManager (jodd.db.oom.DbOomManager)1 DbSqlGenerator (jodd.db.oom.DbSqlGenerator)1 DbSqlBuilder (jodd.db.oom.sqlgen.DbSqlBuilder)1 ParsedSql (jodd.db.oom.sqlgen.ParsedSql)1 MutableLong (jodd.mutable.MutableLong)1