Search in sources :

Example 6 with OSQLFunctionRuntime

use of com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime in project orientdb by orientechnologies.

the class ORuntimeResult method getResult.

public static ODocument getResult(final ODocument iValue, final Map<String, Object> iProjections) {
    if (iValue != null) {
        boolean canExcludeResult = false;
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            if (!iValue.containsField(projection.getKey())) {
                // ONLY IF NOT ALREADY CONTAINS A VALUE, OTHERWISE HAS BEEN SET MANUALLY (INDEX?)
                final Object v = projection.getValue();
                if (v instanceof OSQLFunctionRuntime) {
                    final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
                    canExcludeResult = f.filterResult();
                    Object fieldValue = f.getResult();
                    if (fieldValue != null)
                        iValue.field(projection.getKey(), fieldValue);
                }
            }
        }
        if (canExcludeResult && iValue.isEmpty())
            // RESULT EXCLUDED FOR EMPTY RECORD
            return null;
        // AVOID SAVING OF TEMP RECORD
        ORecordInternal.unsetDirty(iValue);
    }
    return iValue;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)

Example 7 with OSQLFunctionRuntime

use of com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime in project orientdb by orientechnologies.

the class OSQLHelper method parseValue.

/**
   * Convert fields from text to real value. Supports: String, RID, Boolean, Float, Integer and NULL.
   * 
   * @param iValue
   *          Value to convert.
   * @return The value converted if recognized, otherwise VALUE_NOT_PARSED
   */
public static Object parseValue(String iValue, final OCommandContext iContext) {
    if (iValue == null)
        return null;
    iValue = iValue.trim();
    Object fieldValue = VALUE_NOT_PARSED;
    if (iValue.startsWith("'") && iValue.endsWith("'") || iValue.startsWith("\"") && iValue.endsWith("\""))
        // STRING
        fieldValue = OStringSerializerHelper.decode(OIOUtils.getStringContent(iValue));
    else if (iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.LIST_END) {
        // COLLECTION/ARRAY
        final List<String> items = OStringSerializerHelper.smartSplit(iValue.substring(1, iValue.length() - 1), OStringSerializerHelper.RECORD_SEPARATOR);
        final List<Object> coll = new ArrayList<Object>();
        for (String item : items) {
            coll.add(parseValue(item, iContext));
        }
        fieldValue = coll;
    } else if (iValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.MAP_END) {
        // MAP
        final List<String> items = OStringSerializerHelper.smartSplit(iValue.substring(1, iValue.length() - 1), OStringSerializerHelper.RECORD_SEPARATOR);
        final Map<Object, Object> map = new HashMap<Object, Object>();
        for (String item : items) {
            final List<String> parts = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR);
            if (parts == null || parts.size() != 2)
                throw new OCommandSQLParsingException("Map found but entries are not defined as <key>:<value>");
            Object key = OStringSerializerHelper.decode(parseValue(parts.get(0), iContext).toString());
            Object value = parseValue(parts.get(1), iContext);
            if (VALUE_NOT_PARSED == value) {
                value = new OSQLPredicate(parts.get(1)).evaluate(iContext);
            }
            map.put(key, value);
        }
        if (map.containsKey(ODocumentHelper.ATTRIBUTE_TYPE))
            // IT'S A DOCUMENT
            // TODO: IMPROVE THIS CASE AVOIDING DOUBLE PARSING
            fieldValue = new ODocument().fromJSON(iValue);
        else
            fieldValue = map;
    } else if (iValue.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.EMBEDDED_END) {
        // SUB-COMMAND
        fieldValue = new OCommandSQL(iValue.substring(1, iValue.length() - 1));
        ((OCommandSQL) fieldValue).getContext().setParent(iContext);
    } else if (ORecordId.isA(iValue))
        // RID
        fieldValue = new ORecordId(iValue.trim());
    else {
        if (iValue.equalsIgnoreCase("null"))
            // NULL
            fieldValue = null;
        else if (iValue.equalsIgnoreCase("not null"))
            // NULL
            fieldValue = NOT_NULL;
        else if (iValue.equalsIgnoreCase("defined"))
            // NULL
            fieldValue = DEFINED;
        else if (iValue.equalsIgnoreCase("true"))
            // BOOLEAN, TRUE
            fieldValue = Boolean.TRUE;
        else if (iValue.equalsIgnoreCase("false"))
            // BOOLEAN, FALSE
            fieldValue = Boolean.FALSE;
        else if (iValue.startsWith("date(")) {
            final OSQLFunctionRuntime func = OSQLHelper.getFunction(null, iValue);
            if (func != null) {
                fieldValue = func.execute(null, null, null, iContext);
            }
        } else {
            final Object v = parseStringNumber(iValue);
            if (v != null)
                fieldValue = v;
        }
    }
    return fieldValue;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) HashMap(java.util.HashMap) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OSQLPredicate(com.orientechnologies.orient.core.sql.filter.OSQLPredicate) ArrayList(java.util.ArrayList) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 8 with OSQLFunctionRuntime

