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-05-07 19:06:21 +02:00
|
|
|
type column interface {
|
|
|
|
|
expression
|
2019-03-17 20:41:03 +01:00
|
|
|
|
2019-03-02 12:34:08 +01:00
|
|
|
Name() string
|
2019-03-30 10:17:32 +01:00
|
|
|
TableName() string
|
2019-05-03 12:51:57 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
DefaultAlias() projection
|
2019-03-30 10:17:32 +01:00
|
|
|
// Internal function for tracking tableName that a column belongs to
|
2019-03-02 12:34:08 +01:00
|
|
|
// for the purpose of serialization
|
2019-05-05 13:49:24 +02:00
|
|
|
setTableName(table string)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-30 10:17:32 +01: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
|
|
|
|
|
}
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
func (c *baseColumn) Name() string {
|
|
|
|
|
return c.name
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
func (c *baseColumn) TableName() string {
|
|
|
|
|
return c.tableName
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 13:49:24 +02:00
|
|
|
func (c *baseColumn) setTableName(table string) {
|
2019-03-30 10:17:32 +01:00
|
|
|
c.tableName = table
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (c *baseColumn) DefaultAlias() projection {
|
2019-05-05 18:03:30 +02:00
|
|
|
return c.AS(c.tableName + "." + c.name)
|
2019-05-03 12:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (c baseColumn) serialize(out *queryData) 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 != "" {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(c.tableName)
|
|
|
|
|
out.WriteString(".")
|
2019-03-09 14:20:44 +01:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
wrapColumnName := strings.Contains(c.name, ".") && !setOrderBy
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if wrapColumnName {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(`"`)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
out.WriteString(c.name)
|
|
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if wrapColumnName {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(`"`)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if setOrderBy {
|
|
|
|
|
out.WriteString(`"`)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
return nil
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|