Search in sources :

Example 1 with OQueryParsingException

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

the class OCommandExecutorSQLInsert method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLInsert parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        // System.out.println("NEW PARSER FROM: " + queryText);
        queryText = preParse(queryText, iRequest);
        // System.out.println("NEW PARSER   TO: " + queryText);
        textRequest.setText(queryText);
        final ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        className = null;
        newRecords = null;
        content = null;
        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("INSERT");
        parserRequiredKeyword("INTO");
        String subjectName = parserRequiredWord(true, "Invalid subject name. Expected cluster, class or index");
        if (subjectName.startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX)) {
            // CLUSTER
            clusterName = subjectName.substring(OCommandExecutorSQLAbstract.CLUSTER_PREFIX.length());
            try {
                int clusterId = Integer.parseInt(clusterName);
                clusterName = database.getClusterNameById(clusterId);
            } catch (Exception e) {
            //not an integer
            }
        } else if (subjectName.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX))
            // INDEX
            indexName = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_PREFIX.length());
        else {
            // CLASS
            if (subjectName.startsWith(OCommandExecutorSQLAbstract.CLASS_PREFIX))
                subjectName = subjectName.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());
            final OClass cls = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName);
            if (cls == null)
                throwParsingException("Class " + subjectName + " not found in database");
            if (!unsafe && cls.isSubClassOf("E"))
                // FOUND EDGE
                throw new OCommandExecutionException("'INSERT' command cannot create Edges. Use 'CREATE EDGE' command instead, or apply the 'UNSAFE' keyword to force it");
            className = cls.getName();
            clazz = database.getMetadata().getSchema().getClass(className);
            if (clazz == null)
                throw new OQueryParsingException("Class '" + className + "' was not found");
        }
        if (clusterName != null && className == null) {
            ODatabaseDocumentInternal db = getDatabase();
            OCluster cluster = db.getStorage().getClusterByName(clusterName);
            if (cluster != null) {
                clazz = db.getMetadata().getSchema().getClassByClusterId(cluster.getId());
                if (clazz != null) {
                    className = clazz.getName();
                }
            }
        }
        parserSkipWhiteSpaces();
        if (parserIsEnded())
            throwSyntaxErrorException("Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");
        final String temp = parseOptionalWord(true);
        if (parserGetLastWord().equalsIgnoreCase("cluster")) {
            clusterName = parserRequiredWord(false);
            parserSkipWhiteSpaces();
            if (parserIsEnded())
                throwSyntaxErrorException("Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");
        } else
            parserGoBack();
        newRecords = new ArrayList<Map<String, Object>>();
        Boolean sourceClauseProcessed = false;
        if (parserGetCurrentChar() == '(') {
            parseValues();
            parserNextWord(true, " \r\n");
            sourceClauseProcessed = true;
        } else {
            parserNextWord(true, " ,\r\n");
            if (parserGetLastWord().equals(KEYWORD_CONTENT)) {
                newRecords = null;
                parseContent();
                sourceClauseProcessed = true;
            } else if (parserGetLastWord().equals(KEYWORD_SET)) {
                final List<OPair<String, Object>> fields = new ArrayList<OPair<String, Object>>();
                parseSetFields(clazz, fields);
                newRecords.add(OPair.convertToMap(fields));
                sourceClauseProcessed = true;
            }
        }
        if (sourceClauseProcessed)
            parserNextWord(true, " \r\n");
        // it has to be processed before KEYWORD_FROM in order to not be taken as part of SELECT
        if (parserGetLastWord().equals(KEYWORD_RETURN)) {
            parseReturn(!sourceClauseProcessed);
            parserNextWord(true, " \r\n");
        }
        if (!sourceClauseProcessed) {
            if (parserGetLastWord().equals(KEYWORD_FROM)) {
                newRecords = null;
                subQuery = new OSQLAsynchQuery<OIdentifiable>(parserText.substring(parserGetCurrentPosition()), this);
            }
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OPair(com.orientechnologies.common.util.OPair) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OCluster(com.orientechnologies.orient.core.storage.OCluster)

Example 2 with OQueryParsingException

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

the class OCommandExecutorSQLTraverse method parseFields.

protected int parseFields() {
    int currentPos = 0;
    final StringBuilder word = new StringBuilder();
    currentPos = nextWord(parserText, parserTextUpperCase, currentPos, word, true);
    if (!word.toString().equals(KEYWORD_TRAVERSE))
        return -1;
    int fromPosition = parserTextUpperCase.indexOf(KEYWORD_FROM_2FIND, currentPos);
    if (fromPosition == -1)
        throw new OQueryParsingException("Missed " + KEYWORD_FROM, parserText, currentPos);
    Set<Object> fields = new HashSet<Object>();
    final String fieldString = parserText.substring(currentPos, fromPosition).trim();
    if (fieldString.length() > 0) {
        // EXTRACT PROJECTIONS
        final List<String> items = OStringSerializerHelper.smartSplit(fieldString, ',');
        for (String field : items) {
            final String fieldName = field.trim();
            if (fieldName.contains("("))
                fields.add(OSQLHelper.parseValue(null, fieldName, context));
            else
                fields.add(fieldName);
        }
    } else
        throw new OQueryParsingException("Missed field list to cross in TRAVERSE. Use " + getSyntax(), parserText, currentPos);
    currentPos = fromPosition + KEYWORD_FROM.length() + 1;
    traverse.fields(fields);
    return currentPos;
}
Also used : OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException)

