Search in sources :

Example 1 with OIdentifier

use of com.orientechnologies.orient.core.sql.parser.OIdentifier in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateClass method parse.

public OCommandExecutorSQLCreateClass parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        queryText = preParse(queryText, iRequest);
        textRequest.setText(queryText);
        final ODatabaseDocumentInternal database = getDatabase();
        init((OCommandRequestText) iRequest);
        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 (this.preParsedStatement != null) {
            className = ((OCreateClassStatement) preParsedStatement).name.getStringValue();
        }
        if (className == null)
            throw new OCommandSQLParsingException("Expected <class>", parserText, oldPos);
        oldPos = pos;
        while ((pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true)) > -1) {
            final String k = word.toString();
            if (k.equals(KEYWORD_EXTENDS)) {
                boolean hasNext;
                boolean newParser = this.preParsedStatement != null;
                OClass superClass;
                do {
                    oldPos = pos;
                    pos = nextWord(parserText, parserTextUpperCase, pos, word, false);
                    if (pos == -1)
                        throw new OCommandSQLParsingException("Syntax error after EXTENDS for class " + className + ". Expected the super-class name. Use " + getSyntax(), parserText, oldPos);
                    String superclassName = decodeClassName(word.toString());
                    if (!database.getMetadata().getSchema().existsClass(superclassName) && !newParser)
                        throw new OCommandSQLParsingException("Super-class " + word + " not exists", parserText, oldPos);
                    superClass = database.getMetadata().getSchema().getClass(superclassName);
                    superClasses.add(superClass);
                    hasNext = false;
                    for (; pos < parserText.length(); pos++) {
                        char ch = parserText.charAt(pos);
                        if (ch == ',')
                            hasNext = true;
                        else if (Character.isLetterOrDigit(ch))
                            break;
                    }
                } while (hasNext);
                if (newParser) {
                    OCreateClassStatement statement = (OCreateClassStatement) this.preParsedStatement;
                    List<OIdentifier> superclasses = statement.getSuperclasses();
                    this.superClasses.clear();
                    for (OIdentifier superclass : superclasses) {
                        String superclassName = superclass.getStringValue();
                        if (!database.getMetadata().getSchema().existsClass(superclassName))
                            throw new OCommandSQLParsingException("Super-class " + word + " not exists", parserText, oldPos);
                        superClass = database.getMetadata().getSchema().getClass(superclassName);
                        this.superClasses.add(superClass);
                    }
                }
            } 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);
                        }
                    }
                }
            } else if (k.equals(KEYWORD_CLUSTERS)) {
                oldPos = pos;
                pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false, " =><()");
                if (pos == -1)
                    throw new OCommandSQLParsingException("Syntax error after CLUSTERS for class " + className + ". Expected the number of clusters. Use " + getSyntax(), parserText, oldPos);
                clusters = Integer.parseInt(word.toString());
            } else if (k.equals(KEYWORD_ABSTRACT)) {
                clusterIds = new int[] { -1 };
            } else if (k.equals(KEYWORD_IF)) {
                oldPos = pos;
                pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false, " =><()");
                if (!word.toString().equalsIgnoreCase(KEYWORD_NOT)) {
                    throw new OCommandSQLParsingException("Syntax error after IF for class " + className + ". Expected NOT. Use " + getSyntax(), parserText, oldPos);
                }
                oldPos = pos;
                pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false, " =><()");
                if (!word.toString().equalsIgnoreCase(KEYWORD_EXISTS)) {
                    throw new OCommandSQLParsingException("Syntax error after IF NOT for class " + className + ". Expected EXISTS. Use " + getSyntax(), parserText, oldPos);
                }
                ifNotExists = true;
            } else
                throw new OCommandSQLParsingException("Invalid keyword: " + k);
            oldPos = pos;
        }
        if (clusterIds == null) {
            final int clusterId = database.getStorage().getClusterIdByName(className);
            if (clusterId > -1) {
                clusterIds = new int[] { clusterId };
            }
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCreateClassStatement(com.orientechnologies.orient.core.sql.parser.OCreateClassStatement) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OIdentifier(com.orientechnologies.orient.core.sql.parser.OIdentifier) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 2 with OIdentifier

use of com.orientechnologies.orient.core.sql.parser.OIdentifier in project orientdb by orientechnologies.

the class OCommandExecutorSQLTruncateCluster method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLTruncateCluster 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);
        StringBuilder word = new StringBuilder();
        int oldPos = 0;
        int pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_TRUNCATE))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_TRUNCATE + " not found. Use " + getSyntax(), parserText, oldPos);
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_CLUSTER))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_CLUSTER + " not found. Use " + getSyntax(), parserText, oldPos);
        oldPos = pos;
        pos = nextWord(parserText, parserText, oldPos, word, true);
        if (pos == -1)
            throw new OCommandSQLParsingException("Expected cluster name. Use " + getSyntax(), parserText, oldPos);
        clusterName = decodeClusterName(word.toString());
        if (preParsedStatement != null) {
            // new parser, this will be removed and implemented with the new executor
            OIdentifier name = ((OTruncateClusterStatement) preParsedStatement).clusterName;
            if (name != null) {
                clusterName = name.getStringValue();
            }
        }
        final ODatabaseDocument database = getDatabase();
        if (database.getClusterIdByName(clusterName) == -1)
            throw new OCommandSQLParsingException("Cluster '" + clusterName + "' not found", parserText, oldPos);
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OTruncateClusterStatement(com.orientechnologies.orient.core.sql.parser.OTruncateClusterStatement) OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OIdentifier(com.orientechnologies.orient.core.sql.parser.OIdentifier) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument)

Aggregations

OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)2 OIdentifier (com.orientechnologies.orient.core.sql.parser.OIdentifier)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OCreateClassStatement (com.orientechnologies.orient.core.sql.parser.OCreateClassStatement)1 OTruncateClusterStatement (com.orientechnologies.orient.core.sql.parser.OTruncateClusterStatement)1