3
0

Remove duplicate records with multi value fields

This commit is contained in:
Tomaž Jerman 2021-09-16 10:31:07 +02:00
parent 3113257097
commit 69b087549b
5 changed files with 25 additions and 0 deletions

View File

@ -198,6 +198,11 @@ func (s Store) {{ unexport "fetchFullPageOf" $.Types.Plural }} (
} else {
tryQuery = q
}
{{ if .RDBMS.CustomPreLoadProcessor }}
if tryQuery, err = s.{{ unexport $.Types.Singular }}PreLoadProcessor(tryQuery); err != nil {
return
}
{{- end}}
if limit > 0 {
// fetching + 1 so we know if there are more items

View File

@ -86,6 +86,7 @@ type (
CustomFilterConverter bool `yaml:"customFilterConverter"`
CustomSortConverter bool `yaml:"customSortConverter"`
CustomCursorCollector bool `yaml:"customCursorCollector"`
CustomPreLoadProcessor bool `yaml:"customPreLoadProcessor"`
CustomPostLoadProcessor bool `yaml:"customPostLoadProcessor"`
CustomEncoder bool `yaml:"customEncoder"`

View File

@ -56,6 +56,7 @@ rdbms:
customFilterConverter: true
customSortConverter: true
customCursorCollector: true
customPreLoadProcessor: true
customPostLoadProcessor: true
mapFields:
ModuleID: { column: module_id }

View File

@ -73,6 +73,10 @@ func (s Store) fetchFullPageOfComposeRecords(
tryQuery = q
}
if tryQuery, err = s.composeRecordPreLoadProcessor(tryQuery); err != nil {
return
}
if limit > 0 {
// fetching + 1 so we know if there are more items
// we can fetch (next-page cursor)

View File

@ -221,6 +221,10 @@ func (s Store) composeRecordsPageNavigation(
q = supPageQuery.Where(cursorCond(cursor))
}
if q, err = s.composeRecordPreLoadProcessor(q); err != nil {
return
}
rows, err = s.Query(ctx, q)
if err != nil {
return err
@ -623,6 +627,16 @@ func (s Store) composeRecordPostLoadProcessor(ctx context.Context, m *types.Modu
return nil
}
func (s Store) composeRecordPreLoadProcessor(q squirrel.SelectBuilder) (squirrel.SelectBuilder, error) {
// When filtering over multi value record values we can get multiple rows of the same record
// causing it to show as a duplicate in the output.
// This partitioning removes any duplicate rows (rows where the index is not 1).
return squirrel.Select(s.composeRecordColumns()...).
PlaceholderFormat(s.config.PlaceholderFormat).
FromSelect(q.Column("row_number() over (partition by id) as pp_rn"), "base").
Where("pp_rn = 1"), nil
}
func (s Store) composeRecordsSorter(m *types.Module, q squirrel.SelectBuilder, sort filter.SortExprSet) (squirrel.SelectBuilder, error) {
var (
sortable = s.sortableComposeRecordColumns()