MCPcopy Create free account
hub / github.com/cloudwan/gohan / CountContext

Method CountContext

db/sql/sql.go:1079–1103  ·  view source on GitHub ↗

CountContext count all matching resources in the db

(ctx context.Context, s *schema.Schema, filter transaction.Filter)

Source from the content-addressed store, hash-verified

1077
1078//CountContext count all matching resources in the db
1079func (tx *Transaction) CountContext(ctx context.Context, s *schema.Schema, filter transaction.Filter) (res uint64, err error) {
1080 defer tx.measureTime(time.Now(), s.ID, "count")
1081
1082 q := sq.Select("Count(id) as count").From(quote(s.GetDbTableName()))
1083 //Filter get already tested
1084 q, _ = AddFilterToQuery(s, q, filter, false)
1085 sql, args, err := q.ToSql()
1086 if err != nil {
1087 return
1088 }
1089 result := map[string]interface{}{}
1090 err = tx.transaction.QueryRowxContext(ctx, sql, args...).MapScan(result)
1091 if err != nil {
1092 return
1093 }
1094 count, _ := result["count"]
1095 decoder := &integerHandler{}
1096 decoded, decodeErr := decoder.decode(nil, count)
1097 if decodeErr != nil {
1098 err = fmt.Errorf("SQL List decoding error: %s", decodeErr)
1099 return
1100 }
1101 res = uint64(decoded.(int))
1102 return
1103}
1104
1105func (tx *Transaction) Fetch(s *schema.Schema, filter transaction.Filter, options *transaction.ViewOptions) (*schema.Resource, error) {
1106 return tx.FetchContext(context.Background(), s, filter, options)

Callers 1

executeSelectMethod · 0.95

Calls 6

measureTimeMethod · 0.95
decodeMethod · 0.95
AddFilterToQueryFunction · 0.85
GetDbTableNameMethod · 0.80
quoteFunction · 0.70
ErrorfMethod · 0.65

Tested by

no test coverage detected