Skip to content

Construct query with where clause that can append and_where to an existing or_where #12

@elertan

Description

@elertan

I want to construct a query that looks as following:

SELECT * FROM table WHERE field1 = '' OR (field2 = '' AND field3 = '');

The code below does not work for obvious reasons:

use sql_builder::{SqlBuilder, quote};

let sql = SqlBuilder::select_from("table")
    .field("*")
    .and_where_eq("field1", quote(""))
    .or_where_eq("field2", quote(""))
    .and_where_eq("field3", quote(""))
    .sql()?;

But I can't find a construct in the docs which would allow me to express this query.
It would be ideal if I would just be able to encapsulate the last two where clause statements within ()?
Note: The data for this query is being dynamically build by some dynamic filter data from user input, similar to as shown below.

enum Filter {
    Field1(StringFilter),
    Field2(StringFilter),
    Field2(StringFilter),
    And(Vec<Filter>),
    Or(Vec<Filter>),
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions