Migrate to PostgreSQL and fix related issues
This commit is contained in:
parent
0b8fbea6c3
commit
a89a9776c3
17 changed files with 176 additions and 174 deletions
|
|
@ -44,7 +44,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
}
|
||||
|
||||
// SQL qeury for results
|
||||
sqlQuery := `--sql
|
||||
sqlQuery := `
|
||||
SELECT people.*,
|
||||
sections.name AS Section__name
|
||||
FROM people
|
||||
|
|
@ -52,7 +52,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
ON people.section_id = sections.id`
|
||||
|
||||
// SQL query to count results
|
||||
sqlQueryCount := `--sql
|
||||
sqlQueryCount := `
|
||||
SELECT people.id
|
||||
FROM people
|
||||
`
|
||||
|
|
@ -62,7 +62,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
var sqlFilters string
|
||||
|
||||
// Filter: member or contact
|
||||
sqlFilters = `--sql
|
||||
sqlFilters = `
|
||||
WHERE is_member = @is_member
|
||||
AND is_contact = @is_contact
|
||||
`
|
||||
|
|
@ -78,11 +78,11 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filter: name -> first_name + last_name
|
||||
if len(params.Name) > 0 {
|
||||
nameFilter := `--sql
|
||||
people.first_name LIKE @name
|
||||
OR people.last_name LIKE @name
|
||||
OR CONCAT(people.first_name, ' ', people.last_name) LIKE @name
|
||||
OR CONCAT(people.last_name, ' ', people.first_name) LIKE @name
|
||||
nameFilter := `
|
||||
LOWER(people.first_name) LIKE LOWER(@name)
|
||||
OR LOWER(people.last_name) LIKE LOWER(@name)
|
||||
OR LOWER(CONCAT(people.first_name, ' ', people.last_name)) LIKE LOWER(@name)
|
||||
OR LOWER(CONCAT(people.last_name, ' ', people.first_name)) LIKE LOWER(@name)
|
||||
`
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
"name",
|
||||
|
|
@ -92,10 +92,10 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
if strings.Contains(params.Name, " ") {
|
||||
names := strings.Split(params.Name, " ")
|
||||
for index, name := range names {
|
||||
nameFilter = fmt.Sprintf(`--sql
|
||||
nameFilter = fmt.Sprintf(`
|
||||
%s
|
||||
OR people.first_name LIKE @name_%d
|
||||
OR people.last_name LIKE @name_%d
|
||||
OR LOWER(people.first_name) LIKE LOWER(@name_%d)
|
||||
OR LOWER(people.last_name) LIKE LOWER(@name_%d)
|
||||
`, nameFilter, index, index)
|
||||
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
|
|
@ -105,7 +105,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
}
|
||||
}
|
||||
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND (%s)
|
||||
`, sqlFilters, nameFilter)
|
||||
|
|
@ -113,7 +113,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filter: section
|
||||
if params.Section > 0 {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.section_id = @section
|
||||
`, sqlFilters)
|
||||
|
|
@ -122,7 +122,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filter: active (only apply if archived is false)
|
||||
if params.Active && !params.Archive {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.deleted_at IS NULL
|
||||
`, sqlFilters)
|
||||
|
|
@ -130,7 +130,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filter: archived (only apply if active is false)
|
||||
if params.Archive && !params.Active {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.deleted_at IS NOT NULL
|
||||
`, sqlFilters)
|
||||
|
|
@ -138,7 +138,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filter: if both active and archived are turned off, return nothing
|
||||
if !params.Archive && !params.Active {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND 0=1
|
||||
`, sqlFilters)
|
||||
|
|
@ -146,9 +146,9 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filters: email
|
||||
if len(params.Email) > 0 {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.email LIKE @email
|
||||
AND LOWER(people.email) LIKE LOWER(@email)
|
||||
`, sqlFilters)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
"email",
|
||||
|
|
@ -158,35 +158,29 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filters: phone
|
||||
if len(params.Phone) > 0 {
|
||||
params.Phone = strings.ReplaceAll(params.Phone, " ", "")
|
||||
params.Phone = strings.ReplaceAll(params.Phone, "-", "")
|
||||
params.Phone = strings.ReplaceAll(params.Phone, ".", "")
|
||||
params.Phone = strings.ReplaceAll(params.Phone, "/", "")
|
||||
params.Phone = strings.ReplaceAll(params.Phone, "+", "")
|
||||
var phoneWithoutZeroFilter string
|
||||
if string(params.Phone[0]) == "0" {
|
||||
phoneWithoutZeroFilter = `
|
||||
OR
|
||||
TRANSLATE(people.phone, ' ,-,.,/,+', '') LIKE TRANSLATE(@phone2, ' ,-,.,/,+', '') OR
|
||||
TRANSLATE(people.mobile, ' ,-,.,/,+', '') LIKE TRANSLATE(@phone2, ' ,-,.,/,+', '')
|
||||
`
|
||||
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
phone2 := params.Phone[1:]
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
"phone2",
|
||||
fmt.Sprintf("%%%s%%", phone2),
|
||||
))
|
||||
}
|
||||
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND (
|
||||
REPLACE(
|
||||
REPLACE(
|
||||
REPLACE(
|
||||
REPLACE(
|
||||
REPLACE(people.phone, ' ', '')
|
||||
, '-', '')
|
||||
, '.', '')
|
||||
, '/', '')
|
||||
, '+', '') LIKE @phone
|
||||
OR REPLACE(
|
||||
REPLACE(
|
||||
REPLACE(
|
||||
REPLACE(
|
||||
REPLACE(people.mobile, ' ', '')
|
||||
, '-', '')
|
||||
, '.', '')
|
||||
, '/', '')
|
||||
, '+', '') LIKE @phone
|
||||
TRANSLATE(people.phone, ' ,-,.,/,+', '') LIKE TRANSLATE(@phone, ' ,-,.,/,+', '') OR
|
||||
TRANSLATE(people.mobile, ' ,-,.,/,+', '') LIKE TRANSLATE(@phone, ' ,-,.,/,+', '')
|
||||
%s
|
||||
)
|
||||
`, sqlFilters)
|
||||
`, sqlFilters, phoneWithoutZeroFilter)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
"phone",
|
||||
fmt.Sprintf("%%%s%%", params.Phone),
|
||||
|
|
@ -195,7 +189,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filters: address
|
||||
if len(params.Address) > 0 {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND (
|
||||
people.address1 LIKE @address
|
||||
|
|
@ -210,7 +204,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filters: postal code
|
||||
if len(params.PostalCode) > 0 {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.postal_code = @postal_code
|
||||
`, sqlFilters)
|
||||
|
|
@ -222,7 +216,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
// Filters: city
|
||||
if len(params.City) > 0 {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.city LIKE @city
|
||||
`, sqlFilters)
|
||||
|
|
@ -233,7 +227,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
}
|
||||
|
||||
// Security filter: only show results in allowed secitons
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND people.section_id IN @allowed_sections
|
||||
`, sqlFilters)
|
||||
|
|
@ -250,7 +244,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
continue
|
||||
}
|
||||
|
||||
sqlFieldJoins = fmt.Sprintf(`--sql
|
||||
sqlFieldJoins = fmt.Sprintf(`
|
||||
%s
|
||||
LEFT JOIN field_values
|
||||
AS field_%d
|
||||
|
|
@ -271,36 +265,53 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
default:
|
||||
fmt.Println(v)
|
||||
continue
|
||||
case string:
|
||||
filter = fmt.Sprintf(`--sql
|
||||
%s
|
||||
field_%d.value_string LIKE @field_%d_%d
|
||||
OR field_%d.value_date LIKE @field_%d_%d
|
||||
`, filter, field.ID, field.ID, index, field.ID, field.ID, index)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
fmt.Sprintf("field_%d_%d", field.ID, index),
|
||||
fmt.Sprintf("%%%s%%", v),
|
||||
))
|
||||
if field.FieldType == "date" {
|
||||
filter = fmt.Sprintf(`
|
||||
%s
|
||||
TO_CHAR(field_%d.value_date, 'YYYY-MM-DD') = @field_%d_%d
|
||||
`, filter, field.ID, field.ID, index)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
fmt.Sprintf("field_%d_%d", field.ID, index),
|
||||
v,
|
||||
))
|
||||
} else {
|
||||
filter = fmt.Sprintf(`
|
||||
%s
|
||||
LOWER(field_%d.value_string) LIKE LOWER(@field_%d_%d)
|
||||
`, filter, field.ID, field.ID, index)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
fmt.Sprintf("field_%d_%d", field.ID, index),
|
||||
fmt.Sprintf("%%%s%%", v),
|
||||
))
|
||||
}
|
||||
case float64:
|
||||
filter = fmt.Sprintf(`--sql
|
||||
%s
|
||||
field_%d.value_int = @field_%d_%d
|
||||
OR field_%d.list_item_id = @field_%d_%d
|
||||
`, filter, field.ID, field.ID, index, field.ID, field.ID, index)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
fmt.Sprintf("field_%d_%d", field.ID, index),
|
||||
v,
|
||||
))
|
||||
if field.FieldType == "list" {
|
||||
filter = fmt.Sprintf(`
|
||||
%s
|
||||
field_%d.list_item_id = @field_%d_%d
|
||||
`, filter, field.ID, field.ID, index)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
fmt.Sprintf("field_%d_%d", field.ID, index),
|
||||
v,
|
||||
))
|
||||
} else {
|
||||
filter = fmt.Sprintf(`
|
||||
%s
|
||||
field_%d.value_int = @field_%d_%d
|
||||
`, filter, field.ID, field.ID, index)
|
||||
sqlParams = append(sqlParams, sql.Named(
|
||||
fmt.Sprintf("field_%d_%d", field.ID, index),
|
||||
v,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fieldFilter = fmt.Sprintf("%s %s", fieldFilter, filter)
|
||||
}
|
||||
|
||||
if fieldFilter != "" {
|
||||
sqlFilters = fmt.Sprintf(`--sql
|
||||
sqlFilters = fmt.Sprintf(`
|
||||
%s
|
||||
AND (%s)
|
||||
`, sqlFilters, fieldFilter)
|
||||
|
|
@ -308,7 +319,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
}
|
||||
|
||||
// Build and run count query
|
||||
sqlQueryCount = fmt.Sprintf(`--sql
|
||||
sqlQueryCount = fmt.Sprintf(`
|
||||
%s
|
||||
%s
|
||||
%s
|
||||
|
|
@ -330,7 +341,7 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
|
||||
var sqlPagination string
|
||||
if params.PageSize > 0 {
|
||||
sqlPagination = `--sql
|
||||
sqlPagination = `
|
||||
LIMIT @pagination_limit
|
||||
OFFSET @pagination_offset
|
||||
`
|
||||
|
|
@ -339,12 +350,12 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) {
|
|||
}
|
||||
|
||||
// Build and run paginated result query
|
||||
sqlQuery = fmt.Sprintf(`--sql
|
||||
sqlQuery = fmt.Sprintf(`
|
||||
%s
|
||||
%s
|
||||
%s
|
||||
GROUP BY people.id
|
||||
ORDER BY CONCAT(people.last_name, people.first_name) COLLATE NOCASE ASC
|
||||
GROUP BY people.id, Section__name
|
||||
ORDER BY CONCAT(people.last_name, people.first_name) ASC
|
||||
%s
|
||||
`, sqlQuery, sqlFieldJoins, sqlFilters, sqlPagination)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue