Just get the latest go-mysql-server driver, procedure code not work.
func GetProduct(id int64) (code, name, category, introduction, description, features, specification string, err error) {
db, err := OpenDB()
if err!= nil { return }
ids := strconv.FormatInt(id, 10)
//sql := "select code, name, category, introduction, description, features, specification from products where id= " + ids
sql := "call get_product(" + ids + ")"
defer db.Close()
err = db.QueryRow(sql).Scan(&code, &name, &category, &introduction, &description, &features, &specification)
if err != nil { return }
return
}
product_test.go
package api
import (
"testing"
"fmt"
"strconv"
)
func TestGetProduct(t *testing.T) {
err := addSampleProduct()
if err != nil {
t.Error("Failed to add sample product.")
}
code, name, category, introduction, description, features, specification, err := GetProduct(1)
if err != nil {
t.Fatalf("%s", err)
}
if code != "code" {
t.Error("code Invalid")
}
if name != "name" {
t.Error("name Invalid")
}
if category != "category" {
t.Error("category Invalid")
}
if introduction != "introduction" {
t.Error("introduction Invalid")
}
if description != "description" {
t.Error("description Invalid")
}
if features != "features" {
t.Error("features Invalid")
}
if specification != "specification" {
t.Error("specification Invalid")
}
}
get_product.sql
delimiter //
drop procedure if exists get_product;
create procedure get_product(
in in_id bigint(20)
)
begin
select
code, name, category, introduction, description, features, specification
from
products
where
id = in_id;
end //
delimiter ;
create_table.sql
drop table if exists products;
create table products (
id bigint(20) not null auto_increment primary key,
code varchar(255) not null,
name varchar(255),
category varchar(255) not null,
introduction text,
description text,
features text,
specification text
) engine=innodb;
#test procedure in mysql command line if fine.
call get_product(1);
go test
get error
--- FAIL: TestGetProduct (0.00 seconds)
product_test.go:57: Error 1312: PROCEDURE poreomix.get_product can't return a result set in the given context
Thanks,
Sammi