-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
6 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,26 @@ | ||
package squirrel | ||
|
||
import ( | ||
"fmt" | ||
"github.com/lann/builder" | ||
) | ||
|
||
type fromPart part | ||
|
||
func newFromPart(pred interface{}) Sqlizer { | ||
return &fromPart{pred: pred, args: []interface{}{}} | ||
} | ||
|
||
func (p fromPart) ToSql() (sql string, args []interface{}, err error) { | ||
switch pred := p.pred.(type) { | ||
case SelectBuilder: | ||
entity := builder.GetStruct(pred).(selectData) | ||
sql, args, err = entity.toSql(true) | ||
case string: | ||
sql = pred | ||
args = []interface{}{} | ||
default: | ||
err = fmt.Errorf("expected string or SelectBuilder, not %T", pred) | ||
} | ||
return | ||
} |
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,43 @@ | ||
package squirrel | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFromPartsAppendToSqlWithString(t *testing.T) { | ||
parts := []Sqlizer{ | ||
newFromPart("TABLE_NAME"), | ||
} | ||
|
||
sql := &bytes.Buffer{} | ||
args, _ := appendToSql(parts, sql, "", []interface{}{}) | ||
assert.Equal(t, "TABLE_NAME", sql.String()) | ||
assert.Equal(t, []interface{}{}, args) | ||
} | ||
|
||
func TestFromPartAppendToSqlWithSelect(t *testing.T) { | ||
parts := []Sqlizer{ | ||
newFromPart(Select("*").Where(Eq{"col":"value"})), | ||
} | ||
|
||
sql := &bytes.Buffer{} | ||
args, _ := appendToSql(parts, sql, "", []interface{}{}) | ||
assert.Equal(t, "( SELECT * WHERE col = ? )", sql.String()) | ||
assert.Equal(t, []interface{}{"value"}, args) | ||
} | ||
|
||
func TestFromPartAppendToSqlErr(t *testing.T) { | ||
parts := []Sqlizer{ | ||
newFromPart(1), | ||
} | ||
|
||
_, err := appendToSql(parts, &bytes.Buffer{}, "", []interface{}{}) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestFromPartToSqlErr(t *testing.T) { | ||
_, _, err := newFromPart(1).ToSql() | ||
assert.Error(t, err) | ||
} |
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