Example #1
0
        /// <summary>
        /// Generate cypher DELETE query
        /// </summary>
        /// <param name="node">Node object</param>
        /// <returns>DELETE query</returns>
        private static string DeleteQuery(Neo4jNode node)
        {
            string labelName          = string.Empty;
            Neo4jLabelAttribute label = node.GetType().GetCustomAttribute <Neo4jLabelAttribute>();

            if (label != null)
            {
                labelName = label.Name;
            }
            Dictionary <string, object> values     = new Dictionary <string, object>();
            List <PropertyInfo>         properties = node.GetType().GetProperties().ToList();
            var uuid = properties.FirstOrDefault(p => p.Name.Equals("UUID", StringComparison.InvariantCultureIgnoreCase));
            var id   = properties.FirstOrDefault(p => p.Name.Equals("Id", StringComparison.InvariantCultureIgnoreCase));

            if (uuid != null)
            {
                return($"MATCH (n:{labelName} {{uuid:'{uuid.GetValue(node)}'}} DETACH DELETE n");
            }
            else if (id != null)
            {
                return($"MATCH (n:{labelName} {{id:'{id.GetValue(node)}'}} DETACH DELETE n");
            }

            throw new Neo4jMappingException("No node identity found.", new Exception("Check your custom class attributes."));
        }
Example #2
0
        private static string MatchQuery <T>(T node) where T : Neo4jNode
        {
            string labelName          = string.Empty;
            string cypher             = string.Empty;
            Neo4jLabelAttribute label = node.GetType().GetCustomAttribute <Neo4jLabelAttribute>();

            if (label != null)
            {
                labelName = label.Name;
            }
            var uuidProp = node.GetType().GetProperties().FirstOrDefault(p => p.Name.Equals("UUID", StringComparison.InvariantCultureIgnoreCase));

            if (!String.IsNullOrEmpty(uuidProp?.GetValue(node)?.ToString()))
            {
                cypher = $"MATCH (n:{labelName} {{uuid: '{uuidProp.GetValue(node)}'}}) RETURN n";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                Dictionary <string, object> values = new Dictionary <string, object>();
                foreach (PropertyInfo propInfo in node.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
                {
                    Neo4jPropertyAttribute attr = propInfo.GetCustomAttribute <Neo4jPropertyAttribute>();
                    if (attr != null)
                    {
                        if (propInfo.PropertyType.IsEnum)
                        {
                            values.Add(attr.Name, TryGetEnumValueDescription(propInfo, propInfo.GetValue(node)));
                        }
                        else
                        {
                            values.Add(attr.Name, propInfo.GetValue(node));
                        }
                    }
                }
                foreach (KeyValuePair <string, object> keyValue in values)
                {
                    if (int.TryParse(keyValue.Value.ToString(), out int x))
                    {
                        sb.Append($" {(sb.Length > 0 ? " AND " : string.Empty)} n.{keyValue.Key}={keyValue.Value} ");
                    }
                    else
                    {
                        sb.Append($" {(sb.Length > 0 ? " AND " : string.Empty)} n.{keyValue.Key}=~'(?i).*{keyValue.Value}.*' ");
                    }
                }
                cypher = $"MATCH (n:{labelName}) WHERE {sb.ToString()} RETURN n";
            }

            return(cypher);
        }
Example #3
0
        /// <summary>
        /// Generate cypher CREATE query
        /// </summary>
        /// <param name="node">Node object</param>
        /// <returns>CREATE query</returns>
        private static string CreationQuery(Neo4jNode node)
        {
            string labelName          = string.Empty;
            Neo4jLabelAttribute label = node.GetType().GetCustomAttribute <Neo4jLabelAttribute>();

            if (label != null)
            {
                labelName = label.Name;
            }
            Dictionary <string, object> values = new Dictionary <string, object>();

            foreach (PropertyInfo propInfo in node.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
            {
                Neo4jPropertyAttribute attr = propInfo.GetCustomAttribute <Neo4jPropertyAttribute>();
                if (attr != null)
                {
                    if (propInfo.PropertyType.IsEnum)
                    {
                        values.Add(attr.Name, TryGetEnumValueDescription(propInfo, propInfo.GetValue(node)));
                    }
                    else
                    {
                        values.Add(attr.Name, propInfo.GetValue(node));
                    }
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append($"CREATE (n:{labelName} {{");
            string comma = "";

            foreach (KeyValuePair <string, object> keyValue in values)
            {
                sb.Append($"{comma}{keyValue.Key}: {JsonConvert.SerializeObject(keyValue.Value)}");
                comma = ", ";
            }
            sb.Append("}) RETURN n");

            string cypher = sb.ToString().Replace("\"", "'");

            return(cypher);
        }