use of com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime in project orientdb by orientechnologies.

the class OSQLHelper method parseDefaultValue.

public static Object parseDefaultValue(ODocument iRecord, final String iWord) {
    final Object v = OSQLHelper.parseValue(iWord, null);
    if (v != VALUE_NOT_PARSED) {
        return v;
    }
    // TRY TO PARSE AS FUNCTION
    final OSQLFunctionRuntime func = OSQLHelper.getFunction(null, iWord);
    if (func != null) {
        return func.execute(iRecord, iRecord, null, null);
    }
    // PARSE AS FIELD
    return iWord;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)

Example 9 with OSQLFunctionRuntime

use of com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime in project orientdb by orientechnologies.

the class ODocumentHelper method evaluateFunction.

public static Object evaluateFunction(final Object currentValue, final String iFunction, final OCommandContext iContext) {
    if (currentValue == null)
        return null;
    Object result = null;
    final String function = iFunction.toUpperCase();
    if (function.startsWith("SIZE("))
        result = currentValue instanceof ORecord ? 1 : OMultiValue.getSize(currentValue);
    else if (function.startsWith("LENGTH("))
        result = currentValue.toString().length();
    else if (function.startsWith("TOUPPERCASE("))
        result = currentValue.toString().toUpperCase();
    else if (function.startsWith("TOLOWERCASE("))
        result = currentValue.toString().toLowerCase();
    else if (function.startsWith("TRIM("))
        result = currentValue.toString().trim();
    else if (function.startsWith("TOJSON("))
        result = currentValue instanceof ODocument ? ((ODocument) currentValue).toJSON() : null;
    else if (function.startsWith("KEYS("))
        result = currentValue instanceof Map<?, ?> ? ((Map<?, ?>) currentValue).keySet() : null;
    else if (function.startsWith("VALUES("))
        result = currentValue instanceof Map<?, ?> ? ((Map<?, ?>) currentValue).values() : null;
    else if (function.startsWith("ASSTRING("))
        result = currentValue.toString();
    else if (function.startsWith("ASINTEGER("))
        result = new Integer(currentValue.toString());
    else if (function.startsWith("ASFLOAT("))
        result = new Float(currentValue.toString());
    else if (function.startsWith("ASBOOLEAN(")) {
        if (currentValue instanceof String)
            result = new Boolean((String) currentValue);
        else if (currentValue instanceof Number) {
            final int bValue = ((Number) currentValue).intValue();
            if (bValue == 0)
                result = Boolean.FALSE;
            else if (bValue == 1)
                result = Boolean.TRUE;
        }
    } else if (function.startsWith("ASDATE("))
        if (currentValue instanceof Date)
            result = currentValue;
        else if (currentValue instanceof Number)
            result = new Date(((Number) currentValue).longValue());
        else
            try {
                result = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().getDateFormatInstance().parse(currentValue.toString());
            } catch (ParseException e) {
            }
    else if (function.startsWith("ASDATETIME("))
        if (currentValue instanceof Date)
            result = currentValue;
        else if (currentValue instanceof Number)
            result = new Date(((Number) currentValue).longValue());
        else
            try {
                result = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().getDateTimeFormatInstance().parse(currentValue.toString());
            } catch (ParseException e) {
            }
    else {
        // EXTRACT ARGUMENTS
        final List<String> args = OStringSerializerHelper.getParameters(iFunction.substring(iFunction.indexOf('(')));
        final ORecord currentRecord = iContext != null ? (ORecord) iContext.getVariable("$current") : null;
        for (int i = 0; i < args.size(); ++i) {
            final String arg = args.get(i);
            final Object o = OSQLHelper.getValue(arg, currentRecord, iContext);
            if (o != null)
                args.set(i, o.toString());
        }
        if (function.startsWith("CHARAT("))
            result = currentValue.toString().charAt(Integer.parseInt(args.get(0)));
        else if (function.startsWith("INDEXOF("))
            if (args.size() == 1)
                result = currentValue.toString().indexOf(OIOUtils.getStringContent(args.get(0)));
            else
                result = currentValue.toString().indexOf(OIOUtils.getStringContent(args.get(0)), Integer.parseInt(args.get(1)));
        else if (function.startsWith("SUBSTRING("))
            if (args.size() == 1)
                result = currentValue.toString().substring(Integer.parseInt(args.get(0)));
            else
                result = currentValue.toString().substring(Integer.parseInt(args.get(0)), Integer.parseInt(args.get(1)));
        else if (function.startsWith("APPEND("))
            result = currentValue.toString() + OIOUtils.getStringContent(args.get(0));
        else if (function.startsWith("PREFIX("))
            result = OIOUtils.getStringContent(args.get(0)) + currentValue.toString();
        else if (function.startsWith("FORMAT("))
            if (currentValue instanceof Date) {
                SimpleDateFormat formatter = new SimpleDateFormat(OIOUtils.getStringContent(args.get(0)));
                formatter.setTimeZone(ODateHelper.getDatabaseTimeZone());
                result = formatter.format(currentValue);
            } else
                result = String.format(OIOUtils.getStringContent(args.get(0)), currentValue.toString());
        else if (function.startsWith("LEFT(")) {
            final int len = Integer.parseInt(args.get(0));
            final String stringValue = currentValue.toString();
            result = stringValue.substring(0, len <= stringValue.length() ? len : stringValue.length());
        } else if (function.startsWith("RIGHT(")) {
            final int offset = Integer.parseInt(args.get(0));
            final String stringValue = currentValue.toString();
            result = stringValue.substring(offset < stringValue.length() ? stringValue.length() - offset : 0);
        } else {
            final OSQLFunctionRuntime f = OSQLHelper.getFunction(null, iFunction);
            if (f != null)
                result = f.execute(currentRecord, currentRecord, null, iContext);
        }
    }
    return result;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) ORecord(com.orientechnologies.orient.core.record.ORecord) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 10 with OSQLFunctionRuntime

