Exemplo n.º 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
}
Exemplo n.º 2
0
//获取影响的行数(插入的行数、更新的数行、删除的行数、查询的行数等),
//成功返回0, 否则返回errnum
func (mysql *MySQL) AffectedRows() (rownum uint64) {
	rownum = uint64(C.mysql_affected_rows(mysql.my))
	return
}