Example #1
0
func (self *Connection) ExecuteFetch(query []byte, maxrows int) (qr *QueryResult, err error) {
	defer handleError(&err)
	self.validate()

	if C.mysql_real_query(self.handle, (*C.char)(unsafe.Pointer(&query[0])), C.ulong(len(query))) != 0 {
		return nil, self.lastError(query)
	}

	result := C.mysql_store_result(self.handle)
	if result == nil {
		if int(C.mysql_field_count(self.handle)) != 0 { // Query was supposed to return data, but it didn't
			return nil, self.lastError(query)
		}
		qr = &QueryResult{}
		qr.RowsAffected = uint64(C.mysql_affected_rows(self.handle))
		qr.InsertId = uint64(C.mysql_insert_id(self.handle))
		return qr, nil
	}
	defer C.mysql_free_result(result)
	qr = &QueryResult{}
	qr.RowsAffected = uint64(C.mysql_affected_rows(self.handle))
	if qr.RowsAffected > uint64(maxrows) {
		return nil, &SqlError{0, fmt.Sprintf("Row count exceeded %d", maxrows), string(query)}
	}
	qr.Fields = self.buildFields(result)
	qr.Rows = self.fetchAll(result)
	return qr, nil
}
Example #2
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

}