use of com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime in project orientdb by orientechnologies.

the class OSQLHelper method getFunction.

public static OSQLFunctionRuntime getFunction(final OBaseParser iCommand, final String iWord) {
    final int separator = iWord.indexOf('.');
    final int beginParenthesis = iWord.indexOf(OStringSerializerHelper.EMBEDDED_BEGIN);
    if (beginParenthesis > -1 && (separator == -1 || separator > beginParenthesis)) {
        final int endParenthesis = iWord.indexOf(OStringSerializerHelper.EMBEDDED_END, beginParenthesis);
        final char firstChar = iWord.charAt(0);
        if (endParenthesis > -1 && (firstChar == '_' || Character.isLetter(firstChar)))
            // FUNCTION: CREATE A RUN-TIME CONTAINER FOR IT TO SAVE THE PARAMETERS
            return new OSQLFunctionRuntime(iCommand, iWord);
    }
    return null;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)

Aggregations

OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)17 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)5 ORID (com.orientechnologies.orient.core.id.ORID)3 ORecord (com.orientechnologies.orient.core.record.ORecord)3 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)3 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 OCommandParameters (com.orientechnologies.orient.core.sql.OCommandParameters)2 OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)2 OSQLFunctionCount (com.orientechnologies.orient.core.sql.functions.misc.OSQLFunctionCount)2 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)2 ArrayList (java.util.ArrayList)2 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1 OSortedMultiIterator (com.orientechnologies.common.collection.OSortedMultiIterator)1 OException (com.orientechnologies.common.exception.OException)1 OProfiler (com.orientechnologies.common.profiler.OProfiler)1 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)1 OPair (com.orientechnologies.common.util.OPair)1 OResettable (com.orientechnologies.common.util.OResettable)1