Skip to content

Commit

Permalink
Add simple ToSql implementations (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jan 4, 2023
1 parent 506ea87 commit 840afbb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,31 @@ type ToSQL interface {
// StringStatement is a plain string statement.
type StringStatement string

// Plain is a plain string statement.
type Plain = StringStatement

// ToSql implements query builder result.
func (s StringStatement) ToSql() (string, []interface{}, error) { //nolint // Method name matches ext. implementation.
return string(s), nil, nil
}

type stmt struct {
query string
args []interface{}
}

func (s stmt) ToSql() (string, []interface{}, error) { //nolint // Method name matches ext. implementation.
return s.query, s.args, nil
}

// Stmt is a statement with placeholder arguments.
func Stmt(query string, args ...interface{}) ToSQL {
return stmt{
query: query,
args: args,
}
}

// Open opens a database specified by its database driver name and a
// driver-specific data source name, usually consisting of at least a
// database name and connection information.
Expand Down
14 changes: 14 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,17 @@ func TestStorage_DB(t *testing.T) {

assert.Equal(t, dbx, st.DB())
}

func TestStmt_ToSql(t *testing.T) {
s, a, err := sqluct.Stmt("SELECT * FROM foo WHERE id=? AND name=?", 1, "bar").ToSql()
assert.Equal(t, "SELECT * FROM foo WHERE id=? AND name=?", s)
assert.Equal(t, []interface{}{1, "bar"}, a)
assert.NoError(t, err)
}

func TestPlain_ToSql(t *testing.T) {
s, a, err := sqluct.Plain("SELECT * FROM foo WHERE id=1 AND name='bar'").ToSql()
assert.Equal(t, "SELECT * FROM foo WHERE id=1 AND name='bar'", s)
assert.Nil(t, a)
assert.NoError(t, err)
}

0 comments on commit 840afbb

Please sign in to comment.