Example 3 with OQueryParsingException

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

the class OrientJdbcPreparedStatement method executeQuery.

@SuppressWarnings("unchecked")
public ResultSet executeQuery() throws SQLException {
    //    return super.executeQuery(sql);
    sql = mayCleanForSpark(sql);
    if (sql.equalsIgnoreCase("select 1")) {
        // OPTIMIZATION
        documents = new ArrayList<ODocument>();
        documents.add(new ODocument().field("1", 1));
    } else {
        try {
            query = new OSQLSynchQuery<ODocument>(mayCleanForSpark(sql));
            documents = database.query((OQuery<? extends Object>) query, params.values().toArray());
        } catch (OQueryParsingException e) {
            throw new SQLSyntaxErrorException("Error while parsing query", e);
        } catch (OException e) {
            throw new SQLException("Error while executing query", e);
        }
    }
    // return super.executeQuery(sql);
    resultSet = new OrientJdbcResultSet(this, documents, resultSetType, resultSetConcurrency, resultSetHoldability);
    return resultSet;
}
Also used : OQuery(com.orientechnologies.orient.core.query.OQuery) SQLException(java.sql.SQLException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with OQueryParsingException

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

the class OSQLFunctionDate method execute.

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, final Object iCurrentResult, final Object[] iParams, OCommandContext iContext) {
    if (iParams.length == 0)
        return date;
    if (iParams[0] == null)
        return null;
    if (iParams[0] instanceof Number)
        return new Date(((Number) iParams[0]).longValue());
    if (format == null) {
        if (iParams.length > 1) {
            format = new SimpleDateFormat((String) iParams[1]);
            format.setTimeZone(ODateHelper.getDatabaseTimeZone());
        } else
            format = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().getDateTimeFormatInstance();
        if (iParams.length == 3)
            format.setTimeZone(TimeZone.getTimeZone(iParams[2].toString()));
    }
    try {
        return format.parse((String) iParams[0]);
    } catch (ParseException e) {
        throw OException.wrapException(new OQueryParsingException("Error on formatting date '" + iParams[0] + "' using the format: " + format.toPattern()), e);
    }
}
Also used : OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with OQueryParsingException

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

the class OSQLQuery method run.

/**
   * Delegates to the OQueryExecutor the query execution.
   */
@SuppressWarnings("unchecked")
public List<T> run(final Object... iArgs) {
    final ODatabaseDocumentInternal database = ODatabaseRecordThreadLocal.INSTANCE.get();
    if (database == null)
        throw new OQueryParsingException("No database configured");
    ((OMetadataInternal) database.getMetadata()).makeThreadLocalSchemaSnapshot();
    try {
        setParameters(iArgs);
        return (List<T>) database.getStorage().command(this);
    } finally {
        ((OMetadataInternal) database.getMetadata()).clearThreadLocalSchemaSnapshot();
    }
}
Also used : OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) ArrayList(java.util.ArrayList) List(java.util.List) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

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