forked from ebhomengo/niki
40 lines
933 B
Go
40 lines
933 B
Go
package mysqlquerybuilder
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// TODO: implementation more complete search service.
|
|
|
|
func BuildGetSearchQuery(baseQuery string, filter map[string]interface{}, exist bool) (string, []any) {
|
|
var conditions []string
|
|
var args []any
|
|
for key, value := range filter {
|
|
if key == "id" {
|
|
conditions = append(conditions, fmt.Sprintf("%s = ?", key))
|
|
args = append(args, value)
|
|
} else {
|
|
conditions = append(conditions, fmt.Sprintf("%s LIKE ?", key))
|
|
args = append(args, "%"+value.(string)+"%")
|
|
}
|
|
}
|
|
|
|
query := baseQuery
|
|
if len(conditions) > 0 {
|
|
subQuery := strings.Join(conditions, " OR ")
|
|
if exist {
|
|
query += fmt.Sprintf(" INNER JOIN benefactors ON benefactor_id = benefactors.id WHERE (%s) ", subQuery)
|
|
} else {
|
|
if strings.Contains(strings.ToUpper(baseQuery), "WHERE") {
|
|
query += " AND "
|
|
} else {
|
|
query += " WHERE "
|
|
}
|
|
query += subQuery
|
|
}
|
|
}
|
|
|
|
return query, args
|
|
}
|