2019-08-11 12:13:59 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import "github.com/go-jet/jet/internal/jet"
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// RowLock is interface for SELECT statement row lock types
|
|
|
|
|
type RowLock = jet.RowLock
|
2019-08-11 12:13:59 +02:00
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// Row lock types
|
2019-08-11 12:13:59 +02:00
|
|
|
var (
|
2019-08-17 18:32:01 +02:00
|
|
|
UPDATE = jet.NewRowLock("UPDATE")
|
|
|
|
|
SHARE = jet.NewRowLock("SHARE")
|
2019-08-11 12:13:59 +02:00
|
|
|
)
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// SelectStatement is interface for MySQL SELECT statement
|
2019-08-11 12:13:59 +02:00
|
|
|
type SelectStatement interface {
|
2019-08-15 13:54:05 +02:00
|
|
|
Statement
|
2019-08-11 12:13:59 +02:00
|
|
|
jet.HasProjections
|
2019-08-15 13:54:05 +02:00
|
|
|
Expression
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
DISTINCT() SelectStatement
|
|
|
|
|
FROM(table ReadableTable) SelectStatement
|
|
|
|
|
WHERE(expression BoolExpression) SelectStatement
|
|
|
|
|
GROUP_BY(groupByClauses ...jet.GroupByClause) SelectStatement
|
|
|
|
|
HAVING(boolExpression BoolExpression) SelectStatement
|
|
|
|
|
ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement
|
|
|
|
|
LIMIT(limit int64) SelectStatement
|
|
|
|
|
OFFSET(offset int64) SelectStatement
|
2019-08-17 10:43:16 +02:00
|
|
|
FOR(lock RowLock) SelectStatement
|
2019-08-15 11:59:17 +02:00
|
|
|
LOCK_IN_SHARE_MODE() SelectStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
UNION(rhs SelectStatement) setStatement
|
|
|
|
|
UNION_ALL(rhs SelectStatement) setStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
AsTable(alias string) SelectTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//SELECT creates new SelectStatement with list of projections
|
2019-08-15 13:54:05 +02:00
|
|
|
func SELECT(projection Projection, projections ...Projection) SelectStatement {
|
|
|
|
|
return newSelectStatement(nil, append([]Projection{projection}, projections...))
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 13:54:05 +02:00
|
|
|
func newSelectStatement(table ReadableTable, projections []Projection) SelectStatement {
|
2019-08-11 12:13:59 +02:00
|
|
|
newSelect := &selectStatementImpl{}
|
2019-08-17 18:32:01 +02:00
|
|
|
newSelect.ExpressionStatement = jet.NewExpressionStatementImpl(Dialect, jet.SelectStatementType, newSelect, &newSelect.Select,
|
2019-08-11 12:13:59 +02:00
|
|
|
&newSelect.From, &newSelect.Where, &newSelect.GroupBy, &newSelect.Having, &newSelect.OrderBy,
|
2019-08-15 11:59:17 +02:00
|
|
|
&newSelect.Limit, &newSelect.Offset, &newSelect.For, &newSelect.ShareLock)
|
2019-08-11 12:13:59 +02:00
|
|
|
|
2019-08-15 13:54:05 +02:00
|
|
|
newSelect.Select.Projections = toJetProjectionList(projections)
|
2019-08-11 12:13:59 +02:00
|
|
|
newSelect.From.Table = table
|
|
|
|
|
newSelect.Limit.Count = -1
|
|
|
|
|
newSelect.Offset.Count = -1
|
2019-08-15 11:59:17 +02:00
|
|
|
newSelect.ShareLock.Name = "LOCK IN SHARE MODE"
|
|
|
|
|
newSelect.ShareLock.InNewLine = true
|
2019-08-11 12:13:59 +02:00
|
|
|
|
|
|
|
|
newSelect.setOperatorsImpl.parent = newSelect
|
|
|
|
|
|
|
|
|
|
return newSelect
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type selectStatementImpl struct {
|
2019-08-17 18:32:01 +02:00
|
|
|
jet.ExpressionStatement
|
2019-08-11 12:13:59 +02:00
|
|
|
setOperatorsImpl
|
|
|
|
|
|
2019-08-15 11:59:17 +02:00
|
|
|
Select jet.ClauseSelect
|
|
|
|
|
From jet.ClauseFrom
|
|
|
|
|
Where jet.ClauseWhere
|
|
|
|
|
GroupBy jet.ClauseGroupBy
|
|
|
|
|
Having jet.ClauseHaving
|
|
|
|
|
OrderBy jet.ClauseOrderBy
|
|
|
|
|
Limit jet.ClauseLimit
|
|
|
|
|
Offset jet.ClauseOffset
|
|
|
|
|
For jet.ClauseFor
|
|
|
|
|
ShareLock jet.ClauseOptional
|
2019-08-11 12:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) DISTINCT() SelectStatement {
|
|
|
|
|
s.Select.Distinct = true
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) FROM(table ReadableTable) SelectStatement {
|
|
|
|
|
s.From.Table = table
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) WHERE(condition BoolExpression) SelectStatement {
|
|
|
|
|
s.Where.Condition = condition
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) GROUP_BY(groupByClauses ...jet.GroupByClause) SelectStatement {
|
|
|
|
|
s.GroupBy.List = groupByClauses
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) HAVING(boolExpression BoolExpression) SelectStatement {
|
|
|
|
|
s.Having.Condition = boolExpression
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement {
|
|
|
|
|
s.OrderBy.List = orderByClauses
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) LIMIT(limit int64) SelectStatement {
|
|
|
|
|
s.Limit.Count = limit
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement {
|
|
|
|
|
s.Offset.Count = offset
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
func (s *selectStatementImpl) FOR(lock RowLock) SelectStatement {
|
2019-08-11 12:13:59 +02:00
|
|
|
s.For.Lock = lock
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 11:59:17 +02:00
|
|
|
func (s *selectStatementImpl) LOCK_IN_SHARE_MODE() SelectStatement {
|
|
|
|
|
s.ShareLock.Show = true
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 12:13:59 +02:00
|
|
|
func (s *selectStatementImpl) AsTable(alias string) SelectTable {
|
|
|
|
|
return newSelectTable(s, alias)
|
|
|
|
|
}
|