Exemple #1
0
        public bool InsertEdge <TT, TTT, T>(string database, ref T document, ref TT from, ref TTT to)
        {
            database.ThrowIfNullOrEmpty("database");
            from.ThrowIfNull("from Vertex");
            to.ThrowIfNull("to Vertex");

            if (from.GetType().BaseType != typeof(ODocument))
            {
                throw new InvalidOperationException(
                          "Inconsistent type specified - type for from<T> must type ODocument");
            }

            if (to.GetType().BaseType != typeof(ODocument))
            {
                throw new InvalidOperationException(
                          "Inconsistent type specified - type for to<T> must type ODocument");
            }


            string command = "create edge {0} from {1} to {2} set ".F(typeof(T).Name, (from as ODocument).ORID, (to as ODocument).ORID);


            Type classType = typeof(T);

            foreach (
                PropertyInfo propertyInfo in
                classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
            {
                if (propertyInfo.CanRead && propertyInfo.CanWrite)
                {
                    OrientdbProperty oprop = propertyInfo.GetOrientdbPropertyAttribute();
                    if (oprop == null)
                    {
                        string propertyName  = propertyInfo.Name;
                        object propertyValue = propertyInfo.GetValue(document, null);

                        if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            propertyValue = ((DateTime)propertyValue).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                        }

                        command = "{0} {1} = '{2}',".F(command, propertyName, propertyValue);
                    }
                }
            }
            if (command.EndsWith(","))
            {
                command = command.Remove(command.Length - 1);
            }

            OrientdbResponse <BaseResult <T> > request = Command <BaseResult <T> >(command, database, CommandLanguage.Sql);

            if (request.Success)
            {
                document = request.Response.Result.First();
            }

            return(request.Success);
        }
Exemple #2
0
        /// <summary>
        /// Execute a command against the database. Returns the records affected or the list of records for queries. Command executed via POST.
        /// </summary>
        /// <param name="query">The query containing the command to execute</param>
        /// <param name="database">database name</param>
        /// <param name="language">The name of the language between those supported. OrientDB distribution comes with "sql" and GraphDB distribution has both "sql" and "gremlin"</param>
        /// <returns><see cref="OrientdbResponse{DynamicDictionary}"/></returns>
        /// <see cref="http://www.orientechnologies.com/docs/last/orientdb.wiki/OrientDB-REST.html#command"/>
        public OrientdbResponse <DynamicDictionary> Command(string query, string database, CommandLanguage language)
        {
            query.ThrowIfNullOrEmpty("query");
            database.ThrowIfNullOrEmpty("database");
            string url = "command/{0}/{1}".F(Encoded(database), Encoded(language.ToString().ToLower()));

            return(OrientdbResponse.Wrap(DoRequest <Dictionary <string, object> >("POST", url, query)));
        }
Exemple #3
0
        /// <summary>
        /// Disconnect from remote server
        /// </summary>
        /// <see cref="http://www.orientechnologies.com/docs/last/orientdb.wiki/OrientDB-REST.html#disconnect"/>
        public bool Disconnect()
        {
            const string url = "disconnect";
            OrientdbResponse <DynamicDictionary> request =
                OrientdbResponse.Wrap(DoRequest <Dictionary <string, object> >("GET", url));

            return(request.HttpStatusCode == 401);
        }
Exemple #4
0
        /// <summary>
        /// Connect to a remote server using basic authentication.
        /// </summary>
        /// <param name="name">database name</param>
        /// <see cref="http://www.orientechnologies.com/docs/last/orientdb.wiki/OrientDB-REST.html#connect"/>
        public bool Connect(string name)
        {
            name.ThrowIfNullOrEmpty("name");
            string url = "connect/{0}".F(Encoded(name));

            OrientdbResponse <DynamicDictionary> request =
                OrientdbResponse.Wrap(DoRequest <Dictionary <string, object> >("GET", url));

            return(request.HttpStatusCode == 204);
        }
        public bool DeleteVertex(string database, ORID orid)
        {
            database.ThrowIfNullOrEmpty("database");
            orid.ThrowIfNull("orid");

            string command = "DELETE VERTEX {0}".F(orid.RID);

            OrientdbResponse <DynamicDictionary> response = Command(command, database, CommandLanguage.Sql);

            return(response.Success);
        }
