Search in sources :

Example 6 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OCommandExecutorSQLInsert method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLInsert parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        // System.out.println("NEW PARSER FROM: " + queryText);
        queryText = preParse(queryText, iRequest);
        // System.out.println("NEW PARSER   TO: " + queryText);
        textRequest.setText(queryText);
        final ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        className = null;
        newRecords = null;
        content = null;
        if (parserTextUpperCase.endsWith(KEYWORD_UNSAFE)) {
            unsafe = true;
            parserText = parserText.substring(0, parserText.length() - KEYWORD_UNSAFE.length() - 1);
            parserTextUpperCase = parserTextUpperCase.substring(0, parserTextUpperCase.length() - KEYWORD_UNSAFE.length() - 1);
        }
        parserRequiredKeyword("INSERT");
        parserRequiredKeyword("INTO");
        String subjectName = parserRequiredWord(true, "Invalid subject name. Expected cluster, class or index");
        if (subjectName.startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX)) {
            // CLUSTER
            clusterName = subjectName.substring(OCommandExecutorSQLAbstract.CLUSTER_PREFIX.length());
            try {
                int clusterId = Integer.parseInt(clusterName);
                clusterName = database.getClusterNameById(clusterId);
            } catch (Exception e) {
            //not an integer
            }
        } else if (subjectName.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX))
            // INDEX
            indexName = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_PREFIX.length());
        else {
            // CLASS
            if (subjectName.startsWith(OCommandExecutorSQLAbstract.CLASS_PREFIX))
                subjectName = subjectName.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());
            final OClass cls = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName);
            if (cls == null)
                throwParsingException("Class " + subjectName + " not found in database");
            if (!unsafe && cls.isSubClassOf("E"))
                // FOUND EDGE
                throw new OCommandExecutionException("'INSERT' command cannot create Edges. Use 'CREATE EDGE' command instead, or apply the 'UNSAFE' keyword to force it");
            className = cls.getName();
            clazz = database.getMetadata().getSchema().getClass(className);
            if (clazz == null)
                throw new OQueryParsingException("Class '" + className + "' was not found");
        }
        if (clusterName != null && className == null) {
            ODatabaseDocumentInternal db = getDatabase();
            OCluster cluster = db.getStorage().getClusterByName(clusterName);
            if (cluster != null) {
                clazz = db.getMetadata().getSchema().getClassByClusterId(cluster.getId());
                if (clazz != null) {
                    className = clazz.getName();
                }
            }
        }
        parserSkipWhiteSpaces();
        if (parserIsEnded())
            throwSyntaxErrorException("Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");
        final String temp = parseOptionalWord(true);
        if (parserGetLastWord().equalsIgnoreCase("cluster")) {
            clusterName = parserRequiredWord(false);
            parserSkipWhiteSpaces();
            if (parserIsEnded())
                throwSyntaxErrorException("Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");
        } else
            parserGoBack();
        newRecords = new ArrayList<Map<String, Object>>();
        Boolean sourceClauseProcessed = false;
        if (parserGetCurrentChar() == '(') {
            parseValues();
            parserNextWord(true, " \r\n");
            sourceClauseProcessed = true;
        } else {
            parserNextWord(true, " ,\r\n");
            if (parserGetLastWord().equals(KEYWORD_CONTENT)) {
                newRecords = null;
                parseContent();
                sourceClauseProcessed = true;
            } else if (parserGetLastWord().equals(KEYWORD_SET)) {
                final List<OPair<String, Object>> fields = new ArrayList<OPair<String, Object>>();
                parseSetFields(clazz, fields);
                newRecords.add(OPair.convertToMap(fields));
                sourceClauseProcessed = true;
            }
        }
        if (sourceClauseProcessed)
            parserNextWord(true, " \r\n");
        // it has to be processed before KEYWORD_FROM in order to not be taken as part of SELECT
        if (parserGetLastWord().equals(KEYWORD_RETURN)) {
            parseReturn(!sourceClauseProcessed);
            parserNextWord(true, " \r\n");
        }
        if (!sourceClauseProcessed) {
            if (parserGetLastWord().equals(KEYWORD_FROM)) {
                newRecords = null;
                subQuery = new OSQLAsynchQuery<OIdentifiable>(parserText.substring(parserGetCurrentPosition()), this);
            }
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OPair(com.orientechnologies.common.util.OPair) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OCluster(com.orientechnologies.orient.core.storage.OCluster)

Example 7 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OQueryOperatorContainsText method filterRecords.

@SuppressWarnings({ "unchecked", "deprecation" })
@Override
public Collection<OIdentifiable> filterRecords(final ODatabase<?> iDatabase, final List<String> iTargetClasses, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight) {
    final String fieldName;
    if (iCondition.getLeft() instanceof OSQLFilterItemField)
        fieldName = iCondition.getLeft().toString();
    else
        fieldName = iCondition.getRight().toString();
    final String fieldValue;
    if (iCondition.getLeft() instanceof OSQLFilterItemField)
        fieldValue = iCondition.getRight().toString();
    else
        fieldValue = iCondition.getLeft().toString();
    final String className = iTargetClasses.get(0);
    final OProperty prop = ((OMetadataInternal) iDatabase.getMetadata()).getImmutableSchemaSnapshot().getClass(className).getProperty(fieldName);
    if (prop == null)
        // NO PROPERTY DEFINED
        return null;
    OIndex<?> fullTextIndex = null;
    for (final OIndex<?> indexDefinition : prop.getIndexes()) {
        if (indexDefinition instanceof OIndexFullText) {
            fullTextIndex = indexDefinition;
            break;
        }
    }
    if (fullTextIndex == null) {
        return null;
    }
    return (Collection<OIdentifiable>) fullTextIndex.get(fieldValue);
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OIndexFullText(com.orientechnologies.orient.core.index.OIndexFullText) Collection(java.util.Collection)

Example 8 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OCommandExecutorSQLAbstract method getInvolvedClustersOfIndex.

protected Set<String> getInvolvedClustersOfIndex(final String iIndexName) {
    final ODatabaseDocumentInternal db = getDatabase();
    final Set<String> clusters = new HashSet<String>();
    final OMetadataInternal metadata = (OMetadataInternal) db.getMetadata();
    final OIndex<?> idx = metadata.getIndexManager().getIndex(iIndexName);
    if (idx != null && idx.getDefinition() != null) {
        final String clazz = idx.getDefinition().getClassName();
        if (clazz != null) {
            final OClass cls = metadata.getImmutableSchemaSnapshot().getClass(clazz);
            if (cls != null)
                for (int clId : cls.getClusterIds()) {
                    final String clName = db.getClusterNameById(clId);
                    if (clName != null)
                        clusters.add(clName.toLowerCase());
                }
        }
    }
    return clusters;
}
Also used : OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) HashSet(java.util.HashSet)

Example 9 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OSQLQuery method run.

/**
   * Delegates to the OQueryExecutor the query execution.
   */
@SuppressWarnings("unchecked")
public List<T> run(final Object... iArgs) {
    final ODatabaseDocumentInternal database = ODatabaseRecordThreadLocal.INSTANCE.get();
    if (database == null)
        throw new OQueryParsingException("No database configured");
    ((OMetadataInternal) database.getMetadata()).makeThreadLocalSchemaSnapshot();
    try {
        setParameters(iArgs);
        return (List<T>) database.getStorage().command(this);
    } finally {
        ((OMetadataInternal) database.getMetadata()).clearThreadLocalSchemaSnapshot();
    }
}
Also used : OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) ArrayList(java.util.ArrayList) List(java.util.List) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 10 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OObjectDatabaseTx method open.

@Override
public <THISDB extends ODatabase> THISDB open(OToken iToken) {
    super.open(iToken);
    entityManager.registerEntityClass(OUser.class);
    entityManager.registerEntityClass(ORole.class);
    metadata = new OMetadataObject((OMetadataInternal) underlying.getMetadata());
    return (THISDB) this;
}
Also used : OMetadataObject(com.orientechnologies.orient.object.metadata.OMetadataObject) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal)

Aggregations

OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)22 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)8 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)2 OCluster (com.orientechnologies.orient.core.storage.OCluster)2 OMetadataObject (com.orientechnologies.orient.object.metadata.OMetadataObject)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 OException (com.orientechnologies.common.exception.OException)1 OSystemException (com.orientechnologies.common.exception.OSystemException)1