Jet internal refactor.

This commit is contained in:
go-jet 2019-08-11 14:29:03 +02:00
parent 4fbf576370
commit ee4897a1e2
49 changed files with 481 additions and 2528 deletions

View file

@ -37,13 +37,13 @@ type cast interface {
}
type castImpl struct {
jet.CastImpl
jet.Cast
}
func CAST(expr Expression) cast {
castImpl := &castImpl{}
castImpl.CastImpl = jet.NewCastImpl(expr)
castImpl.Cast = jet.NewCastImpl(expr)
return castImpl
}

21
postgres/clauses.go Normal file
View file

@ -0,0 +1,21 @@
package postgres
import (
"github.com/go-jet/jet/internal/jet"
)
type ClauseReturning struct {
Projections []jet.Projection
}
func (r *ClauseReturning) Serialize(statementType jet.StatementType, out *jet.SqlBuilder) error {
if len(r.Projections) == 0 {
return nil
}
out.NewLine()
out.WriteString("RETURNING")
out.IncreaseIdent()
return out.WriteProjections(statementType, r.Projections)
}

View file

@ -2,7 +2,7 @@ package postgres
import "github.com/go-jet/jet/internal/jet"
type Column jet.Column
type Column jet.ColumnExpression
type IColumnList jet.IColumnList

View file

@ -15,7 +15,7 @@ type deleteStatementImpl struct {
Delete jet.ClauseStatementBegin
Where jet.ClauseWhere
Returning jet.ClauseReturning
Returning ClauseReturning
}
func newDeleteStatement(table WritableTable) DeleteStatement {

View file

@ -13,3 +13,48 @@ func TestString_REGEXP_LIKE_function(t *testing.T) {
assertClauseSerialize(t, REGEXP_LIKE(table3StrCol, String("JOHN"), "c"), "table3.col2 ~ $1", "JOHN")
assertClauseSerialize(t, REGEXP_LIKE(table3StrCol, String("JOHN"), "i"), "table3.col2 ~* $1", "JOHN")
}
func TestExists(t *testing.T) {
assertClauseSerialize(t, EXISTS(
table2.
SELECT(Int(1)).
WHERE(table1Col1.EQ(table2Col3)),
),
`(EXISTS (
SELECT $1
FROM db.table2
WHERE table1.col1 = table2.col3
))`, int64(1))
}
func TestIN(t *testing.T) {
assertClauseSerialize(t, Float(1.11).IN(table1.SELECT(table1Col1)),
`($1 IN ((
SELECT table1.col1 AS "table1.col1"
FROM db.table1
)))`, float64(1.11))
assertClauseSerialize(t, ROW(Int(12), table1Col1).IN(table2.SELECT(table2Col3, table3Col1)),
`(ROW($1, table1.col1) IN ((
SELECT table2.col3 AS "table2.col3",
table3.col1 AS "table3.col1"
FROM db.table2
)))`, int64(12))
}
func TestNOT_IN(t *testing.T) {
assertClauseSerialize(t, Float(1.11).NOT_IN(table1.SELECT(table1Col1)),
`($1 NOT IN ((
SELECT table1.col1 AS "table1.col1"
FROM db.table1
)))`, float64(1.11))
assertClauseSerialize(t, ROW(Int(12), table1Col1).NOT_IN(table2.SELECT(table2Col3, table3Col1)),
`(ROW($1, table1.col1) NOT IN ((
SELECT table2.col3 AS "table2.col3",
table3.col1 AS "table3.col1"
FROM db.table2
)))`, int64(12))
}

View file

@ -19,9 +19,9 @@ type InsertStatement interface {
RETURNING(projections ...jet.Projection) InsertStatement
}
func newInsertStatement(table WritableTable, columns []jet.IColumn) InsertStatement {
func newInsertStatement(table WritableTable, columns []jet.Column) InsertStatement {
newInsert := &insertStatementImpl{}
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.DeleteStatementType, newInsert,
newInsert.StatementImpl = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
&newInsert.Insert, &newInsert.Values, &newInsert.Select, &newInsert.Returning)
newInsert.Insert.Table = table
@ -36,7 +36,7 @@ type insertStatementImpl struct {
Insert jet.ClauseInsert
Values jet.ClauseValues
Select jet.ClauseQuery
Returning jet.ClauseReturning
Returning ClauseReturning
}
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
@ -45,12 +45,12 @@ func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) I
}
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowFromModel(i.getColumns(), data))
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowFromModel(i.Insert.GetColumns(), data))
return i
}
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowsFromModels(i.getColumns(), data)...)
i.Values.Rows = append(i.Values.Rows, jet.UnwindRowsFromModels(i.Insert.GetColumns(), data)...)
return i
}
@ -63,11 +63,3 @@ func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertState
i.Select.Query = selectStatement
return i
}
func (i *insertStatementImpl) getColumns() []jet.IColumn {
if len(i.Insert.Columns) > 0 {
return i.Insert.Columns
}
return i.Insert.Table.Columns()
}

View file

