예제 #1
0
func (b *QueryBuilder) Where(parts ...interface{}) *QueryBuilder {
	// Note : in theory there should be only one where part
	if len(parts) > 1 {
		return b.add(where, []interface{}{expression.And(parts...)}, false)
	} else if len(parts) == 1 {
		return b.add(where, parts, false)
	}
	return b
}
예제 #2
0
func (b *QueryBuilder) RightJoin(fromAlias, joinTable, joinAlias string, joinCondition ...interface{}) *QueryBuilder {
	if len(joinCondition) > 1 {
		joinCondition = []interface{}{expression.And(joinCondition...)}
	}
	return b.add(join, []interface{}{Join{
		FromAlias:  fromAlias,
		Table:      joinTable,
		Alias:      joinAlias,
		Type:       rightJoinType,
		Conditions: joinCondition,
	}}, true)
}
예제 #3
0
func mapToExpression(criteria map[string]interface{}) (Expression *expression.Expression, data []interface{}) {
	parts := []interface{}{}
	for key, value := range criteria {
		switch {
		case regexp.MustCompile(`^\w+$`).MatchString(strings.TrimSpace(key)):
			parts = append(parts, expression.Eq(key, "?"))
		default:
			parts = append(parts, key+" ? ")
		}
		data = append(data, value)
	}
	Expression = expression.And(parts...)
	return
}
예제 #4
0
// AndWhere adds one or more restrictions to the query results, forming a logical
// conjunction with any previously specified restrictions.
func (b *QueryBuilder) AndWhere(parts ...interface{}) *QueryBuilder {
	wherePart := b.sqlParts[where]
	// if there is already a where part
	if len(wherePart) == 1 {
		// if this where part is an expression
		if exp, ok := wherePart[0].(*expression.Expression); ok && exp.Type == expression.AND {
			// if that expression is of type AND
			// just add the parts to that expression
			return b.add(where, []interface{}{exp.Add(parts...)}, false)
		}
		// if not and wherePart and parts to a AND expression
		return b.add(where,
			[]interface{}{expression.And(append([]interface{}{wherePart[0]}, parts...)...)},
			false)
	}
	return b.Where(parts...)
}
예제 #5
0
// AndHaving adds a restriction over the groups of the query, forming a logical
// conjunction with any existing having restrictions.
func (b *QueryBuilder) AndHaving(parts ...interface{}) *QueryBuilder {
	if len(b.sqlParts[having]) > 0 {
		return b.add(having, []interface{}{expression.And(b.sqlParts[having][0]).Add(parts...)}, false)
	}
	return b.add(having, []interface{}{expression.And(parts...)}, false)
}
예제 #6
0
// Having Specifies a restriction over the groups of the query.
// Replaces any previous having restrictions, if any.
func (b *QueryBuilder) Having(parts ...interface{}) *QueryBuilder {
	return b.add(having, []interface{}{expression.And(parts...)}, false)
}