func (f *Formatter) AppendBytes(dst []byte, src []byte) ([]byte, error) { p := parser.New(src) for p.Valid() { ch := p.Read() if ch == '\\' { if p.Peek() == '?' { p.Skip('?') dst = append(dst, '?') continue } } else if ch != '?' { dst = append(dst, ch) continue } if name := string(p.ReadIdentifier()); name != "" { if f.model == nil { if len(f.params) == 0 { return nil, errors.New("pg: expected at least one parameter, got nothing") } last := f.params[len(f.params)-1] f.params = f.params[:len(f.params)-1] var err error f.model, err = NewTableModel(last) if err != nil { return nil, err } } var err error dst, err = f.model.AppendParam(dst, name) if err != nil { return nil, err } continue } if f.paramIndex >= len(f.params) { err := fmt.Errorf( "pg: expected at least %d parameters, got %d", f.paramIndex+1, len(f.params), ) return nil, err } dst = types.Append(dst, f.params[f.paramIndex], true) f.paramIndex++ } return dst, nil }
func newArrayParser(b []byte) *arrayParser { var err error if len(b) < 2 || b[0] != '{' || b[len(b)-1] != '}' { err = fmt.Errorf("pg: can't parse string slice: %q", b) } else { b = b[1 : len(b)-1] } return &arrayParser{ Parser: parser.New(b), err: err, } }
func AppendJSONB(b, jsonb []byte, quote bool) []byte { if quote { b = append(b, '\'') } p := parser.New(jsonb) for p.Valid() { c := p.Read() switch c { case '\'': if quote { b = append(b, '\'', '\'') } else { b = append(b, '\'') } case '\000': continue case '\\': if p.Got("u0000") { b = append(b, "\\\\u0000"...) } else { b = append(b, '\\') if p.Valid() { b = append(b, p.Read()) } } default: b = append(b, c) } } if quote { b = append(b, '\'') } return b }
func newHstoreParser(b []byte) *hstoreParser { return &hstoreParser{ Parser: parser.New(b), } }
func AppendFieldBytes(b []byte, field []byte, quote bool) []byte { p := parser.New(field) var quoted bool for p.Valid() { c := p.Read() switch c { case '\\': if p.Got("?") { c = '?' } case '*': if !quoted { b = append(b, '*') continue } case '.': if quote { b = append(b, '"') } b = append(b, '.') if p.Got("*") { b = append(b, '*') quoted = false } else if quote { b = append(b, '"') } continue case ' ': if p.Got("AS ") || p.Got("as ") { if quote { b = append(b, '"') } b = append(b, ` AS `...) if quote { b = append(b, '"') } } else { b = append(b, ' ') } continue case '?': b = append(b, '?') b = append(b, p.ReadIdentifier()...) continue } if quote && !quoted { b = append(b, '"') quoted = true } if quote && c == '"' { b = append(b, '"', '"') } else { b = append(b, c) } } if quote && quoted { b = append(b, '"') } return b }