The rest of linter errors.
This commit is contained in:
parent
ab6d85f886
commit
a657b76bef
64 changed files with 637 additions and 507 deletions
|
|
@ -20,7 +20,7 @@ func (a *alias) fromImpl(subQuery SelectTable) Projection {
|
|||
return &column
|
||||
}
|
||||
|
||||
func (a *alias) serializeForProjection(statement StatementType, out *SqlBuilder) {
|
||||
func (a *alias) serializeForProjection(statement StatementType, out *SQLBuilder) {
|
||||
a.expression.serialize(statement, out)
|
||||
|
||||
out.WriteString("AS")
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ func (b *boolInterfaceImpl) IS_NOT_UNKNOWN() BoolExpression {
|
|||
|
||||
//---------------------------------------------------//
|
||||
type binaryBoolExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
boolInterfaceImpl
|
||||
|
||||
binaryOpExpression
|
||||
|
|
@ -96,7 +96,7 @@ func newBinaryBoolOperator(lhs, rhs Expression, operator string, additionalParam
|
|||
binaryBoolExpression := binaryBoolExpression{}
|
||||
|
||||
binaryBoolExpression.binaryOpExpression = newBinaryExpression(lhs, rhs, operator, additionalParams...)
|
||||
binaryBoolExpression.ExpressionInterfaceImpl.Parent = &binaryBoolExpression
|
||||
binaryBoolExpression.expressionInterfaceImpl.Parent = &binaryBoolExpression
|
||||
binaryBoolExpression.boolInterfaceImpl.parent = &binaryBoolExpression
|
||||
|
||||
return &binaryBoolExpression
|
||||
|
|
@ -104,7 +104,7 @@ func newBinaryBoolOperator(lhs, rhs Expression, operator string, additionalParam
|
|||
|
||||
//---------------------------------------------------//
|
||||
type prefixBoolExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
boolInterfaceImpl
|
||||
|
||||
prefixOpExpression
|
||||
|
|
@ -114,7 +114,7 @@ func newPrefixBoolOperator(expression Expression, operator string) BoolExpressio
|
|||
exp := prefixBoolExpression{}
|
||||
exp.prefixOpExpression = newPrefixExpression(expression, operator)
|
||||
|
||||
exp.ExpressionInterfaceImpl.Parent = &exp
|
||||
exp.expressionInterfaceImpl.Parent = &exp
|
||||
exp.boolInterfaceImpl.parent = &exp
|
||||
|
||||
return &exp
|
||||
|
|
@ -122,7 +122,7 @@ func newPrefixBoolOperator(expression Expression, operator string) BoolExpressio
|
|||
|
||||
//---------------------------------------------------//
|
||||
type postfixBoolOpExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
boolInterfaceImpl
|
||||
|
||||
postfixOpExpression
|
||||
|
|
@ -132,7 +132,7 @@ func newPostifxBoolExpression(expression Expression, operator string) BoolExpres
|
|||
exp := postfixBoolOpExpression{}
|
||||
exp.postfixOpExpression = newPostfixOpExpression(expression, operator)
|
||||
|
||||
exp.ExpressionInterfaceImpl.Parent = &exp
|
||||
exp.expressionInterfaceImpl.Parent = &exp
|
||||
exp.boolInterfaceImpl.parent = &exp
|
||||
|
||||
return &exp
|
||||
|
|
|
|||
|
|
@ -1,40 +1,42 @@
|
|||
package jet
|
||||
|
||||
// Cast interface
|
||||
type Cast interface {
|
||||
AS(castType string) Expression
|
||||
}
|
||||
|
||||
type CastImpl struct {
|
||||
type castImpl struct {
|
||||
expression Expression
|
||||
}
|
||||
|
||||
// NewCastImpl creates new generic cast
|
||||
func NewCastImpl(expression Expression) Cast {
|
||||
castImpl := CastImpl{
|
||||
castImpl := castImpl{
|
||||
expression: expression,
|
||||
}
|
||||
|
||||
return &castImpl
|
||||
}
|
||||
|
||||
func (b *CastImpl) AS(castType string) Expression {
|
||||
func (b *castImpl) AS(castType string) Expression {
|
||||
castExp := &castExpression{
|
||||
expression: b.expression,
|
||||
cast: string(castType),
|
||||
}
|
||||
|
||||
castExp.ExpressionInterfaceImpl.Parent = castExp
|
||||
castExp.expressionInterfaceImpl.Parent = castExp
|
||||
|
||||
return castExp
|
||||
}
|
||||
|
||||
type castExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
|
||||
expression Expression
|
||||
cast string
|
||||
}
|
||||
|
||||
func (b *castExpression) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (b *castExpression) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
|
||||
expression := b.expression
|
||||
castType := b.cast
|
||||
|
|
|
|||
|
|
@ -4,16 +4,19 @@ import (
|
|||
"github.com/go-jet/jet/internal/utils"
|
||||
)
|
||||
|
||||
// Clause interface
|
||||
type Clause interface {
|
||||
Serialize(statementType StatementType, out *SqlBuilder)
|
||||
Serialize(statementType StatementType, out *SQLBuilder)
|
||||
}
|
||||
|
||||
// ClauseWithProjections interface
|
||||
type ClauseWithProjections interface {
|
||||
Clause
|
||||
|
||||
projections() ProjectionList
|
||||
}
|
||||
|
||||
// ClauseSelect struct
|
||||
type ClauseSelect struct {
|
||||
Distinct bool
|
||||
Projections []Projection
|
||||
|
|
@ -23,7 +26,8 @@ func (s *ClauseSelect) projections() ProjectionList {
|
|||
return s.Projections
|
||||
}
|
||||
|
||||
func (s *ClauseSelect) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (s *ClauseSelect) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
out.NewLine()
|
||||
out.WriteString("SELECT")
|
||||
|
||||
|
|
@ -38,11 +42,13 @@ func (s *ClauseSelect) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
out.WriteProjections(statementType, s.Projections)
|
||||
}
|
||||
|
||||
// ClauseFrom struct
|
||||
type ClauseFrom struct {
|
||||
Table Serializer
|
||||
}
|
||||
|
||||
func (f *ClauseFrom) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (f *ClauseFrom) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if f.Table == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -54,12 +60,14 @@ func (f *ClauseFrom) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
out.DecreaseIdent()
|
||||
}
|
||||
|
||||
// ClauseWhere struct
|
||||
type ClauseWhere struct {
|
||||
Condition BoolExpression
|
||||
Mandatory bool
|
||||
}
|
||||
|
||||
func (c *ClauseWhere) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (c *ClauseWhere) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if c.Condition == nil {
|
||||
if c.Mandatory {
|
||||
panic("jet: WHERE clause not set")
|
||||
|
|
@ -74,11 +82,13 @@ func (c *ClauseWhere) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
out.DecreaseIdent()
|
||||
}
|
||||
|
||||
// ClauseGroupBy struct
|
||||
type ClauseGroupBy struct {
|
||||
List []GroupByClause
|
||||
}
|
||||
|
||||
func (c *ClauseGroupBy) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (c *ClauseGroupBy) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if len(c.List) == 0 {
|
||||
return
|
||||
}
|
||||
|
|
@ -87,15 +97,29 @@ func (c *ClauseGroupBy) Serialize(statementType StatementType, out *SqlBuilder)
|
|||
out.WriteString("GROUP BY")
|
||||
|
||||
out.IncreaseIdent()
|
||||
serializeGroupByClauseList(statementType, c.List, out)
|
||||
|
||||
for i, c := range c.List {
|
||||
if i > 0 {
|
||||
out.WriteString(", ")
|
||||
}
|
||||
|
||||
if c == nil {
|
||||
panic("jet: nil clause in GROUP BY list")
|
||||
}
|
||||
|
||||
c.serializeForGroupBy(statementType, out)
|
||||
}
|
||||
|
||||
out.DecreaseIdent()
|
||||
}
|
||||
|
||||
// ClauseHaving struct
|
||||
type ClauseHaving struct {
|
||||
Condition BoolExpression
|
||||
}
|
||||
|
||||
func (c *ClauseHaving) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (c *ClauseHaving) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if c.Condition == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -108,11 +132,13 @@ func (c *ClauseHaving) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
out.DecreaseIdent()
|
||||
}
|
||||
|
||||
// ClauseOrderBy struct
|
||||
type ClauseOrderBy struct {
|
||||
List []OrderByClause
|
||||
}
|
||||
|
||||
func (o *ClauseOrderBy) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (o *ClauseOrderBy) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if o.List == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -121,15 +147,25 @@ func (o *ClauseOrderBy) Serialize(statementType StatementType, out *SqlBuilder)
|
|||
out.WriteString("ORDER BY")
|
||||
|
||||
out.IncreaseIdent()
|
||||
serializeOrderByClauseList(statementType, o.List, out)
|
||||
|
||||
for i, value := range o.List {
|
||||
if i > 0 {
|
||||
out.WriteString(", ")
|
||||
}
|
||||
|
||||
value.serializeForOrderBy(statementType, out)
|
||||
}
|
||||
|
||||
out.DecreaseIdent()
|
||||
}
|
||||
|
||||
// ClauseLimit struct
|
||||
type ClauseLimit struct {
|
||||
Count int64
|
||||
}
|
||||
|
||||
func (l *ClauseLimit) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (l *ClauseLimit) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if l.Count >= 0 {
|
||||
out.NewLine()
|
||||
out.WriteString("LIMIT")
|
||||
|
|
@ -137,11 +173,13 @@ func (l *ClauseLimit) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
}
|
||||
}
|
||||
|
||||
// ClauseOffset struct
|
||||
type ClauseOffset struct {
|
||||
Count int64
|
||||
}
|
||||
|
||||
func (o *ClauseOffset) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (o *ClauseOffset) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if o.Count >= 0 {
|
||||
out.NewLine()
|
||||
out.WriteString("OFFSET")
|
||||
|
|
@ -149,11 +187,13 @@ func (o *ClauseOffset) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
}
|
||||
}
|
||||
|
||||
// ClauseFor struct
|
||||
type ClauseFor struct {
|
||||
Lock RowLock
|
||||
}
|
||||
|
||||
func (f *ClauseFor) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (f *ClauseFor) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if f.Lock == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -163,6 +203,7 @@ func (f *ClauseFor) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
f.Lock.serialize(statementType, out)
|
||||
}
|
||||
|
||||
// ClauseSetStmtOperator struct
|
||||
type ClauseSetStmtOperator struct {
|
||||
Operator string
|
||||
All bool
|
||||
|
|
@ -179,7 +220,8 @@ func (s *ClauseSetStmtOperator) projections() ProjectionList {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *ClauseSetStmtOperator) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (s *ClauseSetStmtOperator) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if len(s.Selects) < 2 {
|
||||
panic("jet: UNION Statement must contain at least two SELECT statements")
|
||||
}
|
||||
|
|
@ -207,11 +249,13 @@ func (s *ClauseSetStmtOperator) Serialize(statementType StatementType, out *SqlB
|
|||
s.Offset.Serialize(statementType, out)
|
||||
}
|
||||
|
||||
// ClauseUpdate struct
|
||||
type ClauseUpdate struct {
|
||||
Table SerializerTable
|
||||
}
|
||||
|
||||
func (u *ClauseUpdate) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (u *ClauseUpdate) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
out.NewLine()
|
||||
out.WriteString("UPDATE")
|
||||
|
||||
|
|
@ -222,12 +266,14 @@ func (u *ClauseUpdate) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
u.Table.serialize(statementType, out)
|
||||
}
|
||||
|
||||
// ClauseSet struct
|
||||
type ClauseSet struct {
|
||||
Columns []Column
|
||||
Values []Serializer
|
||||
}
|
||||
|
||||
func (s *ClauseSet) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (s *ClauseSet) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
out.NewLine()
|
||||
out.WriteString("SET")
|
||||
|
||||
|
|
@ -255,11 +301,13 @@ func (s *ClauseSet) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
out.DecreaseIdent(4)
|
||||
}
|
||||
|
||||
// ClauseInsert struct
|
||||
type ClauseInsert struct {
|
||||
Table SerializerTable
|
||||
Columns []Column
|
||||
}
|
||||
|
||||
// GetColumns gets list of columns for insert
|
||||
func (i *ClauseInsert) GetColumns() []Column {
|
||||
if len(i.Columns) > 0 {
|
||||
return i.Columns
|
||||
|
|
@ -268,7 +316,8 @@ func (i *ClauseInsert) GetColumns() []Column {
|
|||
return i.Table.columns()
|
||||
}
|
||||
|
||||
func (i *ClauseInsert) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (i *ClauseInsert) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
out.NewLine()
|
||||
out.WriteString("INSERT INTO")
|
||||
|
||||
|
|
@ -287,12 +336,14 @@ func (i *ClauseInsert) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
}
|
||||
}
|
||||
|
||||
// ClauseValuesQuery struct
|
||||
type ClauseValuesQuery struct {
|
||||
ClauseValues
|
||||
ClauseQuery
|
||||
}
|
||||
|
||||
func (v *ClauseValuesQuery) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (v *ClauseValuesQuery) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if len(v.Rows) == 0 && v.Query == nil {
|
||||
panic("jet: VALUES or QUERY has to be specified for INSERT statement")
|
||||
}
|
||||
|
|
@ -305,11 +356,13 @@ func (v *ClauseValuesQuery) Serialize(statementType StatementType, out *SqlBuild
|
|||
v.ClauseQuery.Serialize(statementType, out)
|
||||
}
|
||||
|
||||
// ClauseValues struct
|
||||
type ClauseValues struct {
|
||||
Rows [][]Serializer
|
||||
}
|
||||
|
||||
func (v *ClauseValues) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (v *ClauseValues) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if len(v.Rows) == 0 {
|
||||
return
|
||||
}
|
||||
|
|
@ -332,11 +385,13 @@ func (v *ClauseValues) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
}
|
||||
}
|
||||
|
||||
// ClauseQuery struct
|
||||
type ClauseQuery struct {
|
||||
Query SerializerStatement
|
||||
}
|
||||
|
||||
func (v *ClauseQuery) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (v *ClauseQuery) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if v.Query == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -344,11 +399,13 @@ func (v *ClauseQuery) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
v.Query.serialize(statementType, out)
|
||||
}
|
||||
|
||||
// ClauseDelete struct
|
||||
type ClauseDelete struct {
|
||||
Table SerializerTable
|
||||
}
|
||||
|
||||
func (d *ClauseDelete) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (d *ClauseDelete) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
out.NewLine()
|
||||
out.WriteString("DELETE FROM")
|
||||
|
||||
|
|
@ -359,12 +416,14 @@ func (d *ClauseDelete) Serialize(statementType StatementType, out *SqlBuilder) {
|
|||
d.Table.serialize(statementType, out)
|
||||
}
|
||||
|
||||
// ClauseStatementBegin struct
|
||||
type ClauseStatementBegin struct {
|
||||
Name string
|
||||
Tables []SerializerTable
|
||||
}
|
||||
|
||||
func (d *ClauseStatementBegin) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (d *ClauseStatementBegin) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
out.NewLine()
|
||||
out.WriteString(d.Name)
|
||||
|
||||
|
|
@ -377,13 +436,15 @@ func (d *ClauseStatementBegin) Serialize(statementType StatementType, out *SqlBu
|
|||
}
|
||||
}
|
||||
|
||||
// ClauseOptional struct
|
||||
type ClauseOptional struct {
|
||||
Name string
|
||||
Show bool
|
||||
InNewLine bool
|
||||
}
|
||||
|
||||
func (d *ClauseOptional) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (d *ClauseOptional) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if !d.Show {
|
||||
return
|
||||
}
|
||||
|
|
@ -393,11 +454,13 @@ func (d *ClauseOptional) Serialize(statementType StatementType, out *SqlBuilder)
|
|||
out.WriteString(d.Name)
|
||||
}
|
||||
|
||||
// ClauseIn struct
|
||||
type ClauseIn struct {
|
||||
LockMode string
|
||||
}
|
||||
|
||||
func (i *ClauseIn) Serialize(statementType StatementType, out *SqlBuilder) {
|
||||
// Serialize serializes clause into SQLBuilder
|
||||
func (i *ClauseIn) Serialize(statementType StatementType, out *SQLBuilder) {
|
||||
if i.LockMode == "" {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,5 +12,5 @@ func TestClauseSelect_Serialize(t *testing.T) {
|
|||
}()
|
||||
|
||||
selectClause := &ClauseSelect{}
|
||||
selectClause.Serialize(SelectStatementType, &SqlBuilder{})
|
||||
selectClause.Serialize(SelectStatementType, &SQLBuilder{})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ type Column interface {
|
|||
defaultAlias() string
|
||||
}
|
||||
|
||||
// ColumnExpression interface
|
||||
type ColumnExpression interface {
|
||||
Column
|
||||
Expression
|
||||
|
|
@ -19,7 +20,7 @@ type ColumnExpression interface {
|
|||
|
||||
// The base type for real materialized columns.
|
||||
type columnImpl struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
|
||||
name string
|
||||
tableName string
|
||||
|
|
@ -33,7 +34,7 @@ func newColumn(name string, tableName string, parent ColumnExpression) columnImp
|
|||
tableName: tableName,
|
||||
}
|
||||
|
||||
bc.ExpressionInterfaceImpl.Parent = parent
|
||||
bc.expressionInterfaceImpl.Parent = parent
|
||||
|
||||
return bc
|
||||
}
|
||||
|
|
@ -62,7 +63,7 @@ func (c *columnImpl) defaultAlias() string {
|
|||
return c.name
|
||||
}
|
||||
|
||||
func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SqlBuilder) {
|
||||
func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SQLBuilder) {
|
||||
if statement == SetStatementType {
|
||||
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
|
||||
out.WriteAlias(c.defaultAlias()) //always quote
|
||||
|
|
@ -73,14 +74,14 @@ func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SqlBuilde
|
|||
c.serialize(statement, out)
|
||||
}
|
||||
|
||||
func (c columnImpl) serializeForProjection(statement StatementType, out *SqlBuilder) {
|
||||
func (c columnImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
|
||||
c.serialize(statement, out)
|
||||
|
||||
out.WriteString("AS")
|
||||
out.WriteAlias(c.defaultAlias())
|
||||
}
|
||||
|
||||
func (c columnImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (c columnImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
|
||||
if c.subQuery != nil {
|
||||
out.WriteIdentifier(c.subQuery.Alias())
|
||||
|
|
@ -129,7 +130,7 @@ func (cl columnListImpl) fromImpl(subQuery SelectTable) Projection {
|
|||
return newProjectionList
|
||||
}
|
||||
|
||||
func (cl columnListImpl) serializeForProjection(statement StatementType, out *SqlBuilder) {
|
||||
func (cl columnListImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
|
||||
projections := ColumnListToProjectionList(cl)
|
||||
|
||||
SerializeProjectionList(statement, projections, out)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import "testing"
|
|||
|
||||
func TestColumn(t *testing.T) {
|
||||
column := newColumn("col", "", nil)
|
||||
column.ExpressionInterfaceImpl.Parent = &column
|
||||
column.expressionInterfaceImpl.Parent = &column
|
||||
|
||||
assertClauseSerialize(t, column, "col")
|
||||
column.setTableName("table1")
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
var subQuery = &SelectTableImpl{
|
||||
var subQuery = &selectTableImpl{
|
||||
alias: "sub_query",
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package jet
|
||||
|
||||
// Dialect interface
|
||||
type Dialect interface {
|
||||
Name() string
|
||||
PackageName() string
|
||||
|
|
@ -10,10 +11,16 @@ type Dialect interface {
|
|||
ArgumentPlaceholder() QueryPlaceholderFunc
|
||||
}
|
||||
|
||||
type SerializeFunc func(statement StatementType, out *SqlBuilder, options ...SerializeOption)
|
||||
// SerializeFunc func
|
||||
type SerializeFunc func(statement StatementType, out *SQLBuilder, options ...SerializeOption)
|
||||
|
||||
// SerializeOverride func
|
||||
type SerializeOverride func(expressions ...Expression) SerializeFunc
|
||||
|
||||
// QueryPlaceholderFunc func
|
||||
type QueryPlaceholderFunc func(ord int) string
|
||||
|
||||
// DialectParams struct
|
||||
type DialectParams struct {
|
||||
Name string
|
||||
PackageName string
|
||||
|
|
@ -24,6 +31,7 @@ type DialectParams struct {
|
|||
ArgumentPlaceholder QueryPlaceholderFunc
|
||||
}
|
||||
|
||||
// NewDialect creates new dialect with params
|
||||
func NewDialect(params DialectParams) Dialect {
|
||||
return &dialectImpl{
|
||||
name: params.Name,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package jet
|
||||
|
||||
type enumValue struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
stringInterfaceImpl
|
||||
|
||||
name string
|
||||
|
|
@ -11,12 +11,12 @@ type enumValue struct {
|
|||
func NewEnumValue(name string) StringExpression {
|
||||
enumValue := &enumValue{name: name}
|
||||
|
||||
enumValue.ExpressionInterfaceImpl.Parent = enumValue
|
||||
enumValue.expressionInterfaceImpl.Parent = enumValue
|
||||
enumValue.stringInterfaceImpl.parent = enumValue
|
||||
|
||||
return enumValue
|
||||
}
|
||||
|
||||
func (e enumValue) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (e enumValue) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.insertConstantArgument(e.name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,51 +27,51 @@ type Expression interface {
|
|||
DESC() OrderByClause
|
||||
}
|
||||
|
||||
type ExpressionInterfaceImpl struct {
|
||||
type expressionInterfaceImpl struct {
|
||||
Parent Expression
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) fromImpl(subQuery SelectTable) Projection {
|
||||
func (e *expressionInterfaceImpl) fromImpl(subQuery SelectTable) Projection {
|
||||
return e.Parent
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) IS_NULL() BoolExpression {
|
||||
func (e *expressionInterfaceImpl) IS_NULL() BoolExpression {
|
||||
return newPostifxBoolExpression(e.Parent, "IS NULL")
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) IS_NOT_NULL() BoolExpression {
|
||||
func (e *expressionInterfaceImpl) IS_NOT_NULL() BoolExpression {
|
||||
return newPostifxBoolExpression(e.Parent, "IS NOT NULL")
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) IN(expressions ...Expression) BoolExpression {
|
||||
func (e *expressionInterfaceImpl) IN(expressions ...Expression) BoolExpression {
|
||||
return newBinaryBoolOperator(e.Parent, WRAP(expressions...), "IN")
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) NOT_IN(expressions ...Expression) BoolExpression {
|
||||
func (e *expressionInterfaceImpl) NOT_IN(expressions ...Expression) BoolExpression {
|
||||
return newBinaryBoolOperator(e.Parent, WRAP(expressions...), "NOT IN")
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) AS(alias string) Projection {
|
||||
func (e *expressionInterfaceImpl) AS(alias string) Projection {
|
||||
return newAlias(e.Parent, alias)
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) ASC() OrderByClause {
|
||||
func (e *expressionInterfaceImpl) ASC() OrderByClause {
|
||||
return newOrderByClause(e.Parent, true)
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) DESC() OrderByClause {
|
||||
func (e *expressionInterfaceImpl) DESC() OrderByClause {
|
||||
return newOrderByClause(e.Parent, false)
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) serializeForGroupBy(statement StatementType, out *SqlBuilder) {
|
||||
func (e *expressionInterfaceImpl) serializeForGroupBy(statement StatementType, out *SQLBuilder) {
|
||||
e.Parent.serialize(statement, out, noWrap)
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) serializeForProjection(statement StatementType, out *SqlBuilder) {
|
||||
func (e *expressionInterfaceImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
|
||||
e.Parent.serialize(statement, out, noWrap)
|
||||
}
|
||||
|
||||
func (e *ExpressionInterfaceImpl) serializeForOrderBy(statement StatementType, out *SqlBuilder) {
|
||||
func (e *expressionInterfaceImpl) serializeForOrderBy(statement StatementType, out *SQLBuilder) {
|
||||
e.Parent.serialize(statement, out, noWrap)
|
||||
}
|
||||
|
||||
|
|
@ -96,7 +96,7 @@ func newBinaryExpression(lhs, rhs Expression, operator string, additionalParam .
|
|||
return binaryExpression
|
||||
}
|
||||
|
||||
func (c *binaryOpExpression) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (c *binaryOpExpression) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if c.lhs == nil {
|
||||
panic("jet: lhs is nil for '" + c.operator + "' operator")
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ func newPrefixExpression(expression Expression, operator string) prefixOpExpress
|
|||
return prefixExpression
|
||||
}
|
||||
|
||||
func (p *prefixOpExpression) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (p *prefixOpExpression) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString("(")
|
||||
out.WriteString(p.operator)
|
||||
|
||||
|
|
@ -167,7 +167,7 @@ func newPostfixOpExpression(expression Expression, operator string) postfixOpExp
|
|||
return postfixOpExpression
|
||||
}
|
||||
|
||||
func (p *postfixOpExpression) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (p *postfixOpExpression) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if p.expression == nil {
|
||||
panic("jet: nil prefix expression in postfix operator " + p.operator)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ func (n *floatInterfaceImpl) POW(expression NumericExpression) FloatExpression {
|
|||
|
||||
//---------------------------------------------------//
|
||||
type binaryFloatExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
floatInterfaceImpl
|
||||
|
||||
binaryOpExpression
|
||||
|
|
@ -97,7 +97,7 @@ func newBinaryFloatExpression(lhs, rhs Expression, operator string) FloatExpress
|
|||
|
||||
floatExpression.binaryOpExpression = newBinaryExpression(lhs, rhs, operator)
|
||||
|
||||
floatExpression.ExpressionInterfaceImpl.Parent = &floatExpression
|
||||
floatExpression.expressionInterfaceImpl.Parent = &floatExpression
|
||||
floatExpression.floatInterfaceImpl.parent = &floatExpression
|
||||
|
||||
return &floatExpression
|
||||
|
|
|
|||
|
|
@ -482,7 +482,7 @@ func LEAST(value Expression, values ...Expression) Expression {
|
|||
//--------------------------------------------------------------------//
|
||||
|
||||
type funcExpressionImpl struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
|
||||
name string
|
||||
expressions []Expression
|
||||
|
|
@ -496,15 +496,15 @@ func newFunc(name string, expressions []Expression, parent Expression) *funcExpr
|
|||
}
|
||||
|
||||
if parent != nil {
|
||||
funcExp.ExpressionInterfaceImpl.Parent = parent
|
||||
funcExp.expressionInterfaceImpl.Parent = parent
|
||||
} else {
|
||||
funcExp.ExpressionInterfaceImpl.Parent = funcExp
|
||||
funcExp.expressionInterfaceImpl.Parent = funcExp
|
||||
}
|
||||
|
||||
return funcExp
|
||||
}
|
||||
|
||||
func (f *funcExpressionImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (f *funcExpressionImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if serializeOverride := out.Dialect.FunctionSerializeOverride(f.name); serializeOverride != nil {
|
||||
serializeOverrideFunc := serializeOverride(f.expressions...)
|
||||
serializeOverrideFunc(statement, out, options...)
|
||||
|
|
@ -545,6 +545,7 @@ type floatFunc struct {
|
|||
floatInterfaceImpl
|
||||
}
|
||||
|
||||
// NewFloatFunc creates new float function with name and expressions
|
||||
func NewFloatFunc(name string, expressions ...Expression) FloatExpression {
|
||||
floatFunc := &floatFunc{}
|
||||
|
||||
|
|
@ -629,6 +630,7 @@ type timestampFunc struct {
|
|||
timestampInterfaceImpl
|
||||
}
|
||||
|
||||
// NewTimestampFunc creates new timestamp function with name and expressions
|
||||
func NewTimestampFunc(name string, expressions ...Expression) *timestampFunc {
|
||||
timestampFunc := ×tampFunc{}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package jet
|
||||
|
||||
// GroupByClause interface
|
||||
type GroupByClause interface {
|
||||
serializeForGroupBy(statement StatementType, out *SqlBuilder)
|
||||
serializeForGroupBy(statement StatementType, out *SQLBuilder)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ func (i *integerInterfaceImpl) BIT_SHIFT_RIGHT(intExpression IntegerExpression)
|
|||
|
||||
//---------------------------------------------------//
|
||||
type binaryIntegerExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
integerInterfaceImpl
|
||||
|
||||
binaryOpExpression
|
||||
|
|
@ -140,7 +140,7 @@ type binaryIntegerExpression struct {
|
|||
func newBinaryIntegerExpression(lhs, rhs IntegerExpression, operator string) IntegerExpression {
|
||||
integerExpression := binaryIntegerExpression{}
|
||||
|
||||
integerExpression.ExpressionInterfaceImpl.Parent = &integerExpression
|
||||
integerExpression.expressionInterfaceImpl.Parent = &integerExpression
|
||||
integerExpression.integerInterfaceImpl.parent = &integerExpression
|
||||
|
||||
integerExpression.binaryOpExpression = newBinaryExpression(lhs, rhs, operator)
|
||||
|
|
@ -150,7 +150,7 @@ func newBinaryIntegerExpression(lhs, rhs IntegerExpression, operator string) Int
|
|||
|
||||
//---------------------------------------------------//
|
||||
type prefixIntegerOpExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
integerInterfaceImpl
|
||||
|
||||
prefixOpExpression
|
||||
|
|
@ -160,7 +160,7 @@ func newPrefixIntegerOperator(expression IntegerExpression, operator string) Int
|
|||
integerExpression := prefixIntegerOpExpression{}
|
||||
integerExpression.prefixOpExpression = newPrefixExpression(expression, operator)
|
||||
|
||||
integerExpression.ExpressionInterfaceImpl.Parent = &integerExpression
|
||||
integerExpression.expressionInterfaceImpl.Parent = &integerExpression
|
||||
integerExpression.integerInterfaceImpl.parent = &integerExpression
|
||||
|
||||
return &integerExpression
|
||||
|
|
@ -168,7 +168,7 @@ func newPrefixIntegerOperator(expression IntegerExpression, operator string) Int
|
|||
|
||||
//---------------------------------------------------//
|
||||
type prefixFloatOpExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
floatInterfaceImpl
|
||||
|
||||
prefixOpExpression
|
||||
|
|
@ -178,7 +178,7 @@ func newPrefixFloatOperator(expression FloatExpression, operator string) FloatEx
|
|||
floatOpExpression := prefixFloatOpExpression{}
|
||||
floatOpExpression.prefixOpExpression = newPrefixExpression(expression, operator)
|
||||
|
||||
floatOpExpression.ExpressionInterfaceImpl.Parent = &floatOpExpression
|
||||
floatOpExpression.expressionInterfaceImpl.Parent = &floatOpExpression
|
||||
floatOpExpression.floatInterfaceImpl.parent = &floatOpExpression
|
||||
|
||||
return &floatOpExpression
|
||||
|
|
|
|||
|
|
@ -14,6 +14,6 @@ var (
|
|||
|
||||
type keywordClause string
|
||||
|
||||
func (k keywordClause) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (k keywordClause) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString(string(k))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ type LiteralExpression interface {
|
|||
}
|
||||
|
||||
type literalExpressionImpl struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
|
||||
value interface{}
|
||||
constant bool
|
||||
|
|
@ -27,11 +27,12 @@ func literal(value interface{}, optionalConstant ...bool) *literalExpressionImpl
|
|||
exp.constant = optionalConstant[0]
|
||||
}
|
||||
|
||||
exp.ExpressionInterfaceImpl.Parent = &exp
|
||||
exp.expressionInterfaceImpl.Parent = &exp
|
||||
|
||||
return &exp
|
||||
}
|
||||
|
||||
// ConstLiteral is injected directly to SQL query, and does not appear in argument list.
|
||||
func ConstLiteral(value interface{}) *literalExpressionImpl {
|
||||
exp := literal(value)
|
||||
exp.constant = true
|
||||
|
|
@ -39,7 +40,7 @@ func ConstLiteral(value interface{}) *literalExpressionImpl {
|
|||
return exp
|
||||
}
|
||||
|
||||
func (l *literalExpressionImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (l *literalExpressionImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if l.constant {
|
||||
out.insertConstantArgument(l.value)
|
||||
} else {
|
||||
|
|
@ -272,46 +273,46 @@ func formatNanoseconds(nanoseconds ...time.Duration) string {
|
|||
|
||||
//--------------------------------------------------//
|
||||
type nullLiteral struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
}
|
||||
|
||||
func newNullLiteral() Expression {
|
||||
nullExpression := &nullLiteral{}
|
||||
|
||||
nullExpression.ExpressionInterfaceImpl.Parent = nullExpression
|
||||
nullExpression.expressionInterfaceImpl.Parent = nullExpression
|
||||
|
||||
return nullExpression
|
||||
}
|
||||
|
||||
func (n *nullLiteral) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (n *nullLiteral) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString("NULL")
|
||||
}
|
||||
|
||||
//--------------------------------------------------//
|
||||
type starLiteral struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
}
|
||||
|
||||
func newStarLiteral() Expression {
|
||||
starExpression := &starLiteral{}
|
||||
|
||||
starExpression.ExpressionInterfaceImpl.Parent = starExpression
|
||||
starExpression.expressionInterfaceImpl.Parent = starExpression
|
||||
|
||||
return starExpression
|
||||
}
|
||||
|
||||
func (n *starLiteral) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (n *starLiteral) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString("*")
|
||||
}
|
||||
|
||||
//---------------------------------------------------//
|
||||
|
||||
type wrap struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
expressions []Expression
|
||||
}
|
||||
|
||||
func (n *wrap) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (n *wrap) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString("(")
|
||||
serializeExpressionList(statement, n.expressions, ", ", out)
|
||||
out.WriteString(")")
|
||||
|
|
@ -320,7 +321,7 @@ func (n *wrap) serialize(statement StatementType, out *SqlBuilder, options ...Se
|
|||
// WRAP wraps list of expressions with brackets '(' and ')'
|
||||
func WRAP(expression ...Expression) Expression {
|
||||
wrap := &wrap{expressions: expression}
|
||||
wrap.ExpressionInterfaceImpl.Parent = wrap
|
||||
wrap.expressionInterfaceImpl.Parent = wrap
|
||||
|
||||
return wrap
|
||||
}
|
||||
|
|
@ -328,12 +329,12 @@ func WRAP(expression ...Expression) Expression {
|
|||
//---------------------------------------------------//
|
||||
|
||||
type rawExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
|
||||
raw string
|
||||
}
|
||||
|
||||
func (n *rawExpression) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (n *rawExpression) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString(n.raw)
|
||||
}
|
||||
|
||||
|
|
@ -341,7 +342,7 @@ func (n *rawExpression) serialize(statement StatementType, out *SqlBuilder, opti
|
|||
// For example: Raw("current_database()")
|
||||
func Raw(raw string) Expression {
|
||||
rawExp := &rawExpression{raw: raw}
|
||||
rawExp.ExpressionInterfaceImpl.Parent = rawExp
|
||||
rawExp.expressionInterfaceImpl.Parent = rawExp
|
||||
|
||||
return rawExp
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package jet
|
||||
|
||||
// Operators
|
||||
const (
|
||||
StringConcatOperator = "||"
|
||||
StringRegexpLikeOperator = "REGEXP"
|
||||
|
|
@ -78,7 +79,7 @@ type CaseOperator interface {
|
|||
}
|
||||
|
||||
type caseOperatorImpl struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
|
||||
expression Expression
|
||||
when []Expression
|
||||
|
|
@ -94,7 +95,7 @@ func CASE(expression ...Expression) CaseOperator {
|
|||
caseExp.expression = expression[0]
|
||||
}
|
||||
|
||||
caseExp.ExpressionInterfaceImpl.Parent = caseExp
|
||||
caseExp.expressionInterfaceImpl.Parent = caseExp
|
||||
|
||||
return caseExp
|
||||
}
|
||||
|
|
@ -115,7 +116,7 @@ func (c *caseOperatorImpl) ELSE(els Expression) CaseOperator {
|
|||
return c
|
||||
}
|
||||
|
||||
func (c *caseOperatorImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (c *caseOperatorImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString("(CASE")
|
||||
|
||||
if c.expression != nil {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package jet
|
||||
|
||||
// OrderByClause
|
||||
// OrderByClause interface
|
||||
type OrderByClause interface {
|
||||
serializeForOrderBy(statement StatementType, out *SqlBuilder)
|
||||
serializeForOrderBy(statement StatementType, out *SQLBuilder)
|
||||
}
|
||||
|
||||
type orderByClauseImpl struct {
|
||||
|
|
@ -10,7 +10,7 @@ type orderByClauseImpl struct {
|
|||
ascent bool
|
||||
}
|
||||
|
||||
func (o *orderByClauseImpl) serializeForOrderBy(statement StatementType, out *SqlBuilder) {
|
||||
func (o *orderByClauseImpl) serializeForOrderBy(statement StatementType, out *SQLBuilder) {
|
||||
if o.expression == nil {
|
||||
panic("jet: nil expression in ORDER BY clause")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ package jet
|
|||
|
||||
// Projection is interface for all projection types. Types that can be part of, for instance SELECT clause.
|
||||
type Projection interface {
|
||||
serializeForProjection(statement StatementType, out *SqlBuilder)
|
||||
serializeForProjection(statement StatementType, out *SQLBuilder)
|
||||
fromImpl(subQuery SelectTable) Projection
|
||||
}
|
||||
|
||||
func SerializeForProjection(projection Projection, statementType StatementType, out *SqlBuilder) {
|
||||
// SerializeForProjection is helper function for serializing projection outside of jet package
|
||||
func SerializeForProjection(projection Projection, statementType StatementType, out *SQLBuilder) {
|
||||
projection.serializeForProjection(statementType, out)
|
||||
}
|
||||
|
||||
|
|
@ -23,6 +24,6 @@ func (cl ProjectionList) fromImpl(subQuery SelectTable) Projection {
|
|||
return newProjectionList
|
||||
}
|
||||
|
||||
func (cl ProjectionList) serializeForProjection(statement StatementType, out *SqlBuilder) {
|
||||
func (cl ProjectionList) serializeForProjection(statement StatementType, out *SQLBuilder) {
|
||||
SerializeProjectionList(statement, cl, out)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ type selectLockImpl struct {
|
|||
noWait, skipLocked bool
|
||||
}
|
||||
|
||||
func NewSelectLock(name string) func() RowLock {
|
||||
// NewRowLock creates new RowLock
|
||||
func NewRowLock(name string) func() RowLock {
|
||||
return func() RowLock {
|
||||
return newSelectLock(name)
|
||||
}
|
||||
|
|
@ -33,7 +34,7 @@ func (s *selectLockImpl) SKIP_LOCKED() RowLock {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *selectLockImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (s *selectLockImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
out.WriteString(s.lockStrength)
|
||||
|
||||
if s.noWait {
|
||||
|
|
|
|||
|
|
@ -2,35 +2,37 @@ package jet
|
|||
|
||||
// SelectTable is interface for SELECT sub-queries
|
||||
type SelectTable interface {
|
||||
Serializer
|
||||
Alias() string
|
||||
AllColumns() ProjectionList
|
||||
}
|
||||
|
||||
type SelectTableImpl struct {
|
||||
type selectTableImpl struct {
|
||||
selectStmt StatementWithProjections
|
||||
alias string
|
||||
|
||||
projections ProjectionList
|
||||
}
|
||||
|
||||
func NewSelectTable(selectStmt StatementWithProjections, alias string) SelectTableImpl {
|
||||
selectTable := SelectTableImpl{selectStmt: selectStmt, alias: alias}
|
||||
// NewSelectTable func
|
||||
func NewSelectTable(selectStmt StatementWithProjections, alias string) SelectTable {
|
||||
selectTable := selectTableImpl{selectStmt: selectStmt, alias: alias}
|
||||
|
||||
projectionList := selectStmt.projections().fromImpl(&selectTable)
|
||||
selectTable.projections = projectionList.(ProjectionList)
|
||||
|
||||
return selectTable
|
||||
return &selectTable
|
||||
}
|
||||
|
||||
func (s *SelectTableImpl) Alias() string {
|
||||
func (s *selectTableImpl) Alias() string {
|
||||
return s.alias
|
||||
}
|
||||
|
||||
func (s *SelectTableImpl) AllColumns() ProjectionList {
|
||||
func (s *selectTableImpl) AllColumns() ProjectionList {
|
||||
return s.projections
|
||||
}
|
||||
|
||||
func (s *SelectTableImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (s *selectTableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if s == nil {
|
||||
panic("jet: expression table is nil. ")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,17 @@
|
|||
package jet
|
||||
|
||||
// SerializeOption type
|
||||
type SerializeOption int
|
||||
|
||||
// Serialize options
|
||||
const (
|
||||
noWrap SerializeOption = iota
|
||||
)
|
||||
|
||||
// StatementType is type of the SQL statement
|
||||
type StatementType string
|
||||
|
||||
// Statement types
|
||||
const (
|
||||
SelectStatementType StatementType = "SELECT"
|
||||
InsertStatementType StatementType = "INSERT"
|
||||
|
|
@ -18,11 +22,13 @@ const (
|
|||
UnLockStatementType StatementType = "UNLOCK"
|
||||
)
|
||||
|
||||
// Serializer interface
|
||||
type Serializer interface {
|
||||
serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption)
|
||||
serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption)
|
||||
}
|
||||
|
||||
func Serialize(exp Serializer, statementType StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
// Serialize func
|
||||
func Serialize(exp Serializer, statementType StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
exp.serialize(statementType, out, options...)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ import (
|
|||
"unicode"
|
||||
)
|
||||
|
||||
type SqlBuilder struct {
|
||||
// SQLBuilder generates output SQL
|
||||
type SQLBuilder struct {
|
||||
Dialect Dialect
|
||||
Buff bytes.Buffer
|
||||
Args []interface{}
|
||||
|
|
@ -23,7 +24,8 @@ type SqlBuilder struct {
|
|||
|
||||
const defaultIdent = 5
|
||||
|
||||
func (s *SqlBuilder) IncreaseIdent(ident ...int) {
|
||||
// IncreaseIdent adds ident or defaultIdent number of spaces to each new line
|
||||
func (s *SQLBuilder) IncreaseIdent(ident ...int) {
|
||||
if len(ident) > 0 {
|
||||
s.ident += ident[0]
|
||||
} else {
|
||||
|
|
@ -31,7 +33,8 @@ func (s *SqlBuilder) IncreaseIdent(ident ...int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) DecreaseIdent(ident ...int) {
|
||||
// DecreaseIdent removes ident or defaultIdent number of spaces for each new line
|
||||
func (s *SQLBuilder) DecreaseIdent(ident ...int) {
|
||||
toDecrease := defaultIdent
|
||||
|
||||
if len(ident) > 0 {
|
||||
|
|
@ -45,18 +48,20 @@ func (s *SqlBuilder) DecreaseIdent(ident ...int) {
|
|||
s.ident -= toDecrease
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) WriteProjections(statement StatementType, projections []Projection) {
|
||||
// WriteProjections func
|
||||
func (s *SQLBuilder) WriteProjections(statement StatementType, projections []Projection) {
|
||||
s.IncreaseIdent()
|
||||
SerializeProjectionList(statement, projections, s)
|
||||
s.DecreaseIdent()
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) NewLine() {
|
||||
// NewLine adds new line to output SQL
|
||||
func (s *SQLBuilder) NewLine() {
|
||||
s.write([]byte{'\n'})
|
||||
s.write(bytes.Repeat([]byte{' '}, s.ident))
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) write(data []byte) {
|
||||
func (s *SQLBuilder) write(data []byte) {
|
||||
if len(data) == 0 {
|
||||
return
|
||||
}
|
||||
|
|
@ -77,16 +82,19 @@ func isPostSeparator(b byte) bool {
|
|||
return b == ' ' || b == '.' || b == ',' || b == ')' || b == '\n' || b == ':'
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) WriteAlias(str string) {
|
||||
// WriteAlias is used to add alias to output SQL
|
||||
func (s *SQLBuilder) WriteAlias(str string) {
|
||||
aliasQuoteChar := string(s.Dialect.AliasQuoteChar())
|
||||
s.WriteString(aliasQuoteChar + str + aliasQuoteChar)
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) WriteString(str string) {
|
||||
// WriteString writes sting to output SQL
|
||||
func (s *SQLBuilder) WriteString(str string) {
|
||||
s.write([]byte(str))
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) WriteIdentifier(name string, alwaysQuote ...bool) {
|
||||
// WriteIdentifier adds identifier to output SQL
|
||||
func (s *SQLBuilder) WriteIdentifier(name string, alwaysQuote ...bool) {
|
||||
if shouldQuoteIdentifier(name) || len(alwaysQuote) > 0 {
|
||||
identQuoteChar := string(s.Dialect.IdentifierQuoteChar())
|
||||
s.WriteString(identQuoteChar + name + identQuoteChar)
|
||||
|
|
@ -95,19 +103,20 @@ func (s *SqlBuilder) WriteIdentifier(name string, alwaysQuote ...bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) WriteByte(b byte) {
|
||||
// WriteByte writes byte to output SQL
|
||||
func (s *SQLBuilder) WriteByte(b byte) {
|
||||
s.write([]byte{b})
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) finalize() (string, []interface{}) {
|
||||
func (s *SQLBuilder) finalize() (string, []interface{}) {
|
||||
return s.Buff.String() + ";\n", s.Args
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) insertConstantArgument(arg interface{}) {
|
||||
func (s *SQLBuilder) insertConstantArgument(arg interface{}) {
|
||||
s.WriteString(argToString(arg))
|
||||
}
|
||||
|
||||
func (s *SqlBuilder) insertParametrizedArgument(arg interface{}) {
|
||||
func (s *SQLBuilder) insertParametrizedArgument(arg interface{}) {
|
||||
if s.debug {
|
||||
s.insertConstantArgument(arg)
|
||||
return
|
||||
|
|
@ -139,7 +148,7 @@ func argToString(value interface{}) string {
|
|||
case int32:
|
||||
return strconv.FormatInt(int64(bindVal), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(int64(bindVal), 10)
|
||||
return strconv.FormatInt(bindVal, 10)
|
||||
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(bindVal), 10)
|
||||
|
|
|
|||
|
|
@ -27,30 +27,34 @@ type Statement interface {
|
|||
ExecContext(context context.Context, db execution.DB) (sql.Result, error)
|
||||
}
|
||||
|
||||
// SerializerStatement interface
|
||||
type SerializerStatement interface {
|
||||
Serializer
|
||||
Statement
|
||||
}
|
||||
|
||||
// StatementWithProjections interface
|
||||
type StatementWithProjections interface {
|
||||
Statement
|
||||
HasProjections
|
||||
Serializer
|
||||
}
|
||||
|
||||
// HasProjections interface
|
||||
type HasProjections interface {
|
||||
projections() ProjectionList
|
||||
}
|
||||
|
||||
type SerializerStatementInterfaceImpl struct {
|
||||
// serializerStatementInterfaceImpl struct
|
||||
type serializerStatementInterfaceImpl struct {
|
||||
dialect Dialect
|
||||
statementType StatementType
|
||||
parent SerializerStatement
|
||||
}
|
||||
|
||||
func (s *SerializerStatementInterfaceImpl) Sql() (query string, args []interface{}) {
|
||||
func (s *serializerStatementInterfaceImpl) Sql() (query string, args []interface{}) {
|
||||
|
||||
queryData := &SqlBuilder{Dialect: s.dialect}
|
||||
queryData := &SQLBuilder{Dialect: s.dialect}
|
||||
|
||||
s.parent.serialize(s.statementType, queryData, noWrap)
|
||||
|
||||
|
|
@ -58,8 +62,8 @@ func (s *SerializerStatementInterfaceImpl) Sql() (query string, args []interface
|
|||
return
|
||||
}
|
||||
|
||||
func (s *SerializerStatementInterfaceImpl) DebugSql() (query string) {
|
||||
sqlBuilder := &SqlBuilder{Dialect: s.dialect, debug: true}
|
||||
func (s *serializerStatementInterfaceImpl) DebugSql() (query string) {
|
||||
sqlBuilder := &SQLBuilder{Dialect: s.dialect, debug: true}
|
||||
|
||||
s.parent.serialize(s.statementType, sqlBuilder, noWrap)
|
||||
|
||||
|
|
@ -67,41 +71,64 @@ func (s *SerializerStatementInterfaceImpl) DebugSql() (query string) {
|
|||
return
|
||||
}
|
||||
|
||||
func (s *SerializerStatementInterfaceImpl) Query(db execution.DB, destination interface{}) error {
|
||||
func (s *serializerStatementInterfaceImpl) Query(db execution.DB, destination interface{}) error {
|
||||
query, args := s.Sql()
|
||||
|
||||
return execution.Query(context.Background(), db, query, args, destination)
|
||||
}
|
||||
|
||||
func (s *SerializerStatementInterfaceImpl) QueryContext(context context.Context, db execution.DB, destination interface{}) error {
|
||||
func (s *serializerStatementInterfaceImpl) QueryContext(context context.Context, db execution.DB, destination interface{}) error {
|
||||
query, args := s.Sql()
|
||||
|
||||
return execution.Query(context, db, query, args, destination)
|
||||
}
|
||||
|
||||
func (s *SerializerStatementInterfaceImpl) Exec(db execution.DB) (res sql.Result, err error) {
|
||||
func (s *serializerStatementInterfaceImpl) Exec(db execution.DB) (res sql.Result, err error) {
|
||||
query, args := s.Sql()
|
||||
return db.Exec(query, args...)
|
||||
}
|
||||
|
||||
func (s *SerializerStatementInterfaceImpl) ExecContext(context context.Context, db execution.DB) (res sql.Result, err error) {
|
||||
func (s *serializerStatementInterfaceImpl) ExecContext(context context.Context, db execution.DB) (res sql.Result, err error) {
|
||||
query, args := s.Sql()
|
||||
|
||||
return db.ExecContext(context, query, args...)
|
||||
}
|
||||
|
||||
type ExpressionStatementImpl struct {
|
||||
ExpressionInterfaceImpl
|
||||
StatementImpl
|
||||
// ExpressionStatement interfacess
|
||||
type ExpressionStatement interface {
|
||||
Expression
|
||||
Statement
|
||||
HasProjections
|
||||
}
|
||||
|
||||
func (s *ExpressionStatementImpl) serializeForProjection(statement StatementType, out *SqlBuilder) {
|
||||
// NewExpressionStatementImpl creates new expression statement
|
||||
func NewExpressionStatementImpl(Dialect Dialect, statementType StatementType, parent ExpressionStatement, clauses ...Clause) ExpressionStatement {
|
||||
return &expressionStatementImpl{
|
||||
expressionInterfaceImpl{Parent: parent},
|
||||
statementImpl{
|
||||
serializerStatementInterfaceImpl: serializerStatementInterfaceImpl{
|
||||
parent: parent,
|
||||
dialect: Dialect,
|
||||
statementType: statementType,
|
||||
},
|
||||
Clauses: clauses,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type expressionStatementImpl struct {
|
||||
expressionInterfaceImpl
|
||||
statementImpl
|
||||
}
|
||||
|
||||
func (s *expressionStatementImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
|
||||
s.serialize(statement, out)
|
||||
}
|
||||
|
||||
func NewStatementImpl(Dialect Dialect, statementType StatementType, parent SerializerStatement, clauses ...Clause) StatementImpl {
|
||||
return StatementImpl{
|
||||
SerializerStatementInterfaceImpl: SerializerStatementInterfaceImpl{
|
||||
// NewStatementImpl creates new statementImpl
|
||||
func NewStatementImpl(Dialect Dialect, statementType StatementType, parent SerializerStatement, clauses ...Clause) SerializerStatement {
|
||||
return &statementImpl{
|
||||
serializerStatementInterfaceImpl: serializerStatementInterfaceImpl{
|
||||
parent: parent,
|
||||
dialect: Dialect,
|
||||
statementType: statementType,
|
||||
|
|
@ -110,13 +137,13 @@ func NewStatementImpl(Dialect Dialect, statementType StatementType, parent Seria
|
|||
}
|
||||
}
|
||||
|
||||
type StatementImpl struct {
|
||||
SerializerStatementInterfaceImpl
|
||||
type statementImpl struct {
|
||||
serializerStatementInterfaceImpl
|
||||
|
||||
Clauses []Clause
|
||||
}
|
||||
|
||||
func (s *StatementImpl) projections() ProjectionList {
|
||||
func (s *statementImpl) projections() ProjectionList {
|
||||
for _, clause := range s.Clauses {
|
||||
if selectClause, ok := clause.(ClauseWithProjections); ok {
|
||||
return selectClause.projections()
|
||||
|
|
@ -126,7 +153,7 @@ func (s *StatementImpl) projections() ProjectionList {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *StatementImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (s *statementImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
|
||||
if !contains(options, noWrap) {
|
||||
out.WriteString("(")
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ func (s *stringInterfaceImpl) NOT_REGEXP_LIKE(pattern StringExpression, caseSens
|
|||
//---------------------------------------------------//
|
||||
|
||||
type binaryStringExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
stringInterfaceImpl
|
||||
|
||||
binaryOpExpression
|
||||
|
|
@ -92,7 +92,7 @@ func newBinaryStringExpression(lhs, rhs Expression, operator string) StringExpre
|
|||
boolExpression := binaryStringExpression{}
|
||||
|
||||
boolExpression.binaryOpExpression = newBinaryExpression(lhs, rhs, operator)
|
||||
boolExpression.ExpressionInterfaceImpl.Parent = &boolExpression
|
||||
boolExpression.expressionInterfaceImpl.Parent = &boolExpression
|
||||
boolExpression.stringInterfaceImpl.parent = &boolExpression
|
||||
|
||||
return &boolExpression
|
||||
|
|
|
|||
|
|
@ -4,11 +4,13 @@ import (
|
|||
"github.com/go-jet/jet/internal/utils"
|
||||
)
|
||||
|
||||
// SerializerTable interface
|
||||
type SerializerTable interface {
|
||||
Serializer
|
||||
Table
|
||||
}
|
||||
|
||||
// Table interface
|
||||
type Table interface {
|
||||
columns() []Column
|
||||
SchemaName() string
|
||||
|
|
@ -17,9 +19,9 @@ type Table interface {
|
|||
}
|
||||
|
||||
// NewTable creates new table with schema Name, table Name and list of columns
|
||||
func NewTable(schemaName, name string, columns ...ColumnExpression) TableImpl {
|
||||
func NewTable(schemaName, name string, columns ...ColumnExpression) SerializerTable {
|
||||
|
||||
t := TableImpl{
|
||||
t := tableImpl{
|
||||
schemaName: schemaName,
|
||||
name: name,
|
||||
columnList: columns,
|
||||
|
|
@ -29,17 +31,17 @@ func NewTable(schemaName, name string, columns ...ColumnExpression) TableImpl {
|
|||
c.setTableName(name)
|
||||
}
|
||||
|
||||
return t
|
||||
return &t
|
||||
}
|
||||
|
||||
type TableImpl struct {
|
||||
type tableImpl struct {
|
||||
schemaName string
|
||||
name string
|
||||
alias string
|
||||
columnList []ColumnExpression
|
||||
}
|
||||
|
||||
func (t *TableImpl) AS(alias string) {
|
||||
func (t *tableImpl) AS(alias string) {
|
||||
t.alias = alias
|
||||
|
||||
for _, c := range t.columnList {
|
||||
|
|
@ -47,15 +49,15 @@ func (t *TableImpl) AS(alias string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (t *TableImpl) SchemaName() string {
|
||||
func (t *tableImpl) SchemaName() string {
|
||||
return t.schemaName
|
||||
}
|
||||
|
||||
func (t *TableImpl) TableName() string {
|
||||
func (t *tableImpl) TableName() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (t *TableImpl) columns() []Column {
|
||||
func (t *tableImpl) columns() []Column {
|
||||
ret := []Column{}
|
||||
|
||||
for _, col := range t.columnList {
|
||||
|
|
@ -65,7 +67,7 @@ func (t *TableImpl) columns() []Column {
|
|||
return ret
|
||||
}
|
||||
|
||||
func (t *TableImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (t *tableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if t == nil {
|
||||
panic("jet: tableImpl is nil")
|
||||
}
|
||||
|
|
@ -80,8 +82,10 @@ func (t *TableImpl) serialize(statement StatementType, out *SqlBuilder, options
|
|||
}
|
||||
}
|
||||
|
||||
// JoinType is type of table join
|
||||
type JoinType int
|
||||
|
||||
// Table join types
|
||||
const (
|
||||
InnerJoin JoinType = iota
|
||||
LeftJoin
|
||||
|
|
@ -91,37 +95,44 @@ const (
|
|||
)
|
||||
|
||||
// Join expressions are pseudo readable tables.
|
||||
type JoinTableImpl struct {
|
||||
type joinTableImpl struct {
|
||||
lhs Serializer
|
||||
rhs Serializer
|
||||
joinType JoinType
|
||||
onCondition BoolExpression
|
||||
}
|
||||
|
||||
func NewJoinTableImpl(lhs Serializer, rhs Serializer, joinType JoinType, onCondition BoolExpression) JoinTableImpl {
|
||||
// JoinTable interface
|
||||
type JoinTable SerializerTable
|
||||
|
||||
joinTable := JoinTableImpl{
|
||||
// NewJoinTable creates new join table
|
||||
func NewJoinTable(lhs Serializer, rhs Serializer, joinType JoinType, onCondition BoolExpression) JoinTable {
|
||||
|
||||
joinTable := joinTableImpl{
|
||||
lhs: lhs,
|
||||
rhs: rhs,
|
||||
joinType: joinType,
|
||||
onCondition: onCondition,
|
||||
}
|
||||
|
||||
return joinTable
|
||||
return &joinTable
|
||||
}
|
||||
|
||||
func (t *JoinTableImpl) SchemaName() string {
|
||||
func (t *joinTableImpl) SchemaName() string {
|
||||
if table, ok := t.lhs.(Table); ok {
|
||||
return table.SchemaName()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *JoinTableImpl) TableName() string {
|
||||
func (t *joinTableImpl) TableName() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *JoinTableImpl) Columns() []Column {
|
||||
func (t *joinTableImpl) AS(alias string) {
|
||||
}
|
||||
|
||||
func (t *joinTableImpl) columns() []Column {
|
||||
var ret []Column
|
||||
|
||||
if lhsTable, ok := t.lhs.(Table); ok {
|
||||
|
|
@ -134,7 +145,7 @@ func (t *JoinTableImpl) Columns() []Column {
|
|||
return ret
|
||||
}
|
||||
|
||||
func (t *JoinTableImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) {
|
||||
func (t *joinTableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
||||
if t == nil {
|
||||
panic("jet: Join table is nil. ")
|
||||
}
|
||||
|
|
@ -175,35 +186,3 @@ func (t *JoinTableImpl) serialize(statement StatementType, out *SqlBuilder, opti
|
|||
t.onCondition.serialize(statement, out)
|
||||
}
|
||||
}
|
||||
|
||||
func UnwindColumns(column1 Column, columns ...Column) []Column {
|
||||
columnList := []Column{}
|
||||
|
||||
if val, ok := column1.(IColumnList); ok {
|
||||
for _, col := range val.columns() {
|
||||
columnList = append(columnList, col)
|
||||
}
|
||||
columnList = append(columnList, columns...)
|
||||
} else {
|
||||
columnList = append(columnList, column1)
|
||||
columnList = append(columnList, columns...)
|
||||
}
|
||||
|
||||
return columnList
|
||||
}
|
||||
|
||||
func UnwidColumnList(columns []Column) []Column {
|
||||
ret := []Column{}
|
||||
|
||||
for _, col := range columns {
|
||||
if columnList, ok := col.(IColumnList); ok {
|
||||
for _, c := range columnList.columns() {
|
||||
ret = append(ret, c)
|
||||
}
|
||||
} else {
|
||||
ret = append(ret, col)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
var DefaultDialect = NewDialect(DialectParams{ // just for tests
|
||||
var defaultDialect = NewDialect(DialectParams{ // just for tests
|
||||
AliasQuoteChar: '"',
|
||||
IdentifierQuoteChar: '"',
|
||||
ArgumentPlaceholder: func(ord int) string {
|
||||
|
|
@ -47,7 +47,7 @@ var table3StrCol = StringColumn("col2")
|
|||
var table3 = NewTable("db", "table3", table3Col1, table3ColInt, table3StrCol)
|
||||
|
||||
func assertClauseSerialize(t *testing.T, clause Serializer, query string, args ...interface{}) {
|
||||
out := SqlBuilder{Dialect: DefaultDialect}
|
||||
out := SQLBuilder{Dialect: defaultDialect}
|
||||
clause.serialize(SelectStatementType, &out)
|
||||
|
||||
//fmt.Println(out.Buff.String())
|
||||
|
|
@ -62,12 +62,12 @@ func assertClauseSerializeErr(t *testing.T, clause Serializer, errString string)
|
|||
assert.Equal(t, r, errString)
|
||||
}()
|
||||
|
||||
out := SqlBuilder{Dialect: DefaultDialect}
|
||||
out := SQLBuilder{Dialect: defaultDialect}
|
||||
clause.serialize(SelectStatementType, &out)
|
||||
}
|
||||
|
||||
func assertClauseDebugSerialize(t *testing.T, clause Serializer, query string, args ...interface{}) {
|
||||
out := SqlBuilder{Dialect: DefaultDialect, debug: true}
|
||||
out := SQLBuilder{Dialect: defaultDialect, debug: true}
|
||||
clause.serialize(SelectStatementType, &out)
|
||||
|
||||
//fmt.Println(out.Buff.String())
|
||||
|
|
@ -77,7 +77,7 @@ func assertClauseDebugSerialize(t *testing.T, clause Serializer, query string, a
|
|||
}
|
||||
|
||||
func assertProjectionSerialize(t *testing.T, projection Projection, query string, args ...interface{}) {
|
||||
out := SqlBuilder{Dialect: DefaultDialect}
|
||||
out := SQLBuilder{Dialect: defaultDialect}
|
||||
projection.serializeForProjection(SelectStatementType, &out)
|
||||
|
||||
assert.DeepEqual(t, out.Buff.String(), query)
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ func (t *timeInterfaceImpl) GT_EQ(rhs TimeExpression) BoolExpression {
|
|||
|
||||
//---------------------------------------------------//
|
||||
type prefixTimeExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
timeInterfaceImpl
|
||||
|
||||
prefixOpExpression
|
||||
|
|
@ -63,7 +63,7 @@ type prefixTimeExpression struct {
|
|||
// timeExpr := prefixTimeExpression{}
|
||||
// timeExpr.prefixOpExpression = newPrefixExpression(expression, operator)
|
||||
//
|
||||
// timeExpr.ExpressionInterfaceImpl.parent = &timeExpr
|
||||
// timeExpr.expressionInterfaceImpl.parent = &timeExpr
|
||||
// timeExpr.timeInterfaceImpl.parent = &timeExpr
|
||||
//
|
||||
// return &timeExpr
|
||||
|
|
|
|||
|
|
@ -54,22 +54,12 @@ func (t *timestampzInterfaceImpl) GT_EQ(rhs TimestampzExpression) BoolExpression
|
|||
//---------------------------------------------------//
|
||||
|
||||
type prefixTimestampzOperator struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
timestampzInterfaceImpl
|
||||
|
||||
prefixOpExpression
|
||||
}
|
||||
|
||||
func NewPrefixTimestampOperator(operator string, expression Expression) TimestampzExpression {
|
||||
timeExpr := prefixTimestampzOperator{}
|
||||
timeExpr.prefixOpExpression = newPrefixExpression(expression, operator)
|
||||
|
||||
timeExpr.ExpressionInterfaceImpl.Parent = &timeExpr
|
||||
timeExpr.timestampzInterfaceImpl.parent = &timeExpr
|
||||
|
||||
return &timeExpr
|
||||
}
|
||||
|
||||
//-------------------------------------------------
|
||||
|
||||
type timestampzExpressionWrapper struct {
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ func (t *timezInterfaceImpl) GT_EQ(rhs TimezExpression) BoolExpression {
|
|||
|
||||
//---------------------------------------------------//
|
||||
type prefixTimezExpression struct {
|
||||
ExpressionInterfaceImpl
|
||||
expressionInterfaceImpl
|
||||
timezInterfaceImpl
|
||||
|
||||
prefixOpExpression
|
||||
|
|
@ -71,7 +71,7 @@ type prefixTimezExpression struct {
|
|||
// timeExpr := prefixTimezExpression{}
|
||||
// timeExpr.prefixOpExpression = newPrefixExpression(expression, operator)
|
||||
//
|
||||
// timeExpr.ExpressionInterfaceImpl.parent = &timeExpr
|
||||
// timeExpr.expressionInterfaceImpl.parent = &timeExpr
|
||||
// timeExpr.timezInterfaceImpl.parent = &timeExpr
|
||||
//
|
||||
// return &timeExpr
|
||||
|
|
|
|||
|
|
@ -5,33 +5,8 @@ import (
|
|||
"reflect"
|
||||
)
|
||||
|
||||
func serializeOrderByClauseList(statement StatementType, orderByClauses []OrderByClause, out *SqlBuilder) {
|
||||
|
||||
for i, value := range orderByClauses {
|
||||
if i > 0 {
|
||||
out.WriteString(", ")
|
||||
}
|
||||
|
||||
value.serializeForOrderBy(statement, out)
|
||||
}
|
||||
}
|
||||
|
||||
func serializeGroupByClauseList(statement StatementType, clauses []GroupByClause, out *SqlBuilder) {
|
||||
|
||||
for i, c := range clauses {
|
||||
if i > 0 {
|
||||
out.WriteString(", ")
|
||||
}
|
||||
|
||||
if c == nil {
|
||||
panic("jet: nil clause")
|
||||
}
|
||||
|
||||
c.serializeForGroupBy(statement, out)
|
||||
}
|
||||
}
|
||||
|
||||
func SerializeClauseList(statement StatementType, clauses []Serializer, out *SqlBuilder) {
|
||||
// SerializeClauseList func
|
||||
func SerializeClauseList(statement StatementType, clauses []Serializer, out *SQLBuilder) {
|
||||
|
||||
for i, c := range clauses {
|
||||
if i > 0 {
|
||||
|
|
@ -46,7 +21,7 @@ func SerializeClauseList(statement StatementType, clauses []Serializer, out *Sql
|
|||
}
|
||||
}
|
||||
|
||||
func serializeExpressionList(statement StatementType, expressions []Expression, separator string, out *SqlBuilder) {
|
||||
func serializeExpressionList(statement StatementType, expressions []Expression, separator string, out *SQLBuilder) {
|
||||
|
||||
for i, value := range expressions {
|
||||
if i > 0 {
|
||||
|
|
@ -57,7 +32,8 @@ func serializeExpressionList(statement StatementType, expressions []Expression,
|
|||
}
|
||||
}
|
||||
|
||||
func SerializeProjectionList(statement StatementType, projections []Projection, out *SqlBuilder) {
|
||||
// SerializeProjectionList func
|
||||
func SerializeProjectionList(statement StatementType, projections []Projection, out *SQLBuilder) {
|
||||
for i, col := range projections {
|
||||
if i > 0 {
|
||||
out.WriteString(",")
|
||||
|
|
@ -72,7 +48,8 @@ func SerializeProjectionList(statement StatementType, projections []Projection,
|
|||
}
|
||||
}
|
||||
|
||||
func SerializeColumnNames(columns []Column, out *SqlBuilder) {
|
||||
// SerializeColumnNames func
|
||||
func SerializeColumnNames(columns []Column, out *SQLBuilder) {
|
||||
for i, col := range columns {
|
||||
if i > 0 {
|
||||
out.WriteString(", ")
|
||||
|
|
@ -86,6 +63,7 @@ func SerializeColumnNames(columns []Column, out *SqlBuilder) {
|
|||
}
|
||||
}
|
||||
|
||||
// ColumnListToProjectionList func
|
||||
func ColumnListToProjectionList(columns []ColumnExpression) []Projection {
|
||||
var ret []Projection
|
||||
|
||||
|
|
@ -104,6 +82,7 @@ func valueToClause(value interface{}) Serializer {
|
|||
return literal(value)
|
||||
}
|
||||
|
||||
// UnwindRowFromModel func
|
||||
func UnwindRowFromModel(columns []Column, data interface{}) []Serializer {
|
||||
structValue := reflect.Indirect(reflect.ValueOf(data))
|
||||
|
||||
|
|
@ -135,6 +114,7 @@ func UnwindRowFromModel(columns []Column, data interface{}) []Serializer {
|
|||
return row
|
||||
}
|
||||
|
||||
// UnwindRowsFromModels func
|
||||
func UnwindRowsFromModels(columns []Column, data interface{}) [][]Serializer {
|
||||
sliceValue := reflect.Indirect(reflect.ValueOf(data))
|
||||
utils.ValueMustBe(sliceValue, reflect.Slice, "jet: data has to be a slice.")
|
||||
|
|
@ -150,6 +130,7 @@ func UnwindRowsFromModels(columns []Column, data interface{}) [][]Serializer {
|
|||
return rows
|
||||
}
|
||||
|
||||
// UnwindRowFromValues func
|
||||
func UnwindRowFromValues(value interface{}, values []interface{}) []Serializer {
|
||||
row := []Serializer{}
|
||||
|
||||
|
|
@ -161,3 +142,37 @@ func UnwindRowFromValues(value interface{}, values []interface{}) []Serializer {
|
|||
|
||||
return row
|
||||
}
|
||||
|
||||
// UnwindColumns func
|
||||
func UnwindColumns(column1 Column, columns ...Column) []Column {
|
||||
columnList := []Column{}
|
||||
|
||||
if val, ok := column1.(IColumnList); ok {
|
||||
for _, col := range val.columns() {
|
||||
columnList = append(columnList, col)
|
||||
}
|
||||
columnList = append(columnList, columns...)
|
||||
} else {
|
||||
columnList = append(columnList, column1)
|
||||
columnList = append(columnList, columns...)
|
||||
}
|
||||
|
||||
return columnList
|
||||
}
|
||||
|
||||
// UnwidColumnList func
|
||||
func UnwidColumnList(columns []Column) []Column {
|
||||
ret := []Column{}
|
||||
|
||||
for _, col := range columns {
|
||||
if columnList, ok := col.(IColumnList); ok {
|
||||
for _, c := range columnList.columns() {
|
||||
ret = append(ret, c)
|
||||
}
|
||||
} else {
|
||||
ret = append(ret, col)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
// AssertExec assert statement execution for successful execution and number of rows affected
|
||||
func AssertExec(t *testing.T, stmt jet.Statement, db execution.DB, rowsAffected ...int64) {
|
||||
res, err := stmt.Exec(db)
|
||||
|
||||
|
|
@ -26,6 +27,7 @@ func AssertExec(t *testing.T, stmt jet.Statement, db execution.DB, rowsAffected
|
|||
}
|
||||
}
|
||||
|
||||
// AssertExecErr assert statement execution for failed execution with error string errorStr
|
||||
func AssertExecErr(t *testing.T, stmt jet.Statement, db execution.DB, errorStr string) {
|
||||
_, err := stmt.Exec(db)
|
||||
|
||||
|
|
@ -37,11 +39,13 @@ func getFullPath(relativePath string) string {
|
|||
return filepath.Join(goPath, "src/github.com/go-jet/jet/tests", relativePath)
|
||||
}
|
||||
|
||||
// PrintJson print v as json
|
||||
func PrintJson(v interface{}) {
|
||||
jsonText, _ := json.MarshalIndent(v, "", "\t")
|
||||
fmt.Println(string(jsonText))
|
||||
}
|
||||
|
||||
// AssertJSON check if data json output is the same as expectedJSON
|
||||
func AssertJSON(t *testing.T, data interface{}, expectedJSON string) {
|
||||
jsonData, err := json.MarshalIndent(data, "", "\t")
|
||||
assert.NilError(t, err)
|
||||
|
|
@ -49,7 +53,8 @@ func AssertJSON(t *testing.T, data interface{}, expectedJSON string) {
|
|||
assert.Equal(t, "\n"+string(jsonData)+"\n", expectedJSON)
|
||||
}
|
||||
|
||||
func SaveJsonFile(v interface{}, testRelativePath string) {
|
||||
// SaveJSONFile saves v as json at testRelativePath
|
||||
func SaveJSONFile(v interface{}, testRelativePath string) {
|
||||
jsonText, _ := json.MarshalIndent(v, "", "\t")
|
||||
|
||||
filePath := getFullPath(testRelativePath)
|
||||
|
|
@ -60,6 +65,7 @@ func SaveJsonFile(v interface{}, testRelativePath string) {
|
|||
}
|
||||
}
|
||||
|
||||
// AssertJSONFile check if data json representation is the same as json at testRelativePath
|
||||
func AssertJSONFile(t *testing.T, data interface{}, testRelativePath string) {
|
||||
|
||||
filePath := getFullPath(testRelativePath)
|
||||
|
|
@ -77,6 +83,7 @@ func AssertJSONFile(t *testing.T, data interface{}, testRelativePath string) {
|
|||
//assert.DeepEqual(t, string(fileJSONData), string(jsonData))
|
||||
}
|
||||
|
||||
// AssertStatementSql check if statement Sql() is the same as expectedQuery and expectedArgs
|
||||
func AssertStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) {
|
||||
queryStr, args := query.Sql()
|
||||
assert.Equal(t, queryStr, expectedQuery)
|
||||
|
|
@ -87,6 +94,7 @@ func AssertStatementSql(t *testing.T, query jet.Statement, expectedQuery string,
|
|||
assert.DeepEqual(t, args, expectedArgs)
|
||||
}
|
||||
|
||||
// AssertStatementSqlErr checks if statement Sql() panics with errorStr
|
||||
func AssertStatementSqlErr(t *testing.T, stmt jet.Statement, errorStr string) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
|
|
@ -96,6 +104,7 @@ func AssertStatementSqlErr(t *testing.T, stmt jet.Statement, errorStr string) {
|
|||
stmt.Sql()
|
||||
}
|
||||
|
||||
// AssertDebugStatementSql check if statement Sql() is the same as expectedQuery
|
||||
func AssertDebugStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) {
|
||||
_, args := query.Sql()
|
||||
|
||||
|
|
@ -107,8 +116,9 @@ func AssertDebugStatementSql(t *testing.T, query jet.Statement, expectedQuery st
|
|||
assert.Equal(t, debuqSql, expectedQuery)
|
||||
}
|
||||
|
||||
// AssertClauseSerialize checks if clause serialize produces expected query and args
|
||||
func AssertClauseSerialize(t *testing.T, dialect jet.Dialect, clause jet.Serializer, query string, args ...interface{}) {
|
||||
out := jet.SqlBuilder{Dialect: dialect}
|
||||
out := jet.SQLBuilder{Dialect: dialect}
|
||||
jet.Serialize(clause, jet.SelectStatementType, &out)
|
||||
|
||||
//fmt.Println(out.Buff.String())
|
||||
|
|
@ -120,24 +130,27 @@ func AssertClauseSerialize(t *testing.T, dialect jet.Dialect, clause jet.Seriali
|
|||
}
|
||||
}
|
||||
|
||||
// AssertClauseSerializeErr check if clause serialize panics with errString
|
||||
func AssertClauseSerializeErr(t *testing.T, dialect jet.Dialect, clause jet.Serializer, errString string) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
assert.Equal(t, r, errString)
|
||||
}()
|
||||
|
||||
out := jet.SqlBuilder{Dialect: dialect}
|
||||
out := jet.SQLBuilder{Dialect: dialect}
|
||||
jet.Serialize(clause, jet.SelectStatementType, &out)
|
||||
}
|
||||
|
||||
// AssertProjectionSerialize check if projection serialize produces expected query and args
|
||||
func AssertProjectionSerialize(t *testing.T, dialect jet.Dialect, projection jet.Projection, query string, args ...interface{}) {
|
||||
out := jet.SqlBuilder{Dialect: dialect}
|
||||
out := jet.SQLBuilder{Dialect: dialect}
|
||||
jet.SerializeForProjection(projection, jet.SelectStatementType, &out)
|
||||
|
||||
assert.DeepEqual(t, out.Buff.String(), query)
|
||||
assert.DeepEqual(t, out.Args, args)
|
||||
}
|
||||
|
||||
// AssertQueryPanicErr check if statement Query execution panics with error errString
|
||||
func AssertQueryPanicErr(t *testing.T, stmt jet.Statement, db execution.DB, dest interface{}, errString string) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// Date creates time from t string
|
||||
func Date(t string) *time.Time {
|
||||
newTime, err := time.Parse("2006-01-02", t)
|
||||
|
||||
|
|
@ -15,6 +16,7 @@ func Date(t string) *time.Time {
|
|||
return &newTime
|
||||
}
|
||||
|
||||
// TimestampWithoutTimeZone creates time from t
|
||||
func TimestampWithoutTimeZone(t string, precision int) *time.Time {
|
||||
|
||||
precisionStr := ""
|
||||
|
|
@ -32,6 +34,7 @@ func TimestampWithoutTimeZone(t string, precision int) *time.Time {
|
|||
return &newTime
|
||||
}
|
||||
|
||||
// TimeWithoutTimeZone creates time from t
|
||||
func TimeWithoutTimeZone(t string) *time.Time {
|
||||
newTime, err := time.Parse("15:04:05", t)
|
||||
|
||||
|
|
@ -42,6 +45,7 @@ func TimeWithoutTimeZone(t string) *time.Time {
|
|||
return &newTime
|
||||
}
|
||||
|
||||
// TimeWithTimeZone creates time from t
|
||||
func TimeWithTimeZone(t string) *time.Time {
|
||||
newTimez, err := time.Parse("15:04:05 -0700", t)
|
||||
|
||||
|
|
@ -52,6 +56,7 @@ func TimeWithTimeZone(t string) *time.Time {
|
|||
return &newTimez
|
||||
}
|
||||
|
||||
// TimestampWithTimeZone creates time from t
|
||||
func TimestampWithTimeZone(t string, precision int) *time.Time {
|
||||
|
||||
precisionStr := ""
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ func FormatTimestamp(t time.Time) []byte {
|
|||
return b
|
||||
}
|
||||
|
||||
// IsNill check if v is nil
|
||||
// IsNil check if v is nil
|
||||
func IsNil(v interface{}) bool {
|
||||
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue