github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/core.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  	"reflect"
    20  
    21  	"github.com/ecodeclub/eorm/internal/dialect"
    22  	"github.com/ecodeclub/eorm/internal/errs"
    23  	"github.com/ecodeclub/eorm/internal/model"
    24  	"github.com/ecodeclub/eorm/internal/valuer"
    25  )
    26  
    27  type core struct {
    28  	metaRegistry model.MetaRegistry
    29  	dialect      dialect.Dialect
    30  	valCreator   valuer.PrimitiveCreator
    31  	ms           []Middleware
    32  }
    33  
    34  func getHandler[T any](ctx context.Context, sess Session, c core, qc *QueryContext) *QueryResult {
    35  	rows, err := sess.queryContext(ctx, qc.q)
    36  	if err != nil {
    37  		return &QueryResult{Err: err}
    38  	}
    39  	defer func() {
    40  		_ = rows.Close()
    41  	}()
    42  	if !rows.Next() {
    43  		return &QueryResult{Err: errs.ErrNoRows}
    44  	}
    45  
    46  	tp := new(T)
    47  	meta := qc.meta
    48  	if meta == nil && reflect.TypeOf(tp).Elem().Kind() == reflect.Struct {
    49  		//  当通过 RawQuery 方法调用 Get ,如果 T 是 time.Time, sql.Scanner 的实现,
    50  		//  内置类型或者基本类型时, 在这里都会报错,但是这种情况我们认为是可以接受的
    51  		//  所以在此将报错忽略,因为基本类型取值用不到 meta 里的数据
    52  		meta, _ = c.metaRegistry.Get(tp)
    53  	}
    54  
    55  	val := c.valCreator.NewPrimitiveValue(tp, meta)
    56  	if err = val.SetColumns(rows); err != nil {
    57  		return &QueryResult{Err: err}
    58  	}
    59  
    60  	return &QueryResult{Result: tp}
    61  }
    62  
    63  func get[T any](ctx context.Context, sess Session, core core, qc *QueryContext) *QueryResult {
    64  	var handler HandleFunc = func(ctx context.Context, queryContext *QueryContext) *QueryResult {
    65  		return getHandler[T](ctx, sess, core, queryContext)
    66  	}
    67  	ms := core.ms
    68  	for i := len(ms) - 1; i >= 0; i-- {
    69  		handler = ms[i](handler)
    70  	}
    71  	return handler(ctx, qc)
    72  }
    73  
    74  func getMultiHandler[T any](ctx context.Context, sess Session, c core, qc *QueryContext) *QueryResult {
    75  	rows, err := sess.queryContext(ctx, qc.q)
    76  	if err != nil {
    77  		return &QueryResult{Err: err}
    78  	}
    79  	defer func() {
    80  		_ = rows.Close()
    81  	}()
    82  	res := make([]*T, 0, 16)
    83  	meta := qc.meta
    84  	if meta == nil {
    85  		t := new(T)
    86  		if reflect.TypeOf(t).Elem().Kind() == reflect.Struct {
    87  			//  当通过 RawQuery 方法调用 Get ,如果 T 是 time.Time, sql.Scanner 的实现,
    88  			//  内置类型或者基本类型时, 在这里都会报错,但是这种情况我们认为是可以接受的
    89  			//  所以在此将报错忽略,因为基本类型取值用不到 meta 里的数据
    90  			meta, _ = c.metaRegistry.Get(t)
    91  		}
    92  	}
    93  	for rows.Next() {
    94  		tp := new(T)
    95  		val := c.valCreator.NewPrimitiveValue(tp, meta)
    96  		if err = val.SetColumns(rows); err != nil {
    97  			return &QueryResult{Err: err}
    98  		}
    99  		res = append(res, tp)
   100  	}
   101  
   102  	return &QueryResult{Result: res}
   103  }
   104  
   105  func getMulti[T any](ctx context.Context, sess Session, core core, qc *QueryContext) *QueryResult {
   106  	var handler HandleFunc = func(ctx context.Context, queryContext *QueryContext) *QueryResult {
   107  		return getMultiHandler[T](ctx, sess, core, queryContext)
   108  	}
   109  	ms := core.ms
   110  	for i := len(ms) - 1; i >= 0; i-- {
   111  		handler = ms[i](handler)
   112  	}
   113  	return handler(ctx, qc)
   114  }