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-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-04 12:10:23 +02:00
|
|
|
FOR_UPDATE() SelectStatement
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
AsTable(alias string) expressionTable
|
2019-05-05 12:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func SELECT(projection ...projection) SelectStatement {
|
2019-05-05 12:37:23 +02:00
|
|
|
return newSelectStatement(nil, projection)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
// NOTE: SelectStatement purposely does not implement the tableImpl interface since
|
2019-03-30 10:17:32 +01:00
|
|
|
// mysql's subquery performance is horrible.
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
forUpdate bool
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func defaultProjectionAliasing(projections []projection) []projection {
|
|
|
|
|
aliasedProjections := []projection{}
|
2019-05-03 12:51:57 +02:00
|
|
|
|
|
|
|
|
for _, projection := range projections {
|
2019-06-05 17:15:20 +02:00
|
|
|
if column, ok := projection.(Column); ok {
|
2019-05-03 12:51:57 +02:00
|
|
|
aliasedProjections = append(aliasedProjections, column.DefaultAlias())
|
|
|
|
|
} else if columnList, ok := projection.(ColumnList); ok {
|
|
|
|
|
aliasedProjections = append(aliasedProjections, columnList.DefaultAlias()...)
|
|
|
|
|
} else {
|
|
|
|
|
aliasedProjections = append(aliasedProjections, projection)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return aliasedProjections
|
|
|
|
|
}
|
|
|
|
|
|
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-05-03 12:51:57 +02:00
|
|
|
projections: defaultProjectionAliasing(projections),
|
2019-04-29 14:39:48 +02:00
|
|
|
limit: -1,
|
|
|
|
|
offset: -1,
|
|
|
|
|
forUpdate: false,
|
|
|
|
|
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-05-12 18:15:23 +02:00
|
|
|
out.nextLine()
|
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-05-12 18:15:23 +02:00
|
|
|
out.nextLine()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.projections == nil || len(s.projections) == 0 {
|
|
|
|
|
return errors.New("No column selected for projection.")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
err := out.writeProjection(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-05-12 18:15:23 +02:00
|
|
|
out.nextLine()
|
|
|
|
|
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-05-12 18:15:23 +02:00
|
|
|
out.nextLine()
|
|
|
|
|
out.writeString("OFFSET")
|
2019-06-03 14:41:39 +02:00
|
|
|
out.insertPreparedArgument(s.offset)
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.forUpdate {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.nextLine()
|
|
|
|
|
out.writeString("FOR UPDATE")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
// Return the properly escaped SQL Statement, against the specified database
|
|
|
|
|
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-05-07 19:06:21 +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-04 12:10:23 +02:00
|
|
|
func (s *selectStatementImpl) FOR_UPDATE() SelectStatement {
|
2019-05-12 18:15:23 +02:00
|
|
|
s.forUpdate = true
|
|
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
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-05-27 13:11:15 +02:00
|
|
|
func (s *selectStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
2019-05-12 18:15:23 +02:00
|
|
|
return Execute(s, db)
|
2019-05-01 14:42:46 +02:00
|
|
|
}
|