001/*
002 * (C) Copyright 2010-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.apidoc.api;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.LifeCycleConstants;
023import org.nuxeo.ecm.core.query.sql.NXQL;
024
025/**
026 * Helper to generate queries with proper escaping.
027 */
028public class QueryHelper {
029
030    // utility class
031    private QueryHelper() {
032    }
033
034    public static final String NOT_DELETED = NXQL.ECM_LIFECYCLESTATE + " <> '" + LifeCycleConstants.DELETED_STATE + "'";
035
036    /**
037     * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString()
038     */
039    public static String select(String type, DocumentModel doc) {
040        return "SELECT * FROM " + type + " WHERE " + NXQL.ECM_PATH + " STARTSWITH "
041                + NXQL.escapeString(doc.getPathAsString()) + " AND " + NOT_DELETED;
042    }
043
044    /**
045     * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString AND prop = value
046     */
047    public static String select(String type, DocumentModel doc, String prop, String value) {
048        return select(type, doc) + " AND " + prop + " = " + NXQL.escapeString(value);
049    }
050
051}