Skip to content

Commit

Permalink
feat: add search tool #4 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
afumu authored Jul 15, 2023
1 parent 8bbceba commit a0a6a29
Show file tree
Hide file tree
Showing 11 changed files with 803 additions and 72 deletions.
8 changes: 6 additions & 2 deletions gplus/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func SelectById[T any](id any, opts ...OptionFunc) (*T, *gorm.DB) {
q.Eq(getPkColumnName[T](), id)
var entity T
resultDb := buildCondition(q, opts...)
return &entity, resultDb.First(&entity)
return &entity, resultDb.Take(&entity)
}

// SelectByIds 根据 ID 查询多条记录
Expand All @@ -162,7 +162,7 @@ func SelectByIds[T any](ids any, opts ...OptionFunc) ([]*T, *gorm.DB) {
func SelectOne[T any](q *QueryCond[T], opts ...OptionFunc) (*T, *gorm.DB) {
var entity T
resultDb := buildCondition(q, opts...)
return &entity, resultDb.First(&entity)
return &entity, resultDb.Take(&entity)
}

// SelectList 根据条件查询多条记录
Expand Down Expand Up @@ -279,6 +279,10 @@ func buildCondition[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
resultDb.Select(q.selectColumns)
}

if len(q.omitColumns) > 0 {
resultDb.Omit(q.omitColumns...)
}

expressions := q.queryExpressions
if len(expressions) > 0 {
var sqlBuilder strings.Builder
Expand Down
26 changes: 25 additions & 1 deletion gplus/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

type QueryCond[T any] struct {
selectColumns []string
omitColumns []string
distinctColumns []string
queryExpressions []any
orderBuilder strings.Builder
Expand All @@ -37,6 +38,7 @@ type QueryCond[T any] struct {
limit *int
offset int
updateMap map[string]any
columnTypeMap map[string]reflect.Type
}

func (q *QueryCond[T]) getSqlSegment() string {
Expand All @@ -46,7 +48,6 @@ func (q *QueryCond[T]) getSqlSegment() string {
// NewQuery 构建查询条件
func NewQuery[T any]() (*QueryCond[T], *T) {
q := &QueryCond[T]{}

modelTypeStr := reflect.TypeOf((*T)(nil)).Elem().String()
if model, ok := modelInstanceCache.Load(modelTypeStr); ok {
m, isReal := model.(*T)
Expand Down Expand Up @@ -150,13 +151,27 @@ func (q *QueryCond[T]) LikeLeft(column any, val any) *QueryCond[T] {
return q
}

// NotLikeLeft 非左模糊 NOT LIKE '%值'
func (q *QueryCond[T]) NotLikeLeft(column any, val any) *QueryCond[T] {
s := fmt.Sprintf("%v", val)
q.addExpression(q.buildSqlSegment(column, constants.Not+" "+constants.Like, "%"+s)...)
return q
}

// LikeRight 右模糊 LIKE '值%'
func (q *QueryCond[T]) LikeRight(column any, val any) *QueryCond[T] {
s := fmt.Sprintf("%v", val)
q.addExpression(q.buildSqlSegment(column, constants.Like, s+"%")...)
return q
}

// NotLikeRight 非右模糊 NOT LIKE '值%'
func (q *QueryCond[T]) NotLikeRight(column any, val any) *QueryCond[T] {
s := fmt.Sprintf("%v", val)
q.addExpression(q.buildSqlSegment(column, constants.Not+" "+constants.Like, s+"%")...)
return q
}

// IsNull 是否为空 字段 IS NULL
func (q *QueryCond[T]) IsNull(column any) *QueryCond[T] {
q.addExpression(q.buildSqlSegment(column, constants.IsNull, "")...)
Expand Down Expand Up @@ -284,6 +299,15 @@ func (q *QueryCond[T]) Select(columns ...any) *QueryCond[T] {
return q
}

// Omit 忽略字段
func (q *QueryCond[T]) Omit(columns ...any) *QueryCond[T] {
for _, v := range columns {
columnName := getColumnName(v)
q.omitColumns = append(q.omitColumns, columnName)
}
return q
}

// Set 设置更新的字段
func (q *QueryCond[T]) Set(column any, val any) *QueryCond[T] {
columnName := getColumnName(column)
Expand Down
File renamed without changes.
Loading

0 comments on commit a0a6a29

Please sign in to comment.