Skip to content

Commit

Permalink
accept *[]map[string]interface{} as Select()'s dest (#10)
Browse files Browse the repository at this point in the history
accept *[]map[string]interface{} as Select()'s dest
  • Loading branch information
shibukawa authored Aug 19, 2022
1 parent e523c74 commit 50840e7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ WORKDIR /go/src/twowaysql
COPY . .

RUN go install -v ./...
CMD ["go", "test", "-v", "./..."]
50 changes: 50 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,56 @@ func TestSelect(t *testing.T) {

}

func TestSelectMap(t *testing.T) {
//このテストはinit.sqlに依存しています。
//データベースは/postgres/init以下のsqlファイルを用いて初期化されている。
db := open(t)
defer db.Close()
tw := New(db)
ctx := context.Background()

// SELECT
var people []map[string]interface{}
var params = Info{
MaxEmpNo: 3,
DeptNo: 12,
}

expected := []map[string]interface{}{
{
"first_name": "Evan",
"last_name": "MacMans",
"email": "evanmacmans@example.com",
},
{
"first_name": "Malvina",
"last_name": "FitzSimons",
"email": "malvinafitzsimons@example.com",
},
}

sql := `-- comment
SELECT
first_name
, last_name
, email
FROM
persons
WHERE
employee_no < /*maxEmpNo*/1000 -- comment
/* IF deptNo */
AND dept_no < /*deptNo*/1
/* END */
-- comment`
err := tw.Select(ctx, &people, sql, &params)
if err != nil {
t.Errorf("select: failed: %v", err)
}

assert.Check(t, cmp.DeepEqual(people, expected))

}

func TestUpdate(t *testing.T) {
//このテストはinit.sqlに依存しています。
//データベースは/postgres/init以下のsqlファイルを用いて初期化されている。
Expand Down
29 changes: 28 additions & 1 deletion twowaysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ func New(db *sqlx.DB) *Twowaysql {
// params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`.
// dest takes a pointer to a slice of a struct. The struct tag format must be `db:"tag_name"`.
func (t *Twowaysql) Select(ctx context.Context, dest interface{}, query string, params interface{}) error {

eval, bindParams, err := Eval(query, params)
if err != nil {
return err
}

q := t.db.Rebind(eval)

if destMap, ok := dest.(*[]map[string]interface{}); ok {
rows, err := t.db.QueryxContext(ctx, q, bindParams...)
if err != nil {
return err
}
return convertResultToMap(destMap, rows)
}

return t.db.SelectContext(ctx, dest, q, bindParams...)

}
Expand Down Expand Up @@ -137,6 +144,14 @@ func (t *TwowaysqlTx) Select(ctx context.Context, dest interface{}, query string

q := t.tx.Rebind(eval)

if destMap, ok := dest.(*[]map[string]interface{}); ok {
rows, err := t.tx.QueryxContext(ctx, q, bindParams...)
if err != nil {
return err
}
return convertResultToMap(destMap, rows)
}

return t.tx.SelectContext(ctx, dest, q, bindParams...)

}
Expand All @@ -156,6 +171,18 @@ func (t *TwowaysqlTx) Exec(ctx context.Context, query string, params interface{}
return t.tx.ExecContext(ctx, q, bindParams...)
}

func convertResultToMap(dest *[]map[string]interface{}, rows *sqlx.Rows) error {
defer rows.Close()
for rows.Next() {
row := map[string]interface{}{}
if err := rows.MapScan(row); err != nil {
return err
}
*dest = append(*dest, row)
}
return nil
}

// Tx returns `*sqlx.Tx`
func (t *TwowaysqlTx) Tx() *sqlx.Tx {
return t.tx
Expand Down

0 comments on commit 50840e7

Please sign in to comment.