-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to strings.Builder and expose SQL Builder (#13)
- Loading branch information
Showing
14 changed files
with
274 additions
and
221 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,39 @@ | ||
package sq | ||
|
||
import "strings" | ||
|
||
// Builder is a helper that allows to write many Sqlizers one by one | ||
// without constant checks for errors that may come from Sqlizer | ||
type Builder struct { | ||
strings.Builder | ||
args []any | ||
err error | ||
} | ||
|
||
// WriteSql converts Sqlizer to SQL strings and writes it to strings.Builder | ||
func (b *Builder) WriteSql(item Sqlizer) { | ||
if b.err != nil { | ||
return | ||
} | ||
|
||
var str string | ||
var args []any | ||
str, args, b.err = nestedToSql(item) | ||
|
||
if b.err != nil { | ||
return | ||
} | ||
|
||
if b.Len() > 0 { | ||
b.WriteByte(' ') | ||
} | ||
b.WriteString(str) | ||
b.args = append(b.args, args...) | ||
} | ||
|
||
func (b *Builder) ToSql() (string, []any, error) { | ||
if b.err != nil { | ||
return "", nil, b.err | ||
} | ||
return b.String(), b.args, nil | ||
} |
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,66 @@ | ||
package sq | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBuilder(t *testing.T) { | ||
b := Builder{} | ||
b.WriteSql(Raw("test")) | ||
sql, args, err := b.ToSql() | ||
require.Equal(t, "test", sql) | ||
require.Empty(t, args) | ||
require.NoError(t, err) | ||
|
||
b = Builder{} | ||
b.WriteSql(Raw("one")) | ||
b.WriteSql(Raw("two")) | ||
sql, args, err = b.ToSql() | ||
require.Equal(t, "one two", sql) | ||
require.Empty(t, args) | ||
require.NoError(t, err) | ||
|
||
b = Builder{} | ||
b.WriteString("one") | ||
b.WriteSql(Raw("two")) | ||
sql, args, err = b.ToSql() | ||
require.Equal(t, "one two", sql) | ||
require.Empty(t, args) | ||
require.NoError(t, err) | ||
|
||
b = Builder{} | ||
b.WriteSql(Expr("one = ?", 1)) | ||
sql, args, err = b.ToSql() | ||
require.Equal(t, "one = ?", sql) | ||
require.Len(t, args, 1) | ||
require.Equal(t, 1, args[0]) | ||
require.NoError(t, err) | ||
|
||
b = Builder{} | ||
b.WriteSql(Expr("one = ?", 1)) | ||
b.WriteSql(Expr("AND two = ?", 2)) | ||
sql, args, err = b.ToSql() | ||
require.Equal(t, "one = ? AND two = ?", sql) | ||
require.Len(t, args, 2) | ||
require.Equal(t, 1, args[0]) | ||
require.Equal(t, 2, args[1]) | ||
require.NoError(t, err) | ||
|
||
b = Builder{} | ||
b.WriteSql(Expr("one = ?", 1)) | ||
b.WriteSql(sqlizeError{err: errors.New("fail")}) | ||
b.WriteSql(Expr("two = ?", 2)) | ||
sql, args, err = b.ToSql() | ||
require.Empty(t, sql) | ||
require.Empty(t, args) | ||
require.EqualError(t, err, "fail") | ||
} | ||
|
||
type sqlizeError struct { | ||
err error | ||
} | ||
|
||
func (se sqlizeError) ToSql() (string, []any, error) { return "", nil, se.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
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
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
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
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
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
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
Oops, something went wrong.