forked from ebhomengo/niki
fix: date, date-time filter
Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
parent
4ef5c0ed66
commit
e006ff5898
|
|
@ -1,11 +1,28 @@
|
||||||
package mysqlquerybuilder
|
package mysqlquerybuilder
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DateFormat = "2006-01-02"
|
||||||
|
|
||||||
|
func isTimeType(value interface{}) bool {
|
||||||
|
return reflect.TypeOf(value) == reflect.TypeOf(time.Time{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildFilterQuery constructs the query and handles datetime fields
|
||||||
func BuildFilterQuery(filter map[string]interface{}) (filterQuery string, args []any) {
|
func BuildFilterQuery(filter map[string]interface{}) (filterQuery string, args []any) {
|
||||||
for key, value := range filter {
|
for key, value := range filter {
|
||||||
filterQuery += fmt.Sprintf("AND %s = ? ", key)
|
if isTimeType(value) {
|
||||||
args = append(args, value)
|
filterQuery += fmt.Sprintf("AND DATE(%s) = ? ", key)
|
||||||
|
dateValue := value.(time.Time).Format(DateFormat)
|
||||||
|
args = append(args, dateValue)
|
||||||
|
} else {
|
||||||
|
filterQuery += fmt.Sprintf("AND %s = ? ", key)
|
||||||
|
args = append(args, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return filterQuery, args
|
return filterQuery, args
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue