jet/mysql/table.go

128 lines
3.9 KiB
Go
Raw Normal View History

2019-08-03 14:10:47 +02:00
package mysql
2020-06-27 18:48:19 +02:00
import "github.com/go-jet/jet/v2/internal/jet"
2019-08-03 14:10:47 +02:00
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(columns ...jet.Column) UpdateStatement
2019-08-11 12:13:59 +02:00
DELETE() DeleteStatement
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.
INNER_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
2019-08-11 12:13:59 +02:00
// Creates a left join tableName Expression using onCondition.
LEFT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
2019-08-11 12:13:59 +02:00
// Creates a right join tableName Expression using onCondition.
RIGHT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
2019-08-11 12:13:59 +02:00
// Creates a full join tableName Expression using onCondition.
FULL_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
2019-08-11 12:13:59 +02:00
// Creates a cross join tableName Expression using onCondition.
CROSS_JOIN(table ReadableTable) joinSelectUpdateTable
}
type joinSelectUpdateTable interface {
ReadableTable
UPDATE(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 {
root ReadableTable
2019-08-11 12:13:59 +02:00
}
// Generates a select query on the current tableName.
func (r readableTableInterfaceImpl) SELECT(projection1 Projection, projections ...Projection) SelectStatement {
return newSelectStatement(jet.SelectStatementType, r.root, append([]Projection{projection1}, projections...))
2019-08-11 12:13:59 +02:00
}
// Creates a inner join tableName Expression using onCondition.
func (r readableTableInterfaceImpl) INNER_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
return newJoinTable(r.root, table, jet.InnerJoin, onCondition)
2019-08-11 12:13:59 +02:00
}
// Creates a left join tableName Expression using onCondition.
func (r readableTableInterfaceImpl) LEFT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
return newJoinTable(r.root, table, jet.LeftJoin, onCondition)
2019-08-11 12:13:59 +02:00
}
// Creates a right join tableName Expression using onCondition.
func (r readableTableInterfaceImpl) RIGHT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
return newJoinTable(r.root, table, jet.RightJoin, onCondition)
2019-08-11 12:13:59 +02:00
}
func (r readableTableInterfaceImpl) FULL_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
return newJoinTable(r.root, table, jet.FullJoin, onCondition)
2019-08-11 12:13:59 +02:00
}
func (r readableTableInterfaceImpl) CROSS_JOIN(table ReadableTable) joinSelectUpdateTable {
return newJoinTable(r.root, table, jet.CrossJoin, nil)
2019-08-11 12:13:59 +02:00
}
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
func NewTable(schemaName, name, alias string, columns ...jet.ColumnExpression) Table {
2019-08-11 12:13:59 +02:00
t := &tableImpl{
SerializerTable: jet.NewTable(schemaName, name, alias, columns...),
2019-08-11 12:13:59 +02:00
}
t.readableTableInterfaceImpl.root = t
t.root = t
2019-08-11 12:13:59 +02:00
return t
}
type tableImpl struct {
2019-08-17 18:32:01 +02:00
jet.SerializerTable
2019-08-11 12:13:59 +02:00
readableTableInterfaceImpl
root Table
2019-08-11 12:13:59 +02:00
}
func (t *tableImpl) INSERT(columns ...jet.Column) InsertStatement {
return newInsertStatement(t.root, jet.UnwidColumnList(columns))
2019-08-11 12:13:59 +02:00
}
func (t *tableImpl) UPDATE(columns ...jet.Column) UpdateStatement {
return newUpdateStatement(t.root, jet.UnwidColumnList(columns))
2019-08-11 12:13:59 +02:00
}
func (t *tableImpl) DELETE() DeleteStatement {
return newDeleteStatement(t.root)
2019-08-11 12:13:59 +02:00
}
func (t *tableImpl) LOCK() LockStatement {
return LOCK(t.root)
}
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.root = newJoinTable
newJoinTable.root = newJoinTable
2019-08-11 12:13:59 +02:00
return newJoinTable
2019-08-03 14:10:47 +02:00
}