-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #410 from go-jet/row-exp
Add support for ROW expressions and VALUES statement
- Loading branch information
Showing
53 changed files
with
1,532 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package jet | ||
|
||
// RowExpression interface | ||
type RowExpression interface { | ||
Expression | ||
HasProjections | ||
|
||
EQ(rhs RowExpression) BoolExpression | ||
NOT_EQ(rhs RowExpression) BoolExpression | ||
IS_DISTINCT_FROM(rhs RowExpression) BoolExpression | ||
IS_NOT_DISTINCT_FROM(rhs RowExpression) BoolExpression | ||
|
||
LT(rhs RowExpression) BoolExpression | ||
LT_EQ(rhs RowExpression) BoolExpression | ||
GT(rhs RowExpression) BoolExpression | ||
GT_EQ(rhs RowExpression) BoolExpression | ||
} | ||
|
||
type rowInterfaceImpl struct { | ||
parent Expression | ||
dialect Dialect | ||
elemCount int | ||
} | ||
|
||
func (n *rowInterfaceImpl) EQ(rhs RowExpression) BoolExpression { | ||
return Eq(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) NOT_EQ(rhs RowExpression) BoolExpression { | ||
return NotEq(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) IS_DISTINCT_FROM(rhs RowExpression) BoolExpression { | ||
return IsDistinctFrom(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs RowExpression) BoolExpression { | ||
return IsNotDistinctFrom(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) GT(rhs RowExpression) BoolExpression { | ||
return Gt(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) GT_EQ(rhs RowExpression) BoolExpression { | ||
return GtEq(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) LT(rhs RowExpression) BoolExpression { | ||
return Lt(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) LT_EQ(rhs RowExpression) BoolExpression { | ||
return LtEq(n.parent, rhs) | ||
} | ||
|
||
func (n *rowInterfaceImpl) projections() ProjectionList { | ||
var ret ProjectionList | ||
|
||
for i := 0; i < n.elemCount; i++ { | ||
rowColumn := NewColumnImpl(n.dialect.ValuesDefaultColumnName(i), "", nil) | ||
ret = append(ret, &rowColumn) | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// ---------------------------------------------------// | ||
type rowExpressionWrapper struct { | ||
rowInterfaceImpl | ||
Expression | ||
} | ||
|
||
func newRowExpression(name string, dialect Dialect, expressions ...Expression) RowExpression { | ||
ret := &rowExpressionWrapper{} | ||
ret.rowInterfaceImpl.parent = ret | ||
|
||
ret.Expression = NewFunc(name, expressions, ret) | ||
ret.dialect = dialect | ||
ret.elemCount = len(expressions) | ||
|
||
return ret | ||
} | ||
|
||
// ROW function is used to create a tuple value that consists of a set of expressions or column values. | ||
func ROW(dialect Dialect, expressions ...Expression) RowExpression { | ||
return newRowExpression("ROW", dialect, expressions...) | ||
} | ||
|
||
// WRAP creates row expressions without ROW keyword `( expression1, expression2, ... )`. | ||
func WRAP(dialect Dialect, expressions ...Expression) RowExpression { | ||
return newRowExpression("", dialect, expressions...) | ||
} | ||
|
||
// RowExp serves as a wrapper for an arbitrary expression, treating it as a row expression. | ||
// This enables the Go compiler to interpret any expression as a row expression | ||
// Note: This does not modify the generated SQL builder output by adding a SQL CAST operation. | ||
func RowExp(expression Expression) RowExpression { | ||
rowExpressionWrap := rowExpressionWrapper{Expression: expression} | ||
rowExpressionWrap.rowInterfaceImpl.parent = &rowExpressionWrap | ||
return &rowExpressionWrap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package jet | ||
|
||
// Values hold a set of one or more rows | ||
type Values []RowExpression | ||
|
||
func (v Values) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) { | ||
out.WriteByte('(') | ||
out.IncreaseIdent(5) | ||
|
||
out.NewLine() | ||
out.WriteString("VALUES") | ||
|
||
for rowIndex, row := range v { | ||
if rowIndex > 0 { | ||
out.WriteString(",") | ||
out.NewLine() | ||
} else { | ||
out.IncreaseIdent(7) | ||
} | ||
|
||
row.serialize(statement, out, options...) | ||
} | ||
out.DecreaseIdent(7) | ||
out.DecreaseIdent(5) | ||
out.NewLine() | ||
out.WriteByte(')') | ||
} | ||
|
||
func (v Values) projections() ProjectionList { | ||
if len(v) == 0 { | ||
return nil | ||
} | ||
|
||
return v[0].projections() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.