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,12 +1,29 @@
|
|||
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) {
|
||||
for key, value := range filter {
|
||||
if isTimeType(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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue