* feature extend readthrough for cache module (#5116) * feature 增加readthrough * feature: add write though for cache mode (#5117) * feature: add writethough for cache mode * feature add singleflight cache (#5119) * build(deps): bump go.opentelemetry.io/otel/trace from 1.8.0 to 1.11.2 Bumps [go.opentelemetry.io/otel/trace](https://github.com/open-telemetry/opentelemetry-go) from 1.8.0 to 1.11.2. - [Release notes](https://github.com/open-telemetry/opentelemetry-go/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go/compare/v1.8.0...v1.11.2) --- updated-dependencies: - dependency-name: go.opentelemetry.io/otel/trace dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * fix 5129: must set formatter after init the logger * remove beego.vip * build(deps): bump actions/stale from 5 to 7 Bumps [actions/stale](https://github.com/actions/stale) from 5 to 7. - [Release notes](https://github.com/actions/stale/releases) - [Changelog](https://github.com/actions/stale/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/stale/compare/v5...v7) --- updated-dependencies: - dependency-name: actions/stale dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * fix 5079: only log msg when the channel is not closed (#5132) * optimize test * upgrade otel dependencies to v1.11.2 * format code * Bloom filter cache (#5126) * feature: add bloom filter cache * feature upload remove all temp file * bugfix Controller SaveToFile remove all temp file * rft: motify BeeLogger signalChan (#5139) * add non-block write log in asynchronous mode (#5150) * add non-block write log in asynchronous mode --------- Co-authored-by: chenhaokun <chenhaokun@itiger.com> * fix the docsite URL (#5173) * Unified gopkg.in/yaml version to v2 (#5169) * Unified gopkg.in/yaml version to v2 and go mod tidy * update CHANGELOG * bugfix: protect field access with lock to avoid possible data race (#5211) * fix some comments (#5194) Signed-off-by: cui fliter <imcusg@gmail.com> * build(deps): bump github.com/prometheus/client_golang (#5213) Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.14.0 to 1.15.1. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.14.0...v1.15.1) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * build(deps): bump go.etcd.io/etcd/client/v3 from 3.5.4 to 3.5.9 (#5209) Bumps [go.etcd.io/etcd/client/v3](https://github.com/etcd-io/etcd) from 3.5.4 to 3.5.9. - [Release notes](https://github.com/etcd-io/etcd/releases) - [Commits](https://github.com/etcd-io/etcd/compare/v3.5.4...v3.5.9) --- updated-dependencies: - dependency-name: go.etcd.io/etcd/client/v3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * cache: fix typo and optimize the naming * Release 2.1.0 change log * bugfix: beegoAppConfig String and Strings function has bug * httplib: fix unstable test, do not use httplib.org * chore: pkg imported more than once * chore: fmt modify * chore: Use github.com/go-kit/log * chore: unnecessary use of fmt.Sprintf * fix: golangci-lint error * orm: refactor ORM introducing internal/models pkg * remove adapter package * build(deps): bump github.com/bits-and-blooms/bloom/v3 Bumps [github.com/bits-and-blooms/bloom/v3](https://github.com/bits-and-blooms/bloom) from 3.3.1 to 3.5.0. - [Release notes](https://github.com/bits-and-blooms/bloom/releases) - [Commits](https://github.com/bits-and-blooms/bloom/compare/v3.3.1...v3.5.0) --- updated-dependencies: - dependency-name: github.com/bits-and-blooms/bloom/v3 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * feat: add write-delete cache mode * fix: unnecessary assignment to the blank identifier * fix: add change into .CHANGELOG file * build(deps): bump golang.org/x/sync from 0.1.0 to 0.3.0 Bumps [golang.org/x/sync](https://github.com/golang/sync) from 0.1.0 to 0.3.0. - [Commits](https://github.com/golang/sync/compare/v0.1.0...v0.3.0) --- updated-dependencies: - dependency-name: golang.org/x/sync dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * build(deps): bump golang.org/x/crypto Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.0.0-20220315160706-3147a52a75dd to 0.10.0. - [Commits](https://github.com/golang/crypto/commits/v0.10.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * remove golang--lint-ci * Beego web.Run() runs the server twice * fix 5255: Check the rows.Err() if rows.Next() is false * closes 5254: %COL% should be a common placeholder * build(deps): bump github.com/prometheus/client_golang Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.15.1 to 1.16.0. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.15.1...v1.16.0) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * fix: use of ioutil package (#5261) * fix ioutil.NopCloser * fix ioutil.ReadAll * fix ioutil.ReadFile * fix ioutil.WriteFile * run goimports -w -format-only ./ * update CHANGELOG.md * feature: add write-double-delete cache mode (#5263) * cache/redis: support skipEmptyPrefix option (#5264) * fix: refactor InsertValue method (#5267) * fix: refactor insertValue method and add the test * fix: exec goimports and add Licence file header * fix: modify construct method of dbBase * fix: add modify record into CHANGELOG * fix: modify InsertOrUpdate method (#5269) * fix: modify InsertOrUpdate method, Remove the isMulti variable and its associated code * fix: Delete unnecessary judgment branches * fix: add modify record into CHANGELOG * cache/redis: use redisConfig to receive incoming JSON (previously using a map) (#5268) * refactor cache/redis: Use redisConfig to receive incoming JSON (previously using a map). * refactor cache/redis: Use the string type to receive JSON parameters. --------- Co-authored-by: Tan <tanqianheng@gmail.com> * fix: refactor Delete method (#5271) * fix: refactor Delete method and add test * fix: add modify record into CHANGELOG * fix: refactor update sql (#5274) * fix: refactor UpdateSQL method and add test * fix: add modify record into CHANGELOG * fix: modify url in the CHANGELOG * fix: modify pr url in the CHANGELOG * Fix setPK function for table without primary key (#5276) --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: cui fliter <imcusg@gmail.com> Co-authored-by: Stone-afk <73482944+Stone-afk@users.noreply.github.com> Co-authored-by: hookokoko <hooko@tju.edu.cn> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: hookokoko <648646891@qq.com> Co-authored-by: Stone-afk <1711865140@qq.com> Co-authored-by: chenhaokun <chenhaokun@itiger.com> Co-authored-by: Xuing <admin@xuing.cn> Co-authored-by: cui fliter <imcusg@gmail.com> Co-authored-by: guoguangwu <guoguangwu@magic-shield.com> Co-authored-by: uzziah <uzziahlin@gmail.com> Co-authored-by: Hanjiang Yu <delacroix.yu@gmail.com> Co-authored-by: Kota <mdryzk64smsh@gmail.com> Co-authored-by: Uzziah <120019273+uzziahlin@users.noreply.github.com> Co-authored-by: Handkerchiefs-t <59816423+Handkerchiefs-t@users.noreply.github.com> Co-authored-by: Tan <tanqianheng@gmail.com> Co-authored-by: mlgd <mlgd17@gmail.com>
		
			
				
	
	
		
			925 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			925 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2014 beego Author. All Rights Reserved.
 | |
| //
 | |
| // Licensed under the Apache License, Version 2.0 (the "License");
 | |
| // you may not use this file except in compliance with the License.
 | |
| // You may obtain a copy of the License at
 | |
| //
 | |
| //      http://www.apache.org/licenses/LICENSE-2.0
 | |
| //
 | |
| // 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.
 | |
| 
 | |
| package orm
 | |
| 
 | |
| import (
 | |
| 	"database/sql"
 | |
| 	"fmt"
 | |
| 	"reflect"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/beego/beego/v2/client/orm/internal/utils"
 | |
| 
 | |
| 	"github.com/beego/beego/v2/client/orm/internal/models"
 | |
| 
 | |
| 	"github.com/pkg/errors"
 | |
| )
 | |
| 
 | |
| // raw sql string prepared statement
 | |
| type rawPrepare struct {
 | |
| 	rs     *rawSet
 | |
| 	stmt   stmtQuerier
 | |
| 	closed bool
 | |
| }
 | |
| 
 | |
| func (o *rawPrepare) Exec(args ...interface{}) (sql.Result, error) {
 | |
| 	if o.closed {
 | |
| 		return nil, ErrStmtClosed
 | |
| 	}
 | |
| 	flatParams := getFlatParams(nil, args, o.rs.orm.alias.TZ)
 | |
| 	return o.stmt.Exec(flatParams...)
 | |
| }
 | |
| 
 | |
| func (o *rawPrepare) Close() error {
 | |
| 	o.closed = true
 | |
| 	return o.stmt.Close()
 | |
| }
 | |
| 
 | |
| func newRawPreparer(rs *rawSet) (RawPreparer, error) {
 | |
| 	o := new(rawPrepare)
 | |
| 	o.rs = rs
 | |
| 
 | |
| 	query := rs.query
 | |
| 	rs.orm.alias.DbBaser.ReplaceMarks(&query)
 | |
| 
 | |
| 	st, err := rs.orm.db.Prepare(query)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	if Debug {
 | |
| 		o.stmt = newStmtQueryLog(rs.orm.alias, st, query)
 | |
| 	} else {
 | |
| 		o.stmt = st
 | |
| 	}
 | |
| 	return o, nil
 | |
| }
 | |
| 
 | |
| // raw query seter
 | |
| type rawSet struct {
 | |
| 	query string
 | |
| 	args  []interface{}
 | |
| 	orm   *ormBase
 | |
| }
 | |
| 
 | |
| var _ RawSeter = new(rawSet)
 | |
| 
 | |
| // set args for every query
 | |
| func (o rawSet) SetArgs(args ...interface{}) RawSeter {
 | |
| 	o.args = args
 | |
| 	return &o
 | |
| }
 | |
| 
 | |
| // execute raw sql and return sql.Result
 | |
| func (o *rawSet) Exec() (sql.Result, error) {
 | |
| 	query := o.query
 | |
| 	o.orm.alias.DbBaser.ReplaceMarks(&query)
 | |
| 
 | |
| 	args := getFlatParams(nil, o.args, o.orm.alias.TZ)
 | |
| 	return o.orm.db.Exec(query, args...)
 | |
| }
 | |
| 
 | |
| // set field value to row container
 | |
| func (o *rawSet) setFieldValue(ind reflect.Value, value interface{}) {
 | |
| 	switch ind.Kind() {
 | |
| 	case reflect.Bool:
 | |
| 		if value == nil {
 | |
| 			ind.SetBool(false)
 | |
| 		} else if v, ok := value.(bool); ok {
 | |
| 			ind.SetBool(v)
 | |
| 		} else {
 | |
| 			v, _ := utils.StrTo(utils.ToStr(value)).Bool()
 | |
| 			ind.SetBool(v)
 | |
| 		}
 | |
| 
 | |
| 	case reflect.String:
 | |
| 		if value == nil {
 | |
| 			ind.SetString("")
 | |
| 		} else {
 | |
| 			ind.SetString(utils.ToStr(value))
 | |
| 		}
 | |
| 
 | |
| 	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
 | |
| 		if value == nil {
 | |
| 			ind.SetInt(0)
 | |
| 		} else {
 | |
| 			val := reflect.ValueOf(value)
 | |
| 			switch val.Kind() {
 | |
| 			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
 | |
| 				ind.SetInt(val.Int())
 | |
| 			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
 | |
| 				ind.SetInt(int64(val.Uint()))
 | |
| 			default:
 | |
| 				v, _ := utils.StrTo(utils.ToStr(value)).Int64()
 | |
| 				ind.SetInt(v)
 | |
| 			}
 | |
| 		}
 | |
| 	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
 | |
| 		if value == nil {
 | |
| 			ind.SetUint(0)
 | |
| 		} else {
 | |
| 			val := reflect.ValueOf(value)
 | |
| 			switch val.Kind() {
 | |
| 			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
 | |
| 				ind.SetUint(uint64(val.Int()))
 | |
| 			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
 | |
| 				ind.SetUint(val.Uint())
 | |
| 			default:
 | |
| 				v, _ := utils.StrTo(utils.ToStr(value)).Uint64()
 | |
| 				ind.SetUint(v)
 | |
| 			}
 | |
| 		}
 | |
| 	case reflect.Float64, reflect.Float32:
 | |
| 		if value == nil {
 | |
| 			ind.SetFloat(0)
 | |
| 		} else {
 | |
| 			val := reflect.ValueOf(value)
 | |
| 			switch val.Kind() {
 | |
| 			case reflect.Float64:
 | |
| 				ind.SetFloat(val.Float())
 | |
| 			default:
 | |
| 				v, _ := utils.StrTo(utils.ToStr(value)).Float64()
 | |
| 				ind.SetFloat(v)
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 	case reflect.Struct:
 | |
| 		if value == nil {
 | |
| 			ind.Set(reflect.Zero(ind.Type()))
 | |
| 			return
 | |
| 		}
 | |
| 		switch ind.Interface().(type) {
 | |
| 		case time.Time:
 | |
| 			var str string
 | |
| 			switch d := value.(type) {
 | |
| 			case time.Time:
 | |
| 				o.orm.alias.DbBaser.TimeFromDB(&d, o.orm.alias.TZ)
 | |
| 				ind.Set(reflect.ValueOf(d))
 | |
| 			case []byte:
 | |
| 				str = string(d)
 | |
| 			case string:
 | |
| 				str = d
 | |
| 			}
 | |
| 			if str != "" {
 | |
| 				if len(str) >= 19 {
 | |
| 					str = str[:19]
 | |
| 					t, err := time.ParseInLocation(utils.FormatDateTime, str, o.orm.alias.TZ)
 | |
| 					if err == nil {
 | |
| 						t = t.In(DefaultTimeLoc)
 | |
| 						ind.Set(reflect.ValueOf(t))
 | |
| 					}
 | |
| 				} else if len(str) >= 10 {
 | |
| 					str = str[:10]
 | |
| 					t, err := time.ParseInLocation(utils.FormatDate, str, DefaultTimeLoc)
 | |
| 					if err == nil {
 | |
| 						ind.Set(reflect.ValueOf(t))
 | |
| 					}
 | |
| 				} else if len(str) >= 8 {
 | |
| 					str = str[:8]
 | |
| 					t, err := time.ParseInLocation(utils.FormatTime, str, DefaultTimeLoc)
 | |
| 					if err == nil {
 | |
| 						ind.Set(reflect.ValueOf(t))
 | |
| 					}
 | |
| 				}
 | |
| 			}
 | |
| 		case sql.NullString, sql.NullInt64, sql.NullFloat64, sql.NullBool:
 | |
| 			indi := reflect.New(ind.Type()).Interface()
 | |
| 			sc, ok := indi.(sql.Scanner)
 | |
| 			if !ok {
 | |
| 				return
 | |
| 			}
 | |
| 			err := sc.Scan(value)
 | |
| 			if err == nil {
 | |
| 				ind.Set(reflect.Indirect(reflect.ValueOf(sc)))
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 	case reflect.Ptr:
 | |
| 		if value == nil {
 | |
| 			ind.Set(reflect.Zero(ind.Type()))
 | |
| 			break
 | |
| 		}
 | |
| 		ind.Set(reflect.New(ind.Type().Elem()))
 | |
| 		o.setFieldValue(reflect.Indirect(ind), value)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // set field value in loop for slice container
 | |
| func (o *rawSet) loopSetRefs(refs []interface{}, sInds []reflect.Value, nIndsPtr *[]reflect.Value, eTyps []reflect.Type, init bool) {
 | |
| 	nInds := *nIndsPtr
 | |
| 
 | |
| 	cur := 0
 | |
| 	for i := 0; i < len(sInds); i++ {
 | |
| 		sInd := sInds[i]
 | |
| 		eTyp := eTyps[i]
 | |
| 
 | |
| 		typ := eTyp
 | |
| 		isPtr := false
 | |
| 		if typ.Kind() == reflect.Ptr {
 | |
| 			isPtr = true
 | |
| 			typ = typ.Elem()
 | |
| 		}
 | |
| 		if typ.Kind() == reflect.Ptr {
 | |
| 			isPtr = true
 | |
| 			typ = typ.Elem()
 | |
| 		}
 | |
| 
 | |
| 		var nInd reflect.Value
 | |
| 		if init {
 | |
| 			nInd = reflect.New(sInd.Type()).Elem()
 | |
| 		} else {
 | |
| 			nInd = nInds[i]
 | |
| 		}
 | |
| 
 | |
| 		val := reflect.New(typ)
 | |
| 		ind := val.Elem()
 | |
| 
 | |
| 		tpName := ind.Type().String()
 | |
| 
 | |
| 		if ind.Kind() == reflect.Struct {
 | |
| 			if tpName == "time.Time" {
 | |
| 				value := reflect.ValueOf(refs[cur]).Elem().Interface()
 | |
| 				if isPtr && value == nil {
 | |
| 					val = reflect.New(val.Type()).Elem()
 | |
| 				} else {
 | |
| 					o.setFieldValue(ind, value)
 | |
| 				}
 | |
| 				cur++
 | |
| 			}
 | |
| 		} else {
 | |
| 			value := reflect.ValueOf(refs[cur]).Elem().Interface()
 | |
| 			if isPtr && value == nil {
 | |
| 				val = reflect.New(val.Type()).Elem()
 | |
| 			} else {
 | |
| 				o.setFieldValue(ind, value)
 | |
| 			}
 | |
| 			cur++
 | |
| 		}
 | |
| 
 | |
| 		if nInd.Kind() == reflect.Slice {
 | |
| 			if isPtr {
 | |
| 				nInd = reflect.Append(nInd, val)
 | |
| 			} else {
 | |
| 				nInd = reflect.Append(nInd, ind)
 | |
| 			}
 | |
| 		} else {
 | |
| 			if isPtr {
 | |
| 				nInd.Set(val)
 | |
| 			} else {
 | |
| 				nInd.Set(ind)
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		nInds[i] = nInd
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // query data and map to container
 | |
| func (o *rawSet) QueryRow(containers ...interface{}) error {
 | |
| 	var (
 | |
| 		refs  = make([]interface{}, 0, len(containers))
 | |
| 		sInds []reflect.Value
 | |
| 		eTyps []reflect.Type
 | |
| 		sMi   *models.ModelInfo
 | |
| 	)
 | |
| 	structMode := false
 | |
| 	for _, container := range containers {
 | |
| 		val := reflect.ValueOf(container)
 | |
| 		ind := reflect.Indirect(val)
 | |
| 
 | |
| 		if val.Kind() != reflect.Ptr {
 | |
| 			panic(fmt.Errorf("<RawSeter.QueryRow> all args must be use ptr"))
 | |
| 		}
 | |
| 
 | |
| 		etyp := ind.Type()
 | |
| 		typ := etyp
 | |
| 		if typ.Kind() == reflect.Ptr {
 | |
| 			typ = typ.Elem()
 | |
| 		}
 | |
| 
 | |
| 		sInds = append(sInds, ind)
 | |
| 		eTyps = append(eTyps, etyp)
 | |
| 
 | |
| 		if typ.Kind() == reflect.Struct && typ.String() != "time.Time" {
 | |
| 			if len(containers) > 1 {
 | |
| 				panic(fmt.Errorf("<RawSeter.QueryRow> now support one struct only. see #384"))
 | |
| 			}
 | |
| 
 | |
| 			structMode = true
 | |
| 			fn := models.GetFullName(typ)
 | |
| 			if mi, ok := defaultModelCache.getByFullName(fn); ok {
 | |
| 				sMi = mi
 | |
| 			}
 | |
| 		} else {
 | |
| 			var ref interface{}
 | |
| 			refs = append(refs, &ref)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	query := o.query
 | |
| 	o.orm.alias.DbBaser.ReplaceMarks(&query)
 | |
| 
 | |
| 	args := getFlatParams(nil, o.args, o.orm.alias.TZ)
 | |
| 	rows, err := o.orm.db.Query(query, args...)
 | |
| 	if err != nil {
 | |
| 		if err == sql.ErrNoRows {
 | |
| 			return ErrNoRows
 | |
| 		}
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	structTagMap := make(map[reflect.StructTag]map[string]string)
 | |
| 
 | |
| 	defer rows.Close()
 | |
| 
 | |
| 	if rows.Next() {
 | |
| 		if structMode {
 | |
| 			columns, err := rows.Columns()
 | |
| 			if err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 
 | |
| 			columnsMp := make(map[string]interface{}, len(columns))
 | |
| 
 | |
| 			refs = make([]interface{}, 0, len(columns))
 | |
| 			for _, col := range columns {
 | |
| 				var ref interface{}
 | |
| 				columnsMp[col] = &ref
 | |
| 				refs = append(refs, &ref)
 | |
| 			}
 | |
| 
 | |
| 			if err := rows.Scan(refs...); err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 
 | |
| 			ind := sInds[0]
 | |
| 
 | |
| 			if ind.Kind() == reflect.Ptr {
 | |
| 				if ind.IsNil() || !ind.IsValid() {
 | |
| 					ind.Set(reflect.New(eTyps[0].Elem()))
 | |
| 				}
 | |
| 				ind = ind.Elem()
 | |
| 			}
 | |
| 
 | |
| 			if sMi != nil {
 | |
| 				for _, col := range columns {
 | |
| 					if fi := sMi.Fields.GetByColumn(col); fi != nil {
 | |
| 						value := reflect.ValueOf(columnsMp[col]).Elem().Interface()
 | |
| 						field := ind.FieldByIndex(fi.FieldIndex)
 | |
| 						if fi.FieldType&IsRelField > 0 {
 | |
| 							mf := reflect.New(fi.RelModelInfo.AddrField.Elem().Type())
 | |
| 							field.Set(mf)
 | |
| 							field = mf.Elem().FieldByIndex(fi.RelModelInfo.Fields.Pk.FieldIndex)
 | |
| 						}
 | |
| 						if fi.IsFielder {
 | |
| 							fd := field.Addr().Interface().(models.Fielder)
 | |
| 							err := fd.SetRaw(value)
 | |
| 							if err != nil {
 | |
| 								return errors.Errorf("set raw error:%s", err)
 | |
| 							}
 | |
| 						} else {
 | |
| 							o.setFieldValue(field, value)
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 			} else {
 | |
| 				// define recursive function
 | |
| 				var recursiveSetField func(rv reflect.Value)
 | |
| 				recursiveSetField = func(rv reflect.Value) {
 | |
| 					for i := 0; i < rv.NumField(); i++ {
 | |
| 						f := rv.Field(i)
 | |
| 						fe := rv.Type().Field(i)
 | |
| 
 | |
| 						// check if the field is a Struct
 | |
| 						// recursive the Struct type
 | |
| 						if fe.Type.Kind() == reflect.Struct {
 | |
| 							recursiveSetField(f)
 | |
| 						}
 | |
| 
 | |
| 						// thanks @Gazeboxu.
 | |
| 						tags := structTagMap[fe.Tag]
 | |
| 						if tags == nil {
 | |
| 							_, tags = models.ParseStructTag(fe.Tag.Get(models.DefaultStructTagName))
 | |
| 							structTagMap[fe.Tag] = tags
 | |
| 						}
 | |
| 						var col string
 | |
| 						if col = tags["column"]; col == "" {
 | |
| 							col = models.NameStrategyMap[models.NameStrategy](fe.Name)
 | |
| 						}
 | |
| 						if v, ok := columnsMp[col]; ok {
 | |
| 							value := reflect.ValueOf(v).Elem().Interface()
 | |
| 							o.setFieldValue(f, value)
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 
 | |
| 				// init call the recursive function
 | |
| 				recursiveSetField(ind)
 | |
| 			}
 | |
| 
 | |
| 		} else {
 | |
| 			if err := rows.Scan(refs...); err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 
 | |
| 			nInds := make([]reflect.Value, len(sInds))
 | |
| 			o.loopSetRefs(refs, sInds, &nInds, eTyps, true)
 | |
| 			for i, sInd := range sInds {
 | |
| 				nInd := nInds[i]
 | |
| 				sInd.Set(nInd)
 | |
| 			}
 | |
| 		}
 | |
| 	} else {
 | |
| 		return ErrNoRows
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // QueryRows query data rows and map to container
 | |
| func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
 | |
| 	var (
 | |
| 		refs  = make([]interface{}, 0, len(containers))
 | |
| 		sInds []reflect.Value
 | |
| 		eTyps []reflect.Type
 | |
| 		sMi   *models.ModelInfo
 | |
| 	)
 | |
| 	structMode := false
 | |
| 	for _, container := range containers {
 | |
| 		val := reflect.ValueOf(container)
 | |
| 		sInd := reflect.Indirect(val)
 | |
| 		if val.Kind() != reflect.Ptr || sInd.Kind() != reflect.Slice {
 | |
| 			panic(fmt.Errorf("<RawSeter.QueryRows> all args must be use ptr slice"))
 | |
| 		}
 | |
| 
 | |
| 		etyp := sInd.Type().Elem()
 | |
| 		typ := etyp
 | |
| 		if typ.Kind() == reflect.Ptr {
 | |
| 			typ = typ.Elem()
 | |
| 		}
 | |
| 
 | |
| 		sInds = append(sInds, sInd)
 | |
| 		eTyps = append(eTyps, etyp)
 | |
| 
 | |
| 		if typ.Kind() == reflect.Struct && typ.String() != "time.Time" {
 | |
| 			if len(containers) > 1 {
 | |
| 				panic(fmt.Errorf("<RawSeter.QueryRow> now support one struct only. see #384"))
 | |
| 			}
 | |
| 
 | |
| 			structMode = true
 | |
| 			fn := models.GetFullName(typ)
 | |
| 			if mi, ok := defaultModelCache.getByFullName(fn); ok {
 | |
| 				sMi = mi
 | |
| 			}
 | |
| 		} else {
 | |
| 			var ref interface{}
 | |
| 			refs = append(refs, &ref)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	query := o.query
 | |
| 	o.orm.alias.DbBaser.ReplaceMarks(&query)
 | |
| 
 | |
| 	args := getFlatParams(nil, o.args, o.orm.alias.TZ)
 | |
| 	rows, err := o.orm.db.Query(query, args...)
 | |
| 	if err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 
 | |
| 	defer rows.Close()
 | |
| 
 | |
| 	var cnt int64
 | |
| 	nInds := make([]reflect.Value, len(sInds))
 | |
| 	sInd := sInds[0]
 | |
| 
 | |
| 	for rows.Next() {
 | |
| 		if structMode {
 | |
| 			columns, err := rows.Columns()
 | |
| 			if err != nil {
 | |
| 				return 0, err
 | |
| 			}
 | |
| 
 | |
| 			columnsMp := make(map[string]interface{}, len(columns))
 | |
| 
 | |
| 			refs = make([]interface{}, 0, len(columns))
 | |
| 			for _, col := range columns {
 | |
| 				var ref interface{}
 | |
| 				columnsMp[col] = &ref
 | |
| 				refs = append(refs, &ref)
 | |
| 			}
 | |
| 
 | |
| 			if err := rows.Scan(refs...); err != nil {
 | |
| 				return 0, err
 | |
| 			}
 | |
| 
 | |
| 			if cnt == 0 && !sInd.IsNil() {
 | |
| 				sInd.Set(reflect.New(sInd.Type()).Elem())
 | |
| 			}
 | |
| 
 | |
| 			var ind reflect.Value
 | |
| 			if eTyps[0].Kind() == reflect.Ptr {
 | |
| 				ind = reflect.New(eTyps[0].Elem())
 | |
| 			} else {
 | |
| 				ind = reflect.New(eTyps[0])
 | |
| 			}
 | |
| 
 | |
| 			if ind.Kind() == reflect.Ptr {
 | |
| 				ind = ind.Elem()
 | |
| 			}
 | |
| 
 | |
| 			if sMi != nil {
 | |
| 				for _, col := range columns {
 | |
| 					if fi := sMi.Fields.GetByColumn(col); fi != nil {
 | |
| 						value := reflect.ValueOf(columnsMp[col]).Elem().Interface()
 | |
| 						field := ind.FieldByIndex(fi.FieldIndex)
 | |
| 						if fi.FieldType&IsRelField > 0 {
 | |
| 							mf := reflect.New(fi.RelModelInfo.AddrField.Elem().Type())
 | |
| 							field.Set(mf)
 | |
| 							field = mf.Elem().FieldByIndex(fi.RelModelInfo.Fields.Pk.FieldIndex)
 | |
| 						}
 | |
| 						if fi.IsFielder {
 | |
| 							fd := field.Addr().Interface().(models.Fielder)
 | |
| 							err := fd.SetRaw(value)
 | |
| 							if err != nil {
 | |
| 								return 0, errors.Errorf("set raw error:%s", err)
 | |
| 							}
 | |
| 						} else {
 | |
| 							o.setFieldValue(field, value)
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 			} else {
 | |
| 				// define recursive function
 | |
| 				var recursiveSetField func(rv reflect.Value)
 | |
| 				recursiveSetField = func(rv reflect.Value) {
 | |
| 					for i := 0; i < rv.NumField(); i++ {
 | |
| 						f := rv.Field(i)
 | |
| 						fe := rv.Type().Field(i)
 | |
| 
 | |
| 						// check if the field is a Struct
 | |
| 						// recursive the Struct type
 | |
| 						if fe.Type.Kind() == reflect.Struct {
 | |
| 							recursiveSetField(f)
 | |
| 						}
 | |
| 
 | |
| 						_, tags := models.ParseStructTag(fe.Tag.Get(models.DefaultStructTagName))
 | |
| 						var col string
 | |
| 						if col = tags["column"]; col == "" {
 | |
| 							col = models.NameStrategyMap[models.NameStrategy](fe.Name)
 | |
| 						}
 | |
| 						if v, ok := columnsMp[col]; ok {
 | |
| 							value := reflect.ValueOf(v).Elem().Interface()
 | |
| 							o.setFieldValue(f, value)
 | |
| 						}
 | |
| 					}
 | |
| 				}
 | |
| 
 | |
| 				// init call the recursive function
 | |
| 				recursiveSetField(ind)
 | |
| 			}
 | |
| 
 | |
| 			if eTyps[0].Kind() == reflect.Ptr {
 | |
| 				ind = ind.Addr()
 | |
| 			}
 | |
| 
 | |
| 			sInd = reflect.Append(sInd, ind)
 | |
| 
 | |
| 		} else {
 | |
| 			if err = rows.Scan(refs...); err != nil {
 | |
| 				return 0, err
 | |
| 			}
 | |
| 			o.loopSetRefs(refs, sInds, &nInds, eTyps, cnt == 0)
 | |
| 		}
 | |
| 		cnt++
 | |
| 	}
 | |
| 
 | |
| 	if err = rows.Err(); err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 
 | |
| 	if cnt > 0 {
 | |
| 		if structMode {
 | |
| 			sInds[0].Set(sInd)
 | |
| 		} else {
 | |
| 			for i, sInd := range sInds {
 | |
| 				nInd := nInds[i]
 | |
| 				sInd.Set(nInd)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return cnt, nil
 | |
| }
 | |
| 
 | |
| func (o *rawSet) readValues(container interface{}, needCols []string) (int64, error) {
 | |
| 	var (
 | |
| 		maps  []Params
 | |
| 		lists []ParamsList
 | |
| 		list  ParamsList
 | |
| 	)
 | |
| 
 | |
| 	typ := 0
 | |
| 	switch container.(type) {
 | |
| 	case *[]Params:
 | |
| 		typ = 1
 | |
| 	case *[]ParamsList:
 | |
| 		typ = 2
 | |
| 	case *ParamsList:
 | |
| 		typ = 3
 | |
| 	default:
 | |
| 		panic(fmt.Errorf("<RawSeter> unsupport read values type `%T`", container))
 | |
| 	}
 | |
| 
 | |
| 	query := o.query
 | |
| 	o.orm.alias.DbBaser.ReplaceMarks(&query)
 | |
| 
 | |
| 	args := getFlatParams(nil, o.args, o.orm.alias.TZ)
 | |
| 
 | |
| 	var rs *sql.Rows
 | |
| 	rs, err := o.orm.db.Query(query, args...)
 | |
| 	if err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 
 | |
| 	defer rs.Close()
 | |
| 
 | |
| 	var (
 | |
| 		refs   []interface{}
 | |
| 		cnt    int64
 | |
| 		cols   []string
 | |
| 		indexs []int
 | |
| 	)
 | |
| 
 | |
| 	for rs.Next() {
 | |
| 		if cnt == 0 {
 | |
| 			columns, err := rs.Columns()
 | |
| 			if err != nil {
 | |
| 				return 0, err
 | |
| 			}
 | |
| 			if len(needCols) > 0 {
 | |
| 				indexs = make([]int, 0, len(needCols))
 | |
| 			} else {
 | |
| 				indexs = make([]int, 0, len(columns))
 | |
| 			}
 | |
| 
 | |
| 			cols = columns
 | |
| 			refs = make([]interface{}, len(cols))
 | |
| 			for i := range refs {
 | |
| 				var ref sql.NullString
 | |
| 				refs[i] = &ref
 | |
| 
 | |
| 				if len(needCols) > 0 {
 | |
| 					for _, c := range needCols {
 | |
| 						if c == cols[i] {
 | |
| 							indexs = append(indexs, i)
 | |
| 						}
 | |
| 					}
 | |
| 				} else {
 | |
| 					indexs = append(indexs, i)
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		if err := rs.Scan(refs...); err != nil {
 | |
| 			return 0, err
 | |
| 		}
 | |
| 
 | |
| 		switch typ {
 | |
| 		case 1:
 | |
| 			params := make(Params, len(cols))
 | |
| 			for _, i := range indexs {
 | |
| 				ref := refs[i]
 | |
| 				value := reflect.Indirect(reflect.ValueOf(ref)).Interface().(sql.NullString)
 | |
| 				if value.Valid {
 | |
| 					params[cols[i]] = value.String
 | |
| 				} else {
 | |
| 					params[cols[i]] = nil
 | |
| 				}
 | |
| 			}
 | |
| 			maps = append(maps, params)
 | |
| 		case 2:
 | |
| 			params := make(ParamsList, 0, len(cols))
 | |
| 			for _, i := range indexs {
 | |
| 				ref := refs[i]
 | |
| 				value := reflect.Indirect(reflect.ValueOf(ref)).Interface().(sql.NullString)
 | |
| 				if value.Valid {
 | |
| 					params = append(params, value.String)
 | |
| 				} else {
 | |
| 					params = append(params, nil)
 | |
| 				}
 | |
| 			}
 | |
| 			lists = append(lists, params)
 | |
| 		case 3:
 | |
| 			for _, i := range indexs {
 | |
| 				ref := refs[i]
 | |
| 				value := reflect.Indirect(reflect.ValueOf(ref)).Interface().(sql.NullString)
 | |
| 				if value.Valid {
 | |
| 					list = append(list, value.String)
 | |
| 				} else {
 | |
| 					list = append(list, nil)
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		cnt++
 | |
| 	}
 | |
| 
 | |
| 	if err = rs.Err(); err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 
 | |
| 	switch v := container.(type) {
 | |
| 	case *[]Params:
 | |
| 		*v = maps
 | |
| 	case *[]ParamsList:
 | |
| 		*v = lists
 | |
| 	case *ParamsList:
 | |
| 		*v = list
 | |
| 	}
 | |
| 
 | |
| 	return cnt, nil
 | |
| }
 | |
| 
 | |
| func (o *rawSet) queryRowsTo(container interface{}, keyCol, valueCol string) (int64, error) {
 | |
| 	var (
 | |
| 		maps Params
 | |
| 		ind  *reflect.Value
 | |
| 	)
 | |
| 
 | |
| 	var typ int
 | |
| 	switch container.(type) {
 | |
| 	case *Params:
 | |
| 		typ = 1
 | |
| 	default:
 | |
| 		typ = 2
 | |
| 		vl := reflect.ValueOf(container)
 | |
| 		id := reflect.Indirect(vl)
 | |
| 		if vl.Kind() != reflect.Ptr || id.Kind() != reflect.Struct {
 | |
| 			panic(fmt.Errorf("<RawSeter> RowsTo unsupport type `%T` need ptr struct", container))
 | |
| 		}
 | |
| 
 | |
| 		ind = &id
 | |
| 	}
 | |
| 
 | |
| 	query := o.query
 | |
| 	o.orm.alias.DbBaser.ReplaceMarks(&query)
 | |
| 
 | |
| 	args := getFlatParams(nil, o.args, o.orm.alias.TZ)
 | |
| 
 | |
| 	rs, err := o.orm.db.Query(query, args...)
 | |
| 	if err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 
 | |
| 	defer rs.Close()
 | |
| 
 | |
| 	var (
 | |
| 		refs []interface{}
 | |
| 		cnt  int64
 | |
| 		cols []string
 | |
| 	)
 | |
| 
 | |
| 	var (
 | |
| 		keyIndex   = -1
 | |
| 		valueIndex = -1
 | |
| 	)
 | |
| 
 | |
| 	for rs.Next() {
 | |
| 		if cnt == 0 {
 | |
| 			columns, err := rs.Columns()
 | |
| 			if err != nil {
 | |
| 				return 0, err
 | |
| 			}
 | |
| 			cols = columns
 | |
| 			refs = make([]interface{}, len(cols))
 | |
| 			for i := range refs {
 | |
| 				if keyCol == cols[i] {
 | |
| 					keyIndex = i
 | |
| 				}
 | |
| 				if typ == 1 || keyIndex == i {
 | |
| 					var ref sql.NullString
 | |
| 					refs[i] = &ref
 | |
| 				} else {
 | |
| 					var ref interface{}
 | |
| 					refs[i] = &ref
 | |
| 				}
 | |
| 				if valueCol == cols[i] {
 | |
| 					valueIndex = i
 | |
| 				}
 | |
| 			}
 | |
| 			if keyIndex == -1 || valueIndex == -1 {
 | |
| 				panic(fmt.Errorf("<RawSeter> RowsTo unknown key, value column name `%s: %s`", keyCol, valueCol))
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		if err := rs.Scan(refs...); err != nil {
 | |
| 			return 0, err
 | |
| 		}
 | |
| 
 | |
| 		if cnt == 0 {
 | |
| 			switch typ {
 | |
| 			case 1:
 | |
| 				maps = make(Params)
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		key := reflect.Indirect(reflect.ValueOf(refs[keyIndex])).Interface().(sql.NullString).String
 | |
| 
 | |
| 		switch typ {
 | |
| 		case 1:
 | |
| 			value := reflect.Indirect(reflect.ValueOf(refs[valueIndex])).Interface().(sql.NullString)
 | |
| 			if value.Valid {
 | |
| 				maps[key] = value.String
 | |
| 			} else {
 | |
| 				maps[key] = nil
 | |
| 			}
 | |
| 
 | |
| 		default:
 | |
| 			if id := ind.FieldByName(models.CamelString(key)); id.IsValid() {
 | |
| 				o.setFieldValue(id, reflect.ValueOf(refs[valueIndex]).Elem().Interface())
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		cnt++
 | |
| 	}
 | |
| 
 | |
| 	if err = rs.Err(); err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 	if typ == 1 {
 | |
| 		v, _ := container.(*Params)
 | |
| 		*v = maps
 | |
| 	}
 | |
| 
 | |
| 	return cnt, nil
 | |
| }
 | |
| 
 | |
| // query data to []map[string]interface
 | |
| func (o *rawSet) Values(container *[]Params, cols ...string) (int64, error) {
 | |
| 	return o.readValues(container, cols)
 | |
| }
 | |
| 
 | |
| // query data to [][]interface
 | |
| func (o *rawSet) ValuesList(container *[]ParamsList, cols ...string) (int64, error) {
 | |
| 	return o.readValues(container, cols)
 | |
| }
 | |
| 
 | |
| // query data to []interface
 | |
| func (o *rawSet) ValuesFlat(container *ParamsList, cols ...string) (int64, error) {
 | |
| 	return o.readValues(container, cols)
 | |
| }
 | |
| 
 | |
| // query all rows into map[string]interface with specify key and value column name.
 | |
| // keyCol = "name", valueCol = "value"
 | |
| // table data
 | |
| // name  | value
 | |
| // total | 100
 | |
| // found | 200
 | |
| //
 | |
| //	to map[string]interface{}{
 | |
| //		"total": 100,
 | |
| //		"found": 200,
 | |
| //	}
 | |
| func (o *rawSet) RowsToMap(result *Params, keyCol, valueCol string) (int64, error) {
 | |
| 	return o.queryRowsTo(result, keyCol, valueCol)
 | |
| }
 | |
| 
 | |
| // query all rows into struct with specify key and value column name.
 | |
| // keyCol = "name", valueCol = "value"
 | |
| // table data
 | |
| // name  | value
 | |
| // total | 100
 | |
| // found | 200
 | |
| //
 | |
| //	to struct {
 | |
| //		Total int
 | |
| //		Found int
 | |
| //	}
 | |
| func (o *rawSet) RowsToStruct(ptrStruct interface{}, keyCol, valueCol string) (int64, error) {
 | |
| 	return o.queryRowsTo(ptrStruct, keyCol, valueCol)
 | |
| }
 | |
| 
 | |
| // return prepared raw statement for used in times.
 | |
| func (o *rawSet) Prepare() (RawPreparer, error) {
 | |
| 	return newRawPreparer(o)
 | |
| }
 | |
| 
 | |
| func newRawSet(orm *ormBase, query string, args []interface{}) RawSeter {
 | |
| 	o := new(rawSet)
 | |
| 	o.query = query
 | |
| 	o.args = args
 | |
| 	o.orm = orm
 | |
| 	return o
 | |
| }
 |