Search in sources :

Example 1 with QueryMetaData

use of org.datanucleus.metadata.QueryMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHelper method getXMLForMetaData.

/**
 * Method to convert an internal class/interface metadata into the associated JDO XML metadata.
 * @param cmd Metadata for the class/interface
 * @param prefix Prefix for the XML (e.g "    ")
 * @param indent Indent for each block of XML (e.g "    ")
 * @return The XML
 */
public String getXMLForMetaData(AbstractClassMetaData cmd, String prefix, String indent) {
    StringBuilder str = new StringBuilder();
    boolean intf = (cmd instanceof InterfaceMetaData);
    if (intf) {
        str.append(prefix).append("<interface name=\"" + cmd.getName() + "\"");
    } else {
        str.append(prefix).append("<class name=\"" + cmd.getName() + "\"");
    }
    if (cmd.getIdentityType() != null) {
        str.append("\n").append(prefix).append("       identity-type=\"" + cmd.getIdentityType() + "\"");
    }
    if (cmd.getObjectidClass() != null) {
        str.append("\n").append(prefix).append("       objectid-class=\"" + cmd.getObjectidClass() + "\"");
    }
    if (!cmd.isRequiresExtent()) {
        str.append("\n").append(prefix).append("       requires-extent=\"" + cmd.isRequiresExtent() + "\"");
    }
    if (cmd.isEmbeddedOnly()) {
        str.append("\n").append(prefix).append("       embedded-only=\"" + cmd.isEmbeddedOnly() + "\"");
    }
    if (cmd.getPersistenceModifier() != null) {
        str.append("\n").append(prefix).append("       persistence-modifier=\"" + cmd.getPersistenceModifier() + "\"");
    }
    if (cmd.getCatalog() != null) {
        str.append("\n").append(prefix).append("       catalog=\"" + cmd.getCatalog() + "\"");
    }
    if (cmd.getSchema() != null) {
        str.append("\n").append(prefix).append("       schema=\"" + cmd.getSchema() + "\"");
    }
    if (cmd.getTable() != null) {
        str.append("\n").append(prefix).append("       table=\"" + cmd.getTable() + "\"");
    }
    if (cmd.isDetachable()) {
        str.append("\n").append(prefix).append("       detachable=\"" + cmd.isDetachable() + "\"");
    }
    str.append(">\n");
    // Identity
    if (cmd.getIdentityMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getIdentityMetaData(), prefix + indent, indent));
    }
    // PrimaryKey
    if (cmd.getPrimaryKeyMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getPrimaryKeyMetaData(), prefix + indent, indent));
    }
    // Inheritance
    if (cmd.getInheritanceMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getInheritanceMetaData(), prefix + indent, indent));
    }
    // Add Version
    if (cmd.getVersionMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getVersionMetaData(), prefix + indent, indent));
    }
    // Add joins
    List<JoinMetaData> joins = cmd.getJoinMetaData();
    if (joins != null) {
        for (JoinMetaData joinmd : joins) {
            str.append(getXMLForMetaData(joinmd, prefix + indent, indent));
        }
    }
    // Add foreign-keys
    List<ForeignKeyMetaData> foreignKeys = cmd.getForeignKeyMetaData();
    if (foreignKeys != null) {
        for (ForeignKeyMetaData fkmd : foreignKeys) {
            str.append(getXMLForMetaData(fkmd, prefix + indent, indent));
        }
    }
    // Add indexes
    List<IndexMetaData> indexes = cmd.getIndexMetaData();
    if (indexes != null) {
        for (IndexMetaData idxmd : indexes) {
            str.append(getXMLForMetaData(idxmd, prefix + indent, indent));
        }
    }
    // Add unique constraints
    List<UniqueMetaData> uniqueConstraints = cmd.getUniqueMetaData();
    if (uniqueConstraints != null) {
        for (UniqueMetaData unimd : uniqueConstraints) {
            str.append(getXMLForMetaData(unimd, prefix + indent, indent));
        }
    }
    // Add members
    int numMembers = cmd.getNoOfMembers();
    for (int i = 0; i < numMembers; i++) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForMemberAtRelativePosition(i);
        str.append(getXMLForMetaData(mmd, prefix + indent, indent));
    }
    // Add unmapped columns
    List<ColumnMetaData> unmappedColumns = cmd.getUnmappedColumns();
    if (unmappedColumns != null) {
        for (int i = 0; i < unmappedColumns.size(); i++) {
            ColumnMetaData col = unmappedColumns.get(i);
            str.append(getXMLForMetaData(col, prefix + indent, indent));
        }
    }
    // Add queries
    QueryMetaData[] queries = cmd.getQueries();
    if (queries != null) {
        for (int i = 0; i < queries.length; i++) {
            QueryMetaData q = queries[i];
            str.append(getXMLForMetaData(q, prefix + indent, indent));
        }
    }
    // Add fetch-groups
    Set<FetchGroupMetaData> fetchGroups = cmd.getFetchGroupMetaData();
    if (fetchGroups != null) {
        for (FetchGroupMetaData fgmd : fetchGroups) {
            str.append(getXMLForMetaData(fgmd, prefix + indent, indent));
        }
    }
    // Add extensions
    processExtensions(cmd.getExtensions(), str, prefix, indent);
    if (intf) {
        str.append(prefix + "</interface>\n");
    } else {
        str.append(prefix + "</class>\n");
    }
    return str.toString();
}
Also used : UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) IndexMetaData(org.datanucleus.metadata.IndexMetaData) QueryMetaData(org.datanucleus.metadata.QueryMetaData) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) JoinMetaData(org.datanucleus.metadata.JoinMetaData) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 2 with QueryMetaData

