Beispiel #1
0
// Writes BIND, EXECUTE and SYNC messages.
func writeBindExecuteMsg(buf *pool.Buffer, name string, params ...interface{}) error {
	const paramLenWidth = 4

	buf.StartMessage(bindMsg)
	buf.WriteString("")
	buf.WriteString(name)
	buf.WriteInt16(0)
	buf.WriteInt16(int16(len(params)))
	for _, param := range params {
		buf.StartParam()
		bytes := types.Append(buf.Bytes, param, false)
		if bytes != nil {
			buf.Bytes = bytes
			buf.FinishParam()
		} else {
			buf.FinishNullParam()
		}
	}
	buf.WriteInt16(0)
	buf.FinishMessage()

	buf.StartMessage(executeMsg)
	buf.WriteString("")
	buf.WriteInt32(0)
	buf.FinishMessage()

	buf.StartMessage(syncMsg)
	buf.FinishMessage()

	return nil
}
Beispiel #2
0
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
}
Beispiel #3
0
func (upd updateQuery) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	b = append(b, "UPDATE "...)
	b = types.AppendField(b, upd.model.Table().Name, true)
	b = append(b, " SET "...)

	for fieldName, value := range upd.data {
		b = types.AppendField(b, fieldName, true)
		b = append(b, " = "...)
		b = types.Append(b, value, true)
		b = append(b, ", "...)
	}
	b = b[:len(b)-2]

	b = append(b, " WHERE "...)
	b = appendBytes(b, " AND ", upd.wheres...)

	if len(upd.returning) > 0 {
		b = append(b, " RETURNING "...)
		b = appendValue(b, ", ", upd.returning...)
	}

	return b, nil
}