Example #1
0
        public static Query Update <T>(T instance, dynamic where = null)
        {
            var descriptor = GetDescriptor <T>();
            var set        = Dialect.ResolveColumnNames(descriptor, ColumnScope.Updated).ToList();

            var hash            = ReadAccessor.Create(instance.GetType()).AsReadOnlyDictionary(instance);
            var hashKeysRewrite = hash.Keys.ToDictionary(k => Dialect.ResolveColumnName(descriptor, k), v => v);

            IReadOnlyDictionary <string, object> whereHash;
            List <string> whereFilter;

            if (where == null)
            {
                // WHERE is derived from the instance's primary key
                var keys = Dialect.ResolveKeyNames(descriptor);
                whereFilter = keys.Intersect(hashKeysRewrite.Keys).ToList();
                whereHash   = hash;
            }
            else
            {
                // WHERE is explicitly provided
                var accessor = ReadAccessor.Create(where.GetType());
                whereHash = ReadAccessorExtensions.AsReadOnlyDictionary(accessor, where);
                var whereHashKeysRewrite =
                    whereHash.Keys.ToDictionary(k => Dialect.ResolveColumnName(descriptor, k), v => v);
                whereFilter = Dialect.ResolveColumnNames(descriptor).Intersect(whereHashKeysRewrite.Keys).ToList();
            }

            var setFilter = set.Intersect(hashKeysRewrite.Keys).ToList();

            return(Update(descriptor, setFilter, whereFilter, hash, whereHash));
        }
Example #2
0
        public static Query Update(IDataDescriptor descriptor, dynamic set, dynamic where = null)
        {
            ITypeReadAccessor setAccessor = ReadAccessor.Create(set.GetType());
            IReadOnlyDictionary <string, object> setHash = ReadAccessorExtensions.AsReadOnlyDictionary(setAccessor, set);
            var setFilter = descriptor.Updated.Select(c => c.ColumnName).Intersect(setHash.Keys).ToList();

            IReadOnlyDictionary <string, object> whereHash = null;

            if (where != null)
            {
                ITypeReadAccessor whereAccessor = ReadAccessor.Create(where.GetType());
                whereHash = ReadAccessorExtensions.AsReadOnlyDictionary(whereAccessor, where);
            }

            var whereFilter = Dialect.ResolveColumnNames(descriptor);

            if (whereHash != null)
            {
                whereFilter = whereFilter.Intersect(whereHash.Keys);
            }

            return(Update(descriptor, setFilter, whereFilter.ToList(), setHash, whereHash));
        }