Exemplo n.º 1
0
        public T Merge <T>(
            T entityOnCreate,
            T entityOnUpdate,
            string where) where T : EntityBase, new()
        {
            if (entityOnCreate == null)
            {
                throw new ArgumentNullException("entityOnCreate");
            }

            if (entityOnUpdate == null)
            {
                throw new ArgumentNullException("entityOnUpdate");
            }

            bool firstNode = true;

            var setCaluseOnCreate = new Lazy <StringBuilder>();
            var setCaluseOnUpdate = new Lazy <StringBuilder>();

            var formattedSetClauseOnCreate = new StringFormatter("");
            var formattedSetClauseOnUpdate = new StringFormatter("");

            foreach (PropertyInfo prop in entityOnCreate.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (prop.GetCustomAttribute(typeof(NotMappedAttribute), true) != null ||
                    prop.GetCustomAttribute(typeof(RelationshipAttribute), true) != null)
                {
                    continue;
                }

                if (prop.Name.Equals("Uuid", StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                object value = prop.GetValue(entityOnCreate, null);

                string prefixAndPostfix = (value is string) ? "\"" : string.Empty;

                formattedSetClauseOnCreate.Add(BIND_MARKER + prop.Name + BIND_MARKER, prefixAndPostfix + (value ?? "null") + prefixAndPostfix);

                if (firstNode)
                {
                    firstNode = false;
                }
                else
                {
                    setCaluseOnCreate.Value.Append(",");
                }

                setCaluseOnCreate.Value.Append(string.Format("n.{0}={1}{0}{1}", prop.Name, BIND_MARKER));
            }

            firstNode = true;
            foreach (PropertyInfo prop in entityOnUpdate.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (prop.GetCustomAttribute(typeof(NotMappedAttribute), true) != null ||
                    prop.GetCustomAttribute(typeof(RelationshipAttribute), true) != null)
                {
                    continue;
                }

                if (prop.Name.Equals("Uuid", StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                object value = prop.GetValue(entityOnUpdate, null);

                string prefixAndPostfix = (value is string) ? "\"" : string.Empty;

                formattedSetClauseOnUpdate.Add(BIND_MARKER + prop.Name + BIND_MARKER, prefixAndPostfix + (value ?? "null") + prefixAndPostfix);

                if (firstNode)
                {
                    firstNode = false;
                }
                else
                {
                    setCaluseOnUpdate.Value.Append(",");
                }

                setCaluseOnUpdate.Value.Append(string.Format("n.{0}={1}{0}{1}", prop.Name, BIND_MARKER));
            }

            string uuid = StripHyphens ? Guid.NewGuid().ToString("N") : Guid.NewGuid().ToString();

            formattedSetClauseOnCreate.Add(string.Format("{0}Uuid{0}", BIND_MARKER), "\"" + uuid + "\"");

            setCaluseOnCreate.Value.Append(firstNode ? string.Format("n.Uuid={0}Uuid{0}", BIND_MARKER) : string.Format(",n.Uuid={0}Uuid{0}", BIND_MARKER));

            formattedSetClauseOnCreate.Str = setCaluseOnCreate.Value.ToString();
            formattedSetClauseOnUpdate.Str = setCaluseOnUpdate.Value.ToString();

            var query = new StringFormatter(QueryTemplates.TEMPLATE_MERGE);

            query.Add("@node", entityOnCreate.Label);
            query.Add("@on_create_clause", string.Format("ON CREATE SET {0}", formattedSetClauseOnCreate.ToString()));
            query.Add("@on_match_clause", string.Format("ON MATCH SET {0}", formattedSetClauseOnUpdate.ToString()));
            query.Add("@conditions", where);

            IStatementResult statementResult = ExecuteQuery(query.ToString());

            return(statementResult.Map <T>());
        }