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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations