Search in sources :

Example 11 with OQueryParsingException

use of com.orientechnologies.orient.core.exception.OQueryParsingException in project orientdb by orientechnologies.

the class OSQLTarget method extractTargets.

@SuppressWarnings("unchecked")
private boolean extractTargets() {
    parserSkipWhiteSpaces();
    if (parserIsEnded())
        throw new OQueryParsingException("No query target found", parserText, 0);
    final char c = parserGetCurrentChar();
    if (c == '$') {
        targetVariable = parserRequiredWord(false, "No valid target");
        targetVariable = targetVariable.substring(1);
    } else if (c == OStringSerializerHelper.LINK || Character.isDigit(c)) {
        // UNIQUE RID
        targetRecords = new ArrayList<OIdentifiable>();
        ((List<OIdentifiable>) targetRecords).add(new ORecordId(parserRequiredWord(true, "No valid RID")));
    } else if (c == OStringSerializerHelper.EMBEDDED_BEGIN) {
        // SUB QUERY
        final StringBuilder subText = new StringBuilder(256);
        parserSetCurrentPosition(OStringSerializerHelper.getEmbedded(parserText, parserGetCurrentPosition(), -1, subText) + 1);
        final OCommandSQL subCommand = new OCommandSQLResultset(subText.toString());
        final OCommandExecutorSQLResultsetDelegate executor = (OCommandExecutorSQLResultsetDelegate) OCommandManager.instance().getExecutor(subCommand);
        executor.setProgressListener(subCommand.getProgressListener());
        executor.parse(subCommand);
        OCommandContext childContext = executor.getContext();
        if (childContext != null) {
            childContext.setParent(context);
        }
        if (!(executor instanceof Iterable<?>))
            throw new OCommandSQLParsingException("Sub-query cannot be iterated because doesn't implement the Iterable interface: " + subCommand);
        targetQuery = subText.toString();
        targetRecords = executor;
    } else if (c == OStringSerializerHelper.LIST_BEGIN) {
        // COLLECTION OF RIDS
        final List<String> rids = new ArrayList<String>();
        parserSetCurrentPosition(OStringSerializerHelper.getCollection(parserText, parserGetCurrentPosition(), rids));
        targetRecords = new ArrayList<OIdentifiable>();
        for (String rid : rids) ((List<OIdentifiable>) targetRecords).add(new ORecordId(rid));
        parserMoveCurrentPosition(1);
    } else {
        while (!parserIsEnded() && (targetClasses == null && targetClusters == null && targetIndex == null && targetIndexValues == null && targetRecords == null)) {
            String originalSubjectName = parserRequiredWord(false, "Target not found");
            String subjectName = originalSubjectName.toUpperCase();
            final String alias;
            if (subjectName.equals("AS"))
                alias = parserRequiredWord(true, "Alias not found");
            else
                alias = subjectName;
            final String subjectToMatch = subjectName;
            if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX)) {
                // REGISTER AS CLUSTER
                if (targetClusters == null)
                    targetClusters = new HashMap<String, String>();
                final String clusterNames = subjectName.substring(OCommandExecutorSQLAbstract.CLUSTER_PREFIX.length());
                if (clusterNames.startsWith("[") && clusterNames.endsWith("]")) {
                    final Collection<String> clusters = new HashSet<String>(3);
                    OStringSerializerHelper.getCollection(clusterNames, 0, clusters);
                    for (String cl : clusters) {
                        targetClusters.put(cl, cl);
                    }
                } else
                    targetClusters.put(clusterNames, alias);
            } else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX)) {
                // REGISTER AS INDEX
                targetIndex = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_PREFIX.length());
            } else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.METADATA_PREFIX)) {
                // METADATA
                final String metadataTarget = subjectName.substring(OCommandExecutorSQLAbstract.METADATA_PREFIX.length());
                targetRecords = new ArrayList<OIdentifiable>();
                if (metadataTarget.equals(OCommandExecutorSQLAbstract.METADATA_SCHEMA)) {
                    ((ArrayList<OIdentifiable>) targetRecords).add(new ORecordId(ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().schemaRecordId));
                } else if (metadataTarget.equals(OCommandExecutorSQLAbstract.METADATA_INDEXMGR)) {
                    ((ArrayList<OIdentifiable>) targetRecords).add(new ORecordId(ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().indexMgrRecordId));
                } else
                    throw new OQueryParsingException("Metadata element not supported: " + metadataTarget);
            } else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.DICTIONARY_PREFIX)) {
                // DICTIONARY
                final String key = originalSubjectName.substring(OCommandExecutorSQLAbstract.DICTIONARY_PREFIX.length());
                targetRecords = new ArrayList<OIdentifiable>();
                final OIdentifiable value = ODatabaseRecordThreadLocal.INSTANCE.get().getDictionary().get(key);
                if (value != null)
                    ((List<OIdentifiable>) targetRecords).add(value);
            } else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_VALUES_PREFIX)) {
                targetIndexValues = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_VALUES_PREFIX.length());
                targetIndexValuesAsc = true;
            } else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_VALUES_ASC_PREFIX)) {
                targetIndexValues = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_VALUES_ASC_PREFIX.length());
                targetIndexValuesAsc = true;
            } else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_VALUES_DESC_PREFIX)) {
                targetIndexValues = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_VALUES_DESC_PREFIX.length());
                targetIndexValuesAsc = false;
            } else {
                if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.CLASS_PREFIX))
                    // REGISTER AS CLASS
                    subjectName = subjectName.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());
                // REGISTER AS CLASS
                if (targetClasses == null)
                    targetClasses = new HashMap<String, String>();
                final OClass cls = ODatabaseRecordThreadLocal.INSTANCE.get().getMetadata().getSchema().getClass(subjectName);
                if (cls == null)
                    throw new OCommandExecutionException("Class '" + subjectName + "' was not found in database '" + ODatabaseRecordThreadLocal.INSTANCE.get().getName() + "'");
                targetClasses.put(cls.getName(), alias);
            }
        }
    }
    return !parserIsEnded();
}
Also used : OCommandContext(com.orientechnologies.orient.core.command.OCommandContext) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 12 with OQueryParsingException

