clause.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package clause
  2. // Interface clause interface
  3. type Interface interface {
  4. Name() string
  5. Build(Builder)
  6. MergeClause(*Clause)
  7. }
  8. // ClauseBuilder clause builder, allows to customize how to build clause
  9. type ClauseBuilder func(Clause, Builder)
  10. type Writer interface {
  11. WriteByte(byte) error
  12. WriteString(string) (int, error)
  13. }
  14. // Builder builder interface
  15. type Builder interface {
  16. Writer
  17. WriteQuoted(field interface{})
  18. AddVar(Writer, ...interface{})
  19. }
  20. // Clause
  21. type Clause struct {
  22. Name string // WHERE
  23. BeforeExpression Expression
  24. AfterNameExpression Expression
  25. AfterExpression Expression
  26. Expression Expression
  27. Builder ClauseBuilder
  28. }
  29. // Build build clause
  30. func (c Clause) Build(builder Builder) {
  31. if c.Builder != nil {
  32. c.Builder(c, builder)
  33. } else if c.Expression != nil {
  34. if c.BeforeExpression != nil {
  35. c.BeforeExpression.Build(builder)
  36. builder.WriteByte(' ')
  37. }
  38. if c.Name != "" {
  39. builder.WriteString(c.Name)
  40. builder.WriteByte(' ')
  41. }
  42. if c.AfterNameExpression != nil {
  43. c.AfterNameExpression.Build(builder)
  44. builder.WriteByte(' ')
  45. }
  46. c.Expression.Build(builder)
  47. if c.AfterExpression != nil {
  48. builder.WriteByte(' ')
  49. c.AfterExpression.Build(builder)
  50. }
  51. }
  52. }
  53. const (
  54. PrimaryKey string = "~~~py~~~" // primary key
  55. CurrentTable string = "~~~ct~~~" // current table
  56. Associations string = "~~~as~~~" // associations
  57. )
  58. var (
  59. currentTable = Table{Name: CurrentTable}
  60. PrimaryColumn = Column{Table: CurrentTable, Name: PrimaryKey}
  61. )
  62. // Column quote with name
  63. type Column struct {
  64. Table string
  65. Name string
  66. Alias string
  67. Raw bool
  68. }
  69. // Table quote with name
  70. type Table struct {
  71. Name string
  72. Alias string
  73. Raw bool
  74. }