2019-03-02 12:34:08 +01:00
|
|
|
// Modeling of columns
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
type column interface {
|
2019-03-02 12:34:08 +01:00
|
|
|
Name() string
|
2019-03-30 10:17:32 +01:00
|
|
|
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)
|
2019-06-18 14:35:32 +02:00
|
|
|
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
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2019-07-28 14:57:02 +02:00
|
|
|
noOpVisitorImpl
|
2019-03-31 14:07:58 +02:00
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
name string
|
|
|
|
|
tableName string
|
2019-06-18 14:35:32 +02:00
|
|
|
|
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-03-02 12:34:08 +01:00
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
func (c *columnImpl) Name() string {
|
2019-03-02 12:34:08 +01:00
|
|
|
return c.name
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
func (c *columnImpl) TableName() string {
|
2019-03-30 10:17:32 +01:00
|
|
|
return c.tableName
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
func (c *columnImpl) setTableName(table string) {
|
2019-03-30 10:17:32 +01:00
|
|
|
c.tableName = table
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
func (c *columnImpl) setSubQuery(subQuery SelectTable) {
|
2019-06-18 14:35:32 +02:00
|
|
|
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
|
2019-07-28 14:57:02 +02:00
|
|
|
out.writeAlias(c.defaultAlias()) //always quote
|
2019-05-08 12:49:36 +02:00
|
|
|
|
|
|
|
|
return nil
|
2019-05-03 12:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +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
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 14:57:02 +02:00
|
|
|
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
|
|
|
|
2019-06-18 14:35:32 +02:00
|
|
|
if c.subQuery != nil {
|
|
|
|
|
out.writeIdentifier(c.subQuery.Alias())
|
2019-06-17 12:05:52 +02:00
|
|
|
out.writeByte('.')
|
2019-08-01 16:56:54 +02:00
|
|
|
out.writeIdentifier(c.defaultAlias(), true)
|
2019-06-18 14:35:32 +02:00
|
|
|
} else {
|
|
|
|
|
if c.tableName != "" {
|
|
|
|
|
out.writeIdentifier(c.tableName)
|
|
|
|
|
out.writeByte('.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.writeIdentifier(c.name)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
return nil
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-06-14 14:35:50 +02:00
|
|
|
|
|
|
|
|
//------------------------------------------------------//
|
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
|
2019-06-14 14:35:50 +02:00
|
|
|
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 {
|
2019-06-18 14:35:32 +02:00
|
|
|
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 {
|
2019-06-14 14:35:50 +02:00
|
|
|
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 "" }
|