jet/column.go

148 lines
3.2 KiB
Go
Raw Normal View History

// Modeling of columns
2019-06-21 13:56:57 +02:00
package jet
2019-06-08 16:34:15 +02:00
type column interface {
Name() string
TableName() string
2019-06-08 16:34:15 +02:00
2019-05-05 13:49:24 +02:00
setTableName(table string)
2019-07-18 17:43:11 +02:00
setSubQuery(subQuery SelectTable)
defaultAlias() string
2019-06-08 16:34:15 +02:00
}
2019-07-18 17:43:11 +02:00
// Column is common column interface for all types of columns.
2019-06-08 16:34:15 +02:00
type Column interface {
Expression
column
}
// The base type for real materialized columns.
2019-06-08 16:34:15 +02:00
type columnImpl struct {
2019-03-31 09:17:28 +02:00
expressionInterfaceImpl
noOpVisitorImpl
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-06-08 16:34:15 +02:00
func newColumn(name string, tableName string, parent Column) columnImpl {
bc := columnImpl{
name: name,
tableName: tableName,
2019-03-31 14:07:58 +02:00
}
bc.expressionInterfaceImpl.parent = parent
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-06-08 16:34:15 +02:00
func (c *columnImpl) setTableName(table string) {
c.tableName = table
}
2019-07-18 17:43:11 +02:00
func (c *columnImpl) setSubQuery(subQuery SelectTable) {
c.subQuery = subQuery
}
2019-06-08 16:34:15 +02:00
func (c *columnImpl) defaultAlias() string {
if c.tableName != "" {
return c.tableName + "." + c.name
}
return c.name
2019-06-05 17:15:20 +02:00
}
2019-07-08 10:48:03 +02:00
func (c *columnImpl) serializeForOrderBy(statement statementType, out *sqlBuilder) error {
2019-07-18 17:43:11 +02:00
if statement == setStatement {
2019-05-12 18:15:23 +02:00
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
out.writeAlias(c.defaultAlias()) //always quote
2019-05-08 12:49:36 +02:00
return nil
2019-05-03 12:51:57 +02:00
}
return c.serialize(statement, out)
2019-05-08 12:49:36 +02:00
}
2019-07-08 10:48:03 +02:00
func (c columnImpl) serializeForProjection(statement statementType, out *sqlBuilder) error {
2019-06-09 11:06:08 +02:00
err := c.serialize(statement, out)
if err != nil {
return err
}
out.writeString("AS")
out.writeAlias(c.defaultAlias())
2019-06-09 11:06:08 +02:00
return nil
}
2019-07-08 10:48:03 +02:00
func (c columnImpl) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
2019-05-12 18:15:23 +02:00
if c.subQuery != nil {
out.writeIdentifier(c.subQuery.Alias())
2019-06-17 12:05:52 +02:00
out.writeByte('.')
out.writeAlias(c.defaultAlias())
} else {
if c.tableName != "" {
out.writeIdentifier(c.tableName)
out.writeByte('.')
}
out.writeIdentifier(c.name)
}
2019-03-09 14:20:44 +01:00
2019-03-31 14:07:58 +02:00
return nil
}
//------------------------------------------------------//
2019-07-16 12:17:27 +02:00
2019-07-18 17:43:11 +02:00
// ColumnList is redefined type to support list of columns as single projection
type ColumnList []Column
// projection interface implementation
func (cl ColumnList) isProjectionType() {}
2019-07-18 17:43:11 +02:00
func (cl ColumnList) from(subQuery SelectTable) projection {
newProjectionList := ProjectionList{}
for _, column := range cl {
newProjectionList = append(newProjectionList, column.from(subQuery))
}
return newProjectionList
}
2019-07-08 10:48:03 +02:00
func (cl ColumnList) serializeForProjection(statement statementType, out *sqlBuilder) error {
projections := columnListToProjectionList(cl)
err := serializeProjectionList(statement, projections, out)
if err != nil {
return err
}
return nil
}
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
func (cl ColumnList) Name() string { return "" }
// TableName is placeholder for ColumnList to implement Column interface
func (cl ColumnList) TableName() string { return "" }
func (cl ColumnList) setTableName(name string) {}
func (cl ColumnList) setSubQuery(subQuery SelectTable) {}
func (cl ColumnList) defaultAlias() string { return "" }