use of com.orientechnologies.orient.core.exception.OQueryParsingException in project orientdb by orientechnologies.

the class OCommandExecutorSQLLiveUnsubscribe method parse.

@Override
public OCommandExecutorSQLLiveUnsubscribe parse(OCommandRequest iRequest) {
    OCommandRequestText requestText = (OCommandRequestText) iRequest;
    String originalText = requestText.getText();
    String remainingText = requestText.getText().trim().substring(5).trim();
    requestText.setText(remainingText);
    try {
        if (remainingText.toLowerCase().startsWith("unsubscribe")) {
            remainingText = remainingText.substring("unsubscribe".length()).trim();
            if (remainingText.contains(" ")) {
                throw new OQueryParsingException("invalid unsubscribe token for live query: " + remainingText);
            }
            this.unsubscribeToken = remainingText;
        }
    } finally {
        requestText.setText(originalText);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException)

Example 13 with OQueryParsingException

use of com.orientechnologies.orient.core.exception.OQueryParsingException in project orientdb by orientechnologies.

the class OrientJdbcStatement method execute.

@Override
public boolean execute(final String sqlCommand) throws SQLException {
    //    System.out.println("sqlCommand = " + sqlCommand);
    if ("".equals(sqlCommand))
        return false;
    sql = mayCleanForSpark(sqlCommand);
    if (sql.equalsIgnoreCase("select 1")) {
        documents = new ArrayList<ODocument>(1);
        documents.add(new ODocument().field("1", 1));
    } else {
        query = new OCommandSQL(sql);
        try {
            rawResult = executeCommand(query);
            if (rawResult instanceof List<?>) {
                documents = (List<ODocument>) rawResult;
            } else if (rawResult instanceof OIdentifiable) {
                documents = new ArrayList<ODocument>(1);
                documents.add((ODocument) ((OIdentifiable) rawResult).getRecord());
            } else {
                return false;
            }
        } catch (OQueryParsingException e) {
            throw new SQLSyntaxErrorException("Error while parsing query", e);
        } catch (OException e) {
            throw new SQLException("Error while executing query", e);
        }
    }
    resultSet = new OrientJdbcResultSet(this, documents, resultSetType, resultSetConcurrency, resultSetHoldability);
    return true;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ArrayList(java.util.ArrayList) List(java.util.List) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OException(com.orientechnologies.common.exception.OException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)13 OException (com.orientechnologies.common.exception.OException)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)2 OStorageConfiguration (com.orientechnologies.orient.core.config.OStorageConfiguration)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)2 SQLException (java.sql.SQLException)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 OPair (com.orientechnologies.common.util.OPair)1 OCommandContext (com.orientechnologies.orient.core.command.OCommandContext)1