Improvements on sub-query projection reference.
This commit is contained in:
parent
d9ffa86453
commit
565b670188
17 changed files with 512 additions and 134 deletions
|
|
@ -1,23 +1,36 @@
|
|||
package sqlbuilder
|
||||
|
||||
type Alias struct {
|
||||
type alias struct {
|
||||
expression Expression
|
||||
alias string
|
||||
|
||||
subQuery ExpressionTable
|
||||
}
|
||||
|
||||
func NewAlias(expression Expression, alias string) *Alias {
|
||||
return &Alias{
|
||||
func newAlias(expression Expression, aliasName string) projection {
|
||||
return &alias{
|
||||
expression: expression,
|
||||
alias: alias,
|
||||
alias: aliasName,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Alias) serializeForProjection(statement statementType, out *queryData) error {
|
||||
func (a *alias) from(subQuery ExpressionTable) projection {
|
||||
newAlias := *a
|
||||
newAlias.subQuery = subQuery
|
||||
return &newAlias
|
||||
}
|
||||
|
||||
err := a.expression.serialize(statement, out)
|
||||
func (a *alias) serializeForProjection(statement statementType, out *queryData) error {
|
||||
if a.subQuery != nil {
|
||||
out.writeIdentifier(a.subQuery.Alias())
|
||||
out.writeByte('.')
|
||||
out.writeQuotedString(a.alias)
|
||||
} else {
|
||||
err := a.expression.serialize(statement, out)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
out.writeString(`AS "` + a.alias + `"`)
|
||||
|
|
|
|||
|
|
@ -150,6 +150,10 @@ func isPostSeparator(b byte) bool {
|
|||
return b == ' ' || b == '.' || b == ',' || b == ')' || b == '\n' || b == ':'
|
||||
}
|
||||
|
||||
func (q *queryData) writeQuotedString(str string) {
|
||||
q.writeString(`"` + str + `"`)
|
||||
}
|
||||
|
||||
func (q *queryData) writeString(str string) {
|
||||
q.write([]byte(str))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ type column interface {
|
|||
TableName() string
|
||||
|
||||
setTableName(table string)
|
||||
setSubQuery(subQuery ExpressionTable)
|
||||
defaultAlias() string
|
||||
}
|
||||
|
||||
type Column interface {
|
||||
|
|
@ -20,6 +22,8 @@ type columnImpl struct {
|
|||
|
||||
name string
|
||||
tableName string
|
||||
|
||||
subQuery ExpressionTable
|
||||
}
|
||||
|
||||
func newColumn(name string, tableName string, parent Column) columnImpl {
|
||||
|
|
@ -45,6 +49,10 @@ func (c *columnImpl) setTableName(table string) {
|
|||
c.tableName = table
|
||||
}
|
||||
|
||||
func (c *columnImpl) setSubQuery(subQuery ExpressionTable) {
|
||||
c.subQuery = subQuery
|
||||
}
|
||||
|
||||
func (c *columnImpl) defaultAlias() string {
|
||||
if c.tableName != "" {
|
||||
return c.tableName + "." + c.name
|
||||
|
|
@ -78,12 +86,18 @@ func (c columnImpl) serializeForProjection(statement statementType, out *queryDa
|
|||
|
||||
func (c columnImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
|
||||
if c.tableName != "" {
|
||||
out.writeIdentifier(c.tableName)
|
||||
if c.subQuery != nil {
|
||||
out.writeIdentifier(c.subQuery.Alias())
|
||||
out.writeByte('.')
|
||||
}
|
||||
out.writeString(`"` + c.defaultAlias() + `"`)
|
||||
} else {
|
||||
if c.tableName != "" {
|
||||
out.writeIdentifier(c.tableName)
|
||||
out.writeByte('.')
|
||||
}
|
||||
|
||||
out.writeIdentifier(c.name)
|
||||
out.writeIdentifier(c.name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -95,6 +109,16 @@ type ColumnList []Column
|
|||
// projection interface implementation
|
||||
func (cl ColumnList) isProjectionType() {}
|
||||
|
||||
func (cl ColumnList) from(subQuery ExpressionTable) projection {
|
||||
newProjectionList := ProjectionList{}
|
||||
|
||||
for _, column := range cl {
|
||||
newProjectionList = append(newProjectionList, column.from(subQuery))
|
||||
}
|
||||
|
||||
return newProjectionList
|
||||
}
|
||||
|
||||
func (cl ColumnList) serializeForProjection(statement statementType, out *queryData) error {
|
||||
projections := columnListToProjectionList(cl)
|
||||
|
||||
|
|
@ -108,6 +132,8 @@ func (cl ColumnList) serializeForProjection(statement statementType, out *queryD
|
|||
}
|
||||
|
||||
// column interface implementation
|
||||
func (cl ColumnList) Name() string { return "" }
|
||||
func (cl ColumnList) TableName() string { return "" }
|
||||
func (cl ColumnList) setTableName(name string) {}
|
||||
func (cl ColumnList) Name() string { return "" }
|
||||
func (cl ColumnList) TableName() string { return "" }
|
||||
func (cl ColumnList) setTableName(name string) {}
|
||||
func (cl ColumnList) setSubQuery(subQuery ExpressionTable) {}
|
||||
func (cl ColumnList) defaultAlias() string { return "" }
|
||||
|
|
|
|||
|
|
@ -9,6 +9,6 @@ func TestColumn(t *testing.T) {
|
|||
assertClauseSerialize(t, column, "col")
|
||||
column.setTableName("table1")
|
||||
assertClauseSerialize(t, column, "table1.col")
|
||||
assertProjectionSerialize(t, column, `table1.col AS "table1.col"`)
|
||||
assertProjectionSerialize(t, &column, `table1.col AS "table1.col"`)
|
||||
assertProjectionSerialize(t, column.AS("alias1"), `table1.col AS "alias1"`)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ type ColumnBool interface {
|
|||
BoolExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnBool
|
||||
From(subQuery ExpressionTable) ColumnBool
|
||||
}
|
||||
|
||||
type boolColumnImpl struct {
|
||||
|
|
@ -14,17 +14,23 @@ type boolColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *boolColumnImpl) From(table ExpressionTable) ColumnBool {
|
||||
newBoolColumn := BoolColumn(i.defaultAlias())
|
||||
newBoolColumn.setTableName(table.Alias())
|
||||
func (i *boolColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newBoolColumn := BoolColumn(i.name)
|
||||
newBoolColumn.setTableName(i.tableName)
|
||||
newBoolColumn.setSubQuery(subQuery)
|
||||
|
||||
return newBoolColumn
|
||||
}
|
||||
|
||||
func (i *boolColumnImpl) From(subQuery ExpressionTable) ColumnBool {
|
||||
newBoolColumn := i.from(subQuery).(ColumnBool)
|
||||
|
||||
return newBoolColumn
|
||||
}
|
||||
|
||||
func BoolColumn(name string) ColumnBool {
|
||||
|
||||
boolColumn := &boolColumnImpl{}
|
||||
boolColumn.columnImpl = newColumn(name, "", boolColumn)
|
||||
|
||||
boolColumn.boolInterfaceImpl.parent = boolColumn
|
||||
|
||||
return boolColumn
|
||||
|
|
@ -35,7 +41,7 @@ type ColumnFloat interface {
|
|||
FloatExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnFloat
|
||||
From(subQuery ExpressionTable) ColumnFloat
|
||||
}
|
||||
|
||||
type floatColumnImpl struct {
|
||||
|
|
@ -43,16 +49,22 @@ type floatColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *floatColumnImpl) From(table ExpressionTable) ColumnFloat {
|
||||
newFloatColumn := FloatColumn(i.defaultAlias())
|
||||
newFloatColumn.setTableName(table.Alias())
|
||||
func (i *floatColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newFloatColumn := FloatColumn(i.name)
|
||||
newFloatColumn.setTableName(i.tableName)
|
||||
newFloatColumn.setSubQuery(subQuery)
|
||||
|
||||
return newFloatColumn
|
||||
}
|
||||
|
||||
func (i *floatColumnImpl) From(subQuery ExpressionTable) ColumnFloat {
|
||||
newFloatColumn := i.from(subQuery).(ColumnFloat)
|
||||
|
||||
return newFloatColumn
|
||||
}
|
||||
|
||||
func FloatColumn(name string) ColumnFloat {
|
||||
|
||||
floatColumn := &floatColumnImpl{}
|
||||
|
||||
floatColumn.floatInterfaceImpl.parent = floatColumn
|
||||
floatColumn.columnImpl = newColumn(name, "", floatColumn)
|
||||
|
||||
|
|
@ -64,7 +76,7 @@ type ColumnInteger interface {
|
|||
IntegerExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnInteger
|
||||
From(subQuery ExpressionTable) ColumnInteger
|
||||
}
|
||||
|
||||
type integerColumnImpl struct {
|
||||
|
|
@ -73,15 +85,20 @@ type integerColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *integerColumnImpl) From(table ExpressionTable) ColumnInteger {
|
||||
newIntColumn := IntegerColumn(i.defaultAlias())
|
||||
newIntColumn.setTableName(table.Alias())
|
||||
func (i *integerColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newIntColumn := IntegerColumn(i.name)
|
||||
newIntColumn.setTableName(i.tableName)
|
||||
newIntColumn.setSubQuery(subQuery)
|
||||
|
||||
return newIntColumn
|
||||
}
|
||||
|
||||
func (i *integerColumnImpl) From(subQuery ExpressionTable) ColumnInteger {
|
||||
return i.from(subQuery).(ColumnInteger)
|
||||
}
|
||||
|
||||
func IntegerColumn(name string) ColumnInteger {
|
||||
integerColumn := &integerColumnImpl{}
|
||||
|
||||
integerColumn.integerInterfaceImpl.parent = integerColumn
|
||||
integerColumn.columnImpl = newColumn(name, "", integerColumn)
|
||||
|
||||
|
|
@ -93,7 +110,7 @@ type ColumnString interface {
|
|||
StringExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnString
|
||||
From(subQuery ExpressionTable) ColumnString
|
||||
}
|
||||
|
||||
type stringColumnImpl struct {
|
||||
|
|
@ -102,18 +119,21 @@ type stringColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *stringColumnImpl) From(table ExpressionTable) ColumnString {
|
||||
newStrColumn := StringColumn(i.defaultAlias())
|
||||
newStrColumn.setTableName(table.Alias())
|
||||
func (i *stringColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newStrColumn := StringColumn(i.name)
|
||||
newStrColumn.setTableName(i.tableName)
|
||||
newStrColumn.setSubQuery(subQuery)
|
||||
|
||||
return newStrColumn
|
||||
}
|
||||
|
||||
func (i *stringColumnImpl) From(subQuery ExpressionTable) ColumnString {
|
||||
return i.from(subQuery).(ColumnString)
|
||||
}
|
||||
|
||||
func StringColumn(name string) ColumnString {
|
||||
|
||||
stringColumn := &stringColumnImpl{}
|
||||
|
||||
stringColumn.stringInterfaceImpl.parent = stringColumn
|
||||
|
||||
stringColumn.columnImpl = newColumn(name, "", stringColumn)
|
||||
|
||||
return stringColumn
|
||||
|
|
@ -124,28 +144,30 @@ type ColumnTime interface {
|
|||
TimeExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnTime
|
||||
From(subQuery ExpressionTable) ColumnTime
|
||||
}
|
||||
|
||||
type timeColumnImpl struct {
|
||||
timeInterfaceImpl
|
||||
|
||||
columnImpl
|
||||
}
|
||||
|
||||
func (i *timeColumnImpl) From(table ExpressionTable) ColumnTime {
|
||||
newTimeColumn := TimeColumn(i.defaultAlias())
|
||||
newTimeColumn.setTableName(table.Alias())
|
||||
func (i *timeColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newTimeColumn := TimeColumn(i.name)
|
||||
newTimeColumn.setTableName(i.tableName)
|
||||
newTimeColumn.setSubQuery(subQuery)
|
||||
|
||||
return newTimeColumn
|
||||
}
|
||||
|
||||
func (i *timeColumnImpl) From(subQuery ExpressionTable) ColumnTime {
|
||||
return i.from(subQuery).(ColumnTime)
|
||||
}
|
||||
|
||||
func TimeColumn(name string) ColumnTime {
|
||||
timeColumn := &timeColumnImpl{}
|
||||
|
||||
timeColumn.timeInterfaceImpl.parent = timeColumn
|
||||
|
||||
timeColumn.columnImpl = newColumn(name, "", timeColumn)
|
||||
|
||||
return timeColumn
|
||||
}
|
||||
|
||||
|
|
@ -155,7 +177,7 @@ type ColumnTimez interface {
|
|||
TimezExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnTimez
|
||||
From(subQuery ExpressionTable) ColumnTimez
|
||||
}
|
||||
|
||||
type timezColumnImpl struct {
|
||||
|
|
@ -164,17 +186,21 @@ type timezColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *timezColumnImpl) From(table ExpressionTable) ColumnTimez {
|
||||
newTimezColumn := TimezColumn(i.defaultAlias())
|
||||
newTimezColumn.setTableName(table.Alias())
|
||||
func (i *timezColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newTimezColumn := TimezColumn(i.name)
|
||||
newTimezColumn.setTableName(i.tableName)
|
||||
newTimezColumn.setSubQuery(subQuery)
|
||||
|
||||
return newTimezColumn
|
||||
}
|
||||
|
||||
func (i *timezColumnImpl) From(subQuery ExpressionTable) ColumnTimez {
|
||||
return i.from(subQuery).(ColumnTimez)
|
||||
}
|
||||
|
||||
func TimezColumn(name string) ColumnTimez {
|
||||
timezColumn := &timezColumnImpl{}
|
||||
|
||||
timezColumn.timezInterfaceImpl.parent = timezColumn
|
||||
|
||||
timezColumn.columnImpl = newColumn(name, "", timezColumn)
|
||||
|
||||
return timezColumn
|
||||
|
|
@ -185,7 +211,7 @@ type ColumnTimestamp interface {
|
|||
TimestampExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnTimestamp
|
||||
From(subQuery ExpressionTable) ColumnTimestamp
|
||||
}
|
||||
|
||||
type timestampColumnImpl struct {
|
||||
|
|
@ -194,17 +220,21 @@ type timestampColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *timestampColumnImpl) From(table ExpressionTable) ColumnTimestamp {
|
||||
newTimestampColumn := TimestampColumn(i.defaultAlias())
|
||||
newTimestampColumn.setTableName(table.Alias())
|
||||
func (i *timestampColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newTimestampColumn := TimestampColumn(i.name)
|
||||
newTimestampColumn.setTableName(i.tableName)
|
||||
newTimestampColumn.setSubQuery(subQuery)
|
||||
|
||||
return newTimestampColumn
|
||||
}
|
||||
|
||||
func (i *timestampColumnImpl) From(subQuery ExpressionTable) ColumnTimestamp {
|
||||
return i.from(subQuery).(ColumnTimestamp)
|
||||
}
|
||||
|
||||
func TimestampColumn(name string) ColumnTimestamp {
|
||||
timestampColumn := ×tampColumnImpl{}
|
||||
|
||||
timestampColumn.timestampInterfaceImpl.parent = timestampColumn
|
||||
|
||||
timestampColumn.columnImpl = newColumn(name, "", timestampColumn)
|
||||
|
||||
return timestampColumn
|
||||
|
|
@ -215,7 +245,7 @@ type ColumnTimestampz interface {
|
|||
TimestampzExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnTimestampz
|
||||
From(subQuery ExpressionTable) ColumnTimestampz
|
||||
}
|
||||
|
||||
type timestampzColumnImpl struct {
|
||||
|
|
@ -224,17 +254,21 @@ type timestampzColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *timestampzColumnImpl) From(table ExpressionTable) ColumnTimestampz {
|
||||
newTimestampzColumn := TimestampzColumn(i.defaultAlias())
|
||||
newTimestampzColumn.setTableName(table.Alias())
|
||||
func (i *timestampzColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newTimestampzColumn := TimestampzColumn(i.name)
|
||||
newTimestampzColumn.setTableName(i.tableName)
|
||||
newTimestampzColumn.setSubQuery(subQuery)
|
||||
|
||||
return newTimestampzColumn
|
||||
}
|
||||
|
||||
func (i *timestampzColumnImpl) From(subQuery ExpressionTable) ColumnTimestampz {
|
||||
return i.from(subQuery).(ColumnTimestampz)
|
||||
}
|
||||
|
||||
func TimestampzColumn(name string) ColumnTimestampz {
|
||||
timestampzColumn := ×tampzColumnImpl{}
|
||||
|
||||
timestampzColumn.timestampzInterfaceImpl.parent = timestampzColumn
|
||||
|
||||
timestampzColumn.columnImpl = newColumn(name, "", timestampzColumn)
|
||||
|
||||
return timestampzColumn
|
||||
|
|
@ -245,7 +279,7 @@ type ColumnDate interface {
|
|||
DateExpression
|
||||
column
|
||||
|
||||
From(table ExpressionTable) ColumnDate
|
||||
From(subQuery ExpressionTable) ColumnDate
|
||||
}
|
||||
|
||||
type dateColumnImpl struct {
|
||||
|
|
@ -254,18 +288,21 @@ type dateColumnImpl struct {
|
|||
columnImpl
|
||||
}
|
||||
|
||||
func (i *dateColumnImpl) From(table ExpressionTable) ColumnDate {
|
||||
newDateColumn := DateColumn(i.defaultAlias())
|
||||
newDateColumn.setTableName(table.Alias())
|
||||
func (i *dateColumnImpl) from(subQuery ExpressionTable) projection {
|
||||
newDateColumn := DateColumn(i.name)
|
||||
newDateColumn.setTableName(i.tableName)
|
||||
newDateColumn.setSubQuery(subQuery)
|
||||
|
||||
return newDateColumn
|
||||
}
|
||||
|
||||
func (i *dateColumnImpl) From(subQuery ExpressionTable) ColumnDate {
|
||||
return i.from(subQuery).(ColumnDate)
|
||||
}
|
||||
|
||||
func DateColumn(name string) ColumnDate {
|
||||
dateColumn := &dateColumnImpl{}
|
||||
|
||||
dateColumn.dateInterfaceImpl.parent = dateColumn
|
||||
|
||||
dateColumn.columnImpl = newColumn(name, "", dateColumn)
|
||||
|
||||
return dateColumn
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,36 +10,36 @@ func TestNewBoolColumn(t *testing.T) {
|
|||
boolColumn := BoolColumn("colBool").From(subQuery)
|
||||
assertClauseSerialize(t, boolColumn, `sub_query."colBool"`)
|
||||
assertClauseSerialize(t, boolColumn.EQ(Bool(true)), `(sub_query."colBool" = $1)`, true)
|
||||
assertProjectionSerialize(t, boolColumn, `sub_query."colBool" AS "sub_query.colBool"`)
|
||||
assertProjectionSerialize(t, boolColumn, `sub_query."colBool" AS "colBool"`)
|
||||
|
||||
boolColumn2 := table1ColBool.From(subQuery)
|
||||
assertClauseSerialize(t, boolColumn2, `sub_query."table1.col_bool"`)
|
||||
assertClauseSerialize(t, boolColumn2.EQ(Bool(true)), `(sub_query."table1.col_bool" = $1)`, true)
|
||||
assertProjectionSerialize(t, boolColumn2, `sub_query."table1.col_bool" AS "sub_query.table1.col_bool"`)
|
||||
assertProjectionSerialize(t, boolColumn2, `sub_query."table1.col_bool" AS "table1.col_bool"`)
|
||||
}
|
||||
|
||||
func TestNewIntColumn(t *testing.T) {
|
||||
intColumn := IntegerColumn("col_int").From(subQuery)
|
||||
assertClauseSerialize(t, intColumn, "sub_query.col_int")
|
||||
assertClauseSerialize(t, intColumn.EQ(Int(12)), "(sub_query.col_int = $1)", int64(12))
|
||||
assertProjectionSerialize(t, intColumn, `sub_query.col_int AS "sub_query.col_int"`)
|
||||
assertClauseSerialize(t, intColumn, `sub_query."col_int"`)
|
||||
assertClauseSerialize(t, intColumn.EQ(Int(12)), `(sub_query."col_int" = $1)`, int64(12))
|
||||
assertProjectionSerialize(t, intColumn, `sub_query."col_int" AS "col_int"`)
|
||||
|
||||
intColumn2 := table1ColInt.From(subQuery)
|
||||
assertClauseSerialize(t, intColumn2, `sub_query."table1.col_int"`)
|
||||
assertClauseSerialize(t, intColumn2.EQ(Int(14)), `(sub_query."table1.col_int" = $1)`, int64(14))
|
||||
assertProjectionSerialize(t, intColumn2, `sub_query."table1.col_int" AS "sub_query.table1.col_int"`)
|
||||
assertProjectionSerialize(t, intColumn2, `sub_query."table1.col_int" AS "table1.col_int"`)
|
||||
|
||||
}
|
||||
|
||||
func TestNewFloatColumnColumn(t *testing.T) {
|
||||
floatColumn := FloatColumn("col_float").From(subQuery)
|
||||
assertClauseSerialize(t, floatColumn, "sub_query.col_float")
|
||||
assertClauseSerialize(t, floatColumn.EQ(Float(1.11)), "(sub_query.col_float = $1)", float64(1.11))
|
||||
assertProjectionSerialize(t, floatColumn, `sub_query.col_float AS "sub_query.col_float"`)
|
||||
assertClauseSerialize(t, floatColumn, `sub_query."col_float"`)
|
||||
assertClauseSerialize(t, floatColumn.EQ(Float(1.11)), `(sub_query."col_float" = $1)`, float64(1.11))
|
||||
assertProjectionSerialize(t, floatColumn, `sub_query."col_float" AS "col_float"`)
|
||||
|
||||
floatColumn2 := table1ColFloat.From(subQuery)
|
||||
assertClauseSerialize(t, floatColumn2, `sub_query."table1.col_float"`)
|
||||
assertClauseSerialize(t, floatColumn2.EQ(Float(2.22)), `(sub_query."table1.col_float" = $1)`, float64(2.22))
|
||||
assertProjectionSerialize(t, floatColumn2, `sub_query."table1.col_float" AS "sub_query.table1.col_float"`)
|
||||
assertProjectionSerialize(t, floatColumn2, `sub_query."table1.col_float" AS "table1.col_float"`)
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,10 @@ type expressionInterfaceImpl struct {
|
|||
parent Expression
|
||||
}
|
||||
|
||||
func (e *expressionInterfaceImpl) from(subQuery ExpressionTable) projection {
|
||||
return e.parent
|
||||
}
|
||||
|
||||
func (e *expressionInterfaceImpl) IS_NULL() BoolExpression {
|
||||
return newPostifxBoolExpression(e.parent, "IS NULL")
|
||||
}
|
||||
|
|
@ -61,7 +65,7 @@ func (e *expressionInterfaceImpl) NOT_IN(expressions ...Expression) BoolExpressi
|
|||
}
|
||||
|
||||
func (e *expressionInterfaceImpl) AS(alias string) projection {
|
||||
return NewAlias(e.parent, alias)
|
||||
return newAlias(e.parent, alias)
|
||||
}
|
||||
|
||||
func (e *expressionInterfaceImpl) ASC() OrderByClause {
|
||||
|
|
|
|||
|
|
@ -6,19 +6,29 @@ type ExpressionTable interface {
|
|||
ReadableTable
|
||||
|
||||
Alias() string
|
||||
|
||||
AllColumns() ProjectionList
|
||||
}
|
||||
|
||||
type expressionTableImpl struct {
|
||||
readableTableInterfaceImpl
|
||||
expression Expression
|
||||
alias string
|
||||
|
||||
projections []projection
|
||||
}
|
||||
|
||||
func newExpressionTable(expression Expression, alias string) ExpressionTable {
|
||||
func newExpressionTable(expression Expression, alias string, projections []projection) ExpressionTable {
|
||||
expTable := &expressionTableImpl{expression: expression, alias: alias}
|
||||
|
||||
expTable.readableTableInterfaceImpl.parent = expTable
|
||||
|
||||
for _, projection := range projections {
|
||||
newProjection := projection.from(expTable)
|
||||
|
||||
expTable.projections = append(expTable.projections, newProjection)
|
||||
}
|
||||
|
||||
return expTable
|
||||
}
|
||||
|
||||
|
|
@ -26,6 +36,14 @@ func (e *expressionTableImpl) Alias() string {
|
|||
return e.alias
|
||||
}
|
||||
|
||||
func (e *expressionTableImpl) columns() []Column {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *expressionTableImpl) AllColumns() ProjectionList {
|
||||
return e.projections
|
||||
}
|
||||
|
||||
func (e *expressionTableImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
if e == nil {
|
||||
return errors.New("Expression table is nil. ")
|
||||
|
|
|
|||
|
|
@ -2,4 +2,29 @@ package sqlbuilder
|
|||
|
||||
type projection interface {
|
||||
serializeForProjection(statement statementType, out *queryData) error
|
||||
from(subQuery ExpressionTable) projection
|
||||
}
|
||||
|
||||
//------------------------------------------------------//
|
||||
// Dummy type for projection list
|
||||
type ProjectionList []projection
|
||||
|
||||
func (cl ProjectionList) from(subQuery ExpressionTable) projection {
|
||||
newProjectionList := ProjectionList{}
|
||||
|
||||
for _, projection := range cl {
|
||||
newProjectionList = append(newProjectionList, projection.from(subQuery))
|
||||
}
|
||||
|
||||
return newProjectionList
|
||||
}
|
||||
|
||||
func (cl ProjectionList) serializeForProjection(statement statementType, out *queryData) error {
|
||||
err := serializeProjectionList(statement, cl, out)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,5 @@ package sqlbuilder
|
|||
|
||||
type rowsType interface {
|
||||
clause
|
||||
hasRows()
|
||||
projections() []projection
|
||||
}
|
||||
|
||||
type isRowsType struct{}
|
||||
|
||||
func (i *isRowsType) hasRows() {}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ var (
|
|||
type SelectStatement interface {
|
||||
Statement
|
||||
Expression
|
||||
hasRows()
|
||||
|
||||
DISTINCT() SelectStatement
|
||||
FROM(table ReadableTable) SelectStatement
|
||||
|
|
@ -31,6 +30,8 @@ type SelectStatement interface {
|
|||
FOR(lock SelectLock) SelectStatement
|
||||
|
||||
AsTable(alias string) ExpressionTable
|
||||
|
||||
projections() []projection
|
||||
}
|
||||
|
||||
func SELECT(projection1 projection, projections ...projection) SelectStatement {
|
||||
|
|
@ -39,15 +40,14 @@ func SELECT(projection1 projection, projections ...projection) SelectStatement {
|
|||
|
||||
type selectStatementImpl struct {
|
||||
expressionInterfaceImpl
|
||||
isRowsType
|
||||
|
||||
table ReadableTable
|
||||
distinct bool
|
||||
projections []projection
|
||||
where BoolExpression
|
||||
groupBy []groupByClause
|
||||
having BoolExpression
|
||||
orderBy []OrderByClause
|
||||
table ReadableTable
|
||||
distinct bool
|
||||
projectionList []projection
|
||||
where BoolExpression
|
||||
groupBy []groupByClause
|
||||
having BoolExpression
|
||||
orderBy []OrderByClause
|
||||
|
||||
limit, offset int64
|
||||
|
||||
|
|
@ -56,11 +56,11 @@ type selectStatementImpl struct {
|
|||
|
||||
func newSelectStatement(table ReadableTable, projections []projection) SelectStatement {
|
||||
newSelect := &selectStatementImpl{
|
||||
table: table,
|
||||
projections: projections,
|
||||
limit: -1,
|
||||
offset: -1,
|
||||
distinct: false,
|
||||
table: table,
|
||||
projectionList: projections,
|
||||
limit: -1,
|
||||
offset: -1,
|
||||
distinct: false,
|
||||
}
|
||||
|
||||
newSelect.expressionInterfaceImpl.parent = newSelect
|
||||
|
|
@ -105,11 +105,11 @@ func (s *selectStatementImpl) serializeImpl(out *queryData) error {
|
|||
out.writeString("DISTINCT")
|
||||
}
|
||||
|
||||
if len(s.projections) == 0 {
|
||||
if len(s.projectionList) == 0 {
|
||||
return errors.New("no column selected for projection")
|
||||
}
|
||||
|
||||
err := out.writeProjections(select_statement, s.projections)
|
||||
err := out.writeProjections(select_statement, s.projectionList)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -196,8 +196,12 @@ func (s *selectStatementImpl) DebugSql() (query string, err error) {
|
|||
return DebugSql(s)
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) projections() []projection {
|
||||
return s.projectionList
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) AsTable(alias string) ExpressionTable {
|
||||
return newExpressionTable(s.parent, alias)
|
||||
return newExpressionTable(s.parent, alias, s.projectionList)
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) WHERE(expression BoolExpression) SelectStatement {
|
||||
|
|
@ -216,9 +220,7 @@ func (s *selectStatementImpl) HAVING(expression BoolExpression) SelectStatement
|
|||
}
|
||||
|
||||
func (s *selectStatementImpl) ORDER_BY(clauses ...OrderByClause) SelectStatement {
|
||||
|
||||
s.orderBy = clauses
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,13 +9,14 @@ import (
|
|||
type SetStatement interface {
|
||||
Statement
|
||||
Expression
|
||||
hasRows()
|
||||
|
||||
ORDER_BY(clauses ...OrderByClause) SetStatement
|
||||
LIMIT(limit int64) SetStatement
|
||||
OFFSET(offset int64) SetStatement
|
||||
|
||||
AsTable(alias string) ExpressionTable
|
||||
|
||||
projections() []projection
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
@ -51,7 +52,6 @@ func EXCEPT_ALL(selects ...rowsType) SetStatement {
|
|||
// Similar to selectStatementImpl, but less complete
|
||||
type setStatementImpl struct {
|
||||
expressionInterfaceImpl
|
||||
isRowsType
|
||||
|
||||
operator string
|
||||
selects []rowsType
|
||||
|
|
@ -75,23 +75,30 @@ func newSetStatementImpl(operator string, all bool, selects ...rowsType) SetStat
|
|||
return setStatement
|
||||
}
|
||||
|
||||
func (us *setStatementImpl) ORDER_BY(orderBy ...OrderByClause) SetStatement {
|
||||
us.orderBy = orderBy
|
||||
return us
|
||||
func (s *setStatementImpl) ORDER_BY(orderBy ...OrderByClause) SetStatement {
|
||||
s.orderBy = orderBy
|
||||
return s
|
||||
}
|
||||
|
||||
func (us *setStatementImpl) LIMIT(limit int64) SetStatement {
|
||||
us.limit = limit
|
||||
return us
|
||||
func (s *setStatementImpl) LIMIT(limit int64) SetStatement {
|
||||
s.limit = limit
|
||||
return s
|
||||
}
|
||||
|
||||
func (us *setStatementImpl) OFFSET(offset int64) SetStatement {
|
||||
us.offset = offset
|
||||
return us
|
||||
func (s *setStatementImpl) OFFSET(offset int64) SetStatement {
|
||||
s.offset = offset
|
||||
return s
|
||||
}
|
||||
|
||||
func (us *setStatementImpl) AsTable(alias string) ExpressionTable {
|
||||
return newExpressionTable(us.parent, alias)
|
||||
func (s *setStatementImpl) projections() []projection {
|
||||
if len(s.selects) > 0 {
|
||||
return s.selects[0].projections()
|
||||
}
|
||||
return []projection{}
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) AsTable(alias string) ExpressionTable {
|
||||
return newExpressionTable(s.parent, alias, s.projections())
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
|
|
@ -178,10 +185,10 @@ func (s *setStatementImpl) serializeImpl(out *queryData) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (us *setStatementImpl) Sql() (query string, args []interface{}, err error) {
|
||||
func (s *setStatementImpl) Sql() (query string, args []interface{}, err error) {
|
||||
queryData := &queryData{}
|
||||
|
||||
err = us.serializeImpl(queryData)
|
||||
err = s.serializeImpl(queryData)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -199,6 +206,6 @@ func (s *setStatementImpl) Query(db execution.Db, destination interface{}) error
|
|||
return Query(s, db, destination)
|
||||
}
|
||||
|
||||
func (u *setStatementImpl) Exec(db execution.Db) (res sql.Result, err error) {
|
||||
return Exec(u, db)
|
||||
func (s *setStatementImpl) Exec(db execution.Db) (res sql.Result, err error) {
|
||||
return Exec(s, db)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ type readableTable interface {
|
|||
|
||||
// Creates a cross join tableName Expression using onCondition.
|
||||
CROSS_JOIN(table ReadableTable) ReadableTable
|
||||
|
||||
columns() []Column
|
||||
}
|
||||
|
||||
// The sql tableName write interface.
|
||||
|
|
@ -111,7 +113,7 @@ func NewTable(schemaName, name string, columns ...Column) Table {
|
|||
t := &tableImpl{
|
||||
schemaName: schemaName,
|
||||
name: name,
|
||||
columns: columns,
|
||||
columnList: columns,
|
||||
}
|
||||
for _, c := range columns {
|
||||
c.setTableName(name)
|
||||
|
|
@ -130,13 +132,13 @@ type tableImpl struct {
|
|||
schemaName string
|
||||
name string
|
||||
alias string
|
||||
columns []Column
|
||||
columnList []Column
|
||||
}
|
||||
|
||||
func (t *tableImpl) AS(alias string) {
|
||||
t.alias = alias
|
||||
|
||||
for _, c := range t.columns {
|
||||
for _, c := range t.columnList {
|
||||
c.setTableName(alias)
|
||||
}
|
||||
}
|
||||
|
|
@ -151,8 +153,8 @@ func (t *tableImpl) TableName() string {
|
|||
return t.name
|
||||
}
|
||||
|
||||
func (t *tableImpl) SchemaTableName() string {
|
||||
return t.schemaName
|
||||
func (t *tableImpl) columns() []Column {
|
||||
return t.columnList
|
||||
}
|
||||
|
||||
func (t *tableImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
|
|
@ -218,6 +220,10 @@ func (t *joinTable) TableName() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (t *joinTable) columns() []Column {
|
||||
return append(t.lhs.columns(), t.rhs.columns()...)
|
||||
}
|
||||
|
||||
func (t *joinTable) serialize(statement statementType, out *queryData, options ...serializeOption) (err error) {
|
||||
if t == nil {
|
||||
return errors.New("Join table is nil. ")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue