<commit_before>/*
 * Copyright 2010-2012 <NAME> (l.garulli--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.
 */
package com.orientechnologies.orient.core.sql;

import java.util.Map;

import com.orientechnologies.orient.core.command.OCommandDistributedReplicateRequest;
import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.command.OCommandRequestText;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OClassImpl;
import com.orientechnologies.orient.core.metadata.schema.OSchemaProxy;
import com.orientechnologies.orient.core.metadata.security.ODatabaseSecurityResources;
import com.orientechnologies.orient.core.metadata.security.ORole;

/**
 * SQL CREATE PROPERTY command: Creates a new property in the target class.
 * 
 * @author <NAME>
 * 
 */
@SuppressWarnings("unchecked")
public class OCommandExecutorSQLCreateClass extends OCommandExecutorSQLAbstract implements OCommandDistributedReplicateRequest {
  public static final String KEYWORD_CREATE   = "CREATE";
  public static final String KEYWORD_CLASS    = "CLASS";
  public static final String KEYWORD_EXTENDS  = "EXTENDS";
  public static final String KEYWORD_ABSTRACT = "ABSTRACT";
  public static final String KEYWORD_CLUSTER  = "CLUSTER";

  private String             className;
  private OClass             superClass;
  private int[]              clusterIds;

  public OCommandExecutorSQLCreateClass parse(final OCommandRequest iRequest) {
    final ODatabaseRecord database = getDatabase();
    database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);

    init(((OCommandRequestText) iRequest).getText());

    StringBuilder word = new StringBuilder();

    int oldPos = 0;
    int pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
    if (pos == -1 || !word.toString().equals(KEYWORD_CREATE))
      throw new OCommandSQLParsingException("Keyword " + KEYWORD_CREATE + " not found. Use " + getSyntax(), parserText, oldPos);

    oldPos = pos;
    pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
    if (pos == -1 || !word.toString().equals(KEYWORD_CLASS))
      throw new OCommandSQLParsingException("Keyword " + KEYWORD_CLASS + " not found. Use " + getSyntax(), parserText, oldPos);

