github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/session.go (about) 1 // Copyright 2021 ecodeclub 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package eorm 16 17 import ( 18 "context" 19 "database/sql" 20 21 "github.com/ecodeclub/ekit/list" 22 "github.com/ecodeclub/eorm/internal/datasource" 23 "github.com/ecodeclub/eorm/internal/rows" 24 "golang.org/x/sync/errgroup" 25 ) 26 27 var _ Session = (*baseSession)(nil) 28 29 type baseSession struct { 30 core 31 executor datasource.Executor 32 } 33 34 func (sess *baseSession) queryContext(ctx context.Context, q Query) (rows.Rows, error) { 35 return sess.executor.Query(ctx, q) 36 } 37 38 func (sess *baseSession) queryMulti(ctx context.Context, qs []Query) (list.List[rows.Rows], error) { 39 res := &list.ConcurrentList[rows.Rows]{ 40 List: list.NewArrayList[rows.Rows](len(qs)), 41 } 42 var eg errgroup.Group 43 for _, query := range qs { 44 q := query 45 eg.Go(func() error { 46 rs, err := sess.queryContext(ctx, q) 47 if err == nil { 48 return res.Append(rs) 49 } 50 return err 51 }) 52 } 53 return res, eg.Wait() 54 } 55 56 func (sess *baseSession) execContext(ctx context.Context, q Query) (sql.Result, error) { 57 return sess.executor.Exec(ctx, q) 58 } 59 60 func (sess *baseSession) getCore() core { 61 return sess.core 62 }