Exemple #6
0
        public bool InsertEdge <T>(string database, ref T document, Func <CreateEdgeQueryParameters, CreateEdgeQueryParameters> requestParameters = null)
        {
            database.ThrowIfNullOrEmpty("database");
            document.ThrowIfNull("document");
            requestParameters.ThrowIfNull("CreateEdgeQueryParameters");

            CreateEdgeQueryParameters requestParams = requestParameters(new CreateEdgeQueryParameters());

            requestParams._fromQuery.ThrowIfNullOrEmpty("From Query");
            requestParams._toQuery.ThrowIfNullOrEmpty("To Query");

            Type   classType = typeof(T);
            string command   = "create edge {0} from ({1}) to ({2}) set ".F(typeof(T).Name, requestParams._fromQuery, requestParams._toQuery);

            foreach (
                PropertyInfo propertyInfo in
                classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
            {
                if (propertyInfo.CanRead && propertyInfo.CanWrite)
                {
                    OrientdbProperty oprop = propertyInfo.GetOrientdbPropertyAttribute();
                    if (oprop == null)
                    {
                        string propertyName  = propertyInfo.Name;
                        object propertyValue = propertyInfo.GetValue(document, null);

                        if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            propertyValue = ((DateTime)propertyValue).ToString("yyyy-MM-dd HH:mm:ss");
                        }

                        command = "{0} {1} = '{2}',".F(command, propertyName, propertyValue);
                    }
                }
            }
            if (command.EndsWith(","))
            {
                command = command.Remove(command.Length - 1);
            }

            OrientdbResponse <BaseResult <T> > request = Command <BaseResult <T> >(command, database, CommandLanguage.Sql);

            if (request.Success)
            {
                document = request.Response.Result.First();
            }

            return(request.Success);
        }
Exemple #7
0
        /// <summary>
        ///     The Drop Class command removes a class from the schema.
        ///     NOTE: Pay attention to maintain the schema coherent. For example avoid to
        ///     remove classes that are super classes of others. The associated cluster won't be deleted.
        /// </summary>
        /// <seealso cref="http://www.orientechnologies.com/docs/last/orientdb.wiki/SQL-Drop-Class.html" />
        public bool DropClass(string database, string className)
        {
            database.ThrowIfNullOrEmpty("database");
            className.ThrowIfNullOrEmpty("className");

            if (!ClassExist(database, className))
            {
                return(true);
            }

            string command = "DROP CLASS {0}".F(className);
            OrientdbResponse <DynamicDictionary> response = Command(command, database, CommandLanguage.Sql);

            return(response.Success);
        }
Exemple #8
0
        /// <summary>
        /// Drop a database. Requires additional authentication to the server.
        /// </summary>
        /// <param name="name">database name</param>
        /// <remarks>
        /// Syntax: http://&lt;server&gt;:[&lt;port&gt;]/database/&lt;databaseName&gt;
        /// </remarks>
        public bool DeteteDatabase(string name)
        {
            name.ThrowIfNullOrEmpty("name");

            if (!DatabaseExist(name))
            {
                return(true);
            }

            string url = "database/{0}".F(Encoded(name));

            OrientdbResponse <DynamicDictionary> request =
                OrientdbResponse.Wrap(DoRequest <Dictionary <string, object> >("DELETE", url));

            return(request.HttpStatusCode == 204);
        }
