func (self *Connection) fetchAll(result *C.MYSQL_RES) (rows [][]interface{}) { rowCount := int(C.mysql_num_rows(result)) if rowCount == 0 { return nil } rows = make([][]interface{}, rowCount) colCount := int(C.mysql_num_fields(result)) for i := 0; i < rowCount; i++ { rows[i] = self.fetchNext(result, colCount) } return rows }
func (self *Connection) buildFields(result *C.MYSQL_RES) (fields []Field) { nfields := int(C.mysql_num_fields(result)) cfieldsptr := C.mysql_fetch_fields(result) cfields := (*[1 << 30]C.MYSQL_FIELD)(unsafe.Pointer(cfieldsptr)) arena := hack.NewStringArena(1024) // prealloc a reasonable amount of space fields = make([]Field, nfields) for i := 0; i < nfields; i++ { length := strlen(cfields[i].name) fname := (*[1 << 30]byte)(unsafe.Pointer(cfields[i].name))[:length] fields[i].Name = arena.NewString(fname) fields[i].Type = int64(cfields[i]._type) } return fields }
//获取下一行数据(只用于select语句), //成功返回errnum=0且row有意义, 否则返回errnum func (mysql *MySQL) NextRow() (row map[string]string, errnum int) { if mysql.rs == nil { if C.mysql_field_count(mysql.my) != 0 { return nil, 4 } return nil, 5 } if mysql.fi == nil { n := uint(C.mysql_num_fields(mysql.rs)) mysql.fi = make([]string, n) f := C.mysql_fetch_fields(mysql.rs) for i, _ := range mysql.fi { mysql.fi[i] = C.GoString(f.name) f = C.next_field_name(f) } } r := C.mysql_fetch_row(mysql.rs) if r == nil { mysql.errno = 0 return nil, 0 } row = make(map[string]string) n := uint(len(mysql.fi)) for i := uint(0); i < n; i++ { row[mysql.fi[i]] = C.GoString(*r) r = C.next_field_value(r) } mysql.errno = 0 errnum = 0 return }