use of org.datanucleus.metadata.QueryMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetadataImpl method newQueryMetadata.

/* (non-Javadoc)
     * @see javax.jdo.metadata.JDOMetadata#newQueryMetadata(java.lang.String)
     */
public QueryMetadata newQueryMetadata(String name) {
    QueryMetaData internalQmd = getInternal().newQueryMetadata(name);
    QueryMetadataImpl qmd = new QueryMetadataImpl(internalQmd);
    qmd.parent = this;
    return qmd;
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData)

Example 3 with QueryMetaData

use of org.datanucleus.metadata.QueryMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method newNamedQuery.

/**
 * Construct a query instance with the candidate class and the query name.
 * @param cls The class to query
 * @param queryName Name of the query.
 * @return The query
 * @param <T> Candidate type for the query
 */
public <T> Query<T> newNamedQuery(Class<T> cls, String queryName) {
    assertIsOpen();
    // Throw exception on incomplete input
    if (queryName == null) {
        throw new JDOUserException(Localiser.msg("011005", null, cls));
    }
    // Find the Query for the specified class
    ClassLoaderResolver clr = ec.getClassLoaderResolver();
    QueryMetaData qmd = ec.getMetaDataManager().getMetaDataForQuery(cls, clr, queryName);
    if (qmd == null) {
        throw new JDOUserException(Localiser.msg("011005", queryName, cls));
    }
    // Create the Query
    Query query = newQuery(qmd.getLanguage(), qmd.getQuery());
    if (cls != null) {
        query.setClass(cls);
        if (!ec.getStoreManager().managesClass(cls.getName())) {
            // Load the candidate class since not yet managed
            ec.getStoreManager().manageClasses(clr, cls.getName());
        }
    }
    // Optional args that should only be used with SQL
    if (qmd.getLanguage().equals(QueryLanguage.JDOQL.toString()) && (qmd.isUnique() || qmd.getResultClass() != null)) {
        throw new JDOUserException(Localiser.msg("011007", queryName));
    }
    if (qmd.isUnique()) {
        query.setUnique(true);
    }
    if (qmd.getResultClass() != null) {
        // Set the result class, allowing for it being in the same package as the candidate
        Class resultCls = null;
        try {
            resultCls = clr.classForName(qmd.getResultClass());
        } catch (ClassNotResolvedException cnre) {
            if (cls != null) {
                try {
                    String resultClassName = cls.getPackage().getName() + "." + qmd.getResultClass();
                    resultCls = clr.classForName(resultClassName);
                } catch (ClassNotResolvedException cnre2) {
                    throw new JDOUserException(Localiser.msg("011008", queryName, qmd.getResultClass()));
                }
            }
        }
        query.setResultClass(resultCls);
    }
    // Add any extensions
    Map<String, String> extmds = qmd.getExtensions();
    if (extmds != null) {
        Iterator<Entry<String, String>> entryIter = extmds.entrySet().iterator();
        while (entryIter.hasNext()) {
            Entry<String, String> entry = entryIter.next();
            query.addExtension(entry.getKey(), entry.getValue());
        }
    }
    if (qmd.isUnmodifiable()) {
        query.setUnmodifiable();
    }
    if (qmd.getFetchPlanName() != null) {
        // Apply any named FetchPlan to the query
        FetchPlanMetaData fpmd = ec.getMetaDataManager().getMetaDataForFetchPlan(qmd.getFetchPlanName());
        if (fpmd != null) {
            org.datanucleus.FetchPlan fp = new org.datanucleus.FetchPlan(ec, clr);
            fp.removeGroup(org.datanucleus.FetchPlan.DEFAULT);
            FetchGroupMetaData[] fgmds = fpmd.getFetchGroupMetaData();
            for (FetchGroupMetaData fgmd : fgmds) {
                fp.addGroup(fgmd.getName());
            }
            fp.setMaxFetchDepth(fpmd.getMaxFetchDepth());
            fp.setFetchSize(fpmd.getFetchSize());
            ((JDOQuery) query).getInternalQuery().setFetchPlan(fp);
        }
    }
    return query;
}
Also used : JDOQLTypedQuery(javax.jdo.JDOQLTypedQuery) Query(javax.jdo.Query) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) FetchPlanMetaData(org.datanucleus.metadata.FetchPlanMetaData) FetchPlan(javax.jdo.FetchPlan) JDOUserException(javax.jdo.JDOUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) QueryMetaData(org.datanucleus.metadata.QueryMetaData) Entry(java.util.Map.Entry) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Example 4 with QueryMetaData

