howard roark
howard roark

Reputation: 638

How to write a "UPSERT" query for Orientdb in .net, C#?

I am trying to write a query in .net for orient db and I am not sure if the API supports UPSERT as is in the documentation.

Upvotes: 2

Views: 562

Answers (1)

LucaS
LucaS

Reputation: 1418

I can give you an example of UPDATE / UPSERT with C#. The following code creates a new TestDatabaseName database and relative classes (Users) and properties (userID) otherwise, if the database already exists, it executes the UPDATE / UPSERT command.

C# CODE:

using System;
using Orient.Client;

namespace Stack37995408

{

    static class Stack37995408

    {

        private static string _hostname = "127.0.0.1";
        private static int _port = 2424;
        private static string _rootUserName = "root";
        private static string _rootUserPassword = "root";

        private static OServer _server;

        private static string _DBname = "TestDatabaseName";
        private static string _username = "root";
        private static string _password = "root";
        private static string _aliasDB = "myTestDatabaseAlias";

        static void Main(string[] args)

        {

            _server = new OServer(_hostname, _port, _rootUserName, _rootUserPassword);

            if (!_server.DatabaseExist(_DBname, OStorageType.PLocal))

            {

                _server.CreateDatabase(_DBname, ODatabaseType.Graph, OStorageType.PLocal);

                Console.WriteLine("Database " + _DBname + " created");

                //Connect to the DB

                OClient.CreateDatabasePool(
                    _hostname,
                    _port,
                    _DBname,
                    ODatabaseType.Graph,
                    _username,
                    _password,
                    10,
                    _aliasDB
                );

                Console.WriteLine("Connected to the DB " + _DBname);

                using (ODatabase database = new ODatabase(_aliasDB))

                {

                    //Classes and properties creation

                    database
                        .Create
                        .Class("Users")
                        .Extends<OVertex>()
                        .Run();

                    database
                        .Create
                        .Property("userID", OType.Integer)
                        .Class("users")
                        .Run();

                    //Populate the DB

                    OVertex vertexUser = new OVertex();
                    vertexUser.OClassName = "Users";
                    vertexUser
                        .SetField("userID", 1);

                    OVertex createVertexUser = database
                        .Create.Vertex(vertexUser)
                        .Run();

                    Console.WriteLine("Created vertex " + createVertexUser.OClassName + " with @rid " + createVertexUser.ORID + " and userID " + createVertexUser.GetField<int>("userID"));

                }

            }

            else

            {

                //Connection

                OClient.CreateDatabasePool(
                    _hostname,
                    _port,
                    _DBname,
                    ODatabaseType.Graph,
                    _username,
                    _password,
                    10,
                    _aliasDB
                );

                Console.WriteLine("Connected to the DB " + _DBname);

                using (ODatabase database = new ODatabase(_aliasDB))

                {

                    database
                        .Update()
                        .Class("Users")
                        .Set("userID", 2)
                        .Upsert()
                        .Where("userID")
                        .Equals(2)
                        .Run();

                    Console.WriteLine("Operation executed");

                }

            }

        }

    }

}

First execution (DB, classes, properties and first vertex creation):

Output:

Started Thread 4320
Database TestDatabaseName created
Started Thread 7184
Connected to the DB TestDatabaseName
Created vertex Users with @rid #11:0 and userID 1
Started Thread 4368

Studio Output:

enter image description here

Second execution (I try to update a vertex with userID = 2 but the UPSERT command creates it because it doesn't exist):

Section:

database
    .Update()
    .Class("Users")
    .Set("userID", 2)
    .Upsert()
    .Where("userID")
    .Equals(2)
    .Run();

Output:

Started Thread 6172
Started Thread 7244
Connected to the DB TestDatabaseName
Operation executed
Started Thread 5860

Studio Output:

enter image description here

Hope it helps

Upvotes: 2

Related Questions