jet/sqlbuilder/column.go

123 lines
2.1 KiB
Go
Raw Normal View History

// Modeling of columns
package sqlbuilder
import (
"strings"
)
2019-05-07 19:06:21 +02:00
type column interface {
expression
2019-03-17 20:41:03 +01:00
Name() string
TableName() string
2019-05-03 12:51:57 +02:00
2019-05-07 19:06:21 +02:00
DefaultAlias() projection
// Internal function for tracking tableName that a column belongs to
// for the purpose of serialization
2019-05-05 13:49:24 +02:00
setTableName(table string)
}
type NullableColumn bool
const (
Nullable NullableColumn = true
NotNullable NullableColumn = false
)
type Collation string
const (
UTF8CaseInsensitive Collation = "utf8_unicode_ci"
UTF8CaseSensitive Collation = "utf8_unicode"
UTF8Binary Collation = "utf8_bin"
)
// Representation of MySQL charsets
type Charset string
const (
UTF8 Charset = "utf8"
)
// The base type for real materialized columns.
type baseColumn struct {
2019-03-31 09:17:28 +02:00
expressionInterfaceImpl
2019-03-31 14:07:58 +02:00
name string
nullable NullableColumn
tableName string
2019-03-17 20:41:03 +01:00
}
2019-05-07 19:06:21 +02:00
func newBaseColumn(name string, nullable NullableColumn, tableName string, parent column) baseColumn {
2019-03-31 14:07:58 +02:00
bc := baseColumn{
name: name,
nullable: nullable,
tableName: tableName,
}
bc.expressionInterfaceImpl.parent = parent
return bc
}
func (c *baseColumn) Name() string {
return c.name
}
func (c *baseColumn) TableName() string {
return c.tableName
}
2019-05-05 13:49:24 +02:00
func (c *baseColumn) setTableName(table string) {
c.tableName = table
}
2019-05-07 19:06:21 +02:00
func (c *baseColumn) DefaultAlias() projection {
return c.AS(c.tableName + "." + c.name)
2019-05-03 12:51:57 +02:00
}
func (c *baseColumn) serializeAsOrderBy(statement statementType, out *queryData) error {
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
}
return c.serialize(statement, out)
2019-05-08 12:49:36 +02:00
}
func (c baseColumn) serialize(statement statementType, out *queryData) 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-05-08 12:49:36 +02:00
wrapColumnName := strings.Contains(c.name, ".")
2019-05-03 12:51:57 +02:00
if wrapColumnName {
2019-05-12 18:15:23 +02:00
columnRef += `"`
}
2019-05-12 18:15:23 +02:00
columnRef += c.name
2019-05-03 12:51:57 +02:00
if wrapColumnName {
2019-05-12 18:15:23 +02:00
columnRef += `"`
}
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
}