use of org.datanucleus.metadata.QueryMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOQuery method saveAsNamedQuery.

/**
 * Save this query as a named query with the specified name.
 * @param name The name to refer to it under
 * @return This query
 */
public Query<T> saveAsNamedQuery(String name) {
    assertIsOpen();
    JDOPersistenceManagerFactory.checkJDOPermission(JDOPermission.GET_METADATA);
    String queryName = null;
    if (query.getCandidateClassName() != null) {
        // Candidate, so save as "{candidateName}_{userName}"
        queryName = query.getCandidateClassName() + "_" + name;
    } else {
        // No candidate so save as "{userName}"
        queryName = name;
    }
    QueryMetaData qmd = new QueryMetaData(queryName);
    qmd.setLanguage(language);
    qmd.setQuery(query.toString());
    qmd.setResultClass(query.getResultClassName());
    qmd.setUnique(query.isUnique());
    Map<String, Object> queryExts = query.getExtensions();
    if (queryExts != null && !queryExts.isEmpty()) {
        Iterator<Map.Entry<String, Object>> queryExtsIter = queryExts.entrySet().iterator();
        while (queryExtsIter.hasNext()) {
            Map.Entry<String, Object> queryExtEntry = queryExtsIter.next();
            qmd.addExtension(queryExtEntry.getKey(), "" + queryExtEntry.getValue());
        }
    }
    query.getExecutionContext().getMetaDataManager().registerNamedQuery(qmd);
    return this;
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with QueryMetaData

use of org.datanucleus.metadata.QueryMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHelper method getXMLForMetaData.

public String getXMLForMetaData(FileMetaData filemd, String prefix, String indent) {
    StringBuilder str = new StringBuilder();
    str.append(prefix).append("<jdo");
    if (filemd.getCatalog() != null) {
        str.append(" catalog=\"" + filemd.getCatalog() + "\"");
    }
    if (filemd.getSchema() != null) {
        str.append(" schema=\"" + filemd.getSchema() + "\"");
    }
    str.append(">\n");
    // Add packages
    if (filemd.getNoOfPackages() > 0) {
        for (int i = 0; i < filemd.getNoOfPackages(); i++) {
            PackageMetaData pmd = filemd.getPackage(i);
            str.append(getXMLForMetaData(pmd, prefix + indent, indent));
        }
    }
    // Add queries
    QueryMetaData[] queries = filemd.getQueries();
    if (queries != null) {
        for (QueryMetaData qmd : queries) {
            str.append(getXMLForMetaData(qmd, prefix + indent, indent));
        }
    }
    // Add fetch plans
    FetchPlanMetaData[] fetchPlans = filemd.getFetchPlans();
    if (fetchPlans != null) {
        for (FetchPlanMetaData fpmd : fetchPlans) {
            str.append(getXMLForMetaData(fpmd, prefix + indent, indent));
        }
    }
    // Add extensions
    processExtensions(filemd.getExtensions(), str, prefix, indent);
    str.append("</jdo>");
    return str.toString();
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) PackageMetaData(org.datanucleus.metadata.PackageMetaData) FetchPlanMetaData(org.datanucleus.metadata.FetchPlanMetaData)

Aggregations

QueryMetaData (org.datanucleus.metadata.QueryMetaData)15 ClassMetaData (org.datanucleus.metadata.ClassMetaData)5 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)3 FetchPlanMetaData (org.datanucleus.metadata.FetchPlanMetaData)3 MetaDataManager (org.datanucleus.metadata.MetaDataManager)3 Map (java.util.Map)2 JDOQLTypedQuery (javax.jdo.JDOQLTypedQuery)2 NucleusContext (org.datanucleus.NucleusContext)2 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)2 JDOQuery (org.datanucleus.api.jdo.JDOQuery)2 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)2 InvalidClassMetaDataException (org.datanucleus.metadata.InvalidClassMetaDataException)2 PackageMetaData (org.datanucleus.metadata.PackageMetaData)2 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)2