Files
gorm/clause/clause.go
T

90 lines
1.7 KiB
Go
Raw Normal View History

2020-01-29 19:22:44 +08:00
package clause
2020-02-07 23:45:35 +08:00
// Interface clause interface
type Interface interface {
Name() string
Build(Builder)
MergeClause(*Clause)
}
2021-01-04 04:30:05 +01:00
// ClauseBuilder clause builder, allows to customize how to build clause
2020-05-29 22:34:35 +08:00
type ClauseBuilder func(Clause, Builder)
2020-02-07 23:45:35 +08:00
2020-03-09 17:07:00 +08:00
type Writer interface {
WriteByte(byte) error
WriteString(string) (int, error)
}
2020-02-07 23:45:35 +08:00
// Builder builder interface
type Builder interface {
2020-03-09 17:07:00 +08:00
Writer
2020-07-16 11:27:04 +08:00
WriteQuoted(field interface{})
2020-03-09 17:07:00 +08:00
AddVar(Writer, ...interface{})
2023-02-02 17:15:08 +08:00
AddError(error) error
2020-02-07 23:45:35 +08:00
}
2020-01-30 15:14:48 +08:00
// Clause
type Clause struct {
2020-06-06 22:52:08 +08:00
Name string // WHERE
BeforeExpression Expression
AfterNameExpression Expression
AfterExpression Expression
Expression Expression
Builder ClauseBuilder
2020-01-30 15:14:48 +08:00
}
// Build build clause
func (c Clause) Build(builder Builder) {
if c.Builder != nil {
2020-05-29 22:34:35 +08:00
c.Builder(c, builder)
2020-06-06 22:52:08 +08:00
} else if c.Expression != nil {
if c.BeforeExpression != nil {
c.BeforeExpression.Build(builder)
builder.WriteByte(' ')
}
2020-01-30 15:14:48 +08:00
if c.Name != "" {
2020-06-06 22:52:08 +08:00
builder.WriteString(c.Name)
builder.WriteByte(' ')
2020-01-30 15:14:48 +08:00
}
2020-06-06 22:52:08 +08:00
if c.AfterNameExpression != nil {
2020-06-14 11:46:17 +08:00
c.AfterNameExpression.Build(builder)
2020-06-06 22:52:08 +08:00
builder.WriteByte(' ')
2020-01-30 15:14:48 +08:00
}
2020-06-06 22:52:08 +08:00
c.Expression.Build(builder)
if c.AfterExpression != nil {
builder.WriteByte(' ')
c.AfterExpression.Build(builder)
2020-01-30 15:14:48 +08:00
}
}
2020-01-29 19:22:44 +08:00
}
2020-02-07 23:45:35 +08:00
const (
2021-03-07 08:29:00 +05:30
PrimaryKey string = "~~~py~~~" // primary key
CurrentTable string = "~~~ct~~~" // current table
Associations string = "~~~as~~~" // associations
2020-02-07 23:45:35 +08:00
)
var (
currentTable = Table{Name: CurrentTable}
PrimaryColumn = Column{Table: CurrentTable, Name: PrimaryKey}
)
// Column quote with name
type Column struct {
Table string
Name string
Alias string
Raw bool
2020-01-30 03:03:06 +08:00
}
2020-02-07 23:45:35 +08:00
// Table quote with name
type Table struct {
Name string
Alias string
Raw bool
2020-01-29 19:22:44 +08:00
}