Search in sources :

Example 1 with OSQLFilterItemVariable

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable in project orientdb by orientechnologies.

the class OLuceneOperatorUtil method buildOIndexSearchResult.

public static OIndexSearchResult buildOIndexSearchResult(OClass iSchemaClass, OSQLFilterCondition iCondition, List<OIndexSearchResult> iIndexSearchResults, OCommandContext context) {
    if (iCondition.getLeft() instanceof Collection) {
        OIndexSearchResult lastResult = null;
        Collection left = (Collection) iCondition.getLeft();
        int i = 0;
        Object lastValue = null;
        for (Object obj : left) {
            if (obj instanceof OSQLFilterItemField) {
                OSQLFilterItemField item = (OSQLFilterItemField) obj;
                Object value = null;
                if (iCondition.getRight() instanceof Collection) {
                    List<Object> right = (List<Object>) iCondition.getRight();
                    value = right.get(i);
                } else {
                    value = iCondition.getRight();
                }
                if (lastResult == null) {
                    lastResult = new OIndexSearchResult(iCondition.getOperator(), item.getFieldChain(), value);
                } else {
                    lastResult = lastResult.merge(new OIndexSearchResult(iCondition.getOperator(), item.getFieldChain(), value));
                }
            } else if (obj instanceof OSQLFilterItemVariable) {
                OSQLFilterItemVariable item = (OSQLFilterItemVariable) obj;
                Object value = null;
                if (iCondition.getRight() instanceof Collection) {
                    List<Object> right = (List<Object>) iCondition.getRight();
                    value = right.get(i);
                } else {
                    value = iCondition.getRight();
                }
                context.setVariable(item.toString(), value);
            }
            i++;
        }
        if (lastResult != null && OLuceneOperatorUtil.checkIndexExistence(iSchemaClass, lastResult))
            iIndexSearchResults.add(lastResult);
        return lastResult;
    } else {
        OIndexSearchResult result = OLuceneOperatorUtil.createIndexedProperty(iCondition, iCondition.getLeft());
        if (result == null)
            result = OLuceneOperatorUtil.createIndexedProperty(iCondition, iCondition.getRight());
        if (result == null)
            return null;
        if (OLuceneOperatorUtil.checkIndexExistence(iSchemaClass, result))
            iIndexSearchResults.add(result);
        return result;
    }
}
Also used : OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) Collection(java.util.Collection) List(java.util.List) OIndexSearchResult(com.orientechnologies.orient.core.sql.OIndexSearchResult) OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)

Example 2 with OSQLFilterItemVariable

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable in project orientdb by orientechnologies.

the class OSQLFunctionRuntime method execute.

/**
   * Execute a function.
   * 
   * @param iCurrentRecord
   *          Current record
   * @param iCurrentResult
   *          TODO
   * @param iContext
   * @return
   */
public Object execute(final Object iThis, final OIdentifiable iCurrentRecord, final Object iCurrentResult, final OCommandContext iContext) {
    // RESOLVE VALUES USING THE CURRENT RECORD
    for (int i = 0; i < configuredParameters.length; ++i) {
        runtimeParameters[i] = configuredParameters[i];
        if (configuredParameters[i] instanceof OSQLFilterItemField) {
            runtimeParameters[i] = ((OSQLFilterItemField) configuredParameters[i]).getValue(iCurrentRecord, iCurrentResult, iContext);
        } else if (configuredParameters[i] instanceof OSQLFunctionRuntime)
            runtimeParameters[i] = ((OSQLFunctionRuntime) configuredParameters[i]).execute(iThis, iCurrentRecord, iCurrentResult, iContext);
        else if (configuredParameters[i] instanceof OSQLFilterItemVariable) {
            runtimeParameters[i] = ((OSQLFilterItemVariable) configuredParameters[i]).getValue(iCurrentRecord, iCurrentResult, iContext);
        } else if (configuredParameters[i] instanceof OCommandSQL) {
            try {
                runtimeParameters[i] = ((OCommandSQL) configuredParameters[i]).setContext(iContext).execute();
            } catch (OCommandExecutorNotFoundException e) {
                // TRY WITH SIMPLE CONDITION
                final String text = ((OCommandSQL) configuredParameters[i]).getText();
                final OSQLPredicate pred = new OSQLPredicate(text);
                runtimeParameters[i] = pred.evaluate(iCurrentRecord instanceof ORecord ? (ORecord) iCurrentRecord : null, (ODocument) iCurrentResult, iContext);
                // REPLACE ORIGINAL PARAM
                configuredParameters[i] = pred;
            }
        } else if (configuredParameters[i] instanceof OSQLPredicate)
            runtimeParameters[i] = ((OSQLPredicate) configuredParameters[i]).evaluate(iCurrentRecord.getRecord(), (iCurrentRecord instanceof ODocument ? (ODocument) iCurrentResult : null), iContext);
        else if (configuredParameters[i] instanceof String) {
            if (configuredParameters[i].toString().startsWith("\"") || configuredParameters[i].toString().startsWith("'"))
                runtimeParameters[i] = OIOUtils.getStringContent(configuredParameters[i]);
        }
    }
    if (function.getMaxParams() == -1 || function.getMaxParams() > 0) {
        if (runtimeParameters.length < function.getMinParams() || (function.getMaxParams() > -1 && runtimeParameters.length > function.getMaxParams()))
            throw new OCommandExecutionException("Syntax error: function '" + function.getName() + "' needs " + (function.getMinParams() == function.getMaxParams() ? function.getMinParams() : function.getMinParams() + "-" + function.getMaxParams()) + " argument(s) while has been received " + runtimeParameters.length);
    }
    final Object functionResult = function.execute(iThis, iCurrentRecord, iCurrentResult, runtimeParameters, iContext);
    if (functionResult instanceof OAutoConvertToRecord)
        // FORCE AVOIDING TO CONVERT IN RECORD
        ((OAutoConvertToRecord) functionResult).setAutoConvertToRecord(false);
    return transformValue(iCurrentRecord, iContext, functionResult);
}
Also used : OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable) OCommandExecutorNotFoundException(com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoConvertToRecord(com.orientechnologies.orient.core.db.record.OAutoConvertToRecord) OSQLPredicate(com.orientechnologies.orient.core.sql.filter.OSQLPredicate) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) ORecord(com.orientechnologies.orient.core.record.ORecord) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OSQLFilterItemVariable

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable in project orientdb by orientechnologies.

