Skip to content

Commit

Permalink
fix #35. do not escape cols by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Oct 25, 2019
1 parent 006618a commit aa3d965
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/huandu/go-sqlbuilder

go 1.12
10 changes: 5 additions & 5 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (sb *SelectBuilder) Distinct() *SelectBuilder {

// Select sets columns in SELECT.
func (sb *SelectBuilder) Select(col ...string) *SelectBuilder {
sb.selectCols = EscapeAll(col...)
sb.selectCols = col
return sb
}

Expand Down Expand Up @@ -118,13 +118,13 @@ func (sb *SelectBuilder) Having(andExpr ...string) *SelectBuilder {

// GroupBy sets columns of GROUP BY in SELECT.
func (sb *SelectBuilder) GroupBy(col ...string) *SelectBuilder {
sb.groupByCols = EscapeAll(col...)
sb.groupByCols = col
return sb
}

// OrderBy sets columns of ORDER BY in SELECT.
func (sb *SelectBuilder) OrderBy(col ...string) *SelectBuilder {
sb.orderByCols = EscapeAll(col...)
sb.orderByCols = col
return sb
}

Expand Down Expand Up @@ -154,13 +154,13 @@ func (sb *SelectBuilder) Offset(offset int) *SelectBuilder {

// As returns an AS expression.
func (sb *SelectBuilder) As(name, alias string) string {
return fmt.Sprintf("%v AS %v", name, Escape(alias))
return fmt.Sprintf("%s AS %s", name, alias)
}

// BuilderAs returns an AS expression wrapping a complex SQL.
// According to SQL syntax, SQL built by builder is surrounded by parens.
func (sb *SelectBuilder) BuilderAs(builder Builder, alias string) string {
return fmt.Sprintf("(%v) AS %v", sb.Var(builder), Escape(alias))
return fmt.Sprintf("(%s) AS %s", sb.Var(builder), alias)
}

// String returns the compiled SELECT string.
Expand Down
19 changes: 18 additions & 1 deletion select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func ExampleSelectBuilder_join() {
}

func ExampleSelectBuilder_limit_offset() {

sb := NewSelectBuilder()
sb.Select("*")
sb.From("user")
Expand Down Expand Up @@ -153,3 +152,21 @@ func ExampleSelectBuilder_limit_offset() {
// SELECT * FROM user LIMIT 0
// SELECT * FROM user LIMIT 0
}

func ExampleSelectBuilder_varInCols() {
// Column name may contain some characters, e.g. the $ sign, which have special meanings in builders.
// It's recommended to call Escape() or EscapeAll() to escape the name.

sb := NewSelectBuilder()
v := sb.Var("foo")
sb.Select(Escape("colHasA$Sign"), v)
sb.From("table")

sql, args := sb.Build()
fmt.Println(sql)
fmt.Println(args)

// Output:
// SELECT colHasA$Sign, ? FROM table
// [foo]
}
14 changes: 7 additions & 7 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,43 @@ func (ub *UpdateBuilder) Where(andExpr ...string) *UpdateBuilder {

// Assign represents SET "field = value" in UPDATE.
func (ub *UpdateBuilder) Assign(field string, value interface{}) string {
return fmt.Sprintf("%v = %v", Escape(field), ub.args.Add(value))
return fmt.Sprintf("%s = %s", Escape(field), ub.args.Add(value))
}

// Incr represents SET "field = field + 1" in UPDATE.
func (ub *UpdateBuilder) Incr(field string) string {
f := Escape(field)
return fmt.Sprintf("%v = %v + 1", f, f)
return fmt.Sprintf("%s = %s + 1", f, f)
}

// Decr represents SET "field = field - 1" in UPDATE.
func (ub *UpdateBuilder) Decr(field string) string {
f := Escape(field)
return fmt.Sprintf("%v = %v - 1", f, f)
return fmt.Sprintf("%s = %s - 1", f, f)
}

// Add represents SET "field = field + value" in UPDATE.
func (ub *UpdateBuilder) Add(field string, value interface{}) string {
f := Escape(field)
return fmt.Sprintf("%v = %v + %v", f, f, ub.args.Add(value))
return fmt.Sprintf("%s = %s + %s", f, f, ub.args.Add(value))
}

// Sub represents SET "field = field - value" in UPDATE.
func (ub *UpdateBuilder) Sub(field string, value interface{}) string {
f := Escape(field)
return fmt.Sprintf("%v = %v - %v", f, f, ub.args.Add(value))
return fmt.Sprintf("%s = %s - %s", f, f, ub.args.Add(value))
}

// Mul represents SET "field = field * value" in UPDATE.
func (ub *UpdateBuilder) Mul(field string, value interface{}) string {
f := Escape(field)
return fmt.Sprintf("%v = %v * %v", f, f, ub.args.Add(value))
return fmt.Sprintf("%s = %s * %s", f, f, ub.args.Add(value))
}

// Div represents SET "field = field / value" in UPDATE.
func (ub *UpdateBuilder) Div(field string, value interface{}) string {
f := Escape(field)
return fmt.Sprintf("%v = %v / %v", f, f, ub.args.Add(value))
return fmt.Sprintf("%s = %s / %s", f, f, ub.args.Add(value))
}

// String returns the compiled UPDATE string.
Expand Down

0 comments on commit aa3d965

Please sign in to comment.