Search in sources :

Example 11 with OCommandRequestText

use of com.orientechnologies.orient.core.command.OCommandRequestText in project orientdb by orientechnologies.

the class OCommandExecutorSQLAlterProperty method parse.

public OCommandExecutorSQLAlterProperty 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_ALTER))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_ALTER + " not found. Use " + getSyntax(), parserText, oldPos);
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_PROPERTY))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_PROPERTY + " not found. Use " + getSyntax(), parserText, oldPos);
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false);
        if (pos == -1)
            throw new OCommandSQLParsingException("Expected <class>.<property>. Use " + getSyntax(), parserText, oldPos);
        String[] parts = word.toString().split("\\.");
        if (parts.length != 2) {
            if (parts[1].startsWith("`") && parts[parts.length - 1].endsWith("`")) {
                StringBuilder fullName = new StringBuilder();
                for (int i = 1; i < parts.length; i++) {
                    if (i > 1) {
                        fullName.append(".");
                    }
                    fullName.append(parts[i]);
                }
                parts = new String[] { parts[0], fullName.toString() };
            } else {
                throw new OCommandSQLParsingException("Expected <class>.<property>. Use " + getSyntax(), parserText, oldPos);
            }
        }
        className = decodeClassName(parts[0]);
        if (className == null)
            throw new OCommandSQLParsingException("Class not found", parserText, oldPos);
        fieldName = decodeClassName(parts[1]);
        oldPos = pos;
        pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
        if (pos == -1)
            throw new OCommandSQLParsingException("Missing property attribute to change. Use " + getSyntax(), parserText, oldPos);
        final String attributeAsString = word.toString();
        try {
            attribute = OProperty.ATTRIBUTES.valueOf(attributeAsString.toUpperCase(Locale.ENGLISH));
        } catch (IllegalArgumentException e) {
            throw new OCommandSQLParsingException("Unknown property attribute '" + attributeAsString + "'. Supported attributes are: " + Arrays.toString(OProperty.ATTRIBUTES.values()), parserText, oldPos);
        }
        value = parserText.substring(pos + 1).trim();
        if (attribute.equals(ATTRIBUTES.NAME) || attribute.equals(ATTRIBUTES.LINKEDCLASS)) {
            value = decodeClassName(value);
        }
        if (value.length() == 0) {
            throw new OCommandSQLParsingException("Missing property value to change for attribute '" + attribute + "'. Use " + getSyntax(), parserText, oldPos);
        }
        if (preParsedStatement != null) {
            OAlterPropertyStatement stm = (OAlterPropertyStatement) preParsedStatement;
            OExpression settingExp = stm.settingValue;
            if (settingExp != null) {
                Object expValue = settingExp.execute(null, context);
                if (expValue == null) {
                    expValue = settingExp.toString();
                }
                if (expValue != null) {
                    if (expValue instanceof Date) {
                        value = ODateHelper.getDateTimeFormatInstance().format((Date) expValue);
                    } else
                        value = expValue.toString();
                } else
                    value = null;
                if (attribute.equals(ATTRIBUTES.NAME) || attribute.equals(ATTRIBUTES.LINKEDCLASS)) {
                    value = decodeClassName(value);
                }
            } else if (stm.customPropertyName != null) {
                value = "" + stm.customPropertyName.getStringValue() + "=" + stm.customPropertyValue.toString();
            } else if (stm.clearCustom) {
                value = "clear";
            }
        } else {
            if (value.equalsIgnoreCase("null")) {
                value = null;
            }
            if (value != null && isQuoted(value)) {
                value = removeQuotes(value);
            }
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OAlterPropertyStatement(com.orientechnologies.orient.core.sql.parser.OAlterPropertyStatement) OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OExpression(com.orientechnologies.orient.core.sql.parser.OExpression) Date(java.util.Date)

Example 12 with OCommandRequestText

use of com.orientechnologies.orient.core.command.OCommandRequestText in project orientdb by orientechnologies.

the class OCommandExecutorSQLAlterSequence method parse.

@Override
public OCommandExecutorSQLAlterSequence parse(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);
        final ODatabaseDocumentInternal database = getDatabase();
        final StringBuilder word = new StringBuilder();
        parserRequiredKeyword(KEYWORD_ALTER);
        parserRequiredKeyword(KEYWORD_SEQUENCE);
        this.sequenceName = parserRequiredWord(false, "Expected <sequence name>");
        this.params = new OSequence.CreateParams();
        String temp;
        while ((temp = parseOptionalWord(true)) != null) {
            if (parserIsEnded()) {
                break;
            }
            if (temp.equals(KEYWORD_START)) {
                String startAsString = parserRequiredWord(true, "Expected <start value>");
                this.params.start = Long.parseLong(startAsString);
            } else if (temp.equals(KEYWORD_INCREMENT)) {
                String incrementAsString = parserRequiredWord(true, "Expected <increment value>");
                this.params.increment = Integer.parseInt(incrementAsString);
            } else if (temp.equals(KEYWORD_CACHE)) {
                String cacheAsString = parserRequiredWord(true, "Expected <cache value>");
                this.params.cacheSize = Integer.parseInt(cacheAsString);
            }
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OSequence(com.orientechnologies.orient.core.metadata.sequence.OSequence) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 13 with OCommandRequestText

use of com.orientechnologies.orient.core.command.OCommandRequestText in project orientdb by orientechnologies.

the class OCommandExecutorSQLDelete method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLDelete 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 ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        query = null;
        recordCount = 0;
        if (parserTextUpperCase.endsWith(KEYWORD_UNSAFE)) {
            unsafe = true;
            parserText = parserText.substring(0, parserText.length() - KEYWORD_UNSAFE.length() - 1);
            parserTextUpperCase = parserTextUpperCase.substring(0, parserTextUpperCase.length() - KEYWORD_UNSAFE.length() - 1);
        }
        parserRequiredKeyword(OCommandExecutorSQLDelete.KEYWORD_DELETE);
        parserRequiredKeyword(OCommandExecutorSQLDelete.KEYWORD_FROM);
        String subjectName = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
        if (subjectName == null)
            throwSyntaxErrorException("Invalid subject name. Expected cluster, class, index or sub-query");
        if (OStringParser.startsWithIgnoreCase(subjectName, OCommandExecutorSQLAbstract.INDEX_PREFIX)) {
            // INDEX
            indexName = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_PREFIX.length());
            if (!parserIsEnded()) {
                while (!parserIsEnded()) {
                    final String word = parserGetLastWord();
                    if (word.equals(KEYWORD_LOCK))
                        lockStrategy = parseLock();
                    else if (word.equals(KEYWORD_RETURN))
                        returning = parseReturn();
                    else if (word.equals(KEYWORD_UNSAFE))
                        unsafe = true;
                    else if (word.equalsIgnoreCase(KEYWORD_WHERE))
                        compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(), KEYWORD_WHERE);
                    parserNextWord(true);
                }
            } else
                parserSetCurrentPosition(-1);
        } else if (subjectName.startsWith("(")) {
            subjectName = subjectName.trim();
            query = database.command(new OSQLAsynchQuery<ODocument>(subjectName.substring(1, subjectName.length() - 1), this));
            parserNextWord(true);
            if (!parserIsEnded()) {
                while (!parserIsEnded()) {
                    final String word = parserGetLastWord();
                    if (word.equals(KEYWORD_LOCK))
                        lockStrategy = parseLock();
                    else if (word.equals(KEYWORD_RETURN))
                        returning = parseReturn();
                    else if (word.equals(KEYWORD_UNSAFE))
                        unsafe = true;
                    else if (word.equalsIgnoreCase(KEYWORD_WHERE))
                        compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(), KEYWORD_WHERE);
                    parserNextWord(true);
                }
            }
        } else {
            parserNextWord(true);
            while (!parserIsEnded()) {
                final String word = parserGetLastWord();
                if (word.equals(KEYWORD_LOCK))
                    lockStrategy = parseLock();
                else if (word.equals(KEYWORD_RETURN))
                    returning = parseReturn();
                else {
                    parserGoBack();
                    break;
                }
                parserNextWord(true);
            }
            final String condition = parserGetCurrentPosition() > -1 ? " " + parserText.substring(parserGetCurrentPosition()) : "";
            query = database.command(new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget(subjectName) + condition, this));
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 14 with OCommandRequestText

