protected void parseValues() { final int beginFields = parserGetCurrentPosition(); final int endFields = parserText.indexOf(')', beginFields + 1); if (endFields == -1) throwSyntaxErrorException("Missed closed brace"); final ArrayList<String> fieldNames = new ArrayList<String>(); parserSetCurrentPosition( OStringSerializerHelper.getParameters(parserText, beginFields, endFields, fieldNames)); if (fieldNames.size() == 0) throwSyntaxErrorException("Set of fields is empty. Example: (name, surname)"); // REMOVE QUOTATION MARKS IF ANY for (int i = 0; i < fieldNames.size(); ++i) fieldNames.set(i, OStringSerializerHelper.removeQuotationMarks(fieldNames.get(i))); parserRequiredKeyword(KEYWORD_VALUES); parserSkipWhiteSpaces(); if (parserIsEnded() || parserText.charAt(parserGetCurrentPosition()) != '(') { throwParsingException("Set of values is missed. Example: ('Bill', 'Stuart', 300)"); } int blockStart = parserGetCurrentPosition(); int blockEnd = parserGetCurrentPosition(); final List<String> records = OStringSerializerHelper.smartSplit( parserText, new char[] {','}, blockStart, -1, true, true, false); for (String record : records) { final List<String> values = new ArrayList<String>(); blockEnd += OStringSerializerHelper.getParameters(record, 0, -1, values); if (blockEnd == -1) throw new OCommandSQLParsingException( "Missed closed brace. Use " + getSyntax(), parserText, blockStart); if (values.isEmpty()) throw new OCommandSQLParsingException( "Set of values is empty. Example: ('Bill', 'Stuart', 300). Use " + getSyntax(), parserText, blockStart); if (values.size() != fieldNames.size()) throw new OCommandSQLParsingException( "Fields not match with values", parserText, blockStart); // TRANSFORM FIELD VALUES final Map<String, Object> fields = new LinkedHashMap<String, Object>(); for (int i = 0; i < values.size(); ++i) fields.put( fieldNames.get(i), OSQLHelper.parseValue( this, OStringSerializerHelper.decode(values.get(i).trim()), context)); newRecords.add(fields); blockStart = blockEnd; } }
public static Object simpleValueFromStream(final Object iValue, final OType iType) { switch (iType) { case STRING: if (iValue instanceof String) { final String s = OStringSerializerHelper.getStringContent(iValue); return OStringSerializerHelper.decode(s); } return iValue.toString(); case INTEGER: if (iValue instanceof Integer) return iValue; return new Integer(iValue.toString()); case BOOLEAN: if (iValue instanceof Boolean) return iValue; return new Boolean(iValue.toString()); case FLOAT: if (iValue instanceof Float) return iValue; return convertValue((String) iValue, iType); case DECIMAL: if (iValue instanceof BigDecimal) return iValue; return convertValue((String) iValue, iType); case LONG: if (iValue instanceof Long) return iValue; return convertValue((String) iValue, iType); case DOUBLE: if (iValue instanceof Double) return iValue; return convertValue((String) iValue, iType); case SHORT: if (iValue instanceof Short) return iValue; return convertValue((String) iValue, iType); case BYTE: if (iValue instanceof Byte) return iValue; return convertValue((String) iValue, iType); case BINARY: return OStringSerializerHelper.getBinaryContent(iValue); case DATE: case DATETIME: if (iValue instanceof Date) return iValue; return convertValue((String) iValue, iType); case LINK: if (iValue instanceof ORID) return iValue.toString(); else if (iValue instanceof String) return new ORecordId((String) iValue); else return ((ORecord<?>) iValue).getIdentity().toString(); } throw new IllegalArgumentException("Type " + iType + " is not simple type."); }
/** * Parses a string returning the value with the closer type. Numbers by default are INTEGER if * haven't decimal separator, otherwise FLOAT. To treat all the number types numbers are postponed * with a character that tells the type: b=byte, s=short, l=long, f=float, d=double, t=date. If * starts with # it's a RecordID. Most of the code is equals to getType() but has been copied to * speed-up it. * * @param iUnusualSymbols Localized decimal number separators * @param iValue Value to parse * @return The closest type recognized */ public static Object getTypeValue(final String iValue) { if (iValue == null) return null; if (iValue.length() == 0) return ""; if (iValue.length() > 1) if (iValue.charAt(0) == '"' && iValue.charAt(iValue.length() - 1) == '"') // STRING return OStringSerializerHelper.decode(iValue.substring(1, iValue.length() - 1)); else if (iValue.charAt(0) == OStringSerializerHelper.BINARY_BEGINEND && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.BINARY_BEGINEND) // STRING return OStringSerializerHelper.getBinaryContent(iValue); else if (iValue.charAt(0) == OStringSerializerHelper.COLLECTION_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.COLLECTION_END) { // COLLECTION final ArrayList<String> coll = new ArrayList<String>(); OStringSerializerHelper.getCollection(iValue, 0, coll); return coll; } else if (iValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.MAP_END) { // MAP return OStringSerializerHelper.getMap(iValue); } if (iValue.charAt(0) == ORID.PREFIX) // RID return new ORecordId(iValue); boolean integer = true; char c; for (int index = 0; index < iValue.length(); ++index) { c = iValue.charAt(index); if (c < '0' || c > '9') if ((index == 0 && (c == '+' || c == '-'))) continue; else if (c == DECIMAL_SEPARATOR) integer = false; else { if (index > 0) { if (!integer && c == 'E') { // CHECK FOR SCIENTIFIC NOTATION if (index < iValue.length()) index++; if (iValue.charAt(index) == '-') continue; } final String v = iValue.substring(0, index); if (c == 'f') return new Float(v); else if (c == 'c') return new BigDecimal(v); else if (c == 'l') return new Long(v); else if (c == 'd') return new Double(v); else if (c == 'b') return new Byte(v); else if (c == 'a' || c == 't') return new Date(Long.parseLong(v)); else if (c == 's') return new Short(v); } return iValue; } } if (integer) { try { return new Integer(iValue); } catch (NumberFormatException e) { return new Long(iValue); } } else return new BigDecimal(iValue); }