001/*
002 * (C) Copyright 2010 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     * @deprecated since 5.7, 5.6.0-HF08 use {{@link NXQL#escapeString} instead
038     */
039    @Deprecated
040    public static String quoted(String string) {
041        return NXQL.escapeString(string);
042    }
043
044    /**
045     * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString()
046     */
047    public static String select(String type, DocumentModel doc) {
048        return "SELECT * FROM " + type + " WHERE " + NXQL.ECM_PATH + " STARTSWITH "
049                + NXQL.escapeString(doc.getPathAsString()) + " AND " + NOT_DELETED;
050    }
051
052    /**
053     * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString AND prop
054     * = value
055     */
056    public static String select(String type, DocumentModel doc, String prop, String value) {
057        return select(type, doc) + " AND " + prop + " = " + NXQL.escapeString(value);
058    }
059
060}