本文整理匯總了Golang中github.com/mibk/ql/query.Writer類的典型用法代碼示例。如果您正苦於以下問題:Golang Writer類的具體用法?Golang Writer怎麽用?Golang Writer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Writer類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: EscapeBool
func (Mysql) EscapeBool(w query.Writer, b bool) {
if b {
w.WriteRune('1')
} else {
w.WriteRune('0')
}
}
示例2: ApplyLimitAndOffset
func (Mysql) ApplyLimitAndOffset(w query.Writer, limit, offset uint64) {
w.WriteString(" LIMIT ")
if limit == 0 {
// In MYSQL, OFFSET cannot be used alone. Set the limit to the max possible value.
w.WriteString("18446744073709551615")
} else {
fmt.Fprint(w, limit)
}
if offset > 0 {
fmt.Fprintf(w, " OFFSET %d", offset)
}
}
示例3: writeWhereFragmentsToSql
// Invariant: only called when len(fragments) > 0.
func writeWhereFragmentsToSql(w query.Writer, fragments []*whereFragment, args *[]interface{}) {
anyConditions := false
for _, f := range fragments {
if f.Condition == "" {
panic("invalid condition expression")
}
if anyConditions {
w.WriteString(" AND ")
}
anyConditions = true
w.WriteString("(" + f.Condition + ")")
if len(f.Values) > 0 {
*args = append(*args, f.Values...)
}
}
}
示例4: buildOrder
func (b *baseBuilder) buildOrder(w query.Writer) {
if len(b.OrderBys) > 0 {
w.WriteString(" ORDER BY ")
for i, s := range b.OrderBys {
if i > 0 {
w.WriteString(", ")
}
w.WriteString(s)
}
}
}
示例5: buildWhere
func (b *baseBuilder) buildWhere(w query.Writer, args *[]interface{}) {
if len(b.WhereFragments) > 0 {
w.WriteString(" WHERE ")
writeWhereFragmentsToSql(w, b.WhereFragments, args)
}
}
示例6: EscapeString
// Need to turn \x00, \n, \r, \, ', " and \x1a.
// Returns an escaped, quoted string. eg, "hello 'world'" -> "'hello \'world\''".
func (Mysql) EscapeString(w query.Writer, s string) {
w.WriteRune('\'')
for _, char := range s {
switch char {
case '\'':
w.WriteString(`\'`)
case '"':
w.WriteString(`\"`)
case '\\':
w.WriteString(`\\`)
case '\n':
w.WriteString(`\n`)
case '\r':
w.WriteString(`\r`)
case 0:
w.WriteString(`\x00`)
case 0x1a:
w.WriteString(`\x1a`)
default:
w.WriteRune(char)
}
}
w.WriteRune('\'')
}
示例7: EscapeIdent
func (Mysql) EscapeIdent(w query.Writer, ident string) {
w.WriteRune('`')
r := strings.NewReplacer("`", "``", ".", "`.`")
w.WriteString(r.Replace(ident))
w.WriteRune('`')
}
示例8: interpolate
func interpolate(w query.Writer, v interface{}) error {
valuer, ok := v.(driver.Valuer)
if ok {
val, err := valuer.Value()
if err != nil {
return err
}
v = val
}
valueOfV := reflect.ValueOf(v)
kindOfV := valueOfV.Kind()
switch {
case v == nil:
w.WriteString("NULL")
case isInt(kindOfV):
var ival = valueOfV.Int()
w.WriteString(strconv.FormatInt(ival, 10))
case isUint(kindOfV):
var uival = valueOfV.Uint()
w.WriteString(strconv.FormatUint(uival, 10))
case kindOfV == reflect.String:
var str = valueOfV.String()
if !utf8.ValidString(str) {
return ErrNotUTF8
}
D.EscapeString(w, str)
case isFloat(kindOfV):
var fval = valueOfV.Float()
w.WriteString(strconv.FormatFloat(fval, 'f', -1, 64))
case kindOfV == reflect.Bool:
D.EscapeBool(w, valueOfV.Bool())
case kindOfV == reflect.Struct:
if typeOfV := valueOfV.Type(); typeOfV == typeOfTime {
t := valueOfV.Interface().(time.Time)
D.EscapeTime(w, t)
} else {
return ErrInvalidValue
}
case kindOfV == reflect.Slice:
typeOfV := reflect.TypeOf(v)
subtype := typeOfV.Elem()
kindOfSubtype := subtype.Kind()
sliceLen := valueOfV.Len()
stringSlice := make([]string, 0, sliceLen)
switch {
case sliceLen == 0:
return ErrInvalidSliceLength
case isInt(kindOfSubtype):
for i := 0; i < sliceLen; i++ {
var ival = valueOfV.Index(i).Int()
stringSlice = append(stringSlice, strconv.FormatInt(ival, 10))
}
case isUint(kindOfSubtype):
for i := 0; i < sliceLen; i++ {
var uival = valueOfV.Index(i).Uint()
stringSlice = append(stringSlice, strconv.FormatUint(uival, 10))
}
case kindOfSubtype == reflect.String:
for i := 0; i < sliceLen; i++ {
var str = valueOfV.Index(i).String()
if !utf8.ValidString(str) {
return ErrNotUTF8
}
buf := new(bytes.Buffer)
D.EscapeString(buf, str)
stringSlice = append(stringSlice, buf.String())
}
default:
return ErrInvalidSliceValue
}
w.WriteRune('(')
w.WriteString(strings.Join(stringSlice, ","))
w.WriteRune(')')
default:
return ErrInvalidValue
}
return nil
}