有没有更好的方法来编组 SQL 行?

huangapple go评论74阅读模式
英文:

Is there a better way to marshal sql rows?

问题

我有以下的结构体:

type MyTable struct{
  DBColA []byte `db:"cola" json:"-"`
  ColA string `json:"cola"`
  DBColB []byte `db:"colb" json:"-"`
  ColB string `json:"colb"`
}

我将[]byte映射为string,以便更好地处理SQL中的空值。

当我获取所需的行时,我需要将其输出为JSON格式。为了做到这一点,我将[]byte转换为string

var rows []*MyTable
if _, err := Session.Select(&rows, sql, args...); err != nil {
    log.Println(err)
}
for _, row := range rows{
   row.ColA = string(row.DBColA)
   row.ColB = string(row.DBColB)
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(rows); err != nil {
  http.Error(w, err.Error(), http.StatusInternalServerError)
}

在我的结构体中同时拥有DBColAColA,然后将DBColA转换为字符串似乎非常低效...我有很多列。有没有更好的方法?

英文:

I have the following struct:

type MyTable struct{
  DBColA []byte `db:"cola" json:"-"`
  ColA string `json:"cola"`
  DBColB []byte `db:"colb" json:"-"`
  ColB string `json:"colb"`
}

I map to []byte [to better handle null values in my sql][1]

When I grab the rows I need to output it as json. In order to do that I convert []byte to string:

var rows []*MyTable
if _, err := Session.Select(&rows, sql, args...); err != nil {
	log.Println(err)
}
for _, row := range rows{
   row.ColA = string(row.DBColA)
   row.ColB = string(row.DBColB)
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(rows); err != nil {
  http.Error(w, err.Error(), http.StatusInternalServerError)
}

It seems very inefficient to have DBColA and ColA in my struct and then converting DBColA to a string....I have a lot of columns. Is there a better way?

  [1]: https://github.com/go-sql-driver/mysql/wiki/Examples

答案1

得分: 5

你是否尝试过在https://github.com/elgs/gosqljson中使用gosqljson

请参考以下示例:

package main

import (
	"database/sql"
	"fmt"
	"github.com/elgs/gosqljson"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	ds := "username:password@tcp(host:3306)/db"
	db, err := sql.Open("mysql", ds)
	defer db.Close()

	if err != nil {
		fmt.Println("sql.Open:", err)
	}

	theCase := "lower" // "lower" 默认值, "upper", camel

	a, _ := gosqljson.QueryDbToArrayJson(db, theCase, "SELECT ID,NAME FROM t LIMIT ?,?", 0, 3)
	fmt.Println(a)
	// [[ "id","name"],["0","Alicia"],["1","Brian"],["2","Chloe"]]

	m, _ := gosqljson.QueryDbToMapJson(db, theCase, "SELECT ID,NAME FROM t LIMIT ?,?", 0, 3)
	fmt.Println(m)
	// [{"id":"0","name":"Alicia"},{"id":"1","name":"Brian"},{"id":"2","name":"Chloe"}]
}
英文:

Have you tried gosqljson in https://github.com/elgs/gosqljson ?

See example:


	package main

	import (
		"database/sql"
		"fmt"
		"github.com/elgs/gosqljson"
		_ "github.com/go-sql-driver/mysql"
	)

	func main() {
		ds := "username:password@tcp(host:3306)/db"
		db, err := sql.Open("mysql", ds)
		defer db.Close()

		if err != nil {
			fmt.Println("sql.Open:", err)
		}

		theCase := "lower" // "lower" default, "upper", camel

		a, _ := gosqljson.QueryDbToArrayJson(db, theCase, "SELECT ID,NAME FROM t LIMIT ?,?", 0, 3)
		fmt.Println(a)
		// [["id","name"],["0","Alicia"],["1","Brian"],["2","Chloe"]]

		m, _ := gosqljson.QueryDbToMapJson(db, theCase, "SELECT ID,NAME FROM t LIMIT ?,?", 0, 3)
		fmt.Println(m)
		// [{"id":"0","name":"Alicia"},{"id":"1","name":"Brian"},{"id":"2","name":"Chloe"}]

	}


huangapple
  • 本文由 发表于 2015年1月19日 03:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/28014078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定