the class OSQLFunctionIntersect method execute.

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams, OCommandContext iContext) {
    Object value = iParams[0];
    if (value instanceof OSQLFilterItemVariable)
        value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);
    if (value == null)
        return Collections.emptySet();
    if (iParams.length == 1) {
        // AGGREGATION MODE (STATEFUL)
        if (context == null) {
            // ADD ALL THE ITEMS OF THE FIRST COLLECTION
            if (value instanceof Collection) {
                context = ((Collection) value).iterator();
            } else if (value instanceof Iterator) {
                context = (Iterator) value;
            } else {
                context = Arrays.asList(value).iterator();
            }
        } else {
            context = intersectWith((Iterator) context, value);
        }
        return null;
    }
    // IN-LINE MODE (STATELESS)
    Iterator iterator = OMultiValue.getMultiValueIterator(value, false);
    for (int i = 1; i < iParams.length; ++i) {
        value = iParams[i];
        if (value instanceof OSQLFilterItemVariable)
            value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);
        if (value != null) {
            value = intersectWith(iterator, value);
            iterator = OMultiValue.getMultiValueIterator(value, false);
        } else {
            return new ArrayList().iterator();
        }
    }
    return iterator;
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) Collection(java.util.Collection) OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)

Example 4 with OSQLFilterItemVariable

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable in project orientdb by orientechnologies.

the class ORuntimeResult method applyRecord.

@SuppressWarnings("unchecked")
public static ODocument applyRecord(final ODocument iValue, final Map<String, Object> iProjections, final OCommandContext iContext, final OIdentifiable iRecord) {
    // APPLY PROJECTIONS
    ORecord record = (iRecord != null ? iRecord.getRecord() : null);
    //MANAGE SPECIFIC CASES FOR RECORD BYTES
    if (ORecordBytes.RECORD_TYPE == ORecordInternal.getRecordType(record)) {
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            if ("@rid".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getIdentity());
            } else if ("@size".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getSize());
            } else if ("@version".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getVersion());
            } else {
                Object val = projection.getValue();
                if (val instanceof Number || val instanceof String || val instanceof Boolean) {
                    iValue.field(projection.getKey(), val);
                } else {
                    iValue.field(projection.getKey(), (Object) null);
                }
            }
        }
        return iValue;
    }
    final ODocument inputDocument = (ODocument) record;
    if (iProjections.isEmpty())
        // SELECT * CASE
        inputDocument.copyTo(iValue);
    else {
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            final String prjName = projection.getKey();
            final Object v = projection.getValue();
            if (v == null && prjName != null) {
                iValue.field(prjName, (Object) null);
                continue;
            }
            final Object projectionValue;
            if (v != null && v.equals("*")) {
                // COPY ALL
                inputDocument.copyTo(iValue);
                // CONTINUE WITH NEXT ITEM
                continue;
            } else if (v instanceof OSQLFilterItemVariable || v instanceof OSQLFilterItemField) {
                final OSQLFilterItemAbstract var = (OSQLFilterItemAbstract) v;
                final OPair<OSQLMethodRuntime, Object[]> last = var.getLastChainOperator();
                if (last != null && last.getKey().getMethod() instanceof OSQLMethodField && last.getValue() != null && last.getValue().length == 1 && last.getValue()[0].equals("*")) {
                    final Object value = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
                    if (inputDocument != null && value != null && inputDocument instanceof ODocument && value instanceof ODocument) {
                        // COPY FIELDS WITH PROJECTION NAME AS PREFIX
                        for (String fieldName : ((ODocument) value).fieldNames()) {
                            iValue.field(prjName + fieldName, ((ODocument) value).field(fieldName));
                        }
                    }
                    projectionValue = null;
                } else
                    // RETURN A VARIABLE FROM THE CONTEXT
                    projectionValue = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
            } else if (v instanceof OSQLFunctionRuntime) {
                final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
                projectionValue = f.execute(inputDocument, inputDocument, iValue, iContext);
            } else {
                if (v == null) {
                    // SIMPLE NULL VALUE: SET IT IN DOCUMENT
                    iValue.field(prjName, v);
                    continue;
                }
                projectionValue = v;
            }
            if (projectionValue != null)
                if (projectionValue instanceof ORidBag)
                    iValue.field(prjName, new ORidBag((ORidBag) projectionValue));
                else if (projectionValue instanceof OIdentifiable && !(projectionValue instanceof ORID) && !(projectionValue instanceof ORecord))
                    iValue.field(prjName, ((OIdentifiable) projectionValue).getRecord());
                else if (projectionValue instanceof Iterator) {
                    boolean link = true;
                    // make temporary value typical case graph database elemenet's iterator edges
                    if (projectionValue instanceof OResettable)
                        ((OResettable) projectionValue).reset();
                    final List<Object> iteratorValues = new ArrayList<Object>();
                    final Iterator projectionValueIterator = (Iterator) projectionValue;
                    while (projectionValueIterator.hasNext()) {
                        Object value = projectionValueIterator.next();
                        if (value instanceof OIdentifiable) {
                            value = ((OIdentifiable) value).getRecord();
                            if (value != null && !((OIdentifiable) value).getIdentity().isPersistent())
                                link = false;
                        }
                        if (value != null)
                            iteratorValues.add(value);
                    }
                    iValue.field(prjName, iteratorValues, link ? OType.LINKLIST : OType.EMBEDDEDLIST);
                } else if (projectionValue instanceof ODocument && !((ODocument) projectionValue).getIdentity().isPersistent()) {
                    iValue.field(prjName, projectionValue, OType.EMBEDDED);
                } else if (projectionValue instanceof Set<?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKSET && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
                        type = OType.EMBEDDEDSET;
                    iValue.field(prjName, projectionValue, type);
                } else if (projectionValue instanceof Map<?, ?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKMAP && !entriesPersistent(((Map<?, OIdentifiable>) projectionValue).values()))
                        type = OType.EMBEDDEDMAP;
                    iValue.field(prjName, projectionValue, type);
                } else if (projectionValue instanceof List<?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKLIST && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
                        type = OType.EMBEDDEDLIST;
                    iValue.field(prjName, projectionValue, type);
                } else
                    iValue.field(prjName, projectionValue);
        }
    }
    return iValue;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OSQLFilterItemAbstract(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemAbstract) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLMethodField(com.orientechnologies.orient.core.sql.method.misc.OSQLMethodField) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OResettable(com.orientechnologies.common.util.OResettable) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OType(com.orientechnologies.orient.core.metadata.schema.OType) OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable) OPair(com.orientechnologies.common.util.OPair) ORecord(com.orientechnologies.orient.core.record.ORecord)

Aggregations

OSQLFilterItemVariable (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)4 OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)3 ORecord (com.orientechnologies.orient.core.record.ORecord)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 Collection (java.util.Collection)2 OPair (com.orientechnologies.common.util.OPair)1 OResettable (com.orientechnologies.common.util.OResettable)1 OCommandExecutorNotFoundException (com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException)1 OAutoConvertToRecord (com.orientechnologies.orient.core.db.record.OAutoConvertToRecord)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 OIndexSearchResult (com.orientechnologies.orient.core.sql.OIndexSearchResult)1 OSQLFilterItemAbstract (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemAbstract)1 OSQLPredicate (com.orientechnologies.orient.core.sql.filter.OSQLPredicate)1 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)1 OSQLMethodField (com.orientechnologies.orient.core.sql.method.misc.OSQLMethodField)1