use of com.orientechnologies.orient.core.command.OCommandRequestText in project orientdb by orientechnologies.

the class OCommandExecutorSQLDropClass method parse.

public OCommandExecutorSQLDropClass 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 boolean strict = getDatabase().getStorage().getConfiguration().isStrictSql();
        if (strict) {
            this.className = ((ODropClassStatement) this.preParsedStatement).name.getStringValue();
            this.unsafe = ((ODropClassStatement) this.preParsedStatement).unsafe;
            this.ifExists = ((ODropClassStatement) this.preParsedStatement).ifExists;
        } else {
            oldParsing((OCommandRequestText) iRequest);
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODropClassStatement(com.orientechnologies.orient.core.sql.parser.ODropClassStatement)

Example 15 with OCommandRequestText

use of com.orientechnologies.orient.core.command.OCommandRequestText in project orientdb by orientechnologies.

the class OCommandExecutorSQLDropCluster method parse.

public OCommandExecutorSQLDropCluster 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);
        final StringBuilder word = new StringBuilder();
        int oldPos = 0;
        int pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_DROP))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_DROP + " not found. Use " + getSyntax(), parserText, oldPos);
        pos = nextWord(parserText, parserTextUpperCase, pos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_CLUSTER))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_CLUSTER + " not found. Use " + getSyntax(), parserText, oldPos);
        pos = nextWord(parserText, parserTextUpperCase, pos, word, false);
        if (pos == -1)
            throw new OCommandSQLParsingException("Expected <cluster>. Use " + getSyntax(), parserText, pos);
        clusterName = word.toString();
        if (clusterName == null)
            throw new OCommandSQLParsingException("Cluster is null. Use " + getSyntax(), parserText, pos);
        clusterName = decodeClassName(clusterName);
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText)

Aggregations

OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)44 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)12 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)9 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)7 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)5 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)4 OSQLAsynchQuery (com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)4 OException (com.orientechnologies.common.exception.OException)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OCommandExecutor (com.orientechnologies.orient.core.command.OCommandExecutor)2 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)2 OIdentifier (com.orientechnologies.orient.core.sql.parser.OIdentifier)2 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)2 IOException (java.io.IOException)2 Socket (java.net.Socket)2 Entry (java.util.Map.Entry)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Matcher (java.util.regex.Matcher)2