Пример #1
0
//获取下一行数据(只用于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

}
Пример #2
0
func (self *Connection) fetchNext(result *C.MYSQL_RES, colCount int) (row []interface{}) {
	rowPtr := (*[1 << 30]*[1 << 30]byte)(unsafe.Pointer(C.mysql_fetch_row(result)))
	if rowPtr == nil {
		panic(self.lastError(nil))
	}
	row = make([]interface{}, colCount)
	lengths := (*[1 << 30]uint64)(unsafe.Pointer(C.mysql_fetch_lengths(result)))
	totalLength := uint64(0)
	for i := 0; i < colCount; i++ {
		totalLength += (*lengths)[i]
	}
	arena := hack.NewStringArena(int(totalLength))
	for i := 0; i < colCount; i++ {
		colLength := (*lengths)[i]
		colPtr := (*rowPtr)[i]
		if colPtr == nil {
			continue
		}
		row[i] = arena.NewString((*colPtr)[:colLength])
	}
	return row
}