Fix warnings and typos

This commit is contained in:
Aleksander Zaruczewski
2022-02-17 16:07:23 +03:00
parent c93cf88737
commit 157445026c
8 changed files with 58 additions and 57 deletions
+4 -4
View File
@@ -90,18 +90,18 @@ func (args *Args) add(arg interface{}) int {
// $0 $1 ... $n refers nth-argument passed in the call. Next $? will use arguments n+1.
// ${name} refers a named argument created by `Named` with `name`.
// $$ is a "$" string.
func (args *Args) Compile(format string, intialValue ...interface{}) (query string, values []interface{}) {
return args.CompileWithFlavor(format, args.Flavor, intialValue...)
func (args *Args) Compile(format string, initialValue ...interface{}) (query string, values []interface{}) {
return args.CompileWithFlavor(format, args.Flavor, initialValue...)
}
// CompileWithFlavor compiles builder's format to standard sql with flavor and returns associated args.
//
// See doc for `Compile` to learn details.
func (args *Args) CompileWithFlavor(format string, flavor Flavor, intialValue ...interface{}) (query string, values []interface{}) {
func (args *Args) CompileWithFlavor(format string, flavor Flavor, initialValue ...interface{}) (query string, values []interface{}) {
buf := &bytes.Buffer{}
idx := strings.IndexRune(format, '$')
offset := 0
values = intialValue
values = initialValue
if flavor == invalidFlavor {
flavor = DefaultFlavor
+6 -6
View File
@@ -16,8 +16,8 @@ func ExampleBuildf() {
sb.Select("id").From("user")
explain := Buildf("EXPLAIN %v LEFT JOIN SELECT * FROM banned WHERE state IN (%v, %v)", sb, 1, 2)
sql, args := explain.Build()
fmt.Println(sql)
s, args := explain.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
@@ -31,9 +31,9 @@ func ExampleBuild() {
b := Build("EXPLAIN $? LEFT JOIN SELECT * FROM $? WHERE created_at > $? AND state IN (${states}) AND modified_at BETWEEN $2 AND $?",
sb, Raw("banned"), 1514458225, 1514544625, Named("states", List([]int{3, 4, 5})))
sql, args := b.Build()
s, args := b.Build()
fmt.Println(sql)
fmt.Println(s)
fmt.Println(args)
// Output:
@@ -49,9 +49,9 @@ func ExampleBuildNamed() {
"name": "Huan%",
"table": Raw("user"),
})
sql, args := b.Build()
s, args := b.Build()
fmt.Println(sql)
fmt.Println(s)
fmt.Println(args)
// Output:
-5
View File
@@ -15,11 +15,6 @@ type injection struct {
type injectionMarker int
type injectedSQL struct {
marker injectionMarker
sql string
}
// newInjection creates a new injection.
func newInjection() *injection {
return &injection{
+14 -14
View File
@@ -10,7 +10,7 @@ import (
func ExampleSelect() {
// Build a SQL to create a HIVE table.
sql := CreateTable("users").
s := CreateTable("users").
SQL("PARTITION BY (year)").
SQL("AS").
SQL(
@@ -21,7 +21,7 @@ func ExampleSelect() {
).
String()
fmt.Println(sql)
fmt.Println(s)
// Output:
// CREATE TABLE users PARTITION BY (year) AS SELECT columns[0] id, columns[1] name, columns[2] year FROM `all-users.csv` LIMIT 100
@@ -48,8 +48,8 @@ func ExampleSelectBuilder() {
sb.OrderBy("modified_at").Asc()
sb.Limit(10).Offset(5)
sql, args := sb.Build()
fmt.Println(sql)
s, args := sb.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
@@ -77,8 +77,8 @@ func ExampleSelectBuilder_advancedUsage() {
innerSb.NotIn("name", Flatten([]string{"Huan Du", "Charmy Liu"})...),
)
sql, args := sb.Build()
fmt.Println(sql)
s, args := sb.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
@@ -117,8 +117,8 @@ func ExampleSelectBuilder_limit_offset() {
sb := NewSelectBuilder()
saveResults := func() {
for i, f := range flavors {
sql, _ := sb.BuildWithFlavor(f)
results[i] = append(results[i], sql)
s, _ := sb.BuildWithFlavor(f)
results[i] = append(results[i], s)
}
}
@@ -159,8 +159,8 @@ func ExampleSelectBuilder_limit_offset() {
fmt.Println()
fmt.Println(flavors[i])
for n, sql := range result {
fmt.Printf("#%d: %s\n", n+1, sql)
for n, s := range result {
fmt.Printf("#%d: %s\n", n+1, s)
}
}
@@ -215,8 +215,8 @@ func ExampleSelectBuilder_varInCols() {
sb.Select(Escape("colHasA$Sign"), v)
sb.From("table")
sql, args := sb.Build()
fmt.Println(sql)
s, args := sb.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
@@ -249,8 +249,8 @@ func ExampleSelectBuilder_SQL() {
sb.ForShare()
sb.SQL("/* after for */")
sql := sb.String()
fmt.Println(sql)
s := sb.String()
fmt.Println(s)
// Output:
// /* before */ SELECT u.id, u.name, c.type, p.nickname /* after select */ FROM user u /* after from */ JOIN contract c ON u.id = c.user_id RIGHT OUTER JOIN person p ON u.id = p.user_id /* after join */ WHERE u.modified_at > u.created_at /* after where */ ORDER BY id /* after order by */ LIMIT 10 /* after limit */ FOR SHARE /* after for */
+8 -8
View File
@@ -42,7 +42,7 @@ type Struct struct {
Flavor Flavor
structType reflect.Type
structFieldsParser structFieldsieldsParser
structFieldsParser structFieldsParser
}
var emptyStruct Struct
@@ -68,9 +68,9 @@ func NewStruct(structValue interface{}) *Struct {
// For sets the default flavor of s and returns a shadow copy of s.
// The original s.Flavor is not changed.
func (s *Struct) For(flavor Flavor) *Struct {
copy := *s
copy.Flavor = flavor
return &copy
c := *s
c.Flavor = flavor
return &c
}
// WithFieldMapper returns a new Struct based on s with custom field mapper.
@@ -80,9 +80,9 @@ func (s *Struct) WithFieldMapper(mapper FieldMapperFunc) *Struct {
return &emptyStruct
}
copy := *s
copy.structFieldsParser = makeCustomFieldsParser(s.structType, mapper)
return &copy
c := *s
c.structFieldsParser = makeCustomFieldsParser(s.structType, mapper)
return &c
}
// SelectFrom creates a new `SelectBuilder` with table name.
@@ -291,7 +291,7 @@ func (s *Struct) buildColsAndValuesForTag(ib *InsertBuilder, tag string, value .
filteredValues := make([][]interface{}, len(values))
for i, cnt := range nilCols {
// If all values are nil in a column, ignore the column completedly.
// If all values are nil in a column, ignore the column completely.
if cnt == len(values) {
continue
}
+20 -14
View File
@@ -181,7 +181,7 @@ func TestStructAddr(t *testing.T) {
CreatedAt: 1234567890,
}
str := fmt.Sprintf("%v %v %v %v", expected.ID, expected.Name, expected.Status, expected.CreatedAt)
fmt.Sscanf(str, "%d%s%d%d", userForTest.Addr(user)...)
_, _ = fmt.Sscanf(str, "%d%s%d%d", userForTest.Addr(user)...)
a.Equal(user, expected)
}
@@ -197,7 +197,7 @@ func TestStructAddrForTag(t *testing.T) {
}
user.CreatedAt = 9876543210
str := fmt.Sprintf("%v %v %v %v", expected.ID, expected.Name, expected.Status, expected.CreatedAt)
fmt.Sscanf(str, "%d%s%d%d", userForTest.AddrForTag("important", user)...)
_, _ = fmt.Sscanf(str, "%d%s%d%d", userForTest.AddrForTag("important", user)...)
expected.CreatedAt = 9876543210
a.Equal(user, expected)
@@ -213,7 +213,7 @@ func TestStructAddrWithCols(t *testing.T) {
CreatedAt: 1234567890,
}
str := fmt.Sprintf("%v %v %v %v", expected.Name, expected.ID, expected.CreatedAt, expected.Status)
fmt.Sscanf(str, "%s%d%d%d", userForTest.AddrWithCols([]string{"Name", "id", "created_at", "status"}, user)...)
_, _ = fmt.Sscanf(str, "%s%d%d%d", userForTest.AddrWithCols([]string{"Name", "id", "created_at", "status"}, user)...)
a.Equal(user, expected)
}
@@ -240,11 +240,11 @@ type State int
type testDB int
type testRows int
func (db testDB) Query(query string, args ...interface{}) (testRows, error) {
func (db testDB) Query(string, ...interface{}) (testRows, error) {
return 0, nil
}
func (db testDB) Exec(query string, args ...interface{}) {
func (db testDB) Exec(string, ...interface{}) {
return
}
@@ -253,7 +253,7 @@ func (rows testRows) Close() error {
}
func (rows testRows) Scan(dest ...interface{}) error {
fmt.Sscan("1234 huandu 1", dest...)
_, _ = fmt.Sscan("1234 huandu 1", dest...)
return nil
}
@@ -285,11 +285,13 @@ func ExampleStruct_useStructAsORM() {
// Execute the query.
sql, args := sb.Build()
rows, _ := db.Query(sql, args...)
defer rows.Close()
defer func(rows testRows) {
_ = rows.Close()
}(rows)
// Scan row data to user.
var user User
rows.Scan(userStruct.Addr(&user)...)
_ = rows.Scan(userStruct.Addr(&user)...)
fmt.Println(sql)
fmt.Println(args)
@@ -343,8 +345,10 @@ func ExampleStruct_useTag() {
var order Order
sql, args := orderStruct.SelectFromForTag(table, tag).Where("id = 1234").Build()
rows, _ := db.Query(sql, args...)
defer rows.Close()
rows.Scan(orderStruct.AddrForTag(tag, &order)...)
defer func(rows testRows) {
_ = rows.Close()
}(rows)
_ = rows.Scan(orderStruct.AddrForTag(tag, &order)...)
// Discount for this user.
// Use tag "update" to update necessary columns only.
@@ -365,8 +369,10 @@ func ExampleStruct_useTag() {
var order Order
sql, args := orderStruct.SelectFromForTag(table, tag).Where("id = 1234").Build()
rows, _ := db.Query(sql, args...)
defer rows.Close()
rows.Scan(orderStruct.AddrForTag(tag, &order)...)
defer func(rows testRows) {
_ = rows.Close()
}(rows)
_ = rows.Scan(orderStruct.AddrForTag(tag, &order)...)
// Update state to paid when user has paid for the order.
// Use tag "paid" to update necessary columns only.
@@ -725,13 +731,13 @@ func TestStructFieldMapper(t *testing.T) {
}
var actual structWithMapper
str := fmt.Sprintf("%v %v %v %v", expected.FieldName1, expected.FieldNameSetByTag, expected.EmbeddedField2, expected.EmbeddedAndEmbeddedField1)
fmt.Sscanf(str, "%d%s%d%d", s.Addr(&actual)...)
_, _ = fmt.Sscanf(str, "%d%s%d%d", s.Addr(&actual)...)
sql, _ = sWithoutMapper.SelectFrom("t").Build()
a.Equal(sql, "SELECT t.`FieldName1`, t.set_by_tag, t.field_name1, t.EmbeddedField2, t.EmbeddedAndEmbeddedField1 FROM t")
}
func SomeOtherMapper(s string) string {
func SomeOtherMapper(string) string {
return ""
}
+4 -4
View File
@@ -12,17 +12,17 @@ type structFields struct {
omitEmptyFields map[string]omitEmptyTagMap
}
type structFieldsieldsParser func() *structFields
type structFieldsParser func() *structFields
func makeDefaultFieldsParser(t reflect.Type) structFieldsieldsParser {
func makeDefaultFieldsParser(t reflect.Type) structFieldsParser {
return makeFieldsParser(t, nil, true)
}
func makeCustomFieldsParser(t reflect.Type, mapper FieldMapperFunc) structFieldsieldsParser {
func makeCustomFieldsParser(t reflect.Type, mapper FieldMapperFunc) structFieldsParser {
return makeFieldsParser(t, mapper, false)
}
func makeFieldsParser(t reflect.Type, mapper FieldMapperFunc, useDefault bool) structFieldsieldsParser {
func makeFieldsParser(t reflect.Type, mapper FieldMapperFunc, useDefault bool) structFieldsParser {
var once sync.Once
sf := &structFields{
fieldAlias: map[string]string{},
+2 -2
View File
@@ -67,14 +67,14 @@ func (ub *UpdateBuilder) Update(table string) *UpdateBuilder {
return ub
}
// Set sets the assignements in SET.
// Set sets the assignments in SET.
func (ub *UpdateBuilder) Set(assignment ...string) *UpdateBuilder {
ub.assignments = assignment
ub.marker = updateMarkerAfterSet
return ub
}
// SetMore appends the assignements in SET.
// SetMore appends the assignments in SET.
func (ub *UpdateBuilder) SetMore(assignment ...string) *UpdateBuilder {
ub.assignments = append(ub.assignments, assignment...)
ub.marker = updateMarkerAfterSet