Add support for VALUES statement.

This commit is contained in:
go-jet 2024-10-17 14:12:21 +02:00
parent 3fcbbec427
commit 8d112f7db8
41 changed files with 1296 additions and 131 deletions

View file

@ -6,23 +6,27 @@ import (
)
type cast interface {
// Cast expressions as castType type
// AS casts expressions as castType type
AS(castType string) Expression
// Cast expression as char with optional length
// AS_CHAR casts expression as char with optional length
AS_CHAR(length ...int) StringExpression
// Cast expression AS date type
// AS_DATE casts expression AS date type
AS_DATE() DateExpression
// Cast expression AS numeric type, using precision and optionally scale
// AS_FLOAT casts expressions as float type
AS_FLOAT() FloatExpression
// AS_DOUBLE casts expressions as double type
AS_DOUBLE() FloatExpression
// AS_DECIMAL casts expression AS numeric type
AS_DECIMAL() FloatExpression
// Cast expression AS time type
// AS_TIME casts expression AS time type
AS_TIME() TimeExpression
// Cast expression as datetime type
// AS_DATETIME casts expression as datetime type
AS_DATETIME() DateTimeExpression
// Cast expressions as signed integer type
// AS_SIGNED casts expressions as signed integer type
AS_SIGNED() IntegerExpression
// Cast expression as unsigned integer type
// AS_UNSIGNED casts expression as unsigned integer type
AS_UNSIGNED() IntegerExpression
// Cast expression as binary type
// AS_BINARY casts expression as binary type
AS_BINARY() StringExpression
}
@ -73,6 +77,14 @@ func (c *castImpl) AS_DATE() DateExpression {
return DateExp(c.AS("DATE"))
}
func (c *castImpl) AS_FLOAT() FloatExpression {
return FloatExp(c.AS("FLOAT"))
}
func (c *castImpl) AS_DOUBLE() FloatExpression {
return FloatExp(c.AS("DOUBLE"))
}
// AS_DECIMAL casts expression AS DECIMAL type
func (c *castImpl) AS_DECIMAL() FloatExpression {
return FloatExp(c.AS("DECIMAL"))

View file

@ -1,6 +1,7 @@
package mysql
import (
"fmt"
"github.com/go-jet/jet/v2/internal/jet"
)
@ -28,6 +29,9 @@ func newDialect() jet.Dialect {
},
ReservedWords: reservedWords,
SerializeOrderBy: serializeOrderBy,
ValuesDefaultColumnName: func(index int) string {
return fmt.Sprintf("column_%d", index)
},
}
return jet.NewDialect(mySQLDialectParams)

View file

@ -12,7 +12,9 @@ var (
)
// ROW function is used to create a tuple value that consists of a set of expressions or column values.
var ROW = jet.ROW
func ROW(expressions ...Expression) RowExpression {
return jet.ROW(Dialect, expressions...)
}
// ------------------ Mathematical functions ---------------//

View file

@ -172,7 +172,7 @@ func (s *selectStatementImpl) LOCK_IN_SHARE_MODE() SelectStatement {
}
func (s *selectStatementImpl) AsTable(alias string) SelectTable {
return newSelectTable(s, alias)
return newSelectTable(s, alias, nil)
}
//-----------------------------------------------------

View file

@ -13,9 +13,9 @@ type selectTableImpl struct {
readableTableInterfaceImpl
}
func newSelectTable(selectStmt jet.SerializerHasProjections, alias string) SelectTable {
func newSelectTable(selectStmt jet.SerializerHasProjections, alias string, columnAliases []jet.ColumnExpression) SelectTable {
subQuery := &selectTableImpl{
SelectTable: jet.NewSelectTable(selectStmt, alias),
SelectTable: jet.NewSelectTable(selectStmt, alias, columnAliases),
}
subQuery.readableTableInterfaceImpl.parent = subQuery

View file

@ -85,7 +85,7 @@ func (s *setStatementImpl) OFFSET(offset int64) setStatement {
}
func (s *setStatementImpl) AsTable(alias string) SelectTable {
return newSelectTable(s, alias)
return newSelectTable(s, alias, nil)
}
const (

View file

@ -3,6 +3,6 @@ package mysql
import "github.com/go-jet/jet/v2/internal/jet"
// RawStatement creates new sql statements from raw query and optional map of named arguments
func RawStatement(rawQuery string, namedArguments ...RawArgs) Statement {
func RawStatement(rawQuery string, namedArguments ...RawArgs) jet.SerializerStatement {
return jet.RawStatement(Dialect, rawQuery, namedArguments...)
}

32
mysql/values.go Normal file
View file

@ -0,0 +1,32 @@
package mysql
import "github.com/go-jet/jet/v2/internal/jet"
type values struct {
jet.Values
}
// VALUES is a table value constructor that computes a set of one or more rows as a temporary constant table.
// Each row is defined by the ROW constructor, which takes one or more expressions.
//
// Example usage:
//
// VALUES(
// ROW(Int32(204), Float32(1.21)),
// ROW(Int32(207), Float32(1.02)),
// )
func VALUES(rows ...RowExpression) values {
return values{Values: jet.Values(rows)}
}
// AS assigns an alias to the temporary VALUES table, allowing it to be referenced
// within SQL FROM clauses, just like a regular table.
// By default, VALUES columns are named `column1`, `column2`, etc... Default column aliasing can be
// overwritten by passing new list of columns.
//
// Example usage:
//
// VALUES(...).AS("film_values", IntegerColumn("length"), TimestampColumn("update_date"))
func (v values) AS(alias string, columns ...Column) SelectTable {
return newSelectTable(v, alias, columns)
}

View file

@ -6,7 +6,7 @@ import "github.com/go-jet/jet/v2/internal/jet"
type CommonTableExpression interface {
SelectTable
AS(statement jet.SerializerStatement) CommonTableExpression
AS(statement jet.SerializerHasProjections) CommonTableExpression
// ALIAS is used to create another alias of the CTE, if a CTE needs to appear multiple times in the main query.
ALIAS(alias string) SelectTable
@ -41,7 +41,7 @@ func CTE(name string, columns ...jet.ColumnExpression) CommonTableExpression {
}
// AS is used to define a CTE query
func (c *commonTableExpression) AS(statement jet.SerializerStatement) CommonTableExpression {
func (c *commonTableExpression) AS(statement jet.SerializerHasProjections) CommonTableExpression {
c.CommonTableExpression.Statement = statement
return c
}
@ -52,7 +52,7 @@ func (c *commonTableExpression) internalCTE() *jet.CommonTableExpression {
// ALIAS is used to create another alias of the CTE, if a CTE needs to appear multiple times in the main query.
func (c *commonTableExpression) ALIAS(name string) SelectTable {
return newSelectTable(c, name)
return newSelectTable(c, name, nil)
}
func toInternalCTE(ctes []CommonTableExpression) []*jet.CommonTableExpression {