Examples of QueryManager


Examples of javax.jcr.query.QueryManager

    private Query createQuery(Session session,
                              String statement,
                              String language,
                              Map namespaces)
            throws InvalidQueryException, RepositoryException {
        QueryManager qMgr = session.getWorkspace().getQueryManager();

        // apply namespace mappings to session
        Map previous = setNamespaceMappings(session, namespaces);
        try {
            return qMgr.createQuery(statement, language);
        } finally {
            // reset namespace mappings
            setNamespaceMappings(session, previous);
        }
    }
View Full Code Here

Examples of javax.jcr.query.QueryManager

     * @see SearchResource#getQueryGrammerSet()
     */
    public QueryGrammerSet getQueryGrammerSet()  {
        QueryGrammerSet qgs = new QueryGrammerSet();
        try {
            QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
            String[] langs = qMgr.getSupportedQueryLanguages();
            for (String lang : langs) {
                // todo: define proper namespace
                qgs.addQueryLanguage(lang, Namespace.EMPTY_NAMESPACE);
            }
        } catch (RepositoryException e) {
View Full Code Here

Examples of javax.jcr.query.QueryManager

            throws InvalidQueryException, RepositoryException, DavException {

        Session session = getRepositorySession();
        NamespaceRegistry nsReg = session.getWorkspace().getNamespaceRegistry();
        Node rootNode = session.getRootNode();
        QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();

        // test if query is defined by requested repository node
        String itemPath = locator.getRepositoryPath();
        if (itemPath != null && !rootNode.getPath().equals(itemPath)) {
            String qNodeRelPath = itemPath.substring(1);
            if (rootNode.hasNode(qNodeRelPath)) {
                Node qNode = rootNode.getNode(qNodeRelPath);
                if (qNode.isNodeType(JcrConstants.NT_QUERY)) {
                    return qMgr.getQuery(qNode);
                }
            }
        }

        Query q;
        if (sInfo != null) {
            // apply namespace mappings to session
            Map<String, String> namespaces = sInfo.getNamespaces();
            try {
                for (Map.Entry<String, String> entry : namespaces.entrySet()) {
                    String prefix = entry.getKey();
                    String uri = entry.getValue();
                    session.setNamespacePrefix(prefix, uri);
                }
                q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());

                if (SearchInfo.NRESULTS_UNDEFINED != sInfo.getNumberResults()) {
                    q.setLimit(sInfo.getNumberResults());
                }
                if (SearchInfo.OFFSET_UNDEFINED != sInfo.getOffset()) {
View Full Code Here

Examples of javax.jcr.query.QueryManager

        }
        stmt.append("]");
       
        QueryResult result;
        try {
            QueryManager qm = session.getWorkspace().getQueryManager();
            Query q = qm.createQuery(stmt.toString(), Query.XPATH);
            result = q.execute();
        } catch (RepositoryException e) {
            log.error("Unexpected error while searching effective policies.", e.getMessage());           
            throw new UnsupportedOperationException("Retrieve effective policies for set of principals not supported.", e);
        }
View Full Code Here

Examples of javax.jcr.query.QueryManager

                    .append(sortCol)
                    .append(' ')
                    .append(sortDir.getDirection());
        }

        QueryManager queryManager = session.getWorkspace().getQueryManager();
        Query query = queryManager.createQuery(xPath.toString(), Query.XPATH);
        long maxCount = builder.getMaxCount();
        if (maxCount == 0) {
            return Iterators.empty();
        }
View Full Code Here

Examples of javax.jcr.query.QueryManager

        }
        stmt.append("]");
       
        QueryResult result;
        try {
            QueryManager qm = session.getWorkspace().getQueryManager();
            Query q = qm.createQuery(stmt.toString(), Query.XPATH);
            result = q.execute();
        } catch (RepositoryException e) {
            log.error("Unexpected error while searching effective policies.", e.getMessage());
            throw new UnsupportedOperationException("Retrieve effective policies at absPath '" +jcrPath+ "' not supported.", e);
        }
View Full Code Here

Examples of javax.jcr.query.QueryManager

*/
public class QueryFulltextTest extends AbstractQueryTest {
   
    public void testFulltext() throws Exception {
        Session session = superuser;
        QueryManager qm = session.getWorkspace().getQueryManager();
        Node n1 = testRootNode.addNode("node1");
        n1.setProperty("text", "hello");
        Node n2 = testRootNode.addNode("node2");
        n2.setProperty("text", "hallo");
        Node n3 = testRootNode.addNode("node3");
        n3.setProperty("text", "hello hallo");
        session.save();
      
        String sql2 = "select [jcr:path] as [path] from [nt:base] " +
                "where contains([text], 'hello OR hallo') order by [jcr:path]";
       
        Query q;
       
        q = qm.createQuery("explain " + sql2, Query.JCR_SQL2);

// TODO the plan should actually be:
//            assertEquals("[nt:base] as [nt:base] /* " +
//                    "+((text:hallo text:hello)~1) +text:{* TO *} " +
//                    "ft:(text:\"hallo\" OR text:\"hello\") " +
//                    "where contains([nt:base].[text], cast('hello OR hallo' as string)) */",
//                    getResult(q.execute(), "plan"));
        assertEquals("[nt:base] as [nt:base] /* " +
                "aggregate +(:fulltext:hallo :fulltext:hello) +text:{* TO *} " +
                "ft:(text:\"hallo\" OR text:\"hello\") " +
                "where contains([nt:base].[text], cast('hello OR hallo' as string)) */",
                getResult(q.execute(), "plan"));
  
        // lowercase "or" mean search for the term "or"
        sql2 = "select [jcr:path] as [path] from [nt:base] " +
                "where contains([text], 'hello or hallo') order by [jcr:path]";
        q = qm.createQuery(sql2, Query.JCR_SQL2);
        assertEquals("",
                getResult(q.execute(), "path"));

    }
View Full Code Here

Examples of org.apache.jackrabbit.ocm.query.QueryManager

            valueFactory = ValueFactoryImpl.getInstance();
        }

        ObjectCache objectCache = new ObservingObjectCache(session);

        QueryManager queryManager = new QueryManagerImpl(mapper,
            converterProvider.getAtomicTypeConverters(), valueFactory);

        ObjectConverter objectConverter = new ObjectConverterImpl(mapper,
            converterProvider, new ProxyManagerImpl(), objectCache);
View Full Code Here

Examples of org.chromattic.core.query.QueryManager

  private final QueryManager queryManager;

  protected DomainSession(Domain domain) {
    this.domain = domain;
    this.broadcaster = new EventBroadcaster();
    this.queryManager = new QueryManager(this);
  }
View Full Code Here

Examples of org.datanucleus.store.query.QueryManager

        // Create the SQL statement, and its result/parameter definitions
        RDBMSStoreManager storeMgr = (RDBMSStoreManager)getStoreManager();
        ClassLoaderResolver clr = ec.getClassLoaderResolver();
        AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(candidateClass, clr);
        QueryManager qm = getQueryManager();
        String datastoreKey = storeMgr.getQueryCacheKey();
        String queryCacheKey = getQueryCacheKey();

        if (useCaching() && queryCacheKey != null)
        {
            // Check if we have any parameters set to null, since this can invalidate a datastore compilation
            // e.g " field == :val" can be "COL IS NULL" or "COL = <val>"
            boolean nullParameter = false;
            if (parameterValues != null)
            {
                Iterator iter = parameterValues.values().iterator();
                while (iter.hasNext())
                {
                    Object val = iter.next();
                    if (val == null)
                    {
                        nullParameter = true;
                        break;
                    }
                }
            }

            if (!nullParameter)
            {
                // Allowing caching so try to find compiled (datastore) query
                datastoreCompilation = (RDBMSQueryCompilation)qm.getDatastoreQueryCompilation(datastoreKey,
                    getLanguage(), queryCacheKey);
                if (datastoreCompilation != null)
                {
                    // Cached compilation exists for this datastore so reuse it
                    return;
                }
            }
        }

        // No cached compilation for this query in this datastore so compile it
        if (type == Query.BULK_UPDATE)
        {
            datastoreCompilation = new RDBMSQueryCompilation();
            compileQueryUpdate(parameterValues, acmd);
        }
        else if (type == Query.BULK_DELETE)
        {
            datastoreCompilation = new RDBMSQueryCompilation();
            compileQueryDelete(parameterValues, acmd);
        }
        else
        {
            datastoreCompilation = new RDBMSQueryCompilation();
            if (inMemory)
            {
                // Generate statement to just retrieve all candidate objects for later processing
                compileQueryToRetrieveCandidates(parameterValues, acmd);
            }
            else
            {
                // Generate statement to perform the full query in the datastore
                compileQueryFull(parameterValues, acmd);

                if (result != null)
                {
                    StatementResultMapping resultMapping = datastoreCompilation.getResultDefinition();
                    for (int i=0;i<resultMapping.getNumberOfResultExpressions();i++)
                    {
                        Object stmtMap = resultMapping.getMappingForResultExpression(i);
                        if (stmtMap instanceof StatementMappingIndex)
                        {
                            StatementMappingIndex idx = (StatementMappingIndex)stmtMap;
                            AbstractMemberMetaData mmd = idx.getMapping().getMemberMetaData();
                            if (mmd != null)
                            {
                                if (mmd.hasCollection() || mmd.hasMap() || mmd.hasArray())
                                {
                                    throw new NucleusUserException(LOCALISER.msg("021213"));
                                }
                            }
                        }
                    }
                }
            }

            if (resultClass != null && result != null)
            {
                // Do as PrivilegedAction since uses reflection
                AccessController.doPrivileged(new PrivilegedAction()
                {
                    public Object run()
                    {
                        // Check that this class has the necessary constructor/setters/fields to be used
                        StatementResultMapping resultMapping = datastoreCompilation.getResultDefinition();
                        if (QueryUtils.resultClassIsSimple(resultClass.getName()))
                        {
                            if (resultMapping.getNumberOfResultExpressions() > 1)
                            {
                                // Invalid number of result expressions
                                throw new NucleusUserException(LOCALISER.msg("021201", resultClass.getName()));
                            }

                            Object stmtMap = resultMapping.getMappingForResultExpression(0);
                            // TODO Handle StatementNewObjectMapping
                            StatementMappingIndex idx = (StatementMappingIndex)stmtMap;
                            Class exprType = idx.getMapping().getJavaType();
                            boolean typeConsistent = false;
                            if (exprType == resultClass)
                            {
                                typeConsistent = true;
                            }
                            else if (exprType.isPrimitive())
                            {
                                Class resultClassPrimitive = ClassUtils.getPrimitiveTypeForType(resultClass);
                                if (resultClassPrimitive == exprType)
                                {
                                    typeConsistent = true;
                                }
                            }
                            if (!typeConsistent)
                            {
                                // Inconsistent expression type not matching the result class type
                                throw new NucleusUserException(LOCALISER.msg("021202", resultClass.getName(), exprType));
                            }
                        }
                        else if (QueryUtils.resultClassIsUserType(resultClass.getName()))
                        {
                            // Check for valid constructor (either using param types, or using default ctr)
                            Class[] ctrTypes = new Class[resultMapping.getNumberOfResultExpressions()];
                            for (int i=0;i<ctrTypes.length;i++)
                            {
                                Object stmtMap = resultMapping.getMappingForResultExpression(i);
                                if (stmtMap instanceof StatementMappingIndex)
                                {
                                    ctrTypes[i] = ((StatementMappingIndex)stmtMap).getMapping().getJavaType();
                                }
                                else if (stmtMap instanceof StatementNewObjectMapping)
                                {
                                    // TODO Handle this
                                }
                            }
                            Constructor ctr = ClassUtils.getConstructorWithArguments(resultClass, ctrTypes);
                            if (ctr == null && !ClassUtils.hasDefaultConstructor(resultClass))
                            {
                                // No valid constructor found!
                                throw new NucleusUserException(LOCALISER.msg("021205", resultClass.getName()));
                            }
                            else if (ctr == null)
                            {
                                // We are using default constructor, so check the types of the result expressions for means of input
                                for (int i=0;i<resultMapping.getNumberOfResultExpressions();i++)
                                {
                                    Object stmtMap = resultMapping.getMappingForResultExpression(i);
                                    if (stmtMap instanceof StatementMappingIndex)
                                    {
                                        StatementMappingIndex mapIdx = (StatementMappingIndex)stmtMap;
                                        AbstractMemberMetaData mmd = mapIdx.getMapping().getMemberMetaData();
                                        String fieldName = mapIdx.getColumnAlias();
                                        Class fieldType = mapIdx.getMapping().getJavaType();
                                        if (fieldName == null && mmd != null)
                                        {
                                            fieldName = mmd.getName();
                                        }

                                        if (fieldName != null)
                                        {
                                            // Check for the field of that name in the result class
                                            Class resultFieldType = null;
                                            boolean publicField = true;
                                            try
                                            {
                                                Field fld = resultClass.getDeclaredField(fieldName);
                                                resultFieldType = fld.getType();

                                                // Check the type of the field
                                                if (!ClassUtils.typesAreCompatible(fieldType, resultFieldType) &&
                                                        !ClassUtils.typesAreCompatible(resultFieldType, fieldType))
                                                {
                                                    throw new NucleusUserException(LOCALISER.msg("021211",
                                                        fieldName, fieldType.getName(), resultFieldType.getName()));
                                                }
                                                if (!Modifier.isPublic(fld.getModifiers()))
                                                {
                                                    publicField = false;
                                                }
                                            }
                                            catch (NoSuchFieldException nsfe)
                                            {
                                                publicField = false;
                                            }

                                            // Check for a public set method
                                            if (!publicField)
                                            {
                                                Method setMethod = QueryUtils.getPublicSetMethodForFieldOfResultClass(resultClass, fieldName, resultFieldType);
                                                if (setMethod == null)
                                                {
                                                    // No setter, so check for a public put(Object, Object) method
                                                    Method putMethod = QueryUtils.getPublicPutMethodForResultClass(resultClass);
                                                    if (putMethod == null)
                                                    {
                                                        throw new NucleusUserException(LOCALISER.msg("021212",
                                                            resultClass.getName(), fieldName));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else if (stmtMap instanceof StatementNewObjectMapping)
                                    {
                                        // TODO Handle this
                                    }
                                }
                            }
                        }

                        return null;
                    }
                });
            }

            if (NucleusLogger.QUERY.isDebugEnabled())
            {
                NucleusLogger.QUERY.debug(LOCALISER.msg("021085", this, datastoreCompilation.getSQL()));
            }

            boolean hasParams = false;
            if (explicitParameters != null)
            {
                hasParams = true;
            }
            else if (parameterValues != null && parameterValues.size() > 0)
            {
                hasParams = true;
            }
            if (!datastoreCompilation.isPrecompilable() ||
                (datastoreCompilation.getSQL().indexOf('?') < 0 && hasParams))
            {
                // Some parameters had their clauses evaluated during compilation so the query
                // didn't gain any parameters, so don't cache it
                NucleusLogger.QUERY.debug(LOCALISER.msg("021075"));
            }
            else
            {
                if (useCaching() && queryCacheKey != null)
                {
                  qm.addDatastoreQueryCompilation(datastoreKey, getLanguage(), queryCacheKey, datastoreCompilation);
                }
            }
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.