예제 #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);
        }
예제 #2
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);
        }
        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);
        }
예제 #4
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);
        }