2019-03-30 10:17:32 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-04-20 19:49:29 +02:00
|
|
|
"database/sql"
|
2019-06-05 17:15:20 +02:00
|
|
|
"errors"
|
2019-06-05 17:56:24 +02:00
|
|
|
"github.com/go-jet/jet/sqlbuilder/execution"
|
2019-03-30 10:17:32 +01:00
|
|
|
)
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
var (
|
|
|
|
|
UPDATE = newLock("UPDATE")
|
|
|
|
|
NO_KEY_UPDATE = newLock("NO KEY UPDATE")
|
|
|
|
|
SHARE = newLock("SHARE")
|
|
|
|
|
KEY_SHARE = newLock("KEY SHARE")
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
type SelectStatement interface {
|
2019-05-12 18:15:23 +02:00
|
|
|
Statement
|
2019-06-04 12:10:23 +02:00
|
|
|
Expression
|
2019-05-13 11:48:58 +02:00
|
|
|
hasRows()
|
2019-03-30 10:17:32 +01:00
|
|
|
|
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
|
|
|
|
|
ORDER_BY(orderByClauses ...OrderByClause) SelectStatement
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
LIMIT(limit int64) SelectStatement
|
|
|
|
|
OFFSET(offset int64) SelectStatement
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
FOR(lock SelectLock) SelectStatement
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
AsTable(alias string) ExpressionTable
|
2019-05-05 12:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func SELECT(projection1 projection, projections ...projection) SelectStatement {
|
|
|
|
|
return newSelectStatement(nil, append([]projection{projection1}, projections...))
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type selectStatementImpl struct {
|
2019-03-31 09:17:28 +02:00
|
|
|
expressionInterfaceImpl
|
2019-05-13 11:48:58 +02:00
|
|
|
isRowsType
|
2019-03-31 09:17:28 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
table ReadableTable
|
2019-04-29 14:39:48 +02:00
|
|
|
distinct bool
|
2019-05-07 19:06:21 +02:00
|
|
|
projections []projection
|
2019-05-31 12:59:57 +02:00
|
|
|
where BoolExpression
|
2019-05-07 19:06:21 +02:00
|
|
|
groupBy []groupByClause
|
2019-05-31 12:59:57 +02:00
|
|
|
having BoolExpression
|
2019-06-04 12:10:23 +02:00
|
|
|
orderBy []OrderByClause
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
limit, offset int64
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
lockFor SelectLock
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newSelectStatement(table ReadableTable, projections []projection) SelectStatement {
|
2019-05-05 12:37:23 +02:00
|
|
|
newSelect := &selectStatementImpl{
|
2019-04-29 14:39:48 +02:00
|
|
|
table: table,
|
2019-06-09 11:06:08 +02:00
|
|
|
projections: projections,
|
2019-04-29 14:39:48 +02:00
|
|
|
limit: -1,
|
|
|
|
|
offset: -1,
|
|
|
|
|
distinct: false,
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
2019-05-05 12:37:23 +02:00
|
|
|
|
|
|
|
|
newSelect.expressionInterfaceImpl.parent = newSelect
|
|
|
|
|
|
|
|
|
|
return newSelect
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) FROM(table ReadableTable) SelectStatement {
|
2019-05-05 12:37:23 +02:00
|
|
|
s.table = table
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:37:51 +02:00
|
|
|
func (s *selectStatementImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
2019-05-13 12:33:11 +02:00
|
|
|
if s == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("Select expression is nil. ")
|
2019-05-13 12:33:11 +02:00
|
|
|
}
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString("(")
|
2019-04-29 14:39:48 +02:00
|
|
|
|
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()
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(")")
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (s *selectStatementImpl) serializeImpl(out *queryData) error {
|
2019-05-13 12:33:11 +02:00
|
|
|
if s == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("Select expression is nil. ")
|
2019-05-13 12:33:11 +02:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("SELECT")
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if s.distinct {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("DISTINCT")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-09 11:06:08 +02:00
|
|
|
if len(s.projections) == 0 {
|
|
|
|
|
return errors.New("no column selected for projection")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-09 11:06:08 +02:00
|
|
|
err := out.writeProjections(select_statement, s.projections)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
if s.table != nil {
|
|
|
|
|
if err := out.writeFrom(select_statement, s.table); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.where != nil {
|
2019-05-08 13:47:01 +02:00
|
|
|
err := out.writeWhere(select_statement, s.where)
|
2019-05-03 12:51:57 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.groupBy != nil && len(s.groupBy) > 0 {
|
2019-05-08 13:47:01 +02:00
|
|
|
err := out.writeGroupBy(select_statement, s.groupBy)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.having != nil {
|
2019-05-08 13:47:01 +02:00
|
|
|
err := out.writeHaving(select_statement, s.having)
|
2019-05-03 12:51:57 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.orderBy != nil {
|
2019-05-08 13:47:01 +02:00
|
|
|
err := out.writeOrderBy(select_statement, s.orderBy)
|
2019-05-03 12:51:57 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.limit >= 0 {
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("LIMIT")
|
2019-06-03 14:41:39 +02:00
|
|
|
out.insertPreparedArgument(s.limit)
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.offset >= 0 {
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("OFFSET")
|
2019-06-03 14:41:39 +02:00
|
|
|
out.insertPreparedArgument(s.offset)
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
if s.lockFor != nil {
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-06-15 13:58:45 +02:00
|
|
|
out.writeString("FOR")
|
|
|
|
|
err := s.lockFor.serialize(select_statement, out)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
func (s *selectStatementImpl) Sql() (query string, args []interface{}, err error) {
|
2019-04-29 14:39:48 +02:00
|
|
|
queryData := queryData{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err = s.serializeImpl(&queryData)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
query, args = queryData.finalize()
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *selectStatementImpl) DebugSql() (query string, err error) {
|
|
|
|
|
return DebugSql(s)
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
func (s *selectStatementImpl) AsTable(alias string) ExpressionTable {
|
2019-06-05 17:15:20 +02:00
|
|
|
return newExpressionTable(s.parent, alias)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) WHERE(expression BoolExpression) SelectStatement {
|
2019-05-12 18:15:23 +02:00
|
|
|
s.where = expression
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) GROUP_BY(groupByClauses ...groupByClause) SelectStatement {
|
2019-05-07 19:06:21 +02:00
|
|
|
s.groupBy = groupByClauses
|
2019-04-29 14:39:48 +02:00
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) HAVING(expression BoolExpression) SelectStatement {
|
2019-05-12 18:15:23 +02:00
|
|
|
s.having = expression
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) ORDER_BY(clauses ...OrderByClause) SelectStatement {
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
s.orderBy = clauses
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement {
|
2019-05-12 18:15:23 +02:00
|
|
|
s.offset = offset
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) LIMIT(limit int64) SelectStatement {
|
2019-05-12 18:15:23 +02:00
|
|
|
s.limit = limit
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) DISTINCT() SelectStatement {
|
2019-05-12 18:15:23 +02:00
|
|
|
s.distinct = true
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func (s *selectStatementImpl) FOR(lock SelectLock) SelectStatement {
|
|
|
|
|
s.lockFor = lock
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SelectLock interface {
|
|
|
|
|
clause
|
|
|
|
|
|
|
|
|
|
NOWAIT() SelectLock
|
|
|
|
|
SKIP_LOCKED() SelectLock
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type selectLockImpl struct {
|
|
|
|
|
lockStrength string
|
|
|
|
|
noWait, skipLocked bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newLock(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-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func (s *selectLockImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
|
|
|
|
out.writeString(s.lockStrength)
|
|
|
|
|
|
|
|
|
|
if s.noWait {
|
|
|
|
|
out.writeString("NOWAIT")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.skipLocked {
|
|
|
|
|
out.writeString("SKIP LOCKED")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 13:11:15 +02:00
|
|
|
func (s *selectStatementImpl) Query(db execution.Db, destination interface{}) error {
|
2019-05-01 14:42:46 +02:00
|
|
|
return Query(s, db, destination)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func (s *selectStatementImpl) Exec(db execution.Db) (res sql.Result, err error) {
|
|
|
|
|
return Exec(s, db)
|
2019-05-01 14:42:46 +02:00
|
|
|
}
|