Search in sources :

Example 61 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OCSVExtractor method fetchNext.

private OExtractedItem fetchNext(CSVRecord csvRecord) {
    ODocument doc = new ODocument();
    final Map<String, String> recordAsMap = csvRecord.toMap();
    if (columnTypes.isEmpty()) {
        for (Map.Entry<String, String> en : recordAsMap.entrySet()) {
            final String value = en.getValue();
            if (!csvFormat.getAllowMissingColumnNames() || !en.getKey().isEmpty()) {
                if (value == null || nullValue.equals(value) || value.isEmpty())
                    doc.field(en.getKey(), null, OType.ANY);
                else
                    doc.field(en.getKey(), determineTheType(value));
            }
        }
    } else {
        for (Map.Entry<String, OType> typeEntry : columnTypes.entrySet()) {
            final String fieldName = typeEntry.getKey();
            final OType fieldType = typeEntry.getValue();
            String fieldValueAsString = recordAsMap.get(fieldName);
            try {
                if (fieldType.getDefaultJavaType().equals(Date.class)) {
                    if (fieldType.equals(OType.DATE))
                        doc.field(fieldName, transformToDate(fieldValueAsString));
                    else
                        doc.field(fieldName, transformToDateTime(fieldValueAsString));
                } else {
                    Object fieldValue = OType.convert(fieldValueAsString, fieldType.getDefaultJavaType());
                    doc.field(fieldName, fieldValue);
                }
            } catch (Exception e) {
                processor.getStats().incrementErrors();
                log(OETLProcessor.LOG_LEVELS.ERROR, "Error on converting row %d field '%s' , value '%s' (class:%s) to type: %s", csvRecord.getRecordNumber(), fieldName, fieldValueAsString, fieldValueAsString.getClass().getName(), fieldType);
            }
        }
    }
    log(DEBUG, "document=%s", doc);
    current++;
    return new OExtractedItem(current, doc);
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OExtractedItem(com.orientechnologies.orient.etl.OExtractedItem) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) ParseException(java.text.ParseException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 62 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OrientJdbcDatabaseMetaData method getPropertyAsDocument.

private ODocument getPropertyAsDocument(final OClass clazz, final OProperty prop) {
    database.activateOnCurrentThread();
    final OType type = prop.getType();
    return new ODocument().field("TABLE_CAT", database.getName()).field("TABLE_SCHEM", database.getName()).field("TABLE_NAME", clazz.getName()).field("COLUMN_NAME", prop.getName()).field("DATA_TYPE", OrientJdbcResultSetMetaData.getSqlType(type)).field("TYPE_NAME", type.name()).field("COLUMN_SIZE", 1).field("BUFFER_LENGTH", null, OType.INTEGER).field("DECIMAL_DIGITS", null, OType.INTEGER).field("NUM_PREC_RADIX", 10).field("NULLABLE", !prop.isNotNull() ? columnNoNulls : columnNullable).field("REMARKS", prop.getDescription()).field("COLUMN_DEF", prop.getDefaultValue()).field("SQL_DATA_TYPE", null, OType.INTEGER).field("SQL_DATETIME_SUB", null, OType.INTEGER).field("CHAR_OCTET_LENGTH", null, OType.INTEGER).field("ORDINAL_POSITION", prop.getId(), OType.INTEGER).field("IS_NULLABLE", prop.isNotNull() ? "NO" : "YES");
//
//     *  <LI><B>SCOPE_CATALOG</B> String {@code =>} catalog of table that is the scope
//        *      of a reference attribute (<code>null</code> if DATA_TYPE isn't REF)
//        *  <LI><B>SCOPE_SCHEMA</B> String {@code =>} schema of table that is the scope
//        *      of a reference attribute (<code>null</code> if the DATA_TYPE isn't REF)
//        *  <LI><B>SCOPE_TABLE</B> String {@code =>} table name that this the scope
//     *      of a reference attribute (<code>null</code> if the DATA_TYPE isn't REF)
//        *  <LI><B>SOURCE_DATA_TYPE</B> short {@code =>} source type of a distinct type or user-generated
//        *      Ref type, SQL type from java.sql.Types (<code>null</code> if DATA_TYPE
//        *      isn't DISTINCT or user-generated REF)
//        *   <LI><B>IS_AUTOINCREMENT</B> String  {@code =>} Indicates whether this column is auto incremented
//     *       <UL>
//     *       <LI> YES           --- if the column is auto incremented
//        *       <LI> NO            --- if the column is not auto incremented
//     *       <LI> empty string  --- if it cannot be determined whether the column is auto incremented
//     *       </UL>
//     *   <LI><B>IS_GENERATEDCOLUMN</B> String  {@code =>} Indicates whether this is a generated column
//     *       <UL>
//     *       <LI> YES           --- if this a generated column
//        *       <LI> NO            --- if this not a generated column
//     *       <LI> empty string  --- if it cannot be determined whether this is a generated column
//     *       </UL>
//     *  </OL>
//
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 63 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OrientJdbcResultSetMetaData method getColumnTypeName.

@Override
public String getColumnTypeName(final int column) throws SQLException {
    final ODocument currentRecord = getCurrentRecord();
    OType columnType = currentRecord.fieldType(currentRecord.fieldNames()[column - 1]);
    if (columnType == null)
        return null;
    return columnType.toString();
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 64 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OObjectEntitySerializer method multiValueToStream.

@SuppressWarnings("unchecked")
private static Object multiValueToStream(final Object iMultiValue, OType iType, final ODatabaseObject db, final ODocument iRecord) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (sourceValues.size() == 0)
        return iMultiValue;
    // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
    final Object firstValue = sourceValues.iterator().next();
    if (firstValue == null)
        return iMultiValue;
    if (iType == null) {
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (isToSerialize(firstValue.getClass()))
            result = new HashSet<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDSET)) || OType.isSimpleType(firstValue))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (isToSerialize(firstValue.getClass()))
            result = new ArrayList<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDLIST)) || OType.isSimpleType(firstValue))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ORecordLazyList(iRecord);
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        if (firstValue instanceof List)
            linkedType = OType.EMBEDDEDLIST;
        else if (firstValue instanceof Set)
            linkedType = OType.EMBEDDEDSET;
        else if (firstValue instanceof Map)
            linkedType = OType.EMBEDDEDMAP;
        else
            linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Set<Object>) result).add(typeToStream(o, linkedType, db, null));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, db, null));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (isToSerialize(firstValue.getClass()))
                result = new HashMap<Object, Object>();
            else if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new ORecordLazyMap(iRecord);
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, db, null));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) HashSet(java.util.HashSet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Entry(java.util.Map.Entry) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) Collection(java.util.Collection) ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OSchemaProxyObject(com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject) List(java.util.List) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) HashSet(java.util.HashSet)