@ -25,7 +25,7 @@ type LockStatement interface {
func LOCK(tables ...jet.SerializerTable) LockStatement {
newLock := &lockStatementImpl{}
newLock.StatementImpl = jet.NewStatementImpl(Dialect, jet.DeleteStatementType, newLock,
newLock.StatementImpl = jet.NewStatementImpl(Dialect, jet.LockStatementType, newLock,
&newLock.StatementBegin, &newLock.In, &newLock.NoWait)
newLock.StatementBegin.Name = "LOCK TABLE"

View file

@ -23,8 +23,8 @@ type readableTable interface {
}
type writableTable interface {
INSERT(columns ...jet.IColumn) InsertStatement
UPDATE(column jet.IColumn, columns ...jet.IColumn) UpdateStatement
INSERT(columns ...jet.Column) InsertStatement
UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement
DELETE() DeleteStatement
LOCK() LockStatement
}
@ -47,12 +47,12 @@ type Table interface {
//table
readableTable
writableTable
jet.Serializer
jet.SerializerTable
//acceptsVisitor
SchemaName() string
TableName() string
AS(alias string)
//SchemaName() string
//TableName() string
//As(alias string)
}
type readableTableInterfaceImpl struct {
@ -91,11 +91,11 @@ type writableTableInterfaceImpl struct {
parent WritableTable
}
func (w *writableTableInterfaceImpl) INSERT(columns ...jet.IColumn) InsertStatement {
func (w *writableTableInterfaceImpl) INSERT(columns ...jet.Column) InsertStatement {
return newInsertStatement(w.parent, jet.UnwidColumnList(columns))
}
func (w *writableTableInterfaceImpl) UPDATE(column jet.IColumn, columns ...jet.IColumn) UpdateStatement {
func (w *writableTableInterfaceImpl) UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement {
return newUpdateStatement(w.parent, jet.UnwindColumns(column, columns...))
}
@ -111,13 +111,13 @@ type table2Impl struct {
readableTableInterfaceImpl
writableTableInterfaceImpl
jet.TableImpl2
jet.TableImpl
}
func NewTable(schemaName, name string, columns ...jet.Column) Table {
func NewTable(schemaName, name string, columns ...jet.ColumnExpression) Table {
t := &table2Impl{
TableImpl2: jet.NewTable2(Dialect, schemaName, name, columns...),
TableImpl: jet.NewTable(schemaName, name, columns...),
}
t.readableTableInterfaceImpl.parent = t

101
postgres/table_test.go Normal file
View file

@ -0,0 +1,101 @@
package postgres
import (
"testing"
)
func TestJoinNilInputs(t *testing.T) {
assertClauseSerializeErr(t, table2.INNER_JOIN(nil, table1ColBool.EQ(table2ColBool)),
"jet: right hand side of join operation is nil table")
assertClauseSerializeErr(t, table2.INNER_JOIN(table1, nil),
"jet: join condition is nil")
}
func TestINNER_JOIN(t *testing.T) {
assertClauseSerialize(t, table1.
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1
INNER JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1.
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).
INNER_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1
INNER JOIN db.table2 ON (table1.col_int = table2.col_int)
INNER JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1.
INNER_JOIN(table2, table1ColInt.EQ(Int(1))).
INNER_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1
INNER JOIN db.table2 ON (table1.col_int = $1)
INNER JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
}
func TestLEFT_JOIN(t *testing.T) {
assertClauseSerialize(t, table1.
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1
LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1.
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
LEFT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1
LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)
LEFT JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1.
LEFT_JOIN(table2, table1ColInt.EQ(Int(1))).
LEFT_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1
LEFT JOIN db.table2 ON (table1.col_int = $1)
LEFT JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
}
func TestRIGHT_JOIN(t *testing.T) {
assertClauseSerialize(t, table1.
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1
RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1.
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
RIGHT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1
RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)
RIGHT JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1.
RIGHT_JOIN(table2, table1ColInt.EQ(Int(1))).
RIGHT_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1
RIGHT JOIN db.table2 ON (table1.col_int = $1)
RIGHT JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
}
func TestFULL_JOIN(t *testing.T) {
assertClauseSerialize(t, table1.
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1
FULL JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1.
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)).
FULL_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1
FULL JOIN db.table2 ON (table1.col_int = table2.col_int)
FULL JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1.
FULL_JOIN(table2, table1ColInt.EQ(Int(1))).
FULL_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1
FULL JOIN db.table2 ON (table1.col_int = $1)
FULL JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
}
func TestCROSS_JOIN(t *testing.T) {
assertClauseSerialize(t, table1.
CROSS_JOIN(table2),
`db.table1
CROSS JOIN db.table2`)
assertClauseSerialize(t, table1.
CROSS_JOIN(table2).
CROSS_JOIN(table3),
`db.table1
CROSS JOIN db.table2
CROSS JOIN db.table3`)
}

View file

@ -22,10 +22,10 @@ type updateStatementImpl struct {
Update jet.ClauseUpdate
Set ClauseSet
Where jet.ClauseWhere
Returning jet.ClauseReturning
Returning ClauseReturning
}
func newUpdateStatement(table WritableTable, columns []jet.IColumn) UpdateStatement {
func newUpdateStatement(table WritableTable, columns []jet.Column) UpdateStatement {
update := &updateStatementImpl{}
update.StatementImpl = jet.NewStatementImpl(Dialect, jet.UpdateStatementType, update, &update.Update,
&update.Set, &update.Where, &update.Returning)
@ -58,7 +58,7 @@ func (u *updateStatementImpl) RETURNING(projections ...jet.Projection) UpdateSta
}
type ClauseSet struct {
Columns []jet.IColumn
Columns []jet.Column
Values []jet.Serializer
}