Пример #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
}
Пример #2
0
//执行SQL语句(select, insert, update, delete, ...),
//成功返回0, 否则返回errnum
func (mysql *MySQL) Execute(stmt string) (errnum int) {
	if mysql.my == nil {
		mysql.errno = 3
		return 3
	}

	s := C.CString(stmt)
	defer C.free(unsafe.Pointer(s))

	rc := C.mysql_real_query(mysql.my, s, (C.ulong)(len(stmt)))
	if rc != 0 {
		mysql.errno = 1
		return 1
	}

	if mysql.rs != nil {
		C.mysql_free_result(mysql.rs)
		mysql.fi = nil
	}
	mysql.rs = C.mysql_store_result(mysql.my)

	mysql.errno = 0
	return 0

}