Example 65 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OObjectProxyMethodHandler method manageMapSave.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object manageMapSave(final Object self, final Field f, Map<?, ?> value, final boolean customSerialization) {
    final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
    if (customSerialization) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        setDocFieldValue(f.getName(), map, OType.EMBEDDEDMAP);
        value = new OObjectCustomSerializerMap<TYPE>(OObjectEntitySerializer.getSerializedType(f), doc, map, (Map<Object, Object>) value);
    } else if (genericType != null && genericType.isEnum()) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        setDocFieldValue(f.getName(), map, OType.EMBEDDEDMAP);
        value = new OObjectEnumLazyMap(genericType, doc, map, (Map<Object, Object>) value);
    } else if (!(value instanceof OObjectLazyMultivalueElement)) {
        OType type = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName()) ? OType.EMBEDDEDMAP : OType.LINKMAP;
        if (doc.fieldType(f.getName()) != type)
            doc.field(f.getName(), doc.field(f.getName()), type);
        Map<Object, OIdentifiable> docMap = doc.field(f.getName(), type);
        if (docMap == null) {
            if (OType.EMBEDDEDMAP == type)
                docMap = new OTrackedMap<OIdentifiable>(doc);
            else
                docMap = new ORecordLazyMap(doc);
            setDocFieldValue(f.getName(), docMap, type);
        }
        value = new OObjectLazyMap(self, docMap, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
    }
    return value;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) HashMap(java.util.HashMap) OType(com.orientechnologies.orient.core.metadata.schema.OType) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) TYPE(com.orientechnologies.orient.core.hook.ORecordHook.TYPE) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OObjectCustomSerializerMap(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap) HashMap(java.util.HashMap) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

Aggregations

OType (com.orientechnologies.orient.core.metadata.schema.OType)65 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)21 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)15 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)12 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)11 Map (java.util.Map)10 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)5 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)5 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)5 OBinarySerializerFactory (com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)5 Collection (java.util.Collection)5 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)4 ORID (com.orientechnologies.orient.core.id.ORID)4