Join count filter criteria (#1254)

Co-authored-by: mrbrdo <mrbrdo@gmail.com>
Co-authored-by: peolic <66393006+peolic@users.noreply.github.com>
This commit is contained in:
WithoutPants
2021-04-09 18:46:00 +10:00
committed by GitHub
parent 6a0c73b3a1
commit a2582047ca
17 changed files with 743 additions and 29 deletions

View File

@@ -2,6 +2,7 @@ package sqlite
import (
"database/sql"
"fmt"
"math/rand"
"strconv"
"strings"
@@ -44,10 +45,15 @@ func getPaginationSQL(page int, perPage int) string {
return " LIMIT " + strconv.Itoa(perPage) + " OFFSET " + strconv.Itoa(page) + " "
}
func getSort(sort string, direction string, tableName string) string {
func getSortDirection(direction string) string {
if direction != "ASC" && direction != "DESC" {
direction = "ASC"
return "ASC"
} else {
return direction
}
}
func getSort(sort string, direction string, tableName string) string {
direction = getSortDirection(direction)
const randomSeedPrefix = "random_"
@@ -96,6 +102,10 @@ func getRandomSort(tableName string, direction string, seed float64) string {
return " ORDER BY " + "(substr(" + colName + " * " + randomSortString + ", length(" + colName + ") + 2))" + " " + direction
}
func getCountSort(primaryTable, joinTable, primaryFK, direction string) string {
return fmt.Sprintf(" ORDER BY (SELECT COUNT(*) FROM %s WHERE %s = %s.id) %s", joinTable, primaryFK, primaryTable, getSortDirection(direction))
}
func getSearchBinding(columns []string, q string, not bool) (string, []interface{}) {
var likeClauses []string
var args []interface{}
@@ -213,6 +223,11 @@ func getMultiCriterionClause(primaryTable, foreignTable, joinTable, primaryFK, f
return whereClause, havingClause
}
func getCountCriterionClause(primaryTable, joinTable, primaryFK string, criterion models.IntCriterionInput) (string, int) {
lhs := fmt.Sprintf("(SELECT COUNT(*) FROM %s s WHERE s.%s = %s.id)", joinTable, primaryFK, primaryTable)
return getIntCriterionWhereClause(lhs, criterion)
}
func ensureTx(tx *sqlx.Tx) {
if tx == nil {
panic("must use a transaction")