如何在 .net、C# 中为 Orientdb 编写 "UPSERT" 查询?

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

我正在尝试在 .net 中为 orient db 编写查询,但我不确定 API 是否像文档中那样支持 UPSERT。

我可以举一个 UPDATE / UPSERTC# 的例子。 下面的代码创建一个新的 TestDatabaseName 数据库和相关的 类 (Users) 和属性 (userID) 否则,如果数据库已经存在,它会执行 UPDATE / UPSERT命令。

C# 代码:

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");

                }

            }

        }

    }

}

第一次执行(数据库,类,属性和第一个顶点创建):

输出:

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

工作室输出:

第二次执行(我尝试用 userID = 2 更新一个顶点,但是 UPSERT 命令创建了它,因为它不存在):

栏目:

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

输出:

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

工作室输出:

希望对您有所帮助