MCPcopy Create free account
hub / github.com/go-pg/pg / SelectAndCount

Method SelectAndCount

orm/query.go:888–927  ·  view source on GitHub ↗

SelectAndCount runs Select and Count in two goroutines, waits for them to finish and returns the result. If query limit is -1 it does not select any data and only counts the results.

(values ...interface{})

Source from the content-addressed store, hash-verified

886// waits for them to finish and returns the result. If query limit is -1
887// it does not select any data and only counts the results.
888func (q *Query) SelectAndCount(values ...interface{}) (count int, firstErr error) {
889 if q.stickyErr != nil {
890 return 0, q.stickyErr
891 }
892
893 var wg sync.WaitGroup
894 var mu sync.Mutex
895
896 if q.limit >= 0 {
897 wg.Add(1)
898 go func() {
899 defer wg.Done()
900 err := q.Select(values...)
901 if err != nil {
902 mu.Lock()
903 if firstErr == nil {
904 firstErr = err
905 }
906 mu.Unlock()
907 }
908 }()
909 }
910
911 wg.Add(1)
912 go func() {
913 defer wg.Done()
914 var err error
915 count, err = q.Count()
916 if err != nil {
917 mu.Lock()
918 if firstErr == nil {
919 firstErr = err
920 }
921 mu.Unlock()
922 }
923 }()
924
925 wg.Wait()
926 return count, firstErr
927}
928
929// SelectAndCountEstimate runs Select and CountEstimate in two goroutines,
930// waits for them to finish and returns the result. If query limit is -1

Callers 2

db_test.goFile · 0.80

Calls 3

SelectMethod · 0.95
CountMethod · 0.95
DoneMethod · 0.80

Tested by 1