2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Go】sqlxの使い方メモ

Posted at

はじめに

sqlxというライブラリを使ってみたのでメモ。

sqlxとは

標準ライブラリdatabase/sqlの拡張ライブラリです。
database/sqlでは、DBからとってきたデータをカラムごとにScan(型変換)して構造体にマッピングする必要があるのですが、カラム数が多いとこの部分の記述が非常に苦痛です。
この面倒な作業を勝手にやってくれたりするのがsqlxの役割です。

database/sqlを使った場合のScan処理の例

customerRepositoryDb.go
type CustomerRepositoryDb struct {
	client *sql.DB
}

func (d CustomerRepositoryDb) FindAll() ([]Customer, error) {
	findAllSql := "select customer_id, name, city, zipcode, date_of_birth, status from customers"

	rows, err := d.client.Query(findAllSql)
	if err != nil {
		logger.Error("Error while querying customer table " + err.Error())
		return nil, err
	}

    customers := make([]Customer, 0)
	for rows.Next() {
      var c Customer
	  err := rows.Scan(&c.Id, &c.Name, &c.City, &c.Zipcode, &c.DateofBirth, &c.Status)
	  if err != nil {
 		logger.Error("Error while scanning customers " + err.Error())
 		return nil, err
 	  }
	  customers = append(customers, c)
	}
	return customers, nil
}

実装

以下がsqlxで同じ処理を記述したコードです。
database/sqlだとあったクエリとScan処理の記述が、なんとerr = d.client.Select(&customers, findAllSql)の一行で完結します。

customerRepositoryDb.go
type CustomerRepositoryDb struct {
	client *sqlx.DB
}

func (d CustomerRepositoryDb) FindAll() ([]Customer, error) {
	var err error
	customers := make([]Customer, 0)
	
	findAllSql := "select customer_id, name, city, zipcode, date_of_birth, status from customers"

	err = d.client.Select(&customers, findAllSql)
	if err != nil {
		logger.Error("Error while querying customer table " + err.Error())
		return nil, err
	}

	return customers, nil
}

また、名前付きクエリの場合には、err := d.client.Get(&c, customerSql, id)のように、GETメソッドを使用することができます。

func (d CustomerRepositoryDb) ById(id string) (*Customer, *errs.AppError) {
	customerSql := "select customer_id, name, city, zipcode, date_of_birth, status from customers where customer_id = ?"

	var c Customer
	err := d.client.Get(&c, customerSql, id)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, errs.NewNotFoundError("customer not found")
		} else {
			logger.Error("Error while scanning customer " + err.Error())
			return nil, errs.NewUnexpectedError("unexpected database error")
		}
	}
	return &c, nil
}

参考資料

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?