jet/select_statement.go

355 lines
7.5 KiB
Go
Raw Normal View History

2019-06-21 13:56:57 +02:00
package jet
import (
2019-06-20 12:22:19 +02:00
"context"
2019-04-20 19:49:29 +02:00
"database/sql"
2019-06-05 17:15:20 +02:00
"errors"
2019-06-21 13:56:57 +02:00
"github.com/go-jet/jet/execution"
)
2019-07-18 17:43:11 +02:00
// SelectStatement is interface for SQL SELECT statements
2019-06-04 12:10:23 +02:00
type SelectStatement interface {
2019-05-12 18:15:23 +02:00
Statement
expression
2019-06-04 12:10:23 +02:00
DISTINCT() SelectStatement
FROM(table ReadableTable) SelectStatement
WHERE(expression BoolExpression) SelectStatement
GROUP_BY(groupByClauses ...groupByClause) SelectStatement
HAVING(boolExpression BoolExpression) SelectStatement
2019-07-18 17:43:11 +02:00
ORDER_BY(orderByClauses ...orderByClause) SelectStatement
2019-06-04 12:10:23 +02:00
LIMIT(limit int64) SelectStatement
OFFSET(offset int64) SelectStatement
FOR(lock SelectLock) SelectStatement
2019-07-01 19:41:49 +02:00
UNION(rhs SelectStatement) SelectStatement
UNION_ALL(rhs SelectStatement) SelectStatement
INTERSECT(rhs SelectStatement) SelectStatement
INTERSECT_ALL(rhs SelectStatement) SelectStatement
EXCEPT(rhs SelectStatement) SelectStatement
EXCEPT_ALL(rhs SelectStatement) SelectStatement
2019-07-18 17:43:11 +02:00
AsTable(alias string) SelectTable
projections() []projection
}
2019-07-18 17:43:11 +02:00
//SELECT creates new SelectStatement with list of projections
func SELECT(projection1 projection, projections ...projection) SelectStatement {
return newSelectStatement(nil, append([]projection{projection1}, projections...))
}
type selectStatementImpl struct {
2019-03-31 09:17:28 +02:00
expressionInterfaceImpl
2019-07-01 19:41:49 +02:00
parent SelectStatement
2019-03-31 09:17:28 +02:00
table ReadableTable
distinct bool
projectionList []projection
where BoolExpression
groupBy []groupByClause
having BoolExpression
2019-07-18 17:43:11 +02:00
orderBy []orderByClause
limit, offset int64
lockFor SelectLock
}
2019-06-04 12:10:23 +02:00
func newSelectStatement(table ReadableTable, projections []projection) SelectStatement {
newSelect := &selectStatementImpl{
table: table,
projectionList: projections,
limit: -1,
offset: -1,
distinct: false,
}
newSelect.expressionInterfaceImpl.parent = newSelect
2019-07-01 19:41:49 +02:00
newSelect.parent = newSelect
return newSelect
}
2019-06-04 12:10:23 +02:00
func (s *selectStatementImpl) FROM(table ReadableTable) SelectStatement {
s.table = table
2019-07-01 19:41:49 +02:00
return s.parent
}
2019-07-18 17:43:11 +02:00
func (s *selectStatementImpl) AsTable(alias string) SelectTable {
return newSelectTable(s.parent, alias)
2019-07-01 19:41:49 +02:00
}
func (s *selectStatementImpl) WHERE(expression BoolExpression) SelectStatement {
s.where = expression
return s.parent
}
func (s *selectStatementImpl) GROUP_BY(groupByClauses ...groupByClause) SelectStatement {
s.groupBy = groupByClauses
return s.parent
}
func (s *selectStatementImpl) HAVING(expression BoolExpression) SelectStatement {
s.having = expression
return s.parent
}
2019-07-18 17:43:11 +02:00
func (s *selectStatementImpl) ORDER_BY(clauses ...orderByClause) SelectStatement {
2019-07-01 19:41:49 +02:00
s.orderBy = clauses
return s.parent
}
func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement {
s.offset = offset
return s.parent
}
func (s *selectStatementImpl) LIMIT(limit int64) SelectStatement {
s.limit = limit
return s.parent
}
func (s *selectStatementImpl) DISTINCT() SelectStatement {
s.distinct = true
return s.parent
}
func (s *selectStatementImpl) FOR(lock SelectLock) SelectStatement {
s.lockFor = lock
return s.parent
}
func (s *selectStatementImpl) UNION(rhs SelectStatement) SelectStatement {
return UNION(s.parent, rhs)
}
func (s *selectStatementImpl) UNION_ALL(rhs SelectStatement) SelectStatement {
return UNION_ALL(s.parent, rhs)
}
func (s *selectStatementImpl) INTERSECT(rhs SelectStatement) SelectStatement {
return INTERSECT(s.parent, rhs)
}
func (s *selectStatementImpl) INTERSECT_ALL(rhs SelectStatement) SelectStatement {
return INTERSECT_ALL(s.parent, rhs)
}
func (s *selectStatementImpl) EXCEPT(rhs SelectStatement) SelectStatement {
return EXCEPT(s.parent, rhs)
}
func (s *selectStatementImpl) EXCEPT_ALL(rhs SelectStatement) SelectStatement {
return EXCEPT_ALL(s.parent, rhs)
}
func (s *selectStatementImpl) projections() []projection {
return s.projectionList
}
2019-07-08 10:48:03 +02:00
func (s *selectStatementImpl) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
2019-05-13 12:33:11 +02:00
if s == nil {
2019-07-08 13:00:44 +02:00
return errors.New("jet: Select expression is nil. ")
2019-05-13 12:33:11 +02:00
}
out.writeString("(")
2019-05-12 18:15:23 +02:00
out.increaseIdent()
2019-05-07 19:06:21 +02:00
err := s.serializeImpl(out)
2019-05-12 18:15:23 +02:00
out.decreaseIdent()
if err != nil {
return err
}
out.newLine()
out.writeString(")")
return nil
}
2019-07-08 10:48:03 +02:00
func (s *selectStatementImpl) serializeImpl(out *sqlBuilder) error {
2019-05-13 12:33:11 +02:00
if s == nil {
2019-07-08 13:00:44 +02:00
return errors.New("jet: Select expression is nil. ")
2019-05-13 12:33:11 +02:00
}
out.newLine()
2019-05-12 18:15:23 +02:00
out.writeString("SELECT")
if s.distinct {
2019-05-12 18:15:23 +02:00
out.writeString("DISTINCT")
}
if len(s.projectionList) == 0 {
2019-07-08 13:00:44 +02:00
return errors.New("jet: no column selected for projection")
}
2019-07-18 17:43:11 +02:00
err := out.writeProjections(selectStatement, s.projectionList)
if err != nil {
return err
}
if s.table != nil {
2019-07-18 17:43:11 +02:00
if err := out.writeFrom(selectStatement, s.table); err != nil {
return err
}
}
if s.where != nil {
2019-07-18 17:43:11 +02:00
err := out.writeWhere(selectStatement, s.where)
2019-05-03 12:51:57 +02:00
if err != nil {
return nil
}
}
if s.groupBy != nil && len(s.groupBy) > 0 {
2019-07-18 17:43:11 +02:00
err := out.writeGroupBy(selectStatement, s.groupBy)
if err != nil {
return err
}
}
if s.having != nil {
2019-07-18 17:43:11 +02:00
err := out.writeHaving(selectStatement, s.having)
2019-05-03 12:51:57 +02:00
if err != nil {
return err
}
}
if s.orderBy != nil {
2019-07-18 17:43:11 +02:00
err := out.writeOrderBy(selectStatement, s.orderBy)
2019-05-03 12:51:57 +02:00
if err != nil {
return err
}
}
if s.limit >= 0 {
out.newLine()
2019-05-12 18:15:23 +02:00
out.writeString("LIMIT")
2019-07-18 17:43:11 +02:00
out.insertParametrizedArgument(s.limit)
}
if s.offset >= 0 {
out.newLine()
2019-05-12 18:15:23 +02:00
out.writeString("OFFSET")
2019-07-18 17:43:11 +02:00
out.insertParametrizedArgument(s.offset)
}
if s.lockFor != nil {
out.newLine()
out.writeString("FOR")
2019-07-18 17:43:11 +02:00
err := s.lockFor.serialize(selectStatement, out)
if err != nil {
return err
}
}
return nil
}
func (s *selectStatementImpl) accept(visitor visitor) {
visitor.visit(s)
if s.table != nil {
s.table.accept(visitor)
}
if s.where != nil {
s.where.accept(visitor)
}
if s.having != nil {
s.having.accept(visitor)
}
}
func (s *selectStatementImpl) Sql(dialect ...Dialect) (query string, args []interface{}, err error) {
queryData := &sqlBuilder{
dialect: detectDialect(s, dialect...),
}
err = s.serializeImpl(queryData)
if err != nil {
return "", nil, err
}
2019-05-12 18:15:23 +02:00
query, args = queryData.finalize()
return
}
func (s *selectStatementImpl) DebugSql(dialect ...Dialect) (query string, err error) {
return debugSql(s.parent, dialect...)
}
2019-07-01 19:41:49 +02:00
func (s *selectStatementImpl) Query(db execution.DB, destination interface{}) error {
return query(s.parent, db, destination)
}
func (s *selectStatementImpl) QueryContext(context context.Context, db execution.DB, destination interface{}) error {
2019-07-18 17:43:11 +02:00
return queryContext(context, s.parent, db, destination)
}
2019-07-01 19:41:49 +02:00
func (s *selectStatementImpl) Exec(db execution.DB) (res sql.Result, err error) {
return exec(s.parent, db)
}
func (s *selectStatementImpl) ExecContext(context context.Context, db execution.DB) (res sql.Result, err error) {
2019-07-19 10:46:41 +02:00
return execContext(context, s.parent, db)
}
2019-07-18 17:43:11 +02:00
// SelectLock is interface for SELECT statement locks
type SelectLock interface {
clause
NOWAIT() SelectLock
SKIP_LOCKED() SelectLock
}
type selectLockImpl struct {
lockStrength string
noWait, skipLocked bool
}
2019-07-31 18:43:54 +02:00
func NewSelectLock(name string) func() SelectLock {
return func() SelectLock {
return newSelectLock(name)
}
}
func newSelectLock(lockStrength string) SelectLock {
return &selectLockImpl{lockStrength: lockStrength}
}
func (s *selectLockImpl) NOWAIT() SelectLock {
s.noWait = true
return s
}
func (s *selectLockImpl) SKIP_LOCKED() SelectLock {
s.skipLocked = true
2019-05-12 18:15:23 +02:00
return s
}
2019-07-08 10:48:03 +02:00
func (s *selectLockImpl) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
out.writeString(s.lockStrength)
if s.noWait {
out.writeString("NOWAIT")
}
if s.skipLocked {
out.writeString("SKIP LOCKED")
}
return nil
}