jet/internal/jet/column.go

149 lines
3.5 KiB
Go
Raw Normal View History

// Modeling of columns
2019-06-21 13:56:57 +02:00
package jet
2019-08-17 10:43:16 +02:00
// Column is common column interface for all types of columns.
2019-08-11 14:29:03 +02:00
type Column interface {
Name() string
TableName() string
2019-06-08 16:34:15 +02:00
2019-08-12 11:33:46 +02:00
setTableName(table string)
setSubQuery(subQuery SelectTable)
defaultAlias() string
2019-06-08 16:34:15 +02:00
}
2019-08-17 18:32:01 +02:00
// ColumnExpression interface
2019-08-11 14:29:03 +02:00
type ColumnExpression interface {
Column
2019-06-08 16:34:15 +02:00
Expression
}
// The base type for real materialized columns.
2019-06-08 16:34:15 +02:00
type columnImpl struct {
2019-08-17 18:32:01 +02:00
expressionInterfaceImpl
2019-03-31 14:07:58 +02:00
2019-06-08 16:34:15 +02:00
name string
tableName string
2019-07-18 17:43:11 +02:00
subQuery SelectTable
2019-03-17 20:41:03 +01:00
}
2019-08-11 14:29:03 +02:00
func newColumn(name string, tableName string, parent ColumnExpression) columnImpl {
2019-06-08 16:34:15 +02:00
bc := columnImpl{
name: name,
tableName: tableName,
2019-03-31 14:07:58 +02:00
}
2019-08-17 18:32:01 +02:00
bc.expressionInterfaceImpl.Parent = parent
2019-03-31 14:07:58 +02:00
return bc
}
2019-06-08 16:34:15 +02:00
func (c *columnImpl) Name() string {
return c.name
}
2019-06-08 16:34:15 +02:00
func (c *columnImpl) TableName() string {
return c.tableName
}
2019-08-12 11:33:46 +02:00
func (c *columnImpl) setTableName(table string) {
c.tableName = table
}
2019-08-12 11:33:46 +02:00
func (c *columnImpl) setSubQuery(subQuery SelectTable) {
c.subQuery = subQuery
}
2019-08-12 11:33:46 +02:00
func (c *columnImpl) defaultAlias() string {
2019-06-08 16:34:15 +02:00
if c.tableName != "" {
return c.tableName + "." + c.name
}
return c.name
2019-06-05 17:15:20 +02:00
}
2019-08-17 18:32:01 +02:00
func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SQLBuilder) {
2019-08-03 14:10:47 +02:00
if statement == SetStatementType {
2019-05-12 18:15:23 +02:00
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
2019-08-12 11:33:46 +02:00
out.WriteAlias(c.defaultAlias()) //always quote
2019-05-08 12:49:36 +02:00
return
2019-05-03 12:51:57 +02:00
}
c.serialize(statement, out)
2019-05-08 12:49:36 +02:00
}
2019-08-17 18:32:01 +02:00
func (c columnImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
c.serialize(statement, out)
2019-06-09 11:06:08 +02:00
2019-08-03 14:10:47 +02:00
out.WriteString("AS")
2019-08-12 11:33:46 +02:00
out.WriteAlias(c.defaultAlias())
2019-06-09 11:06:08 +02:00
}
2019-08-17 18:32:01 +02:00
func (c columnImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
2019-05-12 18:15:23 +02:00
if c.subQuery != nil {
2019-08-11 18:57:40 +02:00
out.WriteIdentifier(c.subQuery.Alias())
out.WriteByte('.')
2019-08-12 11:33:46 +02:00
out.WriteIdentifier(c.defaultAlias(), true)
} else {
if c.tableName != "" {
2019-08-11 18:57:40 +02:00
out.WriteIdentifier(c.tableName)
out.WriteByte('.')
}
2019-08-11 18:57:40 +02:00
out.WriteIdentifier(c.name)
}
}
//------------------------------------------------------//
2019-07-16 12:17:27 +02:00
2019-08-17 14:49:35 +02:00
// IColumnList is used to store list of columns for later reuse as single projection or
// column list for UPDATE and INSERT statement.
2019-08-03 14:10:47 +02:00
type IColumnList interface {
Projection
2019-08-11 14:29:03 +02:00
Column
2019-08-12 11:33:46 +02:00
columns() []ColumnExpression
2019-08-03 14:10:47 +02:00
}
2019-08-17 14:49:35 +02:00
// ColumnList function returns list of columns that be used as projection or column list for UPDATE and INSERT statement.
2019-08-11 14:29:03 +02:00
func ColumnList(columns ...ColumnExpression) IColumnList {
2019-08-03 14:10:47 +02:00
return columnListImpl(columns)
}
// ColumnList is redefined type to support list of columns as single Projection
2019-08-11 14:29:03 +02:00
type columnListImpl []ColumnExpression
2019-08-03 14:10:47 +02:00
2019-08-12 11:33:46 +02:00
func (cl columnListImpl) columns() []ColumnExpression {
2019-08-03 14:10:47 +02:00
return cl
}
2019-08-03 14:10:47 +02:00
func (cl columnListImpl) fromImpl(subQuery SelectTable) Projection {
newProjectionList := ProjectionList{}
for _, column := range cl {
2019-08-03 14:10:47 +02:00
newProjectionList = append(newProjectionList, column.fromImpl(subQuery))
}
return newProjectionList
}
2019-08-17 18:32:01 +02:00
func (cl columnListImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
2019-08-03 14:10:47 +02:00
projections := ColumnListToProjectionList(cl)
SerializeProjectionList(statement, projections, out)
}
2019-07-16 12:17:27 +02:00
// dummy column interface implementation
2019-07-18 17:43:11 +02:00
// Name is placeholder for ColumnList to implement Column interface
2019-08-03 14:10:47 +02:00
func (cl columnListImpl) Name() string { return "" }
2019-07-18 17:43:11 +02:00
// TableName is placeholder for ColumnList to implement Column interface
2019-08-03 14:10:47 +02:00
func (cl columnListImpl) TableName() string { return "" }
2019-08-12 11:33:46 +02:00
func (cl columnListImpl) setTableName(name string) {}
func (cl columnListImpl) setSubQuery(subQuery SelectTable) {}
func (cl columnListImpl) defaultAlias() string { return "" }