Skip to content
Diego Fernandez edited this page May 30, 2014 · 49 revisions

Installation

Download/install nuget package or clone repository and build/reference Orient.Client dll file.

Setup database connection pool with OClient

Pool of pre-initialized database connections can be created with OClient.CreateDatabasePool static method with connection parameters. Reason behind connection pooling is to save time during which the client connects with server. Once pool is initiated it contains a number of ready to use persistent connections which are used by ODatabase object transparently. Previously created database connect pools can be removed with OClient.DropDatabasePool static method.

OClient.CreateDatabasePool(
    "127.0.0.1",
    2424,
    "TestDatabaseName",
    ODatabaseType.Graph,
    "admin",
    "admin",
    10,
    "myTestDatabaseAlias"
);

Working with ODatabase instance

Pre-created connections in the database pool are used when ODatabase class is instantiated with alias parameter. Use of ODatabase instance should be short living in order to return assigned connection to the pool as soon as possible for further reuse.

using (ODatabase database = new ODatabase("myTestDatabaseAlias"))
{
    // use database functionality within this context
}

If the database instance isn't enclosed with using keyword it should be closed as soon as possible by calling Close or Dispose methods.

ODatabase database = new ODatabase("myTestDatabaseAlias");

// some code dealing with database instance

database.Close(); // or database.Dispose()

ODocument class

Database operations return results in the form of ODocument objects which contain several OrientDB specific properties and encapsulated fields in the form of Dictionary<string, object> data structure similar to JSON object. These fields can be manipulated by three basic methods that are used to check, retrieve and store data.

ODocument object
OrientDB specific properties Methods for fields manipulation
ORID
OVersion
OType
OClassName
OClassId
HasField
GetField
SetField

It is also possible to retrieve or store nested/embedded objects within fields with dot notation. Example usage is shown below:

ODocument document = new ODocument();
document.SetField("foo", "foo string value");
document.SetField("bar", 12345);
document.SetField("embedded.foo", "embedded foo string value");

if (document.HasField("foo") && document.HasField("bar") && document.HasField("embedded.foo"))
{
    string foo = document.GetField<string>("foo");
    int foo = document.GetField<int>("bar");
    string embeddedFoo = document.GetField<string>("embedded.foo");
}

Query and Command methods

ODatabase class provides Query method for idempotent (e.g. select) and Command method for non-idempotent (e.g. insert, update, ...) commands for executing raw SQL query strings. To know more about OrientDB querying capabilities check out its SQL docs.

using (ODatabase database = new ODatabase("myTestDatabaseAlias"))
{
    database.Command("INSERT INTO Profile (name, surname) values ('Jay', 'Miner')");

    List<ODocument> result = database.Query("SELECT FROM Profile");

    foreach (ODocument document in result)
    {
        Console.WriteLine(
            "Doc {0} class {1}: {2} {3}", 
            document.ORID.ToString(), 
            document.OClassName,
            document.GetField<string>("name"),
            document.GetField<string>("surname")
        );
    }
}

Fluent API for SQL queries

ODatabase instance, apart from raw SQL strings, also provides fluent API (aka chaining) to help developers construct SQL commands in a more generic and structured way. For example instead of writing and executing raw SQL query in string form, fluent API provides chainable method calls which help to construct and execute the query.

Raw string Fluent/chaining
``` csharp List result = database.Query( "SELECT foo AS myFoo, " + "bar AS myBar " + "FROM OGraphVertex " + "WHERE someCollection CONTAINS 'some value' " + "AND someField = 123" ); ``` ```csharp List result = database .Select("foo").As("myFoo") .Also("bar").As("myBar") .From() .Where("someCollection").Contains("some value") .And("someField").Equals(123) .ToList(); ```

ToList method in case of idempotent operations and Run method in case of non-idempotent operations is used to execute the underlying SQL query which was constructed by the fluent API. ToString method returns actual raw SQL query string.

ToString Run ToList
```csharp // CREATE CLASS TestVertexClass EXTENDS OGraphVertex string query = database .Create.Class("TestClass") .Extends("OGraphVertex") .ToString(); ``` ```csharp short classId = database .Create.Class("TestClass") .Run(); ``` ```csharp List result = database .Select("foo", "bar") .From("TestClass") .ToList(); ```

More examples on using the fluent API: