2019-03-30 10:17:32 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-04-20 19:49:29 +02:00
|
|
|
"database/sql"
|
2019-03-30 10:17:32 +01:00
|
|
|
"github.com/dropbox/godropbox/errors"
|
2019-04-07 10:05:41 +02:00
|
|
|
"github.com/sub0zero/go-sqlbuilder/types"
|
2019-03-30 10:17:32 +01:00
|
|
|
)
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
type selectStatement interface {
|
|
|
|
|
statement
|
|
|
|
|
expression
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-07 19:06:21 +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-05-07 19:06:21 +02:00
|
|
|
LIMIT(limit int64) selectStatement
|
|
|
|
|
OFFSET(offset int64) selectStatement
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-07 19:06:21 +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-05-07 19:06:21 +02:00
|
|
|
var SELECT = func(projection ...projection) selectStatement {
|
2019-05-05 12:37:23 +02:00
|
|
|
return newSelectStatement(nil, projection)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
// NOTE: selectStatement purposely does not implement the Table 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-07 19:06:21 +02:00
|
|
|
table readableTable
|
2019-04-29 14:39:48 +02:00
|
|
|
distinct bool
|
2019-05-07 19:06:21 +02:00
|
|
|
projections []projection
|
|
|
|
|
where boolExpression
|
|
|
|
|
groupBy []groupByClause
|
|
|
|
|
having boolExpression
|
|
|
|
|
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-05-07 19:06:21 +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-05-07 19:06:21 +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-05-07 19:06:21 +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-07 19:06:21 +02:00
|
|
|
func (s *selectStatementImpl) serialize(out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
out.WriteString("(")
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
err := s.serializeImpl(out)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.WriteString(")")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (s *selectStatementImpl) serializeImpl(out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
out.WriteString("SELECT ")
|
2019-05-03 12:51:57 +02:00
|
|
|
out.statementType = select_statement
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if s.distinct {
|
|
|
|
|
out.WriteString("DISTINCT ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.projections == nil || len(s.projections) == 0 {
|
|
|
|
|
return errors.New("No column selected for projection.")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
err := out.WriteProjection(s.projections)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.WriteString(" FROM ")
|
|
|
|
|
|
|
|
|
|
if s.table == nil {
|
|
|
|
|
return errors.Newf("nil tableName.")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
if err := s.table.serializeSql(out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.where != nil {
|
2019-05-03 12:51:57 +02:00
|
|
|
err := out.WriteWhere(s.where)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.groupBy != nil && len(s.groupBy) > 0 {
|
2019-05-03 12:51:57 +02:00
|
|
|
err := out.WriteGroupBy(s.groupBy)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.having != nil {
|
2019-05-03 12:51:57 +02:00
|
|
|
err := out.WriteHaving(s.having)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.orderBy != nil {
|
2019-05-03 12:51:57 +02:00
|
|
|
err := out.WriteOrderBy(s.orderBy)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.limit >= 0 {
|
|
|
|
|
out.WriteString(" LIMIT ")
|
|
|
|
|
out.InsertArgument(s.limit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.offset >= 0 {
|
|
|
|
|
out.WriteString(" OFFSET ")
|
|
|
|
|
out.InsertArgument(s.offset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.forUpdate {
|
|
|
|
|
out.WriteString(" FOR UPDATE")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the properly escaped SQL statement, against the specified database
|
|
|
|
|
func (q *selectStatementImpl) Sql() (query string, args []interface{}, err error) {
|
|
|
|
|
queryData := queryData{}
|
|
|
|
|
|
|
|
|
|
err = q.serializeImpl(&queryData)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 14:42:46 +02:00
|
|
|
return queryData.buff.String(), queryData.args, nil
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (s *selectStatementImpl) AsTable(alias string) expressionTable {
|
2019-05-05 12:37:23 +02:00
|
|
|
return &expressionTableImpl{
|
2019-03-30 10:17:32 +01:00
|
|
|
statement: s,
|
|
|
|
|
alias: alias,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) WHERE(expression boolExpression) selectStatement {
|
2019-03-30 10:17:32 +01:00
|
|
|
q.where = expression
|
|
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (s *selectStatementImpl) GROUP_BY(groupByClauses ...groupByClause) selectStatement {
|
|
|
|
|
s.groupBy = groupByClauses
|
2019-04-29 14:39:48 +02:00
|
|
|
return s
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) HAVING(expression boolExpression) selectStatement {
|
2019-03-30 10:17:32 +01:00
|
|
|
q.having = expression
|
|
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) ORDER_BY(clauses ...orderByClause) selectStatement {
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
q.orderBy = clauses
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) OFFSET(offset int64) selectStatement {
|
2019-04-29 14:39:48 +02:00
|
|
|
q.offset = offset
|
2019-03-30 10:17:32 +01:00
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) LIMIT(limit int64) selectStatement {
|
2019-04-29 14:39:48 +02:00
|
|
|
q.limit = limit
|
2019-03-30 10:17:32 +01:00
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) DISTINCT() selectStatement {
|
2019-04-29 14:39:48 +02:00
|
|
|
q.distinct = true
|
2019-03-30 10:17:32 +01:00
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (q *selectStatementImpl) FOR_UPDATE() selectStatement {
|
2019-04-29 14:39:48 +02:00
|
|
|
q.forUpdate = true
|
2019-03-30 10:17:32 +01:00
|
|
|
return q
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 14:42:46 +02:00
|
|
|
func (s *selectStatementImpl) Query(db types.Db, destination interface{}) error {
|
|
|
|
|
return Query(s, db, destination)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *selectStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
|
|
|
|
return Execute(u, db)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func NumExp(statement selectStatement) numericExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return newNumericExpressionWrap(statement)
|
|
|
|
|
}
|