core/src/main/java/com/orientechnologies/orient/core/sql/parser/OCreateLinkStatement.java
private Object execute(OCommandContext ctx) {
    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(getSourceClass().getStringValue());
    if (sourceClass == null)
      throw new OCommandExecutionException("Source class '" + getSourceClass().getStringValue() + "' not found");

    final OClass destClass = database.getMetadata().getSchema().getClass(getDestClass().getStringValue());
    if (destClass == null)
      throw new OCommandExecutionException("Destination class '" + getDestClass().getStringValue() + "' not found");

    Object value;

    String cmd = "select from ";
    if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField)) {
      cmd = "select from " + getDestClass() + " where " + destField + " = ";
    }

    List<ODocument> result;
    ODocument target;
    Object oldValue;
    long total = 0;

    String linkName = name == null ? sourceField.getStringValue() : name.getStringValue();

    boolean multipleRelationship;
    OType linkType = OType.valueOf(type.getStringValue().toUpperCase(Locale.ENGLISH));
    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;

    database.declareIntent(new OIntentMassiveInsert());
    try {
      // BROWSE ALL THE RECORDS OF THE SOURCE CLASS
      for (ODocument doc : db.browseClass(sourceClass.getName())) {
        if (breakExec) {
          break;
        }
        value = doc.getProperty(sourceField.getStringValue());

        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 + "'";

            OResultSet rs = database.query(cmd + value);
            result = toList(rs);
            rs.close();

            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.getProperty(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.setProperty(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.setProperty(linkName, value);
              }
              target.save();

            } else {
              // SET THE REFERENCE
              doc.setProperty(linkName, value);
              doc.save();
            }

            total++;
          }
        }
      }

      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);
        }
      }
    } catch (Exception e) {
      throw OException.wrapException(new OCommandExecutionException("Error on creation of links"), e);
    } finally {
      database.declareIntent(null);
    }
    return total;
  }
private Object execute(OCommandContext ctx) {
    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(getSourceClass().getStringValue());
    if (sourceClass == null)
      throw new OCommandExecutionException("Source class '" + getSourceClass().getStringValue() + "' not found");

    final OClass destClass = database.getMetadata().getSchema().getClass(getDestClass().getStringValue());
    if (destClass == null)
      throw new OCommandExecutionException("Destination class '" + getDestClass().getStringValue() + "' not found");

    Object value;

    String cmd = "select from ";
    if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField)) {
      cmd = "select from " + getDestClass() + " where " + destField + " = ";
    }

    List<ODocument> result;
    ODocument target;
    Object oldValue;
    long total = 0;

    String linkName = name == null ? sourceField.getStringValue() : name.getStringValue();

    boolean multipleRelationship;
    OType linkType = OType.valueOf(type.getStringValue().toUpperCase(Locale.ENGLISH));
    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;

    database.declareIntent(new OIntentMassiveInsert());
    try {
      // BROWSE ALL THE RECORDS OF THE SOURCE CLASS
      for (ODocument doc : db.browseClass(sourceClass.getName())) {
        if (breakExec) {
          break;
        }
        value = doc.getProperty(sourceField.getStringValue());

        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 + "'";

            OResultSet rs = database.query(cmd + value);
            result = toList(rs);
            rs.close();

            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.getProperty(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.setProperty(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.setProperty(linkName, value);
              }
              target.save();

            } else {
              // SET THE REFERENCE
              doc.setProperty(linkName, value);
              doc.save();
            }

            total++;
          }
        }
      }

      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);
        }
      }
    } catch (Exception e) {
      throw OException.wrapException(new OCommandExecutionException("Error on creation of links"), e);
    } finally {
      database.declareIntent(null);
    }
    return total;
  }
private Object execute ( OCommandContext ctx ) { 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 ( getSourceClass ( ) . getStringValue ( ) ) ; if ( sourceClass == null ) throw new OCommandExecutionException ( "Source class '" + getSourceClass ( ) . getStringValue ( ) + "' not found" ) ; final OClass destClass = database . getMetadata ( ) . getSchema ( ) . getClass ( getDestClass ( ) . getStringValue ( ) ) ; if ( destClass == null ) throw new OCommandExecutionException ( "Destination class '" + getDestClass ( ) . getStringValue ( ) + "' not found" ) ; Object value ; String cmd = "select from " ; if ( ! ODocumentHelper . ATTRIBUTE_RID . equals ( destField ) ) { cmd = "select from " + getDestClass ( ) + " where " + destField + " = " ; } List < ODocument > result ; ODocument target ; Object oldValue ; long total = 0 ; String linkName = name == null ? sourceField . getStringValue ( ) : name . getStringValue ( ) ; boolean multipleRelationship ; OType linkType = OType . valueOf ( type . getStringValue ( ) . toUpperCase ( Locale . ENGLISH ) ) ; 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 ; database . declareIntent ( new OIntentMassiveInsert ( ) ) ; try { // BROWSE ALL THE RECORDS OF THE SOURCE CLASS for ( ODocument doc : db . browseClass ( sourceClass . getName ( ) ) ) { if ( breakExec ) { break ; } value = doc . getProperty ( sourceField . getStringValue ( ) ) ; 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 + "'" ; OResultSet rs = database . query ( cmd + value ) ; result = toList ( rs ) ; rs . close ( ) ; 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 . getProperty ( 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 . setProperty ( 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 . setProperty ( linkName , value ) ; } target . save ( ) ; } else { // SET THE REFERENCE doc . setProperty ( linkName , value ) ; doc . save ( ) ; } total ++ ; } } } 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 ) ; } } } catch ( Exception e ) { throw OException . wrapException ( new OCommandExecutionException ( "Error on creation of links" ) , e ) ; } finally { database . declareIntent ( null ) ; } return total ; }
Execute the CREATE LINK.
Execute the CREATE LINK .