Skip to content

Commit

Permalink
chore: use nullBool instead of pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
chenquan authored and kevwan committed Sep 8, 2023
1 parent e32f67b commit a63d378
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
14 changes: 10 additions & 4 deletions core/stores/sqlx/sqlconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type (
}

logOption struct {
EnableStatement *bool
EnableSlow *bool
EnableStatement nullBool
EnableSlow nullBool
}

// thread-safe
Expand Down Expand Up @@ -430,13 +430,19 @@ func WithAcceptable(acceptable func(err error) bool) SqlOption {
// WithStatementLog returns a SqlOption to set whether to output SQL statements in the log.
func WithStatementLog(enable bool) SqlOption {
return func(conn *commonSqlConn) {
conn.logOption.EnableStatement = &enable
conn.logOption.EnableStatement = nullBool{
value: enable,
valid: true,
}
}
}

// WithSlowLog returns a SqlOption to set whether to output slow SQL statements in the log.
func WithSlowLog(enable bool) SqlOption {
return func(conn *commonSqlConn) {
conn.logOption.EnableSlow = &enable
conn.logOption.EnableSlow = nullBool{
value: enable,
valid: true,
}
}
}
8 changes: 4 additions & 4 deletions core/stores/sqlx/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ type (

func newGuard(logOpt logOption, command string) sqlGuard {
logStmt := logStmtSql.True()
if logOpt.EnableStatement != nil {
logStmt = *logOpt.EnableStatement
if logOpt.EnableStatement.valid {
logStmt = logOpt.EnableStatement.value
}

logSlow := logSlowSql.True()
if logOpt.EnableSlow != nil {
logSlow = *logOpt.EnableSlow
if logOpt.EnableSlow.valid {
logSlow = logOpt.EnableSlow.value
}

if logSlow || logStmt {
Expand Down
50 changes: 40 additions & 10 deletions core/stores/sqlx/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,33 +205,63 @@ func TestDisableStmtLog(t *testing.T) {
}

func TestDisableLogContext(t *testing.T) {
yes := true
no := false
t.Run("DisableLog", func(t *testing.T) {

assert.True(t, logStmtSql.True())
assert.True(t, logSlowSql.True())
defer func() {
logStmtSql.Set(true)
logSlowSql.Set(true)
}()

DisableLog()
assert.False(t, logStmtSql.True())
assert.False(t, logSlowSql.True())
guard := newGuard(logOption{}, "any")
assert.IsType(t, &nilGuard{}, guard)
})

t.Run("DisableStmtLog", func(t *testing.T) {
assert.True(t, logStmtSql.True())
assert.True(t, logSlowSql.True())
defer func() {
logStmtSql.Set(true)
logSlowSql.Set(true)
}()

DisableStmtLog()
assert.False(t, logStmtSql.True())
assert.True(t, logSlowSql.True())

guard := newGuard(logOption{}, "any")
assert.True(t, guard.(*realSqlGuard).slowLog(time.Hour))
assert.False(t, guard.(*realSqlGuard).statementLog())
})

t.Run("disable: Statement and Slow ", func(t *testing.T) {
guard := newGuard(logOption{
EnableStatement: &no,
EnableSlow: &no,
EnableStatement: nullBool{valid: true, value: false},
EnableSlow: nullBool{valid: true, value: false},
}, "any")
assert.IsType(t, &nilGuard{}, guard)
})

t.Run("DisableStatementLog", func(t *testing.T) {
t.Run("disable: Statement", func(t *testing.T) {
logStmtSql.Set(false)
defer logStmtSql.Set(true)

guard := newGuard(logOption{
EnableStatement: &yes,
EnableSlow: &no,
EnableStatement: nullBool{valid: true, value: true},
EnableSlow: nullBool{valid: true, value: false},
}, "any")
assert.False(t, guard.(*realSqlGuard).slowLog(time.Hour))
assert.True(t, guard.(*realSqlGuard).statementLog())
})

t.Run("DisableSlowLog", func(t *testing.T) {
t.Run("disable: Slow", func(t *testing.T) {
guard := newGuard(logOption{
EnableStatement: &no,
EnableSlow: &yes,
EnableStatement: nullBool{valid: true, value: false},
EnableSlow: nullBool{valid: true, value: true},
}, "any")
assert.True(t, guard.(*realSqlGuard).slowLog(time.Hour))
assert.False(t, guard.(*realSqlGuard).statementLog())
Expand Down
6 changes: 6 additions & 0 deletions core/stores/sqlx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,9 @@ func newAcceptableError(err error) error {
func (e acceptableError) Error() string {
return e.err.Error()
}

// nullBool represents a bool that may be null.
type nullBool struct {
value bool
valid bool // valid is true if Bool is not NULL
}

0 comments on commit a63d378

Please sign in to comment.