Writer support all stringbuilder's methods #61

Merged
lunny merged 1 commits from lunny/builder_string into master 2019-09-22 07:47:18 +00:00
4 changed files with 46 additions and 41 deletions
Showing only changes of commit 0995dba902 - Show all commits

View File

@ -260,7 +260,7 @@ func (b *Builder) ToSQL() (string, []interface{}, error) {
}
}
var sql = w.writer.String()
var sql = w.String()
var err error
switch b.dialect {
@ -296,5 +296,5 @@ func (b *Builder) ToBoundSQL() (string, error) {
return "", err
}
return ConvertToBoundSQL(w.writer.String(), w.args)
return ConvertToBoundSQL(w.String(), w.args)
}

37
cond.go
View File

@ -4,43 +4,6 @@
package builder
import (
"io"
"strings"
)
// Writer defines the interface
type Writer interface {
io.Writer
Append(...interface{})
}
var _ Writer = NewWriter()
// BytesWriter implments Writer and save SQL in bytes.Buffer
type BytesWriter struct {
writer *strings.Builder
args []interface{}
}
// NewWriter creates a new string writer
func NewWriter() *BytesWriter {
w := &BytesWriter{
writer: &strings.Builder{},
}
return w
}
// Write writes data to Writer
func (s *BytesWriter) Write(buf []byte) (int, error) {
return s.writer.Write(buf)
}
// Append appends args to Writer
func (s *BytesWriter) Append(args ...interface{}) {
s.args = append(s.args, args...)
}
// Cond defines an interface
type Cond interface {
WriteTo(Writer) error

4
sql.go
View File

@ -21,7 +21,7 @@ func condToSQL(cond Cond) (string, []interface{}, error) {
if err := cond.WriteTo(w); err != nil {
return "", nil, err
}
return w.writer.String(), w.args, nil
return w.String(), w.args, nil
}
func condToBoundSQL(cond Cond) (string, error) {
@ -33,7 +33,7 @@ func condToBoundSQL(cond Cond) (string, error) {
if err := cond.WriteTo(w); err != nil {
return "", err
}
return ConvertToBoundSQL(w.writer.String(), w.args)
return ConvertToBoundSQL(w.String(), w.args)
}
// ToSQL convert a builder or conditions to SQL and args

42
writer.go Normal file
View File

@ -0,0 +1,42 @@
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package builder
import (
"io"
"strings"
)
// Writer defines the interface
type Writer interface {
io.Writer
Append(...interface{})
}
var _ Writer = NewWriter()
// BytesWriter implments Writer and save SQL in bytes.Buffer
type BytesWriter struct {
*strings.Builder
args []interface{}
}
// NewWriter creates a new string writer
func NewWriter() *BytesWriter {
w := &BytesWriter{
Builder: &strings.Builder{},
}
return w
}
// Append appends args to Writer
func (w *BytesWriter) Append(args ...interface{}) {
w.args = append(w.args, args...)
}
// Args returns args
func (w *BytesWriter) Args() []interface{} {
return w.args
}