diff --git a/go.mod b/go.mod index 5822871..c843dfa 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/huandu/go-sqlbuilder + +go 1.12 diff --git a/select.go b/select.go index 395a27e..e88e38a 100644 --- a/select.go +++ b/select.go @@ -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 } @@ -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 } @@ -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. diff --git a/select_test.go b/select_test.go index b7304c2..a9b7402 100644 --- a/select_test.go +++ b/select_test.go @@ -93,7 +93,6 @@ func ExampleSelectBuilder_join() { } func ExampleSelectBuilder_limit_offset() { - sb := NewSelectBuilder() sb.Select("*") sb.From("user") @@ -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] +} diff --git a/update.go b/update.go index f499803..9121b85 100644 --- a/update.go +++ b/update.go @@ -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.