volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.7k stars 541 forks source link

[Feature] Support for `NOT condition` QueryMod #777

Open JMounier opened 4 years ago

JMounier commented 4 years ago

I was looking for a way to negate a whole groupe of QueryMod while building a request but I couldn't find any call equivalent to qm.Or2 which would apply a not to its given qm.QueryMod.

Syntax

the NOT condition syntax seems to be supported by most DBMS :

How To

NOT can be handle exactly as OR is handled. Meaning having a SetLastWhereAsNot and setting a boolean (such as notSeparator) in the where structure.

The last modification would be in the query_builder as follow :

// whereClause parses a where slice and converts it into a
// ...
// startAt specifies what number placeholders start at
func whereClause(q *Query, startAt int) (string, []interface{}) {
    ...
    notFirstExpression := false
    buf.WriteString(" WHERE ")
    for _, where := range q.where {
        if where.kind != whereKindRightParen {
            if notFirstExpression {
                if where.orSeparator {
                    buf.WriteString(" OR ")
                } else {
                    buf.WriteString(" AND ")
                }
            }
            if where.notSeparator {
                buf.WriteString(" NOT ")
            }
        } else
    ...
}

I hope this feature is useful enough to make it to production.

aarondl commented 4 years ago

I'm indifferent. We could add this so long as it plays nicely with -everything- and every DBMS. Keep in mind MariaDB/sqlite3/cockroachdb are unofficially part of that list.