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.query.sql.NXQL;
023
024/**
025 * Helper to generate queries with proper escaping.
026 */
027public class QueryHelper {
028
029    // utility class
030    private QueryHelper() {
031    }
032
033    public static final String NOT_DELETED = NXQL.ECM_ISTRASHED + " = 0";
034
035    /**
036     * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString()
037     */
038    public static String select(String type, DocumentModel doc) {
039        return "SELECT * FROM " + type + " WHERE " + NXQL.ECM_PATH + " STARTSWITH "
040                + NXQL.escapeString(doc.getPathAsString()) + " AND " + NOT_DELETED;
041    }
042
043    /**
044     * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString AND prop = value
045     */
046    public static String select(String type, DocumentModel doc, String prop, String value) {
047        return select(type, doc) + " AND " + prop + " = " + NXQL.escapeString(value);
048    }
049
050}