Skip to content

Commit a0a6a29

Browse files
authored
feat: add search tool #4 (#59)
1 parent 8bbceba commit a0a6a29

11 files changed

+803
-72
lines changed

gplus/dao.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func SelectById[T any](id any, opts ...OptionFunc) (*T, *gorm.DB) {
148148
q.Eq(getPkColumnName[T](), id)
149149
var entity T
150150
resultDb := buildCondition(q, opts...)
151-
return &entity, resultDb.First(&entity)
151+
return &entity, resultDb.Take(&entity)
152152
}
153153

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

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

282+
if len(q.omitColumns) > 0 {
283+
resultDb.Omit(q.omitColumns...)
284+
}
285+
282286
expressions := q.queryExpressions
283287
if len(expressions) > 0 {
284288
var sqlBuilder strings.Builder

gplus/query.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
type QueryCond[T any] struct {
2828
selectColumns []string
29+
omitColumns []string
2930
distinctColumns []string
3031
queryExpressions []any
3132
orderBuilder strings.Builder
@@ -37,6 +38,7 @@ type QueryCond[T any] struct {
3738
limit *int
3839
offset int
3940
updateMap map[string]any
41+
columnTypeMap map[string]reflect.Type
4042
}
4143

4244
func (q *QueryCond[T]) getSqlSegment() string {
@@ -46,7 +48,6 @@ func (q *QueryCond[T]) getSqlSegment() string {
4648
// NewQuery 构建查询条件
4749
func NewQuery[T any]() (*QueryCond[T], *T) {
4850
q := &QueryCond[T]{}
49-
5051
modelTypeStr := reflect.TypeOf((*T)(nil)).Elem().String()
5152
if model, ok := modelInstanceCache.Load(modelTypeStr); ok {
5253
m, isReal := model.(*T)
@@ -150,13 +151,27 @@ func (q *QueryCond[T]) LikeLeft(column any, val any) *QueryCond[T] {
150151
return q
151152
}
152153

154+
// NotLikeLeft 非左模糊 NOT LIKE '%值'
155+
func (q *QueryCond[T]) NotLikeLeft(column any, val any) *QueryCond[T] {
156+
s := fmt.Sprintf("%v", val)
157+
q.addExpression(q.buildSqlSegment(column, constants.Not+" "+constants.Like, "%"+s)...)
158+
return q
159+
}
160+
153161
// LikeRight 右模糊 LIKE '值%'
154162
func (q *QueryCond[T]) LikeRight(column any, val any) *QueryCond[T] {
155163
s := fmt.Sprintf("%v", val)
156164
q.addExpression(q.buildSqlSegment(column, constants.Like, s+"%")...)
157165
return q
158166
}
159167

168+
// NotLikeRight 非右模糊 NOT LIKE '值%'
169+
func (q *QueryCond[T]) NotLikeRight(column any, val any) *QueryCond[T] {
170+
s := fmt.Sprintf("%v", val)
171+
q.addExpression(q.buildSqlSegment(column, constants.Not+" "+constants.Like, s+"%")...)
172+
return q
173+
}
174+
160175
// IsNull 是否为空 字段 IS NULL
161176
func (q *QueryCond[T]) IsNull(column any) *QueryCond[T] {
162177
q.addExpression(q.buildSqlSegment(column, constants.IsNull, "")...)
@@ -284,6 +299,15 @@ func (q *QueryCond[T]) Select(columns ...any) *QueryCond[T] {
284299
return q
285300
}
286301

302+
// Omit 忽略字段
303+
func (q *QueryCond[T]) Omit(columns ...any) *QueryCond[T] {
304+
for _, v := range columns {
305+
columnName := getColumnName(v)
306+
q.omitColumns = append(q.omitColumns, columnName)
307+
}
308+
return q
309+
}
310+
287311
// Set 设置更新的字段
288312
func (q *QueryCond[T]) Set(column any, val any) *QueryCond[T] {
289313
columnName := getColumnName(column)
File renamed without changes.

0 commit comments

Comments
 (0)