Search in sources :

Example 16 with OCommandRequestText

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

the class OCommandExecutorToOStatementWrapper method parse.

@SuppressWarnings("unchecked")
@Override
public OCommandExecutorToOStatementWrapper parse(OCommandRequest iCommand) {
    final OCommandRequestText textRequest = (OCommandRequestText) iCommand;
    if (iCommand instanceof OSQLAsynchQuery) {
        request = (OSQLAsynchQuery<ODocument>) iCommand;
    } else {
        // BUILD A QUERY OBJECT FROM THE COMMAND REQUEST
        request = new OSQLSynchQuery<ODocument>(textRequest.getText());
        if (textRequest.getResultListener() != null) {
            request.setResultListener(textRequest.getResultListener());
        }
    }
    String queryText = textRequest.getText();
    statement = OStatementCache.get(queryText, getDatabase());
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with OCommandRequestText

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

the class OCommandExecutorSQLLiveSelect method parse.

@Override
public OCommandExecutorSQLSelect parse(final OCommandRequest iRequest) {
    final OCommandRequestText requestText = (OCommandRequestText) iRequest;
    final String originalText = requestText.getText();
    final String remainingText = requestText.getText().trim().substring(5).trim();
    requestText.setText(remainingText);
    try {
        return super.parse(iRequest);
    } finally {
        requestText.setText(originalText);
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText)

Example 18 with OCommandRequestText

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

the class OCommandExecutorSQLResultsetAbstract method parse.

/**
   * Compile the filter conditions only the first time.
   */
public OCommandExecutorSQLResultsetAbstract parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    init(textRequest);
    if (iRequest instanceof OSQLSynchQuery) {
        request = (OSQLSynchQuery<ODocument>) iRequest;
    } else if (iRequest instanceof OSQLAsynchQuery)
        request = (OSQLAsynchQuery<ODocument>) iRequest;
    else {
        // BUILD A QUERY OBJECT FROM THE COMMAND REQUEST
        request = new OSQLSynchQuery<ODocument>(textRequest.getText());
        if (textRequest.getResultListener() != null)
            request.setResultListener(textRequest.getResultListener());
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 19 with OCommandRequestText

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

the class OCommandExecutorSQLUpdate method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLUpdate parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        queryText = preParse(queryText, iRequest);
        if (isUpdateEdge()) {
            // work-around to use UPDATE syntax without having to
            queryText = queryText.replaceFirst("EDGE ", "");
        }
        textRequest.setText(queryText);
        final ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        setEntries.clear();
        addEntries.clear();
        putEntries.clear();
        removeEntries.clear();
        incrementEntries.clear();
        content = null;
        merge = null;
        query = null;
        parserRequiredKeyword(KEYWORD_UPDATE);
        subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
        if (subjectName == null) {
            throwSyntaxErrorException("Invalid subject name. Expected cluster, class, index or sub-query");
        }
        if (subjectName.equalsIgnoreCase("EDGE")) {
            updateEdge = true;
            subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
        }
        clazz = extractClassFromTarget(subjectName);
        String word = parserNextWord(true);
        if (parserIsEnded() || (!word.equals(KEYWORD_SET) && !word.equals(KEYWORD_ADD) && !word.equals(KEYWORD_PUT) && !word.equals(KEYWORD_REMOVE) && !word.equals(KEYWORD_INCREMENT) && !word.equals(KEYWORD_CONTENT) && !word.equals(KEYWORD_MERGE) && !word.equals(KEYWORD_LOCK) && !word.equals(KEYWORD_RETURN) && !word.equals(KEYWORD_UPSERT) && !word.equals(KEYWORD_EDGE)))
            throwSyntaxErrorException("Expected keyword " + KEYWORD_SET + "," + KEYWORD_ADD + "," + KEYWORD_CONTENT + "," + KEYWORD_MERGE + "," + KEYWORD_PUT + "," + KEYWORD_REMOVE + "," + KEYWORD_INCREMENT + "," + KEYWORD_LOCK + " or " + KEYWORD_RETURN + " or " + KEYWORD_UPSERT + " or " + KEYWORD_EDGE);
        while ((!parserIsEnded() && !parserGetLastWord().equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE)) || parserGetLastWord().equals(KEYWORD_UPSERT)) {
            word = parserGetLastWord();
            if (word.equals(KEYWORD_CONTENT))
                parseContent();
            else if (word.equals(KEYWORD_MERGE))
                parseMerge();
            else if (word.equals(KEYWORD_SET))
                parseSetFields(clazz, setEntries);
            else if (word.equals(KEYWORD_ADD))
                parseAddFields();
            else if (word.equals(KEYWORD_PUT))
                parsePutFields();
            else if (word.equals(KEYWORD_REMOVE))
                parseRemoveFields();
            else if (word.equals(KEYWORD_INCREMENT))
                parseIncrementFields();
            else if (word.equals(KEYWORD_LOCK))
                lockStrategy = parseLock();
            else if (word.equals(KEYWORD_UPSERT))
                upsertMode = true;
            else if (word.equals(KEYWORD_RETURN))
                parseReturn();
            else if (word.equals(KEYWORD_RETRY)) {
                OLogManager.instance().warn(this, "RETRY keyword will be ignored in " + originalQuery);
                parseRetry();
            } else
                break;
            parserNextWord(true);
        }
        final String additionalStatement = parserGetLastWord();
        if (subjectName.startsWith("(")) {
            subjectName = subjectName.trim();
            query = database.command(new OSQLAsynchQuery<ODocument>(subjectName.substring(1, subjectName.length() - 1), this).setContext(context));
            if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT))
                compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(), KEYWORD_WHERE);
        } else if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LET) || additionalStatement.equals(KEYWORD_LOCK)) {
            if (this.preParsedStatement != null) {
                Map<Object, Object> params = ((OCommandRequestText) iRequest).getParameters();
                OUpdateStatement updateStm = (OUpdateStatement) preParsedStatement;
                StringBuilder selectString = new StringBuilder();
                selectString.append("select from ");
                updateStm.target.toString(params, selectString);
                if (updateStm.let != null) {
                    selectString.append(" ");
                    updateStm.let.toString(params, selectString);
                }
                if (updateStm.whereClause != null) {
                    selectString.append(" WHERE ");
                    updateStm.whereClause.toString(params, selectString);
                }
                if (updateStm.limit != null) {
                    selectString.append(" ");
                    updateStm.limit.toString(params, selectString);
                }
                if (updateStm.timeout != null) {
                    selectString.append(" ");
                    updateStm.timeout.toString(params, selectString);
                }
                if (updateStm.lockRecord != null) {
                    selectString.append(" LOCK ");
                    switch(updateStm.lockRecord) {
                        case DEFAULT:
                            selectString.append("DEFAULT");
                            break;
                        case EXCLUSIVE_LOCK:
                            selectString.append("RECORD");
                            break;
                        case SHARED_LOCK:
                            selectString.append("SHARED");
                            break;
                        case NONE:
                            selectString.append("NONE");
                            break;
                    }
                }
                query = new OSQLAsynchQuery<ODocument>(selectString.toString(), this);
            } else {
                query = new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget() + " " + additionalStatement + " " + parserText.substring(parserGetCurrentPosition()), this);
            }
            isUpsertAllowed = (((OMetadataInternal) getDatabase().getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName) != null);
        } else if (!additionalStatement.isEmpty())
            throwSyntaxErrorException("Invalid keyword " + additionalStatement);
        else
            query = new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget(), this);
        if (upsertMode && !isUpsertAllowed)
            throwSyntaxErrorException("Upsert only works with class names ");
        if (upsertMode && !additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE))
            throwSyntaxErrorException("Upsert only works with WHERE keyword");
        if (upsertMode && updateEdge)
            throwSyntaxErrorException("Upsert is not supported with UPDATE EDGE");
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OUpdateStatement(com.orientechnologies.orient.core.sql.parser.OUpdateStatement)

