2019-03-02 12:34:08 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Clause interface {
|
|
|
|
|
SerializeSql(out *bytes.Buffer) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A clause that can be used in order by
|
|
|
|
|
type OrderByClause interface {
|
|
|
|
|
Clause
|
|
|
|
|
isOrderByClauseInterface
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An expression
|
|
|
|
|
type Expression interface {
|
|
|
|
|
Clause
|
|
|
|
|
isExpressionInterface
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type BoolExpression interface {
|
|
|
|
|
Clause
|
|
|
|
|
isBoolExpressionInterface
|
2019-03-15 22:02:59 +01:00
|
|
|
|
|
|
|
|
And(expression BoolExpression) BoolExpression
|
|
|
|
|
Or(expression BoolExpression) BoolExpression
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A clause that is selectable.
|
|
|
|
|
type Projection interface {
|
|
|
|
|
Clause
|
|
|
|
|
isProjectionInterface
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
As(alias string) Projection
|
2019-03-02 12:34:08 +01:00
|
|
|
SerializeSqlForColumnList(out *bytes.Buffer) error
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 21:25:24 +01:00
|
|
|
type ColumnList []NonAliasColumn
|
|
|
|
|
|
|
|
|
|
func (cl ColumnList) SerializeSql(out *bytes.Buffer) error {
|
|
|
|
|
for i, column := range cl {
|
|
|
|
|
column.SerializeSql(out)
|
|
|
|
|
|
|
|
|
|
if i != len(cl)-1 {
|
|
|
|
|
out.WriteString(", ")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cl ColumnList) isProjectionType() {
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
func (cl ColumnList) As(name string) Projection {
|
|
|
|
|
panic("Unallowed operation ")
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 21:25:24 +01:00
|
|
|
func (cl ColumnList) SerializeSqlForColumnList(out *bytes.Buffer) error {
|
|
|
|
|
for i, column := range cl {
|
|
|
|
|
column.SerializeSqlForColumnList(out)
|
|
|
|
|
|
|
|
|
|
if i != len(cl)-1 {
|
|
|
|
|
out.WriteString(", ")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 12:34:08 +01:00
|
|
|
//
|
|
|
|
|
// Boiler plates ...
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
type isOrderByClauseInterface interface {
|
|
|
|
|
isOrderByClauseType()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isOrderByClause struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (o *isOrderByClause) isOrderByClauseType() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isExpressionInterface interface {
|
|
|
|
|
isExpressionType()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isExpression struct {
|
|
|
|
|
isOrderByClause // can always use expression in order by.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *isExpression) isExpressionType() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isBoolExpressionInterface interface {
|
|
|
|
|
isExpressionInterface
|
|
|
|
|
isBoolExpressionType()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isBoolExpression struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *isBoolExpression) isBoolExpressionType() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isProjectionInterface interface {
|
|
|
|
|
isProjectionType()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type isProjection struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *isProjection) isProjectionType() {
|
|
|
|
|
}
|