Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Dec 2, 2024
2 parents 63d0e16 + 2d0cd7b commit 2901ff4
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 75 deletions.
17 changes: 4 additions & 13 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
# Tencent is pleased to support the open source community by making Polaris available.
# Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# This Source Code Form is subject to the terms of the MIT License.
# If a copy of the MIT was not distributed with this file,
# You can obtain one at https://github.com/gogf/gf.

name: GolangCI-Lint
on:
Expand Down
9 changes: 5 additions & 4 deletions cmd/gf/internal/cmd/genenums/genenums.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package genenums

import (
"context"

"golang.org/x/tools/go/packages"

"github.com/gogf/gf/cmd/gf/v2/internal/consts"
Expand All @@ -22,8 +23,8 @@ type (
CGenEnums struct{}
CGenEnumsInput struct {
g.Meta `name:"enums" config:"{CGenEnumsConfig}" brief:"{CGenEnumsBrief}" eg:"{CGenEnumsEg}"`
Src string `name:"src" short:"s" dc:"source folder path to be parsed" d:"."`
Path string `name:"path" short:"p" dc:"output go file path storing enums content" d:"internal/boot/boot_enums.go"`
Src string `name:"src" short:"s" dc:"source folder path to be parsed" d:"api"`
Path string `name:"path" short:"p" dc:"output go file path storing enums content" d:"internal/packed/packed_enums.go"`
Prefixes []string `name:"prefixes" short:"x" dc:"only exports packages that starts with specified prefixes"`
}
CGenEnumsOutput struct{}
Expand All @@ -34,8 +35,8 @@ const (
CGenEnumsBrief = `parse go files in current project and generate enums go file`
CGenEnumsEg = `
gf gen enums
gf gen enums -p internal/boot/boot_enums.go
gf gen enums -p internal/boot/boot_enums.go -s .
gf gen enums -p internal/packed/packed_enums.go
gf gen enums -p internal/packed/packed_enums.go -s .
gf gen enums -x github.com/gogf
`
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/genenums/genenums_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ package genenums
import (
"go/constant"
"go/types"
"golang.org/x/tools/go/packages"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"golang.org/x/tools/go/packages"
)

const pkgLoadMode = 0xffffff
Expand Down
34 changes: 33 additions & 1 deletion contrib/drivers/mysql/mysql_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
Expand Down Expand Up @@ -1283,12 +1284,12 @@ func Test_Issue3754(t *testing.T) {
func Test_Issue3626(t *testing.T) {
table := "issue3626"
array := gstr.SplitAndTrim(gtest.DataContent(`issue3626.sql`), ";")
defer dropTable(table)
for _, v := range array {
if _, err := db.Exec(ctx, v); err != nil {
gtest.Error(err)
}
}
defer dropTable(table)

// Insert.
gtest.C(t, func(t *gtest.T) {
Expand Down Expand Up @@ -1378,6 +1379,37 @@ func Test_Issue3932(t *testing.T) {
})
}

// https://github.com/gogf/gf/issues/3968
func Test_Issue3968(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
var hook = gdb.HookHandler{
Select: func(ctx context.Context, in *gdb.HookSelectInput) (result gdb.Result, err error) {
result, err = in.Next(ctx)
if err != nil {
return nil, err
}
if result != nil {
for i, _ := range result {
result[i]["location"] = gvar.New("ny")
}
}
return
},
}
var (
count int
result gdb.Result
)
err := db.Model(table).Hook(hook).ScanAndCount(&result, &count, false)
t.AssertNil(err)
t.Assert(count, 10)
t.Assert(len(result), 10)
})
}

// https://github.com/gogf/gf/issues/3915
func Test_Issue3915(t *testing.T) {
table := "issue3915"
Expand Down
21 changes: 11 additions & 10 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@ const (
linkPattern = `(\w+):([\w\-\$]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([^\?]*)\?{0,1}(.*)`
)

type queryType int
type SelectType int

const (
queryTypeNormal queryType = iota
queryTypeCount
queryTypeValue
SelectTypeDefault SelectType = iota
SelectTypeCount
SelectTypeValue
SelectTypeArray
)

type joinOperator string
Expand Down Expand Up @@ -700,21 +701,21 @@ func getConfigNodeByWeight(cg ConfigGroup) *ConfigNode {
}
// Exclude the right border value.
var (
min = 0
max = 0
random = grand.N(0, total-1)
minWeight = 0
maxWeight = 0
random = grand.N(0, total-1)
)
for i := 0; i < len(cg); i++ {
max = min + cg[i].Weight*100
if random >= min && random < max {
maxWeight = minWeight + cg[i].Weight*100
if random >= minWeight && random < maxWeight {
// ====================================================
// Return a COPY of the ConfigNode.
// ====================================================
node := ConfigNode{}
node = cg[i]
return &node
}
min = max
minWeight = maxWeight
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (c *Core) doUnion(ctx context.Context, unionType int, unions ...*Model) *Mo
unionTypeStr = "UNION"
}
for _, v := range unions {
sqlWithHolder, holderArgs := v.getFormattedSqlAndArgs(ctx, queryTypeNormal, false)
sqlWithHolder, holderArgs := v.getFormattedSqlAndArgs(ctx, SelectTypeDefault, false)
if composedSqlStr == "" {
composedSqlStr += fmt.Sprintf(`(%s)`, sqlWithHolder)
} else {
Expand Down
2 changes: 0 additions & 2 deletions database/gdb/gdb_core_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ type internalCtxData struct {
}

// column stores column data in ctx for internal usage purpose.
// Deprecated.
// TODO remove this usage in future.
type internalColumnData struct {
// The first column in result response from database server.
// This attribute is used for Value/Count selection statement purpose,
Expand Down
9 changes: 5 additions & 4 deletions database/gdb/gdb_model_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (m *Model) getSelectResultFromCache(ctx context.Context, sql string, args .
}

func (m *Model) saveSelectResultToCache(
ctx context.Context, queryType queryType, result Result, sql string, args ...interface{},
ctx context.Context, selectType SelectType, result Result, sql string, args ...interface{},
) (err error) {
if !m.cacheEnabled || m.tx != nil {
return
Expand All @@ -108,18 +108,19 @@ func (m *Model) saveSelectResultToCache(
// Special handler for Value/Count operations result.
if len(result) > 0 {
var core = m.db.GetCore()
switch queryType {
case queryTypeValue, queryTypeCount:
switch selectType {
case SelectTypeValue, SelectTypeArray, SelectTypeCount:
if internalData := core.getInternalColumnFromCtx(ctx); internalData != nil {
if result[0][internalData.FirstResultColumn].IsEmpty() {
result = nil
}
}
default:
}
}

// In case of Cache Penetration.
if result.IsEmpty() {
if result != nil && result.IsEmpty() {
if m.cacheOption.Force {
result = Result{}
} else {
Expand Down
11 changes: 6 additions & 5 deletions database/gdb/gdb_model_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ type internalParamHookDelete struct {
// which is usually not be interesting for upper business hook handler.
type HookSelectInput struct {
internalParamHookSelect
Model *Model // Current operation Model.
Table string // The table name that to be used. Update this attribute to change target table name.
Schema string // The schema name that to be used. Update this attribute to change target schema name.
Sql string // The sql string that to be committed.
Args []interface{} // The arguments of sql.
Model *Model // Current operation Model.
Table string // The table name that to be used. Update this attribute to change target table name.
Schema string // The schema name that to be used. Update this attribute to change target schema name.
Sql string // The sql string that to be committed.
Args []interface{} // The arguments of sql.
SelectType SelectType // The type of this SELECT operation.
}

// HookInsertInput holds the parameters for insert hook operation.
Expand Down
Loading

0 comments on commit 2901ff4

Please sign in to comment.