Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   *  *  Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
   *  *
   *  *  Licensed under the Apache License, Version 2.0 (the "License");
   *  *  you may not use this file except in compliance with the License.
   *  *  You may obtain a copy of the License at
   *  *
   *  *       http://www.apache.org/licenses/LICENSE-2.0
  *  *
  *  *  Unless required by applicable law or agreed to in writing, software
  *  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  *  See the License for the specific language governing permissions and
  *  *  limitations under the License.
  *  *
  *  * For more information: http://www.orientechnologies.com
  *
  */
 package com.orientechnologies.orient.graph.sql;
 
 
 import java.util.Map;
 import java.util.Set;

SQL DELETE EDGE command.

Author(s):
Luca Garulli
 
   public static final String NAME         = "DELETE EDGE";
   private ORecordId          rid;
   private String             fromExpr;
   private String             toExpr;
   private int                removed      = 0;
   private OCommandRequest    query;
   private OSQLFilter         compiledFilter;
   private OrientGraph        graph;
   private String             label;
 
   @SuppressWarnings("unchecked")
   public OCommandExecutorSQLDeleteEdge parse(final OCommandRequest iRequest) {
     init((OCommandRequestTextiRequest);
 
     parserRequiredKeyword("DELETE");
     parserRequiredKeyword("EDGE");
 
     OClass clazz = null;
     String where = null;
 
     String temp = parseOptionalWord(true);
     String originalTemp = null;
 
     if (temp != null && !parserIsEnded())
 
     final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
     final OrientGraph graph = OGraphCommandExecutorSQLFactory.getGraph(falseshutdownFlag);
     try {
       while (temp != null) {
 
         if (temp.equals("FROM")) {
            = parserRequiredWord(false"Syntax error"" =><,\r\n");
           if ( != null)
             throwSyntaxErrorException("FROM '" +  + "' is not allowed when specify a RID (" +  + ")");
 
         } else if (temp.equals("TO")) {
           = parserRequiredWord(false"Syntax error"" =><,\r\n");
          if ( != null)
            throwSyntaxErrorException("TO '" +  + "' is not allowed when specify a RID (" +  + ")");
        } else if (temp.startsWith("#")) {
           = new ORecordId(temp);
          if ( != null ||  != null)
            throwSyntaxErrorException("Specifying the RID " +  + " is not allowed with FROM/TO");
        } else if (temp.equals()) {
          if (clazz == null)
            // ASSIGN DEFAULT CLASS
            clazz = graph.getEdgeType(.);
          where = parserGetCurrentPosition() > -1 ? " " + .substring(parserGetCurrentPosition()) : "";
           = OSQLEngine.getInstance().parseCondition(wheregetContext(), );
          break;
        } else if (temp.equals()) {
          parseRetry();
        } else if (temp.length() > 0) {
          // GET/CHECK CLASS NAME
           = originalTemp;
          clazz = graph.getEdgeType(temp);
          if (clazz == null)
            throw new OCommandSQLParsingException("Class '" + temp + " was not found");
        }
        temp = parseOptionalWord(true);
        if (parserIsEnded())
          break;
      }
      if (where == null)
        where = "";
      else
        where = " WHERE " + where;
      if ( == null &&  == null &&  == null)
        if (clazz == null)
          // DELETE ALL THE EDGES
           = graph.getRawGraph().command(new OSQLAsynchQuery<ODocument>("select from E" + wherethis));
        else
          // DELETE EDGES OF CLASS X
           = graph.getRawGraph().command(new OSQLAsynchQuery<ODocument>("select from " + clazz.getName() + wherethis));
      return this;
    } finally {
      if (shutdownFlag.getValue())
        graph.shutdown(false);
    }
  }

  
Execute the command and return the ODocument object created.
  public Object execute(final Map<ObjectObjectiArgs) {
    if ( == null &&  == null &&  == null &&  == null &&  == null)
      throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    for (int r = 0; r < ; ++r) {
      try {
        if ( != null) {
          // REMOVE PUNCTUAL RID
          OGraphCommandExecutorSQLFactory.runInTx(new OGraphCommandExecutorSQLFactory.GraphCallBack<Object>() {
            @Override
            public Object call(OrientBaseGraph graph) {
              final OrientEdge e = graph.getEdge();
              if (e != null) {
                e.remove();
                 = 1;
              }
              return null;
            }
          });
        } else {
          // MULTIPLE EDGES
          final Set<OrientEdgeedges = new HashSet<OrientEdge>();
          if ( == null) {
            OGraphCommandExecutorSQLFactory.runInTx(new OGraphCommandExecutorSQLFactory.GraphCallBack<Object>() {
              @Override
              public Object call(OrientBaseGraph graph) {
                Set<OIdentifiablefromIds = null;
                if ( != null)
                  fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), iArgs);
                Set<OIdentifiabletoIds = null;
                if ( != null)
                  toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), iArgs);
                if (fromIds != null && toIds != null) {
                  // REMOVE ALL THE EDGES BETWEEN VERTICES
                  for (OIdentifiable fromId : fromIds) {
                    final OrientVertex v = graph.getVertex(fromId);
                    if (v != null)
                      for (Edge e : v.getEdges(.)) {
                        if ( != null && !.equals(e.getLabel()))
                          continue;
                        final OIdentifiable inV = ((OrientEdgee).getInVertex();
                        if (inV != null && toIds.contains(inV.getIdentity()))
                          edges.add((OrientEdgee);
                      }
                  }
                } else if (fromIds != null) {
                  // REMOVE ALL THE EDGES THAT START FROM A VERTEXES
                  for (OIdentifiable fromId : fromIds) {
                    final OrientVertex v = graph.getVertex(fromId);
                    if (v != null) {
                      for (Edge e : v.getEdges(.)) {
                        if ( != null && !.equals(e.getLabel()))
                          continue;
                        edges.add((OrientEdgee);
                      }
                    }
                  }
                } else if (toIds != null) {
                  // REMOVE ALL THE EDGES THAT ARRIVE TO A VERTEXES
                  for (OIdentifiable toId : toIds) {
                    final OrientVertex v = graph.getVertex(toId);
                    if (v != null) {
                      for (Edge e : v.getEdges(.)) {
                        if ( != null && !.equals(e.getLabel()))
                          continue;
                        edges.add((OrientEdgee);
                      }
                    }
                  }
                } else
                  throw new OCommandExecutionException("Invalid target: " + toIds);
                if ( != null) {
                  // ADDITIONAL FILTERING
                  for (Iterator<OrientEdgeit = edges.iterator(); it.hasNext();) {
                    final OrientEdge edge = it.next();
                    if (!(Boolean.evaluate((ODocumentedge.getRecord(), null))
                      it.remove();
                  }
                }
                // DELETE THE FOUND EDGES
                 = edges.size();
                for (OrientEdge edge : edges)
                  edge.remove();
                return null;
              }
            });
          } else {
             = OGraphCommandExecutorSQLFactory.getGraph(false);
            OGraphCommandExecutorSQLFactory.runInTx(new OGraphCommandExecutorSQLFactory.GraphCallBack<Object>() {
              @Override
              public Object call(OrientBaseGraph graph) {
                // TARGET IS A CLASS + OPTIONAL CONDITION
                return .execute(iArgs);
              }
            });
          }
        }
        break;
      } catch (OConcurrentModificationException e) {
        if (r + 1 >= )
          // NO RETRY; PROPAGATE THE EXCEPTION
          throw e;
        // RETRY?
        if ( > 0)
          try {
            Thread.sleep();
          } catch (InterruptedException e1) {
          }
      }
    }
    return ;
  }

  
Delete the current edge.
  public boolean result(final Object iRecord) {
    final OIdentifiable id = (OIdentifiableiRecord;
    if ( != null) {
      // ADDITIONAL FILTERING
      if (!(Boolean.evaluate((ODocumentid.getRecord(), null))
        return true;
    }
    if (id.getIdentity().isValid()) {
      final OrientEdge e = .getEdge(id);
      if (e != null) {
        e.remove();
        ++;
      }
    }
    return true;
  }
  public String getSyntax() {
    return "DELETE EDGE <rid>|FROM <rid>|TO <rid>|<[<class>] [WHERE <conditions>]>";
  }
  public void end() {
    if ( != null && .getValue())
      .shutdown(false);
  }
  public int getSecurityOperationType() {
    return .;
  }
New to GrepCode? Check out our FAQ X