2019-03-02 12:34:08 +01:00
|
|
|
// Modeling of columns
|
|
|
|
|
|
|
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-30 10:17:32 +01:00
|
|
|
"strings"
|
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-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-03-31 14:07:58 +02:00
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
name string
|
|
|
|
|
tableName string
|
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-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-06-08 16:34:15 +02:00
|
|
|
func (c *columnImpl) serializeAsOrderBy(statement statementType, out *queryData) error {
|
2019-05-08 13:47:01 +02:00
|
|
|
if statement == set_statement {
|
2019-05-12 18:15:23 +02:00
|
|
|
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
|
|
|
|
|
columnRef := ""
|
2019-05-08 12:49:36 +02:00
|
|
|
|
|
|
|
|
if c.tableName != "" {
|
2019-05-12 18:15:23 +02:00
|
|
|
columnRef += c.tableName + "."
|
2019-05-08 12:49:36 +02:00
|
|
|
}
|
2019-05-03 12:51:57 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
columnRef += c.name
|
2019-05-03 12:51:57 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString(`"` + columnRef + `"`)
|
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-06-09 11:06:08 +02:00
|
|
|
func (c columnImpl) serializeForProjection(statement statementType, out *queryData) error {
|
|
|
|
|
err := c.serialize(statement, out)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.writeString(`AS "` + c.defaultAlias() + `"`)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
func (c columnImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
2019-05-12 18:15:23 +02:00
|
|
|
|
|
|
|
|
columnRef := ""
|
|
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if c.tableName != "" {
|
2019-05-12 18:15:23 +02:00
|
|
|
columnRef += c.tableName + "."
|
2019-03-09 14:20:44 +01:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-08 12:49:36 +02:00
|
|
|
wrapColumnName := strings.Contains(c.name, ".")
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if wrapColumnName {
|
2019-05-12 18:15:23 +02:00
|
|
|
columnRef += `"`
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
columnRef += c.name
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if wrapColumnName {
|
2019-05-12 18:15:23 +02:00
|
|
|
columnRef += `"`
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString(columnRef)
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
return nil
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|