/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright © 2013-2015 ForgeRock AS. All rights reserved.
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* http://forgerock.org/license/CDDLv1.0.html
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at http://forgerock.org/license/CDDLv1.0.html
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* $Id$
*/
package org.forgerock.openidm.repo.jdbc.impl;
import static org.forgerock.openidm.repo.QueryConstants.PAGED_RESULTS_OFFSET;
import static org.forgerock.openidm.repo.QueryConstants.PAGE_SIZE;
import static org.forgerock.openidm.repo.QueryConstants.SORT_KEYS;
import org.forgerock.json.JsonPointer;
import org.forgerock.json.JsonValue;
import org.forgerock.json.resource.SortKey;
import org.forgerock.openidm.repo.jdbc.SQLExceptionHandler;
import org.forgerock.util.query.QueryFilter;
import java.util.List;
import java.util.Map;
/**
* TableHandler appropriate to H2-specific query syntax.
*/
public class H2TableHandler extends GenericTableHandler {
public H2TableHandler(JsonValue tableConfig, String dbSchemaName, JsonValue queriesConfig, JsonValue commandsConfig,
int maxBatchSize, SQLExceptionHandler sqlExceptionHandler) {
super(tableConfig, dbSchemaName, queriesConfig, commandsConfig, maxBatchSize, sqlExceptionHandler);
}
protected Map<QueryDefinition, String> initializeQueryMap() {
Map<QueryDefinition, String> result = super.initializeQueryMap();
String typeTable = dbSchemaName == null ? "objecttypes" : dbSchemaName + ".objecttypes";
String mainTable = dbSchemaName == null ? mainTableName : dbSchemaName + "." + mainTableName;
String propertyTable = dbSchemaName == null ? propTableName : dbSchemaName + "." + propTableName;
result.put(QueryDefinition.PROPDELETEQUERYSTR, "DELETE FROM " + propertyTable + " WHERE " + mainTableName + "_id IN (SELECT obj.id FROM " + mainTable + " obj INNER JOIN " + typeTable + " objtype ON obj.objecttypes_id = objtype.id WHERE objtype.objecttype = ? AND obj.objectid = ?)");
return result;
}
/**
* @inheritDoc
*/
@Override
public String renderQueryFilter(QueryFilter<JsonPointer> filter, Map<String, Object> replacementTokens, Map<String, Object> params) {
final int offsetParam = Integer.parseInt((String) params.get(PAGED_RESULTS_OFFSET));
final int pageSizeParam = Integer.parseInt((String) params.get(PAGE_SIZE));
// "SELECT obj.* FROM mainTable obj..."
SQLBuilder builder = new SQLBuilder() {
@Override
public String toSQL() {
return "SELECT " + getColumns().toSQL()
+ getFromClause().toSQL()
+ getJoinClause().toSQL()
+ getWhereClause().toSQL()
+ getOrderByClause().toSQL()
+ " LIMIT " + pageSizeParam
+ " OFFSET " + offsetParam;
}
};
builder.addColumn("SELECT obj.fullobject")
.from("${_dbSchema}.${_mainTable} obj")
.where(filter.accept(new GenericSQLQueryFilterVisitor(SEARCHABLE_LENGTH, builder), replacementTokens));
// JsonValue-cheat to avoid an unchecked cast
final List<SortKey> sortKeys = new JsonValue(params).get(SORT_KEYS).asList(SortKey.class);
// Check for sort keys and build up order-by syntax
prepareSortKeyStatements(builder, sortKeys, replacementTokens);
return builder.toSQL();
}
}