github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/14_find.go.tpl (about)

     1  {{- if .Table.IsView -}}
     2  {{- else -}}
     3  {{- $alias := .Aliases.Table .Table.Name -}}
     4  {{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
     5  {{- $pkNames := $colDefs.Names | stringMap (aliasCols $alias) | stringMap .StringFuncs.camelCase | stringMap .StringFuncs.replaceReserved -}}
     6  {{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}}
     7  {{- $canSoftDelete := .Table.CanSoftDelete $.AutoColumns.Deleted }}
     8  {{if .AddGlobal -}}
     9  // Find{{$alias.UpSingular}}G retrieves a single record by ID.
    10  func Find{{$alias.UpSingular}}G({{if not .NoContext}}ctx context.Context, {{end -}} {{$pkArgs}}, selectCols ...string) (*{{$alias.UpSingular}}, error) {
    11  	return Find{{$alias.UpSingular}}({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, {{$pkNames | join ", "}}, selectCols...)
    12  }
    13  
    14  {{end -}}
    15  
    16  {{if .AddPanic -}}
    17  // Find{{$alias.UpSingular}}P retrieves a single record by ID with an executor, and panics on error.
    18  func Find{{$alias.UpSingular}}P({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, {{$pkArgs}}, selectCols ...string) *{{$alias.UpSingular}} {
    19  	retobj, err := Find{{$alias.UpSingular}}({{if not .NoContext}}ctx, {{end -}} exec, {{$pkNames | join ", "}}, selectCols...)
    20  	if err != nil {
    21  		panic(boil.WrapErr(err))
    22  	}
    23  
    24  	return retobj
    25  }
    26  
    27  {{end -}}
    28  
    29  {{if and .AddGlobal .AddPanic -}}
    30  // Find{{$alias.UpSingular}}GP retrieves a single record by ID, and panics on error.
    31  func Find{{$alias.UpSingular}}GP({{if not .NoContext}}ctx context.Context, {{end -}} {{$pkArgs}}, selectCols ...string) *{{$alias.UpSingular}} {
    32  	retobj, err := Find{{$alias.UpSingular}}({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, {{$pkNames | join ", "}}, selectCols...)
    33  	if err != nil {
    34  		panic(boil.WrapErr(err))
    35  	}
    36  
    37  	return retobj
    38  }
    39  
    40  {{end -}}
    41  
    42  // Find{{$alias.UpSingular}} retrieves a single record by ID with an executor.
    43  // If selectCols is empty Find will return all columns.
    44  func Find{{$alias.UpSingular}}({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, {{$pkArgs}}, selectCols ...string) (*{{$alias.UpSingular}}, error) {
    45  	{{$alias.DownSingular}}Obj := &{{$alias.UpSingular}}{}
    46  
    47  	sel := "*"
    48  	if len(selectCols) > 0 {
    49  		sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
    50  	}
    51  	query := fmt.Sprintf(
    52  		"select %s from {{.Table.Name | .SchemaTable}} where {{if .Dialect.UseIndexPlaceholders}}{{whereClause .LQ .RQ 1 .Table.PKey.Columns}}{{else}}{{whereClause .LQ .RQ 0 .Table.PKey.Columns}}{{end}}{{if and .AddSoftDeletes $canSoftDelete}} and {{or $.AutoColumns.Deleted "deleted_at" | $.Quotes}} is null{{end}}", sel,
    53  	)
    54  
    55  	q := queries.Raw(query, {{$pkNames | join ", "}})
    56  
    57  	err := q.Bind({{if not .NoContext}}ctx{{else}}nil{{end}}, exec, {{$alias.DownSingular}}Obj)
    58  	if err != nil {
    59  		{{if not .AlwaysWrapErrors -}}
    60  		if errors.Is(err, sql.ErrNoRows) {
    61  			return nil, sql.ErrNoRows
    62  		}
    63  		{{end -}}
    64  		return nil, errors.Wrap(err, "{{.PkgName}}: unable to select from {{.Table.Name}}")
    65  	}
    66  
    67  	{{if not .NoHooks -}}
    68  	if err = {{$alias.DownSingular}}Obj.doAfterSelectHooks({{if not .NoContext}}ctx, {{end -}} exec); err != nil {
    69  		return {{$alias.DownSingular}}Obj, err
    70  	}
    71  	{{- end}}
    72  
    73  	return {{$alias.DownSingular}}Obj, nil
    74  }
    75  
    76  {{- end -}}