示例#1
0
文件: conn.go 项目: sdgdsffdsfff/cm
func (c *Conn) writeOK(r *mysql.Result) error {
	if r == nil {
		r = &mysql.Result{Status: c.status}
	}
	data := c.alloc.AllocBytesWithLen(4, 32)
	data = append(data, mysql.OK_HEADER)
	data = append(data, mysql.PutLengthEncodedInt(r.AffectedRows)...)
	data = append(data, mysql.PutLengthEncodedInt(r.InsertId)...)
	if c.capability&mysql.CLIENT_PROTOCOL_41 > 0 {
		data = append(data, byte(r.Status), byte(r.Status>>8))
		data = append(data, 0, 0)
	}

	err := c.writePacket(data)
	if err != nil {
		return errors.Trace(err)
	}

	return errors.Trace(c.flush())
}
示例#2
0
func (c *Conn) writeResultset(status uint16, r *mysql.Resultset) error {
	c.affectedRows = int64(-1)
	columnLen := mysql.PutLengthEncodedInt(uint64(len(r.Fields)))
	data := c.alloc.AllocBytesWithLen(4, 1024)
	data = append(data, columnLen...)
	if err := c.writePacket(data); err != nil {
		return errors.Trace(err)
	}

	for _, v := range r.Fields {
		data = data[0:4]
		data = append(data, v.Dump(c.alloc)...)
		if err := c.writePacket(data); err != nil {
			return errors.Trace(err)
		}
	}

	if err := c.writeEOF(status); err != nil {
		return errors.Trace(err)
	}

	for _, v := range r.RowDatas {
		data = data[0:4]
		data = append(data, v...)
		if err := c.writePacket(data); err != nil {
			return errors.Trace(err)
		}
	}

	err := c.writeEOF(status)
	if err != nil {
		return errors.Trace(err)
	}

	return errors.Trace(c.flush())
}