123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- // Copyright 2016 The Xorm Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package xorm
- import (
- "database/sql"
- "errors"
- "fmt"
- "reflect"
- "strconv"
- "github.com/xormplus/core"
- )
- // Get retrieve one record from database, bean's non-empty fields
- // will be as conditions
- func (session *Session) Get(bean interface{}) (bool, error) {
- if session.isAutoClose {
- defer session.Close()
- }
- return session.get(bean)
- }
- var NoData = errors.New("no data found")
- func (session *Session) MustGet(bean interface{}) error {
- if session.isAutoClose {
- defer session.Close()
- }
- ok, err := session.get(bean)
- if err != nil {
- return err
- }
- if !ok {
- return NoData
- }
- return nil
- }
- func (session *Session) get(bean interface{}) (bool, error) {
- defer session.resetStatement()
- if session.statement.lastError != nil {
- return false, session.statement.lastError
- }
- beanValue := reflect.ValueOf(bean)
- if beanValue.Kind() != reflect.Ptr {
- return false, errors.New("needs a pointer to a value")
- } else if beanValue.Elem().Kind() == reflect.Ptr {
- return false, errors.New("a pointer to a pointer is not allowed")
- }
- if beanValue.Elem().Kind() == reflect.Struct {
- if err := session.statement.setRefBean(bean); err != nil {
- return false, err
- }
- }
- var sqlStr string
- var args []interface{}
- var err error
- if session.statement.RawSQL == "" {
- if len(session.statement.TableName()) <= 0 {
- return false, ErrTableNotFound
- }
- session.statement.Limit(1)
- sqlStr, args, err = session.statement.genGetSQL(bean)
- if err != nil {
- return false, err
- }
- } else {
- sqlStr = session.statement.RawSQL
- params := session.statement.RawParams
- i := len(params)
- if i == 1 {
- vv := reflect.ValueOf(params[0])
- if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
- args = params
- } else {
- sqlStr, args, _ = core.MapToSlice(sqlStr, params[0])
- }
- } else {
- args = params
- }
- }
- table := session.statement.RefTable
- if session.canCache() && beanValue.Elem().Kind() == reflect.Struct {
- if cacher := session.engine.getCacher(session.statement.TableName()); cacher != nil &&
- !session.statement.unscoped {
- has, err := session.cacheGet(bean, sqlStr, args...)
- if err != ErrCacheFailed {
- return has, err
- }
- }
- }
- context := session.statement.context
- if context != nil {
- res := context.Get(fmt.Sprintf("%v-%v", sqlStr, args))
- if res != nil {
- session.engine.logger.Debug("hit context cache", sqlStr)
- structValue := reflect.Indirect(reflect.ValueOf(bean))
- structValue.Set(reflect.Indirect(reflect.ValueOf(res)))
- session.lastSQL = ""
- session.lastSQLArgs = nil
- return true, nil
- }
- }
- has, err := session.nocacheGet(beanValue.Elem().Kind(), table, bean, sqlStr, args...)
- if err != nil || !has {
- return has, err
- }
- if context != nil {
- context.Put(fmt.Sprintf("%v-%v", sqlStr, args), bean)
- }
- return true, nil
- }
- func (session *Session) nocacheGet(beanKind reflect.Kind, table *core.Table, bean interface{}, sqlStr string, args ...interface{}) (bool, error) {
- rows, err := session.queryRows(sqlStr, args...)
- if err != nil {
- return false, err
- }
- defer rows.Close()
- if !rows.Next() {
- if rows.Err() != nil {
- return false, rows.Err()
- }
- return false, nil
- }
- switch bean.(type) {
- case sql.NullInt64, sql.NullBool, sql.NullFloat64, sql.NullString:
- return true, rows.Scan(&bean)
- case *sql.NullInt64, *sql.NullBool, *sql.NullFloat64, *sql.NullString:
- return true, rows.Scan(bean)
- case *map[string]Value, *Record:
- vv := reflect.ValueOf(bean)
- if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
- return true, errors.New("dest should be a map's pointer")
- }
- vvv := vv.Elem()
- tbean := make(map[string][]byte)
- err = rows.ScanMap(&tbean)
- if err != nil {
- return true, err
- }
- for k, v := range tbean {
- vvv.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(Value(v)))
- }
- return true, nil
- case *string:
- var res sql.NullString
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*string)) = res.String
- }
- return true, nil
- case *int:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*int)) = int(res.Int64)
- }
- return true, nil
- case *int8:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*int8)) = int8(res.Int64)
- }
- return true, nil
- case *int16:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*int16)) = int16(res.Int64)
- }
- return true, nil
- case *int32:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*int32)) = int32(res.Int64)
- }
- return true, nil
- case *int64:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*int64)) = int64(res.Int64)
- }
- return true, nil
- case *uint:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*uint)) = uint(res.Int64)
- }
- return true, nil
- case *uint8:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*uint8)) = uint8(res.Int64)
- }
- return true, nil
- case *uint16:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*uint16)) = uint16(res.Int64)
- }
- return true, nil
- case *uint32:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*uint32)) = uint32(res.Int64)
- }
- return true, nil
- case *uint64:
- var res sql.NullInt64
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*uint64)) = uint64(res.Int64)
- }
- return true, nil
- case *bool:
- var res sql.NullBool
- if err := rows.Scan(&res); err != nil {
- return true, err
- }
- if res.Valid {
- *(bean.(*bool)) = res.Bool
- }
- return true, nil
- }
- switch beanKind {
- case reflect.Struct:
- fields, err := rows.Columns()
- if err != nil {
- // WARN: Alougth rows return true, but get fields failed
- return true, err
- }
- scanResults, err := session.row2Slice(rows, fields, bean)
- if err != nil {
- return false, err
- }
- // close it before covert data
- rows.Close()
- dataStruct := rValue(bean)
- _, err = session.slice2Bean(scanResults, fields, bean, &dataStruct, table)
- if err != nil {
- return true, err
- }
- return true, session.executeProcessors()
- case reflect.Slice:
- err = rows.ScanSlice(bean)
- case reflect.Map:
- err = rows.ScanMap(bean)
- case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
- reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- err = rows.Scan(bean)
- default:
- err = rows.Scan(bean)
- }
- return true, err
- }
- func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interface{}) (has bool, err error) {
- // if has no reftable, then don't use cache currently
- if !session.canCache() {
- return false, ErrCacheFailed
- }
- for _, filter := range session.engine.dialect.Filters() {
- sqlStr = filter.Do(sqlStr, session.engine.dialect, session.statement.RefTable)
- }
- newsql := session.statement.convertIDSQL(sqlStr)
- if newsql == "" {
- return false, ErrCacheFailed
- }
- tableName := session.statement.TableName()
- cacher := session.engine.getCacher(tableName)
- session.engine.logger.Debug("[cacheGet] find sql:", newsql, args)
- table := session.statement.RefTable
- ids, err := core.GetCacheSql(cacher, tableName, newsql, args)
- if err != nil {
- var res = make([]string, len(table.PrimaryKeys))
- rows, err := session.NoCache().queryRows(newsql, args...)
- if err != nil {
- return false, err
- }
- defer rows.Close()
- if rows.Next() {
- err = rows.ScanSlice(&res)
- if err != nil {
- return false, err
- }
- } else {
- return false, ErrCacheFailed
- }
- var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
- for i, col := range table.PKColumns() {
- if col.SQLType.IsText() {
- pk[i] = res[i]
- } else if col.SQLType.IsNumeric() {
- n, err := strconv.ParseInt(res[i], 10, 64)
- if err != nil {
- return false, err
- }
- pk[i] = n
- } else {
- return false, errors.New("unsupported")
- }
- }
- ids = []core.PK{pk}
- session.engine.logger.Debug("[cacheGet] cache ids:", newsql, ids)
- err = core.PutCacheSql(cacher, ids, tableName, newsql, args)
- if err != nil {
- return false, err
- }
- } else {
- session.engine.logger.Debug("[cacheGet] cache hit sql:", newsql, ids)
- }
- if len(ids) > 0 {
- structValue := reflect.Indirect(reflect.ValueOf(bean))
- id := ids[0]
- session.engine.logger.Debug("[cacheGet] get bean:", tableName, id)
- sid, err := id.ToString()
- if err != nil {
- return false, err
- }
- cacheBean := cacher.GetBean(tableName, sid)
- if cacheBean == nil {
- cacheBean = bean
- has, err = session.nocacheGet(reflect.Struct, table, cacheBean, sqlStr, args...)
- if err != nil || !has {
- return has, err
- }
- session.engine.logger.Debug("[cacheGet] cache bean:", tableName, id, cacheBean)
- cacher.PutBean(tableName, sid, cacheBean)
- } else {
- session.engine.logger.Debug("[cacheGet] cache hit bean:", tableName, id, cacheBean)
- has = true
- }
- structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean)))
- return has, nil
- }
- return false, nil
- }
|