TinySkin/db/ent/for_update.tmpl
2023-10-11 15:08:39 +08:00

40 lines
1.5 KiB
Cheetah

{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}
{{/* gotype: entgo.io/ent/entc/gen.Type */}}
{{/* Templates used by the "sql/lock" feature-flag to add "SELECT ... FOR UPDATE/SHARE" capabilities. */}}
{{ define "dialect/sql/query/additional/locking_sqlite" }}
{{ if $.FeatureEnabled "sql/lock" }}
{{ template "helper/sqlock_sqlite" $ }}
{{ end }}
{{ end }}
{{ define "helper/sqlock_sqlite" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
// ForUpdate locks the selected rows against concurrent updates, and prevent them from being
// updated, deleted or "selected ... for update" by other sessions, until the transaction is
// either committed or rolled-back.
func ({{ $receiver }} *{{ $builder }}) ForUpdateA(opts ...sql.LockOption) *{{ $builder }} {
if {{ $receiver }}.driver.Dialect() == dialect.SQLite {
return {{ $receiver }}
}
return {{ $receiver }}.ForUpdate(opts...)
}
// ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
// on any rows that are read. Other sessions can read the rows, but cannot modify them
// until your transaction commits.
func ({{ $receiver }} *{{ $builder }}) ForShareA(opts ...sql.LockOption) *{{ $builder }} {
if {{ $receiver }}.driver.Dialect() == dialect.SQLite {
return {{ $receiver }}
}
return {{ $receiver }}.ForShare(opts...)
}
{{ end }}