Debian Codegraph @

clause/clause.go

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