summaryrefslogtreecommitdiff
path: root/option.go
blob: e3d246f4b5803713d78070fa294058dc366e8ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import "github.com/mxk/go-sqlite/sqlite3"
import "errors"

type Option struct {
    id int
    text string
    pollId int64
}

func getOptionFromText(db*sqlite3.Conn, text string) (Option, error) {

    args := sqlite3.NamedArgs{"$a": text}
    sql := "SELECT * FROM options WHERE text = $a"
    s, err := db.Query(sql, args)
    row := make(sqlite3.RowMap)
    
    for ; err == nil ; err = s.Next() {
        var rowid int
        s.Scan(&rowid, row)     // Assigns 1st column to rowid, the rest to row

        option := Option{id:rowid, text:row["text"].(string), pollId:row["poll_id"].(int64)}
        return option, nil
    }

    //If we get here there are no matching options
    return Option{id:0, text:"", pollId:0}, errors.New("No option found matching '" + text + "'")

}