Exemple #9
0
        /// <summary>
        ///     The Create Class command creates a new class in the schema. NOTE: If a cluster
        ///     with the same name exists in the database will be used as default cluster.
        /// </summary>
        /// <remarks>
        ///     Syntax: CREATE CLASS &lt;class&gt; [EXTENDS &lt;super-class&gt;]
        /// </remarks>
        /// <seealso cref="http://www.orientechnologies.com/docs/last/orientdb.wiki/SQL-Create-Class.html" />
        public bool CreateClass <T>(string database,
                                    Func <CreateClassRequestParameters, CreateClassRequestParameters> requestParameters = null)
        {
            database.ThrowIfNullOrEmpty("database");

            string className = typeof(T).Name;

            CreateClassRequestParameters requestParams = null;

            if (ClassExist(database, className))
            {
                throw new Exception("The class {0} already exists.".F(className));
            }

            string command = "CREATE CLASS {0}".F(className);

            if (requestParameters != null)
            {
                requestParams = requestParameters(new CreateClassRequestParameters());
            }


            if (requestParams != null)
            {
                if (!string.IsNullOrEmpty(requestParams._extends))
                {
                    command = "{0} EXTENDS {1}".F(command, requestParams._extends);
                }

                if (requestParams._abstract)
                {
                    command = "{0} ABSTRACT".F(command);
                }
            }

            OrientdbResponse <DynamicDictionary> response = Command(command, database, CommandLanguage.Sql);

            if (!response.Success)
            {
                return(false);
            }


            return(CreateProperties <T>(database));
        }
        public bool InsertVertex <T>(string database, ref T document)
        {
            database.ThrowIfNullOrEmpty("database");
            document.ThrowIfNull("document");

            Type   classType = typeof(T);
            string command   = "create vertex {0} set ".F(typeof(T).Name);

            foreach (
                PropertyInfo propertyInfo in
                classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
            {
                if (propertyInfo.CanRead && propertyInfo.CanWrite)
                {
                    OrientdbProperty oprop = propertyInfo.GetOrientdbPropertyAttribute();
                    if (oprop == null)
                    {
                        string propertyName  = propertyInfo.Name;
                        object propertyValue = propertyInfo.GetValue(document, null);

                        command = "{0} {1} = '{2}',".F(command, propertyName, propertyValue);
                    }
                }
            }
            if (command.EndsWith(","))
            {
                command = command.Remove(command.Length - 1);
            }


            OrientdbResponse <BaseResult <T> > request = Command <BaseResult <T> >(command, database, CommandLanguage.Sql);

            if (request.Success)
            {
                document = request.Response.Result.First();
            }

            return(request.Success);
        }
Exemple #11
0
        public static OrientdbResponse <TTo> CloneFrom <TTo>(IOrientdbResponse from, TTo to)
        {
            var response = new OrientdbResponse <TTo>(from.Settings)
            {
                OriginalException = from.OriginalException,
                HttpStatusCode    = from.HttpStatusCode,
                Request           = from.Request,
                RequestMethod     = from.RequestMethod,
                RequestUrl        = from.RequestUrl,
                Response          = to,
                ResponseRaw       = from.ResponseRaw,
                Serializer        = from.Settings.Serializer,
                Settings          = from.Settings,
                Success           = from.Success,
                Metrics           = from.Metrics
            };
            var tt = to as IResponseWithRequestInformation;

            if (tt != null)
            {
                tt.RequestInformation = response;
            }
            return(response);
        }
Exemple #12
0
        /// <summary>
        /// Execute a command against the database. Returns the records affected or the list of records for queries. Command executed via POST.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query">The query containing the command to execute</param>
        /// <param name="database">database name</param>
        /// <param name="language">The name of the language between those supported. OrientDB distribution comes with "sql" and GraphDB distribution has both "sql" and "gremlin"</param>
        /// <returns><see cref="BaseResult{T}"/></returns>
        public BaseResult <T> BaseResultCommand <T>(string query, string database, CommandLanguage language)
        {
            OrientdbResponse <BaseResult <T> > request = Command <BaseResult <T> >(query, database, language);

            return(request.Response);
        }
Exemple #13
0
        /// <summary>
        ///     The Create Property command creates a new property in the schema. An existing class
        ///     is required to perform this command.
        /// </summary>
        /// <remarks>
        ///     Syntax: CREATE PROPERTY &lt;class&gt;.&lt;property&gt; &lt;type&gt; [&lt;linked-type&gt;|&lt;linked-class&gt;]
        /// </remarks>
        /// <seealso cref="http://www.orientechnologies.com/docs/last/orientdb.wiki/SQL-Create-Property.html" />
        public bool CreateProperties <T>(string database)
        {
            database.ThrowIfNullOrEmpty("database");
            Type   classType = typeof(T);
            string className = typeof(T).Name;

            if (classType != null && classType != typeof(T))
            {
                throw new InvalidOperationException(
                          "Inconsistent type specified - type for CreateProperties<T> must match type for Class<T>");
            }

            foreach (
                PropertyInfo pi in
                classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
            {
                if (pi.CanRead && pi.CanWrite)
                {
                    string propertyTypeName;
                    string propertyName = pi.Name;


                    OrientdbProperty oprop = pi.GetOrientdbPropertyAttribute();

                    if (oprop != null)
                    {
                        if (!oprop.Deserializable && !oprop.Serializable)
                        {
                            continue;
                        }
                        if (oprop.LinkedType == LinkedType.Link)
                        {
                            if (string.IsNullOrEmpty(oprop.LinkedClass))
                            {
                                continue;
                            }

                            if (oprop.IsOut)
                            {
                                propertyName = "out";
                            }
                            if (oprop.IsIn)
                            {
                                propertyName = "in";
                            }

                            propertyTypeName = " LINK {0}".F(oprop.LinkedClass);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (pi.PropertyType == typeof(DateTime))
                        {
                            propertyTypeName = "date";
                        }
                        else if (pi.PropertyType == typeof(int))
                        {
                            propertyTypeName = "integer";
                        }
                        else if (pi.PropertyType == typeof(bool))
                        {
                            propertyTypeName = "boolean";
                        }
                        else if (pi.PropertyType == typeof(short))
                        {
                            propertyTypeName = "short";
                        }
                        else if (pi.PropertyType == typeof(long))
                        {
                            propertyTypeName = "long";
                        }
                        else if (pi.PropertyType == typeof(float))
                        {
                            propertyTypeName = "float";
                        }
                        else if (pi.PropertyType == typeof(double))
                        {
                            propertyTypeName = "double";
                        }
                        else if (pi.PropertyType == typeof(string))
                        {
                            propertyTypeName = "string";
                        }
                        else if (pi.PropertyType == typeof(byte[]))
                        {
                            propertyTypeName = "binary";
                        }
                        else if (pi.PropertyType == typeof(byte))
                        {
                            propertyTypeName = "byte";
                        }
                        else if (pi.PropertyType.BaseType == typeof(Enum))
                        {
                            propertyTypeName = "integer";
                        }
                        else
                        {
                            continue;
                        }
                    }

                    string command = "CREATE PROPERTY {0}.{1} {2}".F(className, propertyName, propertyTypeName);
                    OrientdbResponse <DynamicDictionary> response = Command(command, database, CommandLanguage.Sql);
                    if (!response.Success)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemple #14
0
 private static OrientdbResponse <DynamicDictionary> ToDynamicResponse(
     OrientdbResponse <Dictionary <string, object> > response)
 {
     return(CloneFrom(response, response.Response != null ? DynamicDictionary.Create(response.Response) : null));
 }
Exemple #15
0
 internal static OrientdbResponse <DynamicDictionary> Wrap(OrientdbResponse <Dictionary <string, object> > response)
 {
     return(ToDynamicResponse(response));
 }