cond.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2016 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. "io"
  7. )
  8. // Writer defines the interface
  9. type Writer interface {
  10. io.Writer
  11. Append(...interface{})
  12. }
  13. var _ Writer = NewWriter()
  14. // BytesWriter implments Writer and save SQL in bytes.Buffer
  15. type BytesWriter struct {
  16. writer *StringBuilder
  17. args []interface{}
  18. }
  19. // NewWriter creates a new string writer
  20. func NewWriter() *BytesWriter {
  21. w := &BytesWriter{
  22. writer: &StringBuilder{},
  23. }
  24. return w
  25. }
  26. // Write writes data to Writer
  27. func (s *BytesWriter) Write(buf []byte) (int, error) {
  28. return s.writer.Write(buf)
  29. }
  30. // Append appends args to Writer
  31. func (s *BytesWriter) Append(args ...interface{}) {
  32. s.args = append(s.args, args...)
  33. }
  34. // Cond defines an interface
  35. type Cond interface {
  36. WriteTo(Writer) error
  37. And(...Cond) Cond
  38. Or(...Cond) Cond
  39. IsValid() bool
  40. }
  41. type condEmpty struct{}
  42. var _ Cond = condEmpty{}
  43. // NewCond creates an empty condition
  44. func NewCond() Cond {
  45. return condEmpty{}
  46. }
  47. func (condEmpty) WriteTo(w Writer) error {
  48. return nil
  49. }
  50. func (condEmpty) And(conds ...Cond) Cond {
  51. return And(conds...)
  52. }
  53. func (condEmpty) Or(conds ...Cond) Cond {
  54. return Or(conds...)
  55. }
  56. func (condEmpty) IsValid() bool {
  57. return false
  58. }