go-mcm/model/django/django.go

176 lines
5.0 KiB
Go

package django
import (
"fmt"
"strings"
)
type Field string
const (
ID Field = "id"
Link Field = "link"
)
type Builder struct {
Params []string
}
func NewBuilder(token string) *Builder {
return &Builder{[]string{fmt.Sprintf("api=%v", token)}}
}
func (db *Builder) QueryString() string {
return "?" + strings.Join(db.Params, "&")
}
func (db *Builder) Eq(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__eq=%v", field, value))
return db
}
func (db *Builder) Ne(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__ne=%v", field, value))
return db
}
func (db *Builder) Exact(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__exact=%v", field, value))
return db
}
func (db *Builder) IExact(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iexact=%v", field, value))
return db
}
func (db *Builder) Contains(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__contains=%v", field, value))
return db
}
func (db *Builder) IContains(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__icontains=%v", field, value))
return db
}
func (db *Builder) In(field string, values ...string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__in=[%s]", field, strings.Join(values, ",")))
return db
}
func (db *Builder) Gt(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__gt=%v", field, value))
return db
}
func (db *Builder) Gte(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__gte=%v", field, value))
return db
}
func (db *Builder) Lt(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__lt=%v", field, value))
return db
}
func (db *Builder) Lte(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__lte=%v", field, value))
return db
}
func (db *Builder) Startswith(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__startswith=%v", field, value))
return db
}
func (db *Builder) IStartswith(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__istartswith=%v", field, value))
return db
}
func (db *Builder) Endswith(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__endswith=%v", field, value))
return db
}
func (db *Builder) IEndswith(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iendswith=%v", field, value))
return db
}
func (db *Builder) Range(field Field, from, to string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__range=(%s,%s)", field, from, to))
return db
}
func (db *Builder) Date(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__date=%v", field, value))
return db
}
func (db *Builder) Year(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__year=%v", field, value))
return db
}
func (db *Builder) Month(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__month=%v", field, value))
return db
}
func (db *Builder) Day(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__day=%v", field, value))
return db
}
func (db *Builder) Week(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__week=%v", field, value))
return db
}
func (db *Builder) WeekDay(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__week_day=%v", field, value))
return db
}
func (db *Builder) Quarter(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__quarter=%v", field, value))
return db
}
func (db *Builder) Time(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__time=%v", field, value))
return db
}
func (db *Builder) Hour(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__hour=%v", field, value))
return db
}
func (db *Builder) Minute(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__minute=%v", field, value))
return db
}
func (db *Builder) Second(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__second=%v", field, value))
return db
}
func (db *Builder) IsNull(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__isnull=%v", field, value))
return db
}
func (db *Builder) Regex(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__regex=r'%s'", field, value))
return db
}
func (db *Builder) IRegex(field Field, value interface{}) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iregex=r'%s'", field, value))
return db
}