Skip to content

Commit

Permalink
feat: append column method
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdand-weka committed Jul 8, 2024
1 parent 30e7053 commit 78fc90f
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions orm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,30 @@ func (q *Query) ExcludeColumn(columns ...string) *Query {
return q
}

// AppendColumn appends column to the list of default columns if they weren't set,
// while Column method overrides defaults
func (q *Query) AppendColumn(columns ...string) *Query {
t := q.tableModel.Table()
if q.columns == nil {
for _, f := range t.Fields {
q.columns = append(q.columns, SafeQuery("?.?", t.Alias, f.Column))
}
}
return q.Column(columns...)
}

// AppendColumnExpr appends column expression to the list of default columns if they weren't set,
// while ColumnExpr method overrides defaults
func (q *Query) AppendColumnExpr(expr string, params ...interface{}) *Query {
t := q.tableModel.Table()
if q.columns == nil {
for _, f := range t.Fields {
q.columns = append(q.columns, SafeQuery("?.?", t.Alias, f.Column))
}
}
return q.ColumnExpr(expr, params...)
}

func (q *Query) excludeColumn(column string) bool {
for i := 0; i < len(q.columns); i++ {
app, ok := q.columns[i].(fieldAppender)
Expand Down Expand Up @@ -482,60 +506,60 @@ func (q *Query) WhereOr(condition string, params ...interface{}) *Query {

// WhereGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.WhereOr("FALSE").WhereOr("TRUE").
// return q, nil
// })
// q.Where("TRUE").
// WhereGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.WhereOr("FALSE").WhereOr("TRUE").
// return q, nil
// })
//
// generates
//
// WHERE TRUE AND (FALSE OR TRUE)
// WHERE TRUE AND (FALSE OR TRUE)
func (q *Query) WhereGroup(fn func(*Query) (*Query, error)) *Query {
return q.whereGroup(" AND ", fn)
}

// WhereGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereNotGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.WhereOr("FALSE").WhereOr("TRUE").
// return q, nil
// })
// q.Where("TRUE").
// WhereNotGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.WhereOr("FALSE").WhereOr("TRUE").
// return q, nil
// })
//
// generates
//
// WHERE TRUE AND NOT (FALSE OR TRUE)
// WHERE TRUE AND NOT (FALSE OR TRUE)
func (q *Query) WhereNotGroup(fn func(*Query) (*Query, error)) *Query {
return q.whereGroup(" AND NOT ", fn)
}

// WhereOrGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereOrGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.Where("FALSE").Where("TRUE").
// return q, nil
// })
// q.Where("TRUE").
// WhereOrGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.Where("FALSE").Where("TRUE").
// return q, nil
// })
//
// generates
//
// WHERE TRUE OR (FALSE AND TRUE)
// WHERE TRUE OR (FALSE AND TRUE)
func (q *Query) WhereOrGroup(fn func(*Query) (*Query, error)) *Query {
return q.whereGroup(" OR ", fn)
}

// WhereOrGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereOrGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.Where("FALSE").Where("TRUE").
// return q, nil
// })
// q.Where("TRUE").
// WhereOrGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.Where("FALSE").Where("TRUE").
// return q, nil
// })
//
// generates
//
// WHERE TRUE OR NOT (FALSE AND TRUE)
// WHERE TRUE OR NOT (FALSE AND TRUE)
func (q *Query) WhereOrNotGroup(fn func(*Query) (*Query, error)) *Query {
return q.whereGroup(" OR NOT ", fn)
}
Expand Down Expand Up @@ -591,7 +615,7 @@ func (q *Query) addWhere(f queryWithSepAppender) {
// WherePK adds condition based on the model primary keys.
// Usually it is the same as:
//
// Where("id = ?id")
// Where("id = ?id")
func (q *Query) WherePK() *Query {
if !q.hasTableModel() {
q.err(errModelNil)
Expand Down Expand Up @@ -801,7 +825,7 @@ func (q *Query) countSelectQuery(column string) *SelectQuery {
// First sorts rows by primary key and selects the first row.
// It is a shortcut for:
//
// q.OrderExpr("id ASC").Limit(1)
// q.OrderExpr("id ASC").Limit(1)
func (q *Query) First() error {
table := q.tableModel.Table()

Expand All @@ -816,7 +840,7 @@ func (q *Query) First() error {
// Last sorts rows by primary key and selects the last row.
// It is a shortcut for:
//
// q.OrderExpr("id DESC").Limit(1)
// q.OrderExpr("id DESC").Limit(1)
func (q *Query) Last() error {
table := q.tableModel.Table()

Expand Down

0 comments on commit 78fc90f

Please sign in to comment.