本文整理汇总了Golang中github.com/jmoiron/sqlx.Rows.MapScan方法的典型用法代码示例。如果您正苦于以下问题:Golang Rows.MapScan方法的具体用法?Golang Rows.MapScan怎么用?Golang Rows.MapScan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jmoiron/sqlx.Rows
的用法示例。
在下文中一共展示了Rows.MapScan方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sqlGetRow
func sqlGetRow(rows *sqlx.Rows, bytesToStrings, idTo_id bool) (*map[string]interface{}, error) {
results := make(map[string]interface{})
err := rows.MapScan(results)
if err != nil {
log.Println(err)
} else {
if idTo_id {
// Convert "id" column to "_id" for mongo
if _, ok := results["id"]; ok {
// Mangle id, because mongo is persnickity
results["_id"] = results["id"]
delete(results, "id")
}
}
if bytesToStrings {
// Convert byte arrays to strings
for k, v := range results {
if _, ok := v.([]byte); ok {
// Damn. Byte. Arrays. Sqlx.
results[k] = string(v.([]byte))
}
}
}
}
return &results, err
}
示例2: decodeRows
func (tx *Transaction) decodeRows(s *schema.Schema, rows *sqlx.Rows, list []*schema.Resource) ([]*schema.Resource, error) {
for rows.Next() {
resourceData := map[string]interface{}{}
data := map[string]interface{}{}
rows.MapScan(data)
var resource *schema.Resource
tx.decode(s, s.GetDbTableName(), data, resourceData)
resource, err := schema.NewResource(s, resourceData)
if err != nil {
return nil, fmt.Errorf("Failed to decode rows")
}
list = append(list, resource)
}
return list, nil
}
示例3: scanRow
func (r mapRower) scanRow(rows *sqlx.Rows) (Value, error) {
m := make(map[string]interface{})
e := rows.MapScan(m)
return m, e
}
示例4: Run
/*
Run postgres task
*/
func (p *PostgresTask) Run(r *http.Request, data map[string]interface{}) (response *Response) {
response = NewResponse(http.StatusOK)
queryresults := []*Response{}
for _, query := range p.config.Queries {
qresponse := NewResponse(http.StatusOK).StripStatusData()
var (
args []interface{}
db *sqlx.DB
err error
url string
rows *sqlx.Rows
Rows []map[string]interface{}
errq error
)
if url, err = p.Interpolate(query.URL, data); err != nil {
qresponse.Error(err)
goto Append
}
// interpolate all args
args = []interface{}{}
for _, arg := range query.Args {
interpolated, e := p.Interpolate(arg, data)
if e != nil {
qresponse.Error(e)
goto Append
}
args = append(args, interpolated)
}
// add query with args to response?
if p.config.ReturnQueries {
qresponse.AddValue("query", query.Query).AddValue("args", args)
}
if db, err = sqlx.Connect("postgres", url); err != nil {
if err, ok := err.(*pq.Error); ok {
qresponse.AddValue("error_code", err.Code.Name())
}
qresponse.Error(err)
goto Append
}
// run query
rows, errq = db.Queryx(query.Query, args...)
if errq != nil {
if errq, ok := errq.(*pq.Error); ok {
qresponse.AddValue("error_code", errq.Code.Name())
}
qresponse.Error(errq)
goto Append
}
Rows = []map[string]interface{}{}
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
if err != nil {
if err, ok := err.(*pq.Error); ok {
qresponse.AddValue("error_code", err.Code.Name())
}
qresponse.Error(err)
goto Append
}
Rows = append(Rows, results)
}
qresponse.Result(Rows)
Append:
queryresults = append(queryresults, qresponse)
}
// single result
if p.config.singleResultIndex != -1 {
response.Result(queryresults[p.config.singleResultIndex])
} else {
response.Result(queryresults)
}
return
}