gluesql / gluesql

GlueSQL is quite sticky. It attaches to anywhere.

Home Page:https://gluesql.org/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple Filters

kanekoshoyu opened this issue · comments

Hi,

I was wondering if there is any plan or a current workaround for using multiple filters (esp on select and update)

Originally I had a misunderstanding on AST builder syntax and I thought adding .filter() multiple times will keep adding nested filter, but turns out every new filter was just overriding another filter.

Then I tried adding AND within one filter expression, but I do not see the effect of the nested filter. From what I tested, it seems like only the first condition in the filter gets recognized, and the rest is discarded.

I saw online that we might be able to use subqueries (queries on top of queries) to get it solved. I am wondering if there is anything similar to such a concept in gluesql that I can use as a workaround.

Example of AND/OR within WHERE
https://www.techonthenet.com/sql/where.php

To use AND or OR expression, you should do like this.

        .filter(
            col("name")
                .not_like(text("D%"))
                .and(col("name").not_like(text("M___"))),
        )

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/expr/pattern_matching.rs#L69
or

        .filter("c.name = 'Fruit' OR c.name = 'Meat'")

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/select.rs#L85

Originally I had a misunderstanding on AST builder syntax and I thought adding .filter() multiple times will keep adding nested filter, but turns out every new filter was just overriding another filter.

adding filter multiple times should work like below,
if it wasn't then could you provide the query you tested?

// filter node -> filter node -> build
let actual = table("Bar")
.select()
.filter("id IS NULL")
.filter("id > 10")
.filter("id < 20")
.build();
let expected = "SELECT * FROM Bar WHERE id IS NULL AND id > 10 AND id < 20";
test(actual, expected);

To use AND or OR expression, you should do like this.

        .filter(
            col("name")
                .not_like(text("D%"))
                .and(col("name").not_like(text("M___"))),
        )

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/expr/pattern_matching.rs#L69 or

        .filter("c.name = 'Fruit' OR c.name = 'Meat'")

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/select.rs#L85

Sure, this worked fine for select(). for update() I tried the other day and was not working.

Sure, this worked fine for select(). for update() I tried the other day and was not working.

thanks, yes it was only working for select().
I've just added concat filter support to ast builder update nodes.
this will be included in the next release.

ref.
Update AST builder UpdateFilterNode .filter(..) behavior, #1455
Update AST builder UpdateNode to enforce .filter(..) before .set(..), #1454