Exemple #1
0
        // TODO -- return IEnumerable<ISqlFragment> instead
        protected override ISqlFragment buildWhereFragment(IMartenSession session)
        {
            if (WhereClauses.Count == 0)
            {
                return(Storage.DefaultWhereFragment());
            }

            var parser = new WhereClauseParser(session, this);

            ISqlFragment where = null;

            switch (WhereClauses.Count)
            {
            case 0:
                where = Storage.DefaultWhereFragment();
                break;

            case 1:
                where = parser.Build(WhereClauses.Single());
                break;

            default:
                var wheres = WhereClauses.Select(x => parser.Build(x)).ToArray();

                where = CompoundWhereFragment.And(wheres);
                break;
            }

            return(Storage.FilterDocuments(null, where));
        }
Exemple #2
0
        public EventDocumentStorage(StoreOptions options)
        {
            Events   = options.EventGraph;
            _mapping = new EventQueryMapping(options);

            FromObject = _mapping.TableName.QualifiedName;
            Fields     = _mapping;

            _serializer = options.Serializer();

            IdType = Events.StreamIdentity == StreamIdentity.AsGuid ? typeof(Guid) : typeof(string);

            TenancyStyle = options.Events.TenancyStyle;

            // The json data column has to go first
            var table   = new EventsTable(Events);
            var columns = table.SelectColumns();

            _fields = columns.Select(x => x.Name).ToArray();

            _selectClause = $"select {_fields.Join(", ")} from {Events.DatabaseSchemaName}.mt_events as d";

            _defaultWhere = Events.TenancyStyle == TenancyStyle.Conjoined
                ? CompoundWhereFragment.And(IsNotArchivedFilter.Instance, CurrentTenantFilter.Instance)
                : IsNotArchivedFilter.Instance;
        }
        protected override void configure(CommandBuilder builder)
        {
            _storage.WriteSelectClause(builder);

            var wheres = filters().ToArray();

            switch (wheres.Length)
            {
            case 0:
                break;

            case 1:
                builder.Append(" WHERE ");
                wheres[0].Apply(builder);
                break;

            default:
                var where = CompoundWhereFragment.And(wheres);
                builder.Append(" WHERE ");
                where.Apply(builder);
                break;
            }

            builder.Append(" ORDER BY d.seq_id");
            if (Limit > 0)
            {
                builder.Append(" LIMIT ");
                builder.AppendParameter(Limit);
            }
        }
        public void query_using_where_fragment()
        {
            var user1 = new User {
                UserName = "******"
            };
            var user2 = new User {
                UserName = "******"
            };
            var user3 = new User {
                UserName = "******"
            };
            var user4 = new User {
                UserName = "******"
            };

            using var session = theStore.OpenSession();
            session.Store(user1, user2, user3, user4);
            session.SaveChanges();

            var whereFragment = CompoundWhereFragment.And();

            whereFragment.Add(new WhereFragment("d.data ->> 'UserName' != ?", "baz"));
            whereFragment.Add(new WhereFragment("d.data ->> 'UserName' != ?", "jack"));

            // no where clause
            session.Query <User>().Where(x => x.MatchesSql(whereFragment)).OrderBy(x => x.UserName).Select(x => x.UserName)
            .ToList().ShouldHaveTheSameElementsAs("bar", "foo");

            // with a where clause
            session.Query <User>().Where(x => x.UserName != "bar" && x.MatchesSql(whereFragment))
            .OrderBy(x => x.UserName)
            .ToList()
            .Select(x => x.UserName)
            .Single().ShouldBe("foo");
        }
        public ISqlFragment determineWhereFragment()
        {
            var defaults = defaultFilters().ToArray();

            return(defaults.Length switch
            {
                0 => null,
                1 => defaults[0],
                _ => CompoundWhereFragment.And(defaults)
            });
Exemple #6
0
        private void determineDefaultWhereFragment()
        {
            var defaults = defaultFilters().ToArray();

            _defaultWhere = defaults.Length switch
            {
                0 => null,
                1 => defaults[0],
                _ => CompoundWhereFragment.And(defaults)
            };
        }
Exemple #7
0
        public ISqlFragment FilterDocuments(QueryModel?model, ISqlFragment query)
        {
            var extras = extraFilters(query).ToList();

            if (extras.Count > 0)
            {
                extras.Add(query);
                return(CompoundWhereFragment.And(extras));
            }

            return(query);
        }
Exemple #8
0
        public static ISqlFragment CombineFragments(this IList <ISqlFragment> fragments)
        {
            switch (fragments.Count)
            {
            case 0:
                return(null);

            case 1:
                return(fragments.Single());

            default:
                return(CompoundWhereFragment.And(fragments));
            }
        }
Exemple #9
0
        public IDeletion DeleteForId(TId id, string tenant)
        {
            if (TenancyStyle == TenancyStyle.Conjoined)
            {
                return(new Deletion(this, DeleteFragment)
                {
                    Where = CompoundWhereFragment.And(new SpecificTenantFilter(tenant), ByIdFilter(id)),
                    Id = id
                });
            }

            return(new Deletion(this, DeleteFragment)
            {
                Where = ByIdFilter(id),
                Id = id
            });
        }