cond_not_exists.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2022 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "errors"
  7. "io"
  8. )
  9. type condNotExists struct {
  10. subQuery *Builder
  11. }
  12. var _ Cond = condNotExists{}
  13. // NotExists returns Cond via condition
  14. func NotExists(subQuery *Builder) Cond {
  15. return &condNotExists{
  16. subQuery: subQuery,
  17. }
  18. }
  19. func (condNotExists condNotExists) WriteTo(w Writer) error {
  20. if !condNotExists.IsValid() {
  21. return errors.New("exists condition is nov valid")
  22. }
  23. if _, err := io.WriteString(w, "NOT EXISTS ("); err != nil {
  24. return err
  25. }
  26. if err := condNotExists.subQuery.WriteTo(w); err != nil {
  27. return err
  28. }
  29. _, err := io.WriteString(w, ")")
  30. return err
  31. }
  32. func (condNotExists condNotExists) And(conds ...Cond) Cond {
  33. return And(condNotExists, And(conds...))
  34. }
  35. func (condNotExists condNotExists) Or(conds ...Cond) Cond {
  36. return Or(condNotExists, Or(conds...))
  37. }
  38. func (condNotExists condNotExists) IsValid() bool {
  39. return condNotExists.subQuery != nil
  40. }