golib/gdb/sqldb/db_func_opt.go
2023-06-15 21:22:51 +08:00

76 lines
1.6 KiB
Go

//
// db_func_opt.go
// Copyright (C) 2023 tiglog <me@tiglog.com>
//
// Distributed under terms of the MIT license.
//
package sqldb
type QueryOption struct {
table string
query string
set string
fields []string
sort string
offset int
limit int
args []any
joins []string
}
func NewQueryOption(table string) *QueryOption {
return &QueryOption{
table: table,
fields: []string{"*"},
offset: 0,
limit: 0,
args: make([]any, 0),
joins: make([]string, 0),
}
}
func (opt *QueryOption) Query(query string) *QueryOption {
opt.query = query
return opt
}
func (opt *QueryOption) Fields(args []string) *QueryOption {
opt.fields = args
return opt
}
func (opt *QueryOption) Select(cols ...string) *QueryOption {
opt.fields = cols
return opt
}
func (opt *QueryOption) Offset(offset int) *QueryOption {
opt.offset = offset
return opt
}
func (opt *QueryOption) Limit(limit int) *QueryOption {
opt.limit = limit
return opt
}
func (opt *QueryOption) Sort(sort string) *QueryOption {
opt.sort = sort
return opt
}
func (opt *QueryOption) Set(set string) *QueryOption {
opt.set = set
return opt
}
func (opt *QueryOption) Args(args ...any) *QueryOption {
opt.args = args
return opt
}
func (opt *QueryOption) Join(table string, cond string) *QueryOption {
opt.joins = append(opt.joins, "join "+table+" on "+cond)
return opt
}
func (opt *QueryOption) LeftJoin(table string, cond string) *QueryOption {
opt.joins = append(opt.joins, "left join "+table+" on "+cond)
return opt
}
func (opt *QueryOption) RightJoin(table string, cond string) *QueryOption {
opt.joins = append(opt.joins, "right join "+table+" on "+cond)
return opt
}