    oldPos = pos;
    pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false);
    if (pos == -1)
      throw new OCommandSQLParsingException("Expected <class>", parserText, oldPos);

    className = word.toString();
    if (className == null)
      throw new OCommandSQLParsingException("Class " + className + " already exists", parserText, oldPos);

    oldPos = pos;

    while ((pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true)) > -1) {
      final String k = word.toString();
      if (k.equals(KEYWORD_EXTENDS)) {
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false);
        if (pos == -1)
          throw new OCommandSQLParsingException("Syntax error after EXTENDS for class " + className
              + ". Expected the super-class name. Use " + getSyntax(), parserText, oldPos);

        if (!database.getMetadata().getSchema().existsClass(word.toString()))
          throw new OCommandSQLParsingException("Super-class " + word + " not exists", parserText, oldPos);

        superClass = database.getMetadata().getSchema().getClass(word.toString());
      } else if (k.equals(KEYWORD_CLUSTER)) {
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false, " =><()");
        if (pos == -1)
          throw new OCommandSQLParsingException("Syntax error after CLUSTER for class " + className
              + ". Expected the cluster id or name. Use " + getSyntax(), parserText, oldPos);

        final String[] clusterIdsAsStrings = word.toString().split(",");
        if (clusterIdsAsStrings.length > 0) {
          clusterIds = new int[clusterIdsAsStrings.length];
          for (int i = 0; i < clusterIdsAsStrings.length; ++i) {
            if (Character.isDigit(clusterIdsAsStrings[i].charAt(0)))
              // GET CLUSTER ID FROM NAME
              clusterIds[i] = Integer.parseInt(clusterIdsAsStrings[i]);
            else
              // GET CLUSTER ID
              clusterIds[i] = database.getStorage().getClusterIdByName(clusterIdsAsStrings[i]);

            if (clusterIds[i] == -1)
              throw new OCommandSQLParsingException("Cluster with id " + clusterIds[i] + " does not exists", parserText, oldPos);

            try {
              database.getStorage().getClusterById(clusterIds[i]);
            } catch (IllegalArgumentException e) {
              throw new OCommandSQLParsingException("Cluster with id " + clusterIds[i] + " does not exists", parserText, oldPos);<commit_msg>Minor: fixed bugs reported by Pawel about a more strict syntax check in CREATE CLASS command<commit_after>/*
 * Copyright 2010-2012 <NAME> (l.garulli--at--orient<EMAIL>)
 *
 * 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.
 */
package com.orientechnologies.orient.core.sql;

import java.util.Map;

import com.orientechnologies.orient.core.command.OCommandDistributedReplicateRequest;
import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.command.OCommandRequestText;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OClassImpl;
import com.orientechnologies.orient.core.metadata.schema.OSchemaProxy;
import com.orientechnologies.orient.core.metadata.security.ODatabaseSecurityResources;
import com.orientechnologies.orient.core.metadata.security.ORole;

/**
 * SQL CREATE PROPERTY command: Creates a new property in the target class.
 * 
 * @author <NAME>
 * 
 */
@SuppressWarnings("unchecked")
public class OCommandExecutorSQLCreateClass extends OCommandExecutorSQLAbstract implements OCommandDistributedReplicateRequest {
  public static final String KEYWORD_CREATE   = "CREATE";
  public static final String KEYWORD_CLASS    = "CLASS";
  public static final String KEYWORD_EXTENDS  = "EXTENDS";
  public static final String KEYWORD_ABSTRACT = "ABSTRACT";
  public static final String KEYWORD_CLUSTER  = "CLUSTER";

  private String             className;
  private OClass             superClass;
  private int[]              clusterIds;

  public OCommandExecutorSQLCreateClass parse(final OCommandRequest iRequest) {
    final ODatabaseRecord database = getDatabase();
    database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);

    init(((OCommandRequestText) iRequest).getText());

    StringBuilder word = new StringBuilder();

    int oldPos = 0;
    int pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
    if (pos == -1 || !word.toString().equals(KEYWORD_CREATE))
      throw new OCommandSQLParsingException("Keyword " + KEYWORD_CREATE + " not found. Use " + getSyntax(), parserText, oldPos);

    oldPos = pos;
    pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
    if (pos == -1 || !word.toString().equals(KEYWORD_CLASS))
      throw new OCommandSQLParsingException("Keyword " + KEYWORD_CLASS + " not found. Use " + getSyntax(), parserText, oldPos);

    oldPos = pos;
    pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false);
    if (pos == -1)
      throw new OCommandSQLParsingException("Expected <class>", parserText, oldPos);

    className = word.toString();
    if (className == null)
      throw new OCommandSQLParsingException("Class " + className + " already exists", parserText, oldPos);

    oldPos = pos;

    while ((pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true)) > -1) {
      final String k = word.toString();
      if (k.equals(KEYWORD_EXTENDS)) {
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false);
        if (pos == -1)
          throw new OCommandSQLParsingException("Syntax error after EXTENDS for class " + className
              + ". Expected the super-class name. Use " + getSyntax(), parserText, oldPos);

        if (!database.getMetadata().getSchema().existsClass(word.toString()))
          throw new OCommandSQLParsingException("Super-class " + word + " not exists", parserText, oldPos);

        superClass = database.getMetadata().getSchema().getClass(word.toString());
      } else if (k.equals(KEYWORD_CLUSTER)) {
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false, " =><()");
        if (pos == -1)
          throw new OCommandSQLParsingException("Syntax error after CLUSTER for class " + className
              + ". Expected the cluster id or name. Use " + getSyntax(), parserText, oldPos);

        final String[] clusterIdsAsStrings = word.toString().split(",");
        if (clusterIdsAsStrings.length > 0) {
          clusterIds = new int[clusterIdsAsStrings.length];
          for (int i = 0; i < clusterIdsAsStrings.length; ++i) {
            if (Character.isDigit(clusterIdsAsStrings[i].charAt(0)))
              // GET CLUSTER ID FROM NAME
              clusterIds[i] = Integer.parseInt(clusterIdsAsStrings[i]);
            else
              // GET CLUSTER ID
              clusterIds[i] = database.getStorage().getClusterIdByName(clusterIdsAsStrings[i]);

            if (clusterIds[i] == -1)
              throw new OCommandSQLParsingException("Cluster with id " + clusterIds[i] + " does not exists", parserText, oldPos);

            try {
              database.getStorage().getClusterById(clusterIds[i]);
            } catch (Exception e) {
              throw new OCommandSQLParsingException("Cluster with id " + clusterIds[i] + " does not exists", parserText, oldPos);
{'fraction_non_alphanumeric': 0.10492390331244404, 'fraction_numerical': 0.004118173679498657, 'mean_word_length': 3.6526447313619324, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6652399', 'n_tokens_mistral': 3104, 'n_tokens_neox': 3062, 'n_words': 838}