Search in sources :

Example 51 with OCommandExecutionException

use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateCluster method execute.

/**
   * Execute the CREATE CLUSTER.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (clusterName == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocument database = getDatabase();
    final int clusterId = database.getClusterIdByName(clusterName);
    if (clusterId > -1)
        throw new OCommandSQLParsingException("Cluster '" + clusterName + "' already exists");
    if (blob) {
        if (requestedId == -1) {
            return database.addBlobCluster(clusterName);
        } else {
            throw new OCommandExecutionException("Request id not supported by blob cluster creation.");
        }
    } else {
        if (requestedId == -1) {
            return database.addCluster(clusterName);
        } else {
            return database.addCluster(clusterName, requestedId, null);
        }
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 52 with OCommandExecutionException

use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateFunction method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLCreateFunction parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        queryText = preParse(queryText, iRequest);
        textRequest.setText(queryText);
        init((OCommandRequestText) iRequest);
        parserRequiredKeyword("CREATE");
        parserRequiredKeyword("FUNCTION");
        name = parserNextWord(false);
        code = OIOUtils.getStringContent(parserNextWord(false));
        String temp = parseOptionalWord(true);
        while (temp != null) {
            if (temp.equals("IDEMPOTENT")) {
                parserNextWord(false);
                idempotent = Boolean.parseBoolean(parserGetLastWord());
            } else if (temp.equals("LANGUAGE")) {
                parserNextWord(false);
                language = parserGetLastWord();
            } else if (temp.equals("PARAMETERS")) {
                parserNextWord(false);
                parameters = new ArrayList<String>();
                OStringSerializerHelper.getCollection(parserGetLastWord(), 0, parameters);
                if (parameters.size() == 0)
                    throw new OCommandExecutionException("Syntax Error. Missing function parameter(s): " + getSyntax());
            }
            temp = parserOptionalWord(true);
            if (parserIsEnded())
                break;
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 53 with OCommandExecutionException

use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateFunction method execute.

/**
   * Execute the command and return the ODocument object created.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (name == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    if (name.isEmpty())
        throw new OCommandExecutionException("Syntax Error. You must specify a function name: " + getSyntax());
    if (code == null || code.isEmpty())
        throw new OCommandExecutionException("Syntax Error. You must specify the function code: " + getSyntax());
    ODatabaseDocument database = getDatabase();
    final OFunction f = database.getMetadata().getFunctionLibrary().createFunction(name);
    f.setCode(code);
    f.setIdempotent(idempotent);
    if (parameters != null)
        f.setParameters(parameters);
    if (language != null)
        f.setLanguage(language);
    f.save();
    return f.getId();
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Example 54 with OCommandExecutionException

use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateLink method execute.

/**
   * Execute the CREATE LINK.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (destField == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocumentInternal database = getDatabase();
    if (!(database.getDatabaseOwner() instanceof ODatabaseDocument))
        throw new OCommandSQLParsingException("This command supports only the database type ODatabaseDocumentTx and type '" + database.getClass() + "' was found");
    final ODatabaseDocument db = (ODatabaseDocument) database.getDatabaseOwner();
    final OClass sourceClass = database.getMetadata().getSchema().getClass(sourceClassName);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + sourceClassName + "' not found");
    final OClass destClass = database.getMetadata().getSchema().getClass(destClassName);
    if (destClass == null)
        throw new OCommandExecutionException("Destination class '" + destClassName + "' not found");
    Object value;
    String cmd = "select from ";
    if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField)) {
        cmd = "select from " + destClassName + " where " + destField + " = ";
    }
    List<ODocument> result;
    ODocument target;
    Object oldValue;
    long total = 0;
    if (linkName == null)
        // NO LINK NAME EXPRESSED: OVERWRITE THE SOURCE FIELD
        linkName = sourceField;
    boolean multipleRelationship;
    if (linkType != null)
        // DETERMINE BASED ON FORCED TYPE
        multipleRelationship = linkType == OType.LINKSET || linkType == OType.LINKLIST;
    else
        multipleRelationship = false;
    long totRecords = db.countClass(sourceClass.getName());
    long currRecord = 0;
    if (progressListener != null)
        progressListener.onBegin(this, totRecords, false);
    database.declareIntent(new OIntentMassiveInsert());
    try {
        // BROWSE ALL THE RECORDS OF THE SOURCE CLASS
        for (ODocument doc : db.browseClass(sourceClass.getName())) {
            value = doc.field(sourceField);
            if (value != null) {
                if (value instanceof ODocument || value instanceof ORID) {
                // ALREADY CONVERTED
                } else if (value instanceof Collection<?>) {
                // TODO
                } else {
                    // SEARCH THE DESTINATION RECORD
                    target = null;
                    if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField) && value instanceof String)
                        if (((String) value).length() == 0)
                            value = null;
                        else
                            value = "'" + value + "'";
                    result = database.<OCommandRequest>command(new OSQLSynchQuery<ODocument>(cmd + value)).execute();
                    if (result == null || result.size() == 0)
                        value = null;
                    else if (result.size() > 1)
                        throw new OCommandExecutionException("Cannot create link because multiple records was found in class '" + destClass.getName() + "' with value " + value + " in field '" + destField + "'");
                    else {
                        target = result.get(0);
                        value = target;
                    }
                    if (target != null && inverse) {
                        // INVERSE RELATIONSHIP
                        oldValue = target.field(linkName);
                        if (oldValue != null) {
                            if (!multipleRelationship)
                                multipleRelationship = true;
                            Collection<ODocument> coll;
                            if (oldValue instanceof Collection) {
                                // ADD IT IN THE EXISTENT COLLECTION
                                coll = (Collection<ODocument>) oldValue;
                                target.setDirty();
                            } else {
                                // CREATE A NEW COLLECTION FOR BOTH
                                coll = new ArrayList<ODocument>(2);
                                target.field(linkName, coll);
                                coll.add((ODocument) oldValue);
                            }
                            coll.add(doc);
                        } else {
                            if (linkType != null)
                                if (linkType == OType.LINKSET) {
                                    value = new ORecordLazySet(target);
                                    ((Set<OIdentifiable>) value).add(doc);
                                } else if (linkType == OType.LINKLIST) {
                                    value = new ORecordLazyList(target);
                                    ((ORecordLazyList) value).add(doc);
                                } else
                                    // IGNORE THE TYPE, SET IT AS LINK
                                    value = doc;
                            else
                                value = doc;
                            target.field(linkName, value);
                        }
                        target.save();
                    } else {
                        // SET THE REFERENCE
                        doc.field(linkName, value);
                        doc.save();
                    }
                    total++;
                }
            }
            if (progressListener != null)
                progressListener.onProgress(this, currRecord, currRecord * 100f / totRecords);
        }
        if (total > 0) {
            if (inverse) {
                // REMOVE THE OLD PROPERTY IF ANY
                OProperty prop = destClass.getProperty(linkName);
                if (prop != null)
                    destClass.dropProperty(linkName);
                if (linkType == null)
                    linkType = multipleRelationship ? OType.LINKSET : OType.LINK;
                // CREATE THE PROPERTY
                destClass.createProperty(linkName, linkType, sourceClass);
            } else {
                // REMOVE THE OLD PROPERTY IF ANY
                OProperty prop = sourceClass.getProperty(linkName);
                if (prop != null)
                    sourceClass.dropProperty(linkName);
                // CREATE THE PROPERTY
                sourceClass.createProperty(linkName, OType.LINK, destClass);
            }
        }
        if (progressListener != null)
            progressListener.onCompletition(this, true);
    } catch (Exception e) {
        if (progressListener != null)
            progressListener.onCompletition(this, false);
        throw OException.wrapException(new OCommandExecutionException("Error on creation of links"), e);
    } finally {
        database.declareIntent(null);
    }
    return total;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ArrayList(java.util.ArrayList) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) OException(com.orientechnologies.common.exception.OException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Collection(java.util.Collection) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 55 with OCommandExecutionException

use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateProperty method execute.

/**
   * Execute the CREATE PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (type == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocument database = getDatabase();
    final OClassImpl sourceClass = (OClassImpl) database.getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    OPropertyImpl prop = (OPropertyImpl) sourceClass.getProperty(fieldName);
    if (prop != null) {
        if (ifNotExists) {
            return sourceClass.properties().size();
        }
        throw new OCommandExecutionException("Property '" + className + "." + fieldName + "' already exists. Remove it before to retry.");
    }
    // CREATE THE PROPERTY
    OClass linkedClass = null;
    OType linkedType = null;
    if (linked != null) {
        // FIRST SEARCH BETWEEN CLASSES
        linkedClass = database.getMetadata().getSchema().getClass(linked);
        if (linkedClass == null)
            // NOT FOUND: SEARCH BETWEEN TYPES
            linkedType = OType.valueOf(linked.toUpperCase(Locale.ENGLISH));
    }
    // CREATE IT LOCALLY
    OPropertyImpl internalProp = sourceClass.addPropertyInternal(fieldName, type, linkedType, linkedClass, unsafe);
    boolean toSave = false;
    if (readonly) {
        internalProp.setReadonly(true);
        toSave = true;
    }
    if (mandatory) {
        internalProp.setMandatory(true);
        toSave = true;
    }
    if (notnull) {
        internalProp.setNotNull(true);
        toSave = true;
    }
    if (max != null) {
        internalProp.setMax(max);
        toSave = true;
    }
    if (min != null) {
        internalProp.setMin(min);
        toSave = true;
    }
    if (defaultValue != null) {
        internalProp.setDefaultValue(defaultValue);
        toSave = true;
    }
    if (collate != null) {
        internalProp.setCollate(collate);
        toSave = true;
    }
    if (regex != null) {
        internalProp.setRegexp(regex);
        toSave = true;
    }
    if (toSave) {
        internalProp.save();
    }
    return sourceClass.properties().size();
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)

Aggregations

OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)82 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)20 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)19 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)16 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)15 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 OException (com.orientechnologies.common.exception.OException)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)9 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)6 IOException (java.io.IOException)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)5 OType (com.orientechnologies.orient.core.metadata.schema.OType)5 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)4 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)4 ORecord (com.orientechnologies.orient.core.record.ORecord)4 OHazelcastPlugin (com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin)4 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 ArrayList (java.util.ArrayList)4 ORID (com.orientechnologies.orient.core.id.ORID)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3