1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/jongsz-shorm

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
sqlwhere.go 3.4 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
shengzhi Отправлено 12.01.2018 09:12 bd6245f
// Copyright 2016 The shorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package shorm
import (
"reflect"
"strings"
)
// SqlWhere sql where 子句
type SqlWhere sqlClauseList
// W 新建 sql where 实例
func W() SqlWhere { return SqlWhere{} }
// Where equals < where col=? >
func (s SqlWhere) Where(clause string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_where,
clause: clause,
}
if len(args) > 0 {
subSql.params = append(subSql.params, args...)
}
s = append(s, subSql)
return s
}
// And equals < and col=? >
func (s SqlWhere) And(clause string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_and,
clause: clause,
}
if len(args) > 0 {
subSql.params = append(subSql.params, args...)
}
s = append(s, subSql)
return s
}
// Or equals < or col=? >
func (s SqlWhere) Or(clause string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_or,
clause: clause,
}
if len(args) > 0 {
subSql.params = append(subSql.params, args...)
}
s = append(s, subSql)
return s
}
// In equals < and col in(?) >
func (s SqlWhere) In(colName string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_in,
clause: colName,
}
if len(args) > 0 {
if len(args) > 1 {
subSql.params = append(subSql.params, args...)
} else {
arg := args[0]
val := reflect.ValueOf(arg)
if val.Type().Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
subSql.params = append(subSql.params, val.Index(i).Interface())
}
} else {
subSql.params = append(subSql.params, arg)
}
}
}
s = append(s, subSql)
return s
}
// OrIn equals < or col in (?) >
func (s SqlWhere) OrIn(colName string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_in_or,
clause: colName,
}
if len(args) > 0 {
subSql.params = append(subSql.params, args...)
}
s = append(s, subSql)
return s
}
// Between equals <and col between ? and ? >
func (s SqlWhere) Between(colName string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_between,
clause: colName,
}
if len(args) > 0 {
subSql.params = append(subSql.params, args...)
}
s = append(s, subSql)
return s
}
// OrBetween equals <or col between ? and ? >
func (s SqlWhere) OrBetween(colName string, args ...interface{}) SqlWhere {
subSql := sqlClause{
op: opType_between_or,
clause: colName,
}
if len(args) > 0 {
subSql.params = append(subSql.params, args...)
}
s = append(s, subSql)
return s
}
// Limit equals < limit skip,size >
func (s SqlWhere) Limit(skip, size int) SqlWhere {
subSql := sqlClause{
op: opType_limit,
}
subSql.params = append(subSql.params, skip, size)
s = append(s, subSql)
return s
}
// OrderBy equals <order by ? >
func (s SqlWhere) OrderBy(orderby ...string) SqlWhere {
subSql := sqlClause{
op: opType_orderby,
clause: strings.Join(orderby, ","),
}
s = append(s, subSql)
return s
}
// Cols specify which columns will be selected or affected
func (s SqlWhere) Cols(cols string) SqlWhere {
subSql := sqlClause{
op: opType_cols,
clause: cols,
}
s = append(s, subSql)
return s
}
// Omit specify which columns will not be selected or affected
func (s SqlWhere) Omit(cols string) SqlWhere {
subSql := sqlClause{
op: opType_omit,
clause: cols,
}
s = append(s, subSql)
return s
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/jongsz-shorm.git
git@api.gitlife.ru:oschina-mirror/jongsz-shorm.git
oschina-mirror
jongsz-shorm
jongsz-shorm
master