jet/sqlbuilder/column.go

111 lines
1.9 KiB
Go
Raw Normal View History

// Modeling of columns
package sqlbuilder
import (
"strings"
)
type Column interface {
2019-03-31 09:17:28 +02:00
Expression
2019-03-17 20:41:03 +01:00
Name() string
TableName() string
2019-05-03 12:51:57 +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-03-31 14:07:58 +02:00
func newBaseColumn(name string, nullable NullableColumn, tableName string, parent Column) baseColumn {
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-03 12:51:57 +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) Serialize(out *queryData, options ...serializeOption) error {
2019-05-03 12:51:57 +02:00
setOrderBy := out.statementType == set_statement && out.clauseType == order_by_clause
if setOrderBy {
out.WriteString(`"`)
}
if c.tableName != "" {
out.WriteString(c.tableName)
out.WriteString(".")
2019-03-09 14:20:44 +01:00
}
2019-05-03 12:51:57 +02:00
wrapColumnName := strings.Contains(c.name, ".") && !setOrderBy
2019-05-03 12:51:57 +02:00
if wrapColumnName {
out.WriteString(`"`)
}
out.WriteString(c.name)
2019-05-03 12:51:57 +02:00
if wrapColumnName {
out.WriteString(`"`)
}
2019-03-09 14:20:44 +01:00
2019-05-03 12:51:57 +02:00
if setOrderBy {
out.WriteString(`"`)
}
2019-03-31 14:07:58 +02:00
return nil
}