2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
|
|
|
|
2021-02-01 20:57:56 +00:00
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-10-25 00:40:13 +00:00
|
|
|
"fmt"
|
2021-06-03 10:52:19 +00:00
|
|
|
"strings"
|
2021-11-22 03:59:22 +00:00
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-02-01 20:57:56 +00:00
|
|
|
)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
type queryBuilder struct {
|
|
|
|
repository *repository
|
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
columns []string
|
|
|
|
from string
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
joins joins
|
2021-01-18 01:23:20 +00:00
|
|
|
whereClauses []string
|
|
|
|
havingClauses []string
|
|
|
|
args []interface{}
|
2021-06-03 10:52:19 +00:00
|
|
|
withClauses []string
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
recursiveWith bool
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
sortAndPagination string
|
|
|
|
}
|
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
func (qb queryBuilder) body() string {
|
|
|
|
return fmt.Sprintf("SELECT %s FROM %s%s", strings.Join(qb.columns, ", "), qb.from, qb.joins.toSQL())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) addColumn(column string) {
|
|
|
|
qb.columns = append(qb.columns, column)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb queryBuilder) toSQL(includeSortPagination bool) string {
|
|
|
|
body := qb.body()
|
|
|
|
|
|
|
|
withClause := ""
|
|
|
|
if len(qb.withClauses) > 0 {
|
|
|
|
var recursive string
|
|
|
|
if qb.recursiveWith {
|
|
|
|
recursive = " RECURSIVE "
|
|
|
|
}
|
|
|
|
withClause = "WITH " + recursive + strings.Join(qb.withClauses, ", ") + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
body = withClause + qb.repository.buildQueryBody(body, qb.whereClauses, qb.havingClauses)
|
|
|
|
if includeSortPagination {
|
|
|
|
body += qb.sortAndPagination
|
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb queryBuilder) findIDs(ctx context.Context) ([]int, error) {
|
2021-10-25 00:40:13 +00:00
|
|
|
const includeSortPagination = true
|
2021-11-22 03:59:22 +00:00
|
|
|
sql := qb.toSQL(includeSortPagination)
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.repository.runIdsQuery(ctx, sql, qb.args)
|
2021-10-25 00:40:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb queryBuilder) executeFind(ctx context.Context) ([]int, int, error) {
|
2021-10-25 00:40:13 +00:00
|
|
|
body := qb.body()
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.repository.executeFindQuery(ctx, body, qb.args, qb.sortAndPagination, qb.whereClauses, qb.havingClauses, qb.withClauses, qb.recursiveWith)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb queryBuilder) executeCount(ctx context.Context) (int, error) {
|
2021-10-25 00:40:13 +00:00
|
|
|
body := qb.body()
|
2021-04-15 00:46:31 +00:00
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
withClause := ""
|
|
|
|
if len(qb.withClauses) > 0 {
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
var recursive string
|
|
|
|
if qb.recursiveWith {
|
|
|
|
recursive = " RECURSIVE "
|
|
|
|
}
|
|
|
|
withClause = "WITH " + recursive + strings.Join(qb.withClauses, ", ") + " "
|
2021-06-03 10:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 00:46:31 +00:00
|
|
|
body = qb.repository.buildQueryBody(body, qb.whereClauses, qb.havingClauses)
|
2021-06-03 10:52:19 +00:00
|
|
|
countQuery := withClause + qb.repository.buildCountQuery(body)
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.repository.runCountQuery(ctx, countQuery, qb.args)
|
2021-04-15 00:46:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *queryBuilder) addWhere(clauses ...string) {
|
|
|
|
for _, clause := range clauses {
|
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.whereClauses = append(qb.whereClauses, clause)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) addHaving(clauses ...string) {
|
|
|
|
for _, clause := range clauses {
|
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.havingClauses = append(qb.havingClauses, clause)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
func (qb *queryBuilder) addWith(recursive bool, clauses ...string) {
|
2021-06-03 10:52:19 +00:00
|
|
|
for _, clause := range clauses {
|
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.withClauses = append(qb.withClauses, clause)
|
|
|
|
}
|
|
|
|
}
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
|
|
|
|
qb.recursiveWith = qb.recursiveWith || recursive
|
2021-06-03 10:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *queryBuilder) addArg(args ...interface{}) {
|
|
|
|
qb.args = append(qb.args, args...)
|
|
|
|
}
|
|
|
|
|
2024-08-30 01:43:44 +00:00
|
|
|
func (qb *queryBuilder) hasJoin(alias string) bool {
|
|
|
|
for _, j := range qb.joins {
|
|
|
|
if j.alias() == alias {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
func (qb *queryBuilder) join(table, as, onClause string) {
|
|
|
|
newJoin := join{
|
|
|
|
table: table,
|
|
|
|
as: as,
|
|
|
|
onClause: onClause,
|
2021-12-06 01:30:40 +00:00
|
|
|
joinType: "LEFT",
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qb.joins.add(newJoin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) addJoins(joins ...join) {
|
|
|
|
qb.joins.add(joins...)
|
|
|
|
}
|
|
|
|
|
2023-02-13 01:14:41 +00:00
|
|
|
func (qb *queryBuilder) addFilter(f *filterBuilder) error {
|
2021-03-02 00:27:36 +00:00
|
|
|
err := f.getError()
|
|
|
|
if err != nil {
|
2023-02-13 01:14:41 +00:00
|
|
|
return err
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
clause, args := f.generateWithClauses()
|
|
|
|
if len(clause) > 0 {
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
qb.addWith(f.recursiveWith, clause)
|
2021-06-03 10:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
// WITH clause always comes first and thus precedes alk args
|
|
|
|
qb.args = append(args, qb.args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
clause, args = f.generateWhereClauses()
|
2021-03-02 00:27:36 +00:00
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.addWhere(clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
qb.addArg(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
clause, args = f.generateHavingClauses()
|
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.addHaving(clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
qb.addArg(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
qb.addJoins(f.getAllJoins()...)
|
2023-02-13 01:14:41 +00:00
|
|
|
|
|
|
|
return nil
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
2021-11-22 03:59:22 +00:00
|
|
|
|
|
|
|
func (qb *queryBuilder) parseQueryString(columns []string, q string) {
|
|
|
|
specs := models.ParseSearchString(q)
|
|
|
|
|
|
|
|
for _, t := range specs.MustHave {
|
|
|
|
var clauses []string
|
|
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
clauses = append(clauses, column+" LIKE ?")
|
|
|
|
qb.addArg(like(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
qb.addWhere("(" + strings.Join(clauses, " OR ") + ")")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range specs.MustNot {
|
|
|
|
for _, column := range columns {
|
|
|
|
qb.addWhere(coalesce(column) + " NOT LIKE ?")
|
|
|
|
qb.addArg(like(t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, set := range specs.AnySets {
|
|
|
|
var clauses []string
|
|
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
for _, v := range set {
|
|
|
|
clauses = append(clauses, column+" LIKE ?")
|
|
|
|
qb.addArg(like(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qb.addWhere("(" + strings.Join(clauses, " OR ") + ")")
|
|
|
|
}
|
|
|
|
}
|