Friday, 20 December 2013

OrientDB: working with types

London, December 20th 2013

OrientDB Team is working on SQL language to provide more powerful functions to works against types. Since OrientDB can work in schema-less mode, it's hard to know the real type of a value. For example when you work with numbers, you could have different type stored. How to know if a number is a Long or an Integer?

.type() method (since 2.0)

Here comes the new type() method (all these methods are available on 2.0-SNAPSHOT, "develop" branch), that returns the OrientDB type of a value. Let's try it against the ROLES property of OUser class.

select roles.type() from ouser

It's an LINKSET!

NOTE: .type() method works on the SQL pipeline, so it doesn't access to the stored type, but the type loaded in memory and in case of SET, LIST and MAP sometimes can't distinguish between EMBEDDED types and LINK types.

.javaType() method (since 2.0)

When you want to know the respective Java class, you can use the method .javaType(), example:

select roles.javaType() from ouser

Now we can access to the type it would be cool to convert values at the fly. OrientDB already has few methods to to this:
  • asBoolean()
  • asDate()
  • asDateTime()
  • asDecimal()
  • asFloat()
  • asInteger()
  • asList()
  • asLong()
  • asSet()
  • asString()
But we've implemented a generic convert(<type>) method to convert values dynamically.

.convert(<type>) method (since 2.0)

convert() accepts as unique, mandatory, parameter the OrientDB type (OType enumeration) or any Java type recognized by OrientDB. Example using the STRING OrientDB type:

select roles.convert(STRING) from ouser

And the same using the Java type:

select roles.convert(java.lang.String) from ouser

Since the type is an EMBEDDEDSET, the more interesting thing is converting into a list to get the first value. Example:

select roles.convert(EMBEDDEDLIST)[0] from ouser


Happy Christmas!

Luca Garulli
CEO at Orient Technologies
the Company behind OrientDB

No comments:

Post a Comment

Note: only a member of this blog may post a comment.