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; 024import org.nuxeo.ecm.core.query.sql.SQLQueryParser; 025 026/** 027 * Helper to generate queries with proper escaping. 028 */ 029public class QueryHelper { 030 031 // utility class 032 private QueryHelper() { 033 } 034 035 public static final String NOT_DELETED = NXQL.ECM_LIFECYCLESTATE + " <> '" + LifeCycleConstants.DELETED_STATE + "'"; 036 037 /** 038 * @deprecated since 5.7, 5.6.0-HF08 use {{@link NXQL#escapeString} instead 039 */ 040 @Deprecated 041 public static String quoted(String string) { 042 return NXQL.escapeString(string); 043 } 044 045 /** 046 * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString() 047 */ 048 public static String select(String type, DocumentModel doc) { 049 return "SELECT * FROM " + type + " WHERE " + NXQL.ECM_PATH + " STARTSWITH " 050 + NXQL.escapeString(doc.getPathAsString()) + " AND " + NOT_DELETED; 051 } 052 053 /** 054 * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString AND prop = 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}