2019-08-03 14:10:47 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import "github.com/go-jet/jet/internal/jet"
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// Table is interface for MySQL tables
|
2019-08-11 12:13:59 +02:00
|
|
|
type Table interface {
|
|
|
|
|
jet.SerializerTable
|
|
|
|
|
readableTable
|
|
|
|
|
|
2019-08-11 14:29:03 +02:00
|
|
|
INSERT(columns ...jet.Column) InsertStatement
|
|
|
|
|
UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
DELETE() DeleteStatement
|
2019-08-11 16:55:18 +02:00
|
|
|
LOCK() LockStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type readableTable interface {
|
|
|
|
|
// Generates a select query on the current tableName.
|
2019-08-15 13:54:05 +02:00
|
|
|
SELECT(projection Projection, projections ...Projection) SelectStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
// Creates a inner join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
INNER_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
// Creates a left join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
LEFT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
// Creates a right join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
RIGHT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
// Creates a full join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
FULL_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
// Creates a cross join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
CROSS_JOIN(table ReadableTable) joinSelectUpdateTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type joinSelectUpdateTable interface {
|
|
|
|
|
ReadableTable
|
|
|
|
|
UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// ReadableTable interface
|
2019-08-11 12:13:59 +02:00
|
|
|
type ReadableTable interface {
|
|
|
|
|
readableTable
|
2019-08-11 14:29:03 +02:00
|
|
|
jet.Serializer
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type readableTableInterfaceImpl struct {
|
|
|
|
|
parent ReadableTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generates a select query on the current tableName.
|
2019-08-15 13:54:05 +02:00
|
|
|
func (r *readableTableInterfaceImpl) SELECT(projection1 Projection, projections ...Projection) SelectStatement {
|
|
|
|
|
return newSelectStatement(r.parent, append([]Projection{projection1}, projections...))
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a inner join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
func (r *readableTableInterfaceImpl) INNER_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
2019-08-11 12:13:59 +02:00
|
|
|
return newJoinTable(r.parent, table, jet.InnerJoin, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a left join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
func (r *readableTableInterfaceImpl) LEFT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
2019-08-11 12:13:59 +02:00
|
|
|
return newJoinTable(r.parent, table, jet.LeftJoin, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a right join tableName Expression using onCondition.
|
2019-08-11 18:23:02 +02:00
|
|
|
func (r *readableTableInterfaceImpl) RIGHT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
2019-08-11 12:13:59 +02:00
|
|
|
return newJoinTable(r.parent, table, jet.RightJoin, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 18:23:02 +02:00
|
|
|
func (r *readableTableInterfaceImpl) FULL_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
2019-08-11 12:13:59 +02:00
|
|
|
return newJoinTable(r.parent, table, jet.FullJoin, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 18:23:02 +02:00
|
|
|
func (r *readableTableInterfaceImpl) CROSS_JOIN(table ReadableTable) joinSelectUpdateTable {
|
2019-08-11 12:13:59 +02:00
|
|
|
return newJoinTable(r.parent, table, jet.CrossJoin, nil)
|
|
|
|
|
}
|
2019-08-03 14:10:47 +02:00
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// NewTable creates new table with schema Name, table Name and list of columns
|
2020-04-12 18:53:57 +02:00
|
|
|
func NewTable(schemaName, name string, columns ...jet.ColumnExpression) Table {
|
2019-08-11 12:13:59 +02:00
|
|
|
t := &tableImpl{
|
2020-04-12 18:53:57 +02:00
|
|
|
SerializerTable: jet.NewTable(schemaName, name, columns...),
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.readableTableInterfaceImpl.parent = t
|
|
|
|
|
t.parent = t
|
|
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tableImpl struct {
|
2019-08-17 18:32:01 +02:00
|
|
|
jet.SerializerTable
|
2019-08-11 12:13:59 +02:00
|
|
|
readableTableInterfaceImpl
|
|
|
|
|
parent Table
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 16:55:18 +02:00
|
|
|
func (t *tableImpl) INSERT(columns ...jet.Column) InsertStatement {
|
|
|
|
|
return newInsertStatement(t.parent, jet.UnwidColumnList(columns))
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 16:55:18 +02:00
|
|
|
func (t *tableImpl) UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement {
|
|
|
|
|
return newUpdateStatement(t.parent, jet.UnwindColumns(column, columns...))
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 16:55:18 +02:00
|
|
|
func (t *tableImpl) DELETE() DeleteStatement {
|
|
|
|
|
return newDeleteStatement(t.parent)
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 16:55:18 +02:00
|
|
|
func (t *tableImpl) LOCK() LockStatement {
|
|
|
|
|
return LOCK(t.parent)
|
|
|
|
|
}
|
2019-08-11 12:13:59 +02:00
|
|
|
|
2019-08-17 18:32:01 +02:00
|
|
|
type joinTable struct {
|
2019-08-11 12:13:59 +02:00
|
|
|
tableImpl
|
2019-08-17 18:32:01 +02:00
|
|
|
jet.JoinTable
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newJoinTable(lhs jet.Serializer, rhs jet.Serializer, joinType jet.JoinType, onCondition BoolExpression) Table {
|
2019-08-17 18:32:01 +02:00
|
|
|
newJoinTable := &joinTable{
|
|
|
|
|
JoinTable: jet.NewJoinTable(lhs, rhs, joinType, onCondition),
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newJoinTable.readableTableInterfaceImpl.parent = newJoinTable
|
|
|
|
|
newJoinTable.parent = newJoinTable
|
|
|
|
|
|
|
|
|
|
return newJoinTable
|
2019-08-03 14:10:47 +02:00
|
|
|
}
|