Skip to content

Commit

Permalink
Add StatementBuilder.Where method
Browse files Browse the repository at this point in the history
  • Loading branch information
lann committed May 9, 2020
1 parent ddedf15 commit ae9fddd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions statement.go
Expand Up @@ -41,6 +41,13 @@ func (b StatementBuilderType) RunWith(runner BaseRunner) StatementBuilderType {
return setRunWith(b, runner).(StatementBuilderType)
}

// Where adds WHERE expressions to the query.
//
// See SelectBuilder.Where for more information.
func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) StatementBuilderType {
return builder.Append(b, "WhereParts", newWherePart(pred, args...)).(StatementBuilderType)
}

// StatementBuilder is a parent builder for other builders, e.g. SelectBuilder.
var StatementBuilder = StatementBuilderType(builder.EmptyBuilder).PlaceholderFormat(Question)

Expand Down
13 changes: 13 additions & 0 deletions statement_test.go
Expand Up @@ -66,3 +66,16 @@ func TestRunWithBaseRunnerQueryRowError(t *testing.T) {
assert.Error(t, RunnerNotQueryRunner, sb.Select("test").QueryRow().Scan(nil))

}

func TestStatementBuilderWhere(t *testing.T) {
sb := StatementBuilder.Where("x = ?", 1)

sql, args, err := sb.Select("test").Where("y = ?", 2).ToSql()
assert.NoError(t, err)

expectedSql := "SELECT test WHERE x = ? AND y = ?"
assert.Equal(t, expectedSql, sql)

expectedArgs := []interface{}{1, 2}
assert.Equal(t, expectedArgs, args)
}

0 comments on commit ae9fddd

Please sign in to comment.