Пример #1
0
  private void configureDocument(
      ORecordAbstract<?> document, ODatabaseRecord db, DefinitionGroup definition) {
    // configure document

    // as of OrientDB 1.0rc8 the database may no longer be set on the
    // document
    // instead the current database can be set using
    // ODatabaseRecordThreadLocal.INSTANCE.set(db);
    //		document.setDatabase(db);
    if (document instanceof ODocument) {
      // reset class name
      ODocument doc = (ODocument) document;
      /*
       * Attention: Two long class names cause problems as file names will
       * be based on them.
       */
      String className = null;
      if (definition != null) {
        className = ONamespaceMap.encode(determineName(definition));
      } else if (doc.containsField(OSerializationHelper.BINARY_WRAPPER_FIELD)
          || doc.containsField(OSerializationHelper.FIELD_SERIALIZATION_TYPE)) {
        className = OSerializationHelper.BINARY_WRAPPER_CLASSNAME;
      }

      if (className != null) {
        OSchema schema = db.getMetadata().getSchema();
        if (!schema.existsClass(className)) {
          // if the class doesn't exist yet, create a physical cluster
          // manually for it
          int cluster = db.addCluster(className, CLUSTER_TYPE.PHYSICAL);
          schema.createClass(className, cluster);
        }
        doc.setClassName(className);
      }

      // configure children
      for (Entry<String, Object> field : doc) {
        List<ODocument> docs = new ArrayList<ODocument>();
        List<ORecordAbstract<?>> recs = new ArrayList<ORecordAbstract<?>>();
        if (field.getValue() instanceof Collection<?>) {
          for (Object value : (Collection<?>) field.getValue()) {
            if (value instanceof ODocument && !getSpecialFieldNames().contains(field.getKey())) {
              docs.add((ODocument) value);
            } else if (value instanceof ORecordAbstract<?>) {
              recs.add((ORecordAbstract<?>) value);
            }
          }
        } else if (field.getValue() instanceof ODocument
            && !getSpecialFieldNames().contains(field.getKey())) {
          docs.add((ODocument) field.getValue());
        } else if (field.getValue() instanceof ORecordAbstract<?>) {
          recs.add((ORecordAbstract<?>) field.getValue());
        }

        if (definition != null) {
          for (ODocument valueDoc : docs) {
            ChildDefinition<?> child = definition.getChild(decodeProperty(field.getKey()));
            DefinitionGroup childGroup;
            if (child.asProperty() != null) {
              childGroup = child.asProperty().getPropertyType();
            } else if (child.asGroup() != null) {
              childGroup = child.asGroup();
            } else {
              throw new IllegalStateException(
                  "Document is associated neither with a property nor a property group.");
            }
            configureDocument(valueDoc, db, childGroup);
          }
        }

        for (ORecordAbstract<?> fieldRec : recs) {
          configureDocument(fieldRec, db, null);
        }
      }
    }
  }
  @SuppressWarnings("unchecked")
  public OCommandExecutorSQLInsert parse(final OCommandRequest iRequest) {
    final ODatabaseRecord database = getDatabase();
    database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);

    init((OCommandRequestText) iRequest);

    className = null;
    newRecords = null;
    content = null;

    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());
    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 = database.getMetadata().getSchema().getClass(subjectName);
      if (cls == null) throwParsingException("Class " + subjectName + " not found in database");

      className = cls.getName();
    }

    parserSkipWhiteSpaces();
    if (parserIsEnded())
      throwSyntaxErrorException(
          "Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");

    final String temp = parseOptionalWord(true);
    if (temp.equals("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>>();
    if (parserGetCurrentChar() == '(') {
      parseValues();
    } else {
      parserNextWord(true, " ,\r\n");

      if (parserGetLastWord().equals(KEYWORD_CONTENT)) {
        newRecords = null;
        parseContent();
      } else if (parserGetLastWord().equals(KEYWORD_SET)) {
        final LinkedHashMap<String, Object> fields = new LinkedHashMap<String, Object>();
        newRecords.add(fields);
        parseSetFields(fields);
      }
    }

    return this;
  }