github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/20_exists.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  {{- $schemaTable := .Table.Name | .SchemaTable -}}
     8  {{- $canSoftDelete := .Table.CanSoftDelete $.AutoColumns.Deleted }}
     9  {{if .AddGlobal -}}
    10  // {{$alias.UpSingular}}ExistsG checks if the {{$alias.UpSingular}} row exists.
    11  func {{$alias.UpSingular}}ExistsG({{if not .NoContext}}ctx context.Context, {{end -}} {{$pkArgs}}) (bool, error) {
    12  	return {{$alias.UpSingular}}Exists({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, {{$pkNames | join ", "}})
    13  }
    14  
    15  {{end -}}
    16  
    17  {{if .AddPanic -}}
    18  // {{$alias.UpSingular}}ExistsP checks if the {{$alias.UpSingular}} row exists. Panics on error.
    19  func {{$alias.UpSingular}}ExistsP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, {{$pkArgs}}) bool {
    20  	e, err := {{$alias.UpSingular}}Exists({{if not .NoContext}}ctx, {{end -}} exec, {{$pkNames | join ", "}})
    21  	if err != nil {
    22  		panic(boil.WrapErr(err))
    23  	}
    24  
    25  	return e
    26  }
    27  
    28  {{end -}}
    29  
    30  {{if and .AddGlobal .AddPanic -}}
    31  // {{$alias.UpSingular}}ExistsGP checks if the {{$alias.UpSingular}} row exists. Panics on error.
    32  func {{$alias.UpSingular}}ExistsGP({{if not .NoContext}}ctx context.Context, {{end -}} {{$pkArgs}}) bool {
    33  	e, err := {{$alias.UpSingular}}Exists({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, {{$pkNames | join ", "}})
    34  	if err != nil {
    35  		panic(boil.WrapErr(err))
    36  	}
    37  
    38  	return e
    39  }
    40  
    41  {{end -}}
    42  
    43  // {{$alias.UpSingular}}Exists checks if the {{$alias.UpSingular}} row exists.
    44  func {{$alias.UpSingular}}Exists({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, {{$pkArgs}}) (bool, error) {
    45  	var exists bool
    46  	{{if .Dialect.UseCaseWhenExistsClause -}}
    47  	sql := "select case when exists(select top(1) 1 from {{$schemaTable}} where {{if .Dialect.UseIndexPlaceholders}}{{whereClause .LQ .RQ 1 .Table.PKey.Columns}}{{else}}{{whereClause .LQ .RQ 0 .Table.PKey.Columns}}{{end}}) then 1 else 0 end"
    48  	{{- else -}}
    49  	sql := "select exists(select 1 from {{$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}} limit 1)"
    50  	{{- end}}
    51  
    52  	{{if .NoContext -}}
    53  	if boil.DebugMode {
    54  		fmt.Fprintln(boil.DebugWriter, sql)
    55  		fmt.Fprintln(boil.DebugWriter, {{$pkNames | join ", "}})
    56  	}
    57  	{{else -}}
    58  	if boil.IsDebug(ctx) {
    59  		writer := boil.DebugWriterFrom(ctx)
    60  		fmt.Fprintln(writer, sql)
    61  		fmt.Fprintln(writer, {{$pkNames | join ", "}})
    62  	}
    63  	{{end -}}
    64  
    65  	{{if .NoContext -}}
    66  	row := exec.QueryRow(sql, {{$pkNames | join ", "}})
    67  	{{else -}}
    68  	row := exec.QueryRowContext(ctx, sql, {{$pkNames | join ", "}})
    69  	{{- end}}
    70  
    71  	err := row.Scan(&exists)
    72  	if err != nil {
    73  		return false, errors.Wrap(err, "{{.PkgName}}: unable to check if {{.Table.Name}} exists")
    74  	}
    75  
    76  	return exists, nil
    77  }
    78  
    79  // Exists checks if the {{$alias.UpSingular}} row exists.
    80  func (o *{{$alias.UpSingular}}) Exists({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (bool, error) {
    81  	return {{$alias.UpSingular}}Exists({{if .NoContext}}exec{{else}}ctx, exec{{end}}, o.{{$.Table.PKey.Columns | stringMap (aliasCols $alias) | join ", o."}})
    82  }
    83  
    84  {{- end -}}