From 772c6ca7d724a62ce665b91ac7a9371f0ff98c7c Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 19 Jun 2014 18:10:04 -0500 Subject: [PATCH] Remove MessageWriter --- conn.go | 61 +++++++++++++------------------- message_writer.go | 58 ------------------------------ value_transcoder.go | 86 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 92 insertions(+), 113 deletions(-) delete mode 100644 message_writer.go diff --git a/conn.go b/conn.go index a9983418..d220db2f 100644 --- a/conn.go +++ b/conn.go @@ -588,27 +588,21 @@ func (c *Conn) Prepare(name, sql string) (err error) { // parse buf := c.getBuf() - w := newMessageWriter(buf) - w.WriteCString(name) - w.WriteCString(sql) - w.Write(int16(0)) - if w.Err != nil { - return w.Err - } + buf.WriteString(name) + buf.WriteByte(0) + buf.WriteString(sql) + buf.WriteByte(0) + binary.Write(buf, binary.BigEndian, int16(0)) err = c.txMsg('P', buf, false) if err != nil { - return + return err } // describe buf = c.getBuf() - w = newMessageWriter(buf) - w.WriteByte('S') - w.WriteCString(name) - if w.Err != nil { - return w.Err - } - + buf.WriteByte('S') + buf.WriteString(name) + buf.WriteByte(0) err = c.txMsg('D', buf, false) if err != nil { return @@ -774,46 +768,44 @@ func (c *Conn) sendPreparedQuery(ps *preparedStatement, arguments ...interface{} // bind buf := c.getBuf() - w := newMessageWriter(buf) - w.WriteCString("") - w.WriteCString(ps.Name) - w.Write(int16(len(ps.ParameterOids))) + buf.WriteString("") + buf.WriteByte(0) + buf.WriteString(ps.Name) + buf.WriteByte(0) + binary.Write(buf, binary.BigEndian, int16(len(ps.ParameterOids))) for _, oid := range ps.ParameterOids { transcoder := ValueTranscoders[oid] if transcoder == nil { transcoder = defaultTranscoder } - w.Write(transcoder.EncodeFormat) + binary.Write(buf, binary.BigEndian, transcoder.EncodeFormat) } - w.Write(int16(len(arguments))) + binary.Write(buf, binary.BigEndian, int16(len(arguments))) for i, oid := range ps.ParameterOids { if arguments[i] != nil { transcoder := ValueTranscoders[oid] if transcoder == nil { transcoder = defaultTranscoder } - err = transcoder.EncodeTo(w.buf, arguments[i]) + err = transcoder.EncodeTo(buf, arguments[i]) if err != nil { return err } } else { - w.Write(int32(-1)) + binary.Write(buf, binary.BigEndian, int32(-1)) } } - w.Write(int16(len(ps.FieldDescriptions))) + binary.Write(buf, binary.BigEndian, int16(len(ps.FieldDescriptions))) for _, fd := range ps.FieldDescriptions { transcoder := ValueTranscoders[fd.DataType] if transcoder != nil && transcoder.DecodeBinary != nil { - w.Write(int16(1)) + binary.Write(buf, binary.BigEndian, int16(1)) } else { - w.Write(int16(0)) + binary.Write(buf, binary.BigEndian, int16(0)) } } - if w.Err != nil { - return w.Err - } err = c.txMsg('B', buf, false) if err != nil { @@ -822,14 +814,9 @@ func (c *Conn) sendPreparedQuery(ps *preparedStatement, arguments ...interface{} // execute buf = c.getBuf() - w = newMessageWriter(buf) - w.WriteCString("") - w.Write(int32(0)) - - if w.Err != nil { - return w.Err - } - + buf.WriteString("") + buf.WriteByte(0) + binary.Write(buf, binary.BigEndian, int32(0)) err = c.txMsg('E', buf, false) if err != nil { return err diff --git a/message_writer.go b/message_writer.go deleted file mode 100644 index 20943537..00000000 --- a/message_writer.go +++ /dev/null @@ -1,58 +0,0 @@ -package pgx - -import ( - "bytes" - "encoding/binary" -) - -// MessageWriter is a helper for producing messages to send to PostgreSQL. -// To avoid verbose error handling it internally records errors and no-ops -// any calls that occur after an error. At the end of a sequence of writes -// the Err field should be checked to see if any errors occurred. -type MessageWriter struct { - buf *bytes.Buffer - Err error -} - -func newMessageWriter(buf *bytes.Buffer) *MessageWriter { - return &MessageWriter{buf: buf} -} - -// WriteCString writes a null-terminated string. -func (w *MessageWriter) WriteCString(s string) { - if w.Err != nil { - return - } - if _, w.Err = w.buf.WriteString(s); w.Err != nil { - return - } - w.Err = w.buf.WriteByte(0) -} - -// WriteString writes a string without a null terminator. -func (w *MessageWriter) WriteString(s string) { - if w.Err != nil { - return - } - if _, w.Err = w.buf.WriteString(s); w.Err != nil { - return - } -} - -func (w *MessageWriter) WriteByte(b byte) { - if w.Err != nil { - return - } - - w.Err = w.buf.WriteByte(b) -} - -// Write writes data in the network byte order. data can be an integer type, -// float type, or byte slice. -func (w *MessageWriter) Write(data interface{}) { - if w.Err != nil { - return - } - - w.Err = binary.Write(w.buf, binary.BigEndian, data) -} diff --git a/value_transcoder.go b/value_transcoder.go index 245bb505..113bbd93 100644 --- a/value_transcoder.go +++ b/value_transcoder.go @@ -462,16 +462,32 @@ func decodeInt2ArrayFromText(mr *MessageReader, size int32) interface{} { } func int16SliceToArrayString(nums []int16) (string, error) { - w := newMessageWriter(&bytes.Buffer{}) - w.WriteString("{") + w := &bytes.Buffer{} + _, err := w.WriteString("{") + if err != nil { + return "", err + } + for i, n := range nums { if i > 0 { - w.WriteString(",") + _, err = w.WriteString(",") + if err != nil { + return "", err + } + } + + _, err = w.WriteString(strconv.FormatInt(int64(n), 10)) + if err != nil { + return "", err } - w.WriteString(strconv.FormatInt(int64(n), 10)) } - w.WriteString("}") - return w.buf.String(), w.Err + + _, err = w.WriteString("}") + if err != nil { + return "", err + } + + return w.String(), nil } func encodeInt2Array(w io.Writer, value interface{}) error { @@ -513,16 +529,33 @@ func decodeInt4ArrayFromText(mr *MessageReader, size int32) interface{} { } func int32SliceToArrayString(nums []int32) (string, error) { - w := newMessageWriter(&bytes.Buffer{}) - w.WriteString("{") + w := &bytes.Buffer{} + + _, err := w.WriteString("{") + if err != nil { + return "", err + } + for i, n := range nums { if i > 0 { - w.WriteString(",") + _, err = w.WriteString(",") + if err != nil { + return "", err + } + } + + _, err = w.WriteString(strconv.FormatInt(int64(n), 10)) + if err != nil { + return "", err } - w.WriteString(strconv.FormatInt(int64(n), 10)) } - w.WriteString("}") - return w.buf.String(), w.Err + + _, err = w.WriteString("}") + if err != nil { + return "", err + } + + return w.String(), nil } func encodeInt4Array(w io.Writer, value interface{}) error { @@ -564,16 +597,33 @@ func decodeInt8ArrayFromText(mr *MessageReader, size int32) interface{} { } func int64SliceToArrayString(nums []int64) (string, error) { - w := newMessageWriter(&bytes.Buffer{}) - w.WriteString("{") + w := &bytes.Buffer{} + + _, err := w.WriteString("{") + if err != nil { + return "", err + } + for i, n := range nums { if i > 0 { - w.WriteString(",") + _, err = w.WriteString(",") + if err != nil { + return "", err + } + } + + _, err = w.WriteString(strconv.FormatInt(int64(n), 10)) + if err != nil { + return "", err } - w.WriteString(strconv.FormatInt(int64(n), 10)) } - w.WriteString("}") - return w.buf.String(), w.Err + + _, err = w.WriteString("}") + if err != nil { + return "", err + } + + return w.String(), nil } func encodeInt8Array(w io.Writer, value interface{}) error {