Example 20 with OCommandRequestText

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

the class TransactionAtomicTest method testTransactionPreListenerRollback.

@Test
public void testTransactionPreListenerRollback() throws IOException {
    ODocument record1 = new ODocument();
    record1.field("value", "This is the first version").save();
    final ODatabaseListener listener = new ODatabaseListener() {

        @Override
        public void onAfterTxCommit(ODatabase iDatabase) {
        }

        @Override
        public void onAfterTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxBegin(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxCommit(ODatabase iDatabase) {
            throw new RuntimeException("Rollback test");
        }

        @Override
        public void onBeforeTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onClose(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeCommand(OCommandRequestText iCommand, OCommandExecutor executor) {
        }

        @Override
        public void onAfterCommand(OCommandRequestText iCommand, OCommandExecutor executor, Object result) {
        }

        @Override
        public void onCreate(ODatabase iDatabase) {
        }

        @Override
        public void onDelete(ODatabase iDatabase) {
        }

        @Override
        public void onOpen(ODatabase iDatabase) {
        }

        @Override
        public boolean onCorruptionRepairDatabase(ODatabase iDatabase, final String iReason, String iWhatWillbeFixed) {
            return true;
        }
    };
    database.registerListener(listener);
    database.begin();
    try {
        database.commit();
        Assert.assertTrue(false);
    } catch (OTransactionException e) {
        Assert.assertTrue(true);
    } finally {
        database.unregisterListener(listener);
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandExecutor(com.orientechnologies.orient.core.command.OCommandExecutor) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) ODatabaseListener(com.orientechnologies.orient.core.db.ODatabaseListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

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