golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.84k stars 17.51k forks source link

database/sql exec and ExecContext RowsAffected() The number of affected lines is inaccurate #68852

Closed ICF-M78 closed 1 month ago

ICF-M78 commented 1 month ago

Go version

go 1.22.6

Output of go env in your module/workspace:

module ApiSvr

go 1.22.6

require (
    github.com/denisenkom/go-mssqldb v0.12.3
    github.com/sirupsen/logrus v1.9.3
)

require (
    dario.cat/mergo v1.0.0 // indirect
    github.com/bep/godartsass v1.2.0 // indirect
    github.com/bep/godartsass/v2 v2.0.0 // indirect
    github.com/bep/golibsass v1.1.1 // indirect
    github.com/cli/safeexec v1.0.1 // indirect
    github.com/cosmtrek/air v1.49.0 // indirect
    github.com/creack/pty v1.1.21 // indirect
    github.com/dlclark/regexp2 v1.11.0 // indirect
    github.com/dustin/go-humanize v1.0.1 // indirect
    github.com/fatih/color v1.16.0 // indirect
    github.com/fsnotify/fsnotify v1.7.0 // indirect
    github.com/gohugoio/hugo v0.121.1 // indirect
    github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
    github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
    github.com/hashicorp/hcl v1.0.0 // indirect
    github.com/howeyc/fsnotify v0.9.0 // indirect
    github.com/jmoiron/sqlx v1.3.5 // indirect
    github.com/magiconair/properties v1.8.7 // indirect
    github.com/mattn/go-colorable v0.1.13 // indirect
    github.com/mitchellh/mapstructure v1.5.0 // indirect
    github.com/ncruces/go-strftime v0.1.9 // indirect
    github.com/pelletier/go-toml v1.9.5 // indirect
    github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
    github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
    github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
    github.com/robertkrimen/otto v0.3.0 // indirect
    github.com/sagikazarmark/locafero v0.3.0 // indirect
    github.com/sagikazarmark/slog-shim v0.1.0 // indirect
    github.com/sourcegraph/conc v0.3.0 // indirect
    github.com/spf13/afero v1.11.0 // indirect
    github.com/spf13/cast v1.6.0 // indirect
    github.com/spf13/pflag v1.0.5 // indirect
    github.com/subosito/gotenv v1.6.0 // indirect
    github.com/tdewolff/parse/v2 v2.7.6 // indirect
    go.uber.org/atomic v1.11.0 // indirect
    go.uber.org/multierr v1.9.0 // indirect
    golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
    gopkg.in/ini.v1 v1.67.0 // indirect
    gopkg.in/sourcemap.v1 v1.0.5 // indirect
    modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
    modernc.org/libc v1.50.9 // indirect
    modernc.org/mathutil v1.6.0 // indirect
    modernc.org/memory v1.8.0 // indirect
    modernc.org/sqlite v1.30.0 // indirect
    modernc.org/strutil v1.2.0 // indirect
    modernc.org/token v1.1.0 // indirect
)

require (
    github.com/bytedance/sonic v1.10.2 // indirect
    github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
    github.com/chenzhuoyu/iasm v0.9.1 // indirect
    github.com/gabriel-vasile/mimetype v1.4.3 // indirect
    github.com/gin-contrib/sse v0.1.0 // indirect
    github.com/gin-gonic/gin v1.9.1 // indirect
    github.com/go-playground/locales v0.14.1 // indirect
    github.com/go-playground/universal-translator v0.18.1 // indirect
    github.com/go-playground/validator/v10 v10.16.0 // indirect
    github.com/goccy/go-json v0.10.2 // indirect
    github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
    github.com/golang-sql/sqlexp v0.1.0 // indirect
    github.com/google/uuid v1.6.0
    github.com/json-iterator/go v1.1.12 // indirect
    github.com/klauspost/cpuid/v2 v2.2.7 // indirect
    github.com/leodido/go-urn v1.2.4 // indirect
    github.com/mattn/go-isatty v0.0.20 // indirect
    github.com/mattn/go-sqlite3 v1.14.22
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.2 // indirect
    github.com/pelletier/go-toml/v2 v2.1.0 // indirect
    github.com/spf13/viper v1.17.0
    github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
    github.com/ugorji/go/codec v1.2.12 // indirect
    golang.org/x/arch v0.6.0 // indirect
    golang.org/x/crypto v0.16.0 // indirect
    golang.org/x/net v0.19.0 // indirect
    golang.org/x/sys v0.19.0 // indirect
    golang.org/x/text v0.14.0 // indirect
    google.golang.org/protobuf v1.31.0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)

What did you do?

After connecting to sqlsever, use ExecContext or exec to declare @_id varchar (20) = 1 to return, 1 line is affected

What did you see happen?

// 执行SQL语句
func MsSqlExec(opts MsSqlOptions) (int, error) {
    conn, err := GetMsSqlDbc(opts.ConnStr)
    if err != nil {
        return 0, err
    }
    defer conn.Close()

    // 设置超时时间
    ctx, cancel := context.WithTimeout(context.Background(), time.Duration(opts.ConnTimes)*time.Second)
    defer cancel()

    var execRes sql.Result
    if opts.Args == nil {
        execRes, err = conn.ExecContext(ctx, opts.SqlStr)
    } else {
        params := make([]interface{}, 0, len(opts.Args))
        for k, v := range opts.Args {
            params = append(params, sql.Named(k, v))
        }
        execRes, err = conn.ExecContext(ctx, opts.SqlStr, params...)
    }

    if err != nil {
        return 0, err
    }

    rows, err := execRes.RowsAffected()
    if err != nil {
        return 0, err
    }

    if rows == 0 {
        return 0, nil
    }

    return int(rows), nil
}

What did you expect to see?

Declare affects the number of affected rows returned by subsequent SQL returns

seankhliao commented 1 month ago

That sounds like a driver issue, please report it to them.