github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/sugar/params.go (about)

     1  package sugar
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    13  )
    14  
    15  type constraint interface {
    16  	params.Parameters | []*params.Parameter | *table.QueryParameters | []table.ParameterOption | []sql.NamedArg
    17  }
    18  
    19  // GenerateDeclareSection generates DECLARE section text in YQL query by params
    20  //
    21  // Deprecated: use testutil.QueryBind(ydb.WithAutoDeclare()) helper
    22  func GenerateDeclareSection[T constraint](parameters T) (string, error) {
    23  	switch v := any(parameters).(type) {
    24  	case *params.Parameters:
    25  		return parametersToDeclares(*v), nil
    26  	case []*params.Parameter:
    27  		return parametersToDeclares(v), nil
    28  	case []table.ParameterOption:
    29  		return parameterOptionsToDeclares(v), nil
    30  	case []sql.NamedArg:
    31  		return namedArgsToDeclares(v)
    32  	default:
    33  		return "", xerrors.WithStackTrace(fmt.Errorf("unsupported type: %T", v))
    34  	}
    35  }
    36  
    37  // ToYdbParam converts
    38  //
    39  // Deprecated: use testutil/QueryBind helper
    40  func ToYdbParam(param sql.NamedArg) (*params.Parameter, error) {
    41  	params, err := bind.Params(param)
    42  	if err != nil {
    43  		return nil, xerrors.WithStackTrace(err)
    44  	}
    45  	if len(params) != 1 {
    46  		return nil, xerrors.WithStackTrace(fmt.Errorf("internal error: wrong parameters count: %v", params))
    47  	}
    48  
    49  	return params[0], nil
    50  }
    51  
    52  func parametersToDeclares(v []*params.Parameter) string {
    53  	var (
    54  		buf      = xstring.Buffer()
    55  		names    = make([]string, 0, len(v))
    56  		declares = make(map[string]string, len(v))
    57  	)
    58  	defer buf.Free()
    59  
    60  	for _, p := range v {
    61  		name := p.Name()
    62  		names = append(names, name)
    63  		declares[name] = params.Declare(p)
    64  	}
    65  
    66  	sort.Strings(names)
    67  
    68  	for _, name := range names {
    69  		buf.WriteString(declares[name])
    70  		buf.WriteString(";\n")
    71  	}
    72  
    73  	return buf.String()
    74  }
    75  
    76  func parameterOptionsToDeclares(v []table.ParameterOption) string {
    77  	parameters := make([]*params.Parameter, len(v))
    78  	for i, p := range v {
    79  		parameters[i] = params.Named(p.Name(), p.Value())
    80  	}
    81  
    82  	return parametersToDeclares(parameters)
    83  }
    84  
    85  func namedArgsToDeclares(v []sql.NamedArg) (string, error) {
    86  	vv, err := bind.Params(func() (newArgs []interface{}) {
    87  		for i := range v {
    88  			newArgs = append(newArgs, v[i])
    89  		}
    90  
    91  		return newArgs
    92  	}()...)
    93  	if err != nil {
    94  		return "", xerrors.WithStackTrace(err)
    95  	}
    96  
    97  	return parametersToDeclares(vv), nil
    98  }