glycerine/zygomys

how to response multi array from go function?

taliszhou opened this issue · 2 comments

HI~ glycerine!

I'm trying to write a function that returns rows of data from a mysql query.
But I don't know how to return a multi-array to the repl.
I tried typing "[def multarr [[1, 2, 3] [4, 5, 6]]" directly into the repl and found it supported.

please help to have a look.

`

 type SexpMysqlValueList struct {
      ValueList []orm.ParamsList
  }

 func (r *SexpMysqlValueList) Type() *RegisteredType {
       return nil // TODO what should this be?                                                                                                                                                 
 }


func (t *SexpMysqlValueList) SexpString(ps *PrintState) string {
     Log("valuelist", t.ValueList)
     return "this is mysql result"
}



func MysqlQueryFunction(env *Zlisp, name string, args []Sexp) (Sexp, error) {
if len(args) < 1 {
    return SexpNull, fmt.Errorf("mysql query params error")
}

var sqlstr string
switch expr := args[0].(type) {
case *SexpStr:
    sqlstr = expr.S
default:
    return SexpNull, fmt.Errorf("need sqlstring param")
}
                                                                                                                           
var resultList []orm.ParamsList
var num int64
var err error
o := orm.NewOrm()

switch t := args[1].(type) {
case *SexpArray:
    if len(t.Val) > 0 {
        num, err = o.Raw(sqlstr, t.Val).ValuesList(&resultList)
    } else {
        num, err = o.Raw(sqlstr).ValuesList(&resultList)
    }
default:
    num, err = o.Raw(sqlstr).ValuesList(&resultList)
}

if err == nil && num > 0 {                                                                                                                                 
    resArr := make([][]Sexp, num)                                                                                                                                                     

    for ridx, row := range resultList {
        rowArr := make([]Sexp, len(row))

        for fidx, fld := range row {
            rowArr[fidx] = &SexpStr{S: fld.(string)}
        }

        resArr[ridx] = rowArr
    }

    // resArr now has some data rows.  but  how to respone and show in repl???
    return &SexpMysqlValueList{ValueList: resultList}, nil                                                                                                                      
}

    return SexpNull, err

}

`

I refer to the time.go code, define a SexpMysqlDataList class,
but I need to format the output myself (SexpString()), and I need to get the returned dataset in other code like accessing an array.

Sorry to bother you with so many beginner questions.

Thanks!!!

zygo> (def test [[1 2 3 4] [5 6 7 8]])
[[1 2 3 4] [5 6 7 8]]
zygo> test[1]
[5 6 7 8]
zygo> test[1][1]
6

it works! i want like it.

I recommend that you return a standard SexpArray instead of your own custom type. Then the indexing machinery will already know how to index into it.