-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #61, introduce the new expression api for generating queries
- Loading branch information
Showing
52 changed files
with
2,573 additions
and
1,582 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package qb | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Avg function generates "avg(%s)" statement for column | ||
func Avg(column ColumnElem) AggregateClause { | ||
return Aggregate("AVG", column) | ||
} | ||
|
||
// Count function generates "count(%s)" statement for column | ||
func Count(column ColumnElem) AggregateClause { | ||
return Aggregate("COUNT", column) | ||
} | ||
|
||
// Sum function generates "sum(%s)" statement for column | ||
func Sum(column ColumnElem) AggregateClause { | ||
return Aggregate("SUM", column) | ||
} | ||
|
||
// Min function generates "min(%s)" statement for column | ||
func Min(column ColumnElem) AggregateClause { | ||
return Aggregate("MIN", column) | ||
} | ||
|
||
// Max function generates "max(%s)" statement for column | ||
func Max(column ColumnElem) AggregateClause { | ||
return Aggregate("MAX", column) | ||
} | ||
|
||
// Aggregate generates a new aggregate clause given function & column | ||
func Aggregate(fn string, column ColumnElem) AggregateClause { | ||
return AggregateClause{fn, column} | ||
} | ||
|
||
// AggregateClause is the base struct for building aggregate functions | ||
type AggregateClause struct { | ||
fn string | ||
column ColumnElem | ||
} | ||
|
||
// Build compiles the aggregate clause and returns the sql and bindings | ||
func (c AggregateClause) Build(dialect Dialect) (string, []interface{}) { | ||
bindings := []interface{}{} | ||
sql := fmt.Sprintf("%s(%s)", c.fn, dialect.Escape(c.column.Name)) | ||
return sql, bindings | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package qb | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAggregates(t *testing.T) { | ||
col := Column("id", Varchar().Size(36)) | ||
assert.Equal(t, Avg(col), Aggregate("AVG", col)) | ||
assert.Equal(t, Count(col), Aggregate("COUNT", col)) | ||
assert.Equal(t, Sum(col), Aggregate("SUM", col)) | ||
assert.Equal(t, Min(col), Aggregate("MIN", col)) | ||
assert.Equal(t, Max(col), Aggregate("MAX", col)) | ||
} |
Oops, something went wrong.