bob vs sqlboilerのmodifyの違い
code:bob.go
code:sqlboiler.go
mods := []qm.QueryMod{}
modsをsmで組み立てる
ID検索(例:クエリパラメータを使って)
code:go
if query.ID.Valid {
mods = append(mods, sm.Where("id = ?", query.ID.Int64))
}
sm.Whereは「生SQLのWhere句」を組み立てる修飾子。
code:go
mods = append(mods,
sm.OrderBy("created_at DESC"),
sm.Limit(1),
)
code:go
例)Select(sm)の場合
import "github.com/stephenafamo/bob/dialect/psql/sm"
sm.Where("id = ?", 1),
sm.OrderBy("created_at DESC"),
}
例)Insert(im)の場合
import "github.com/stephenafamo/bob/dialect/psql/im"
im.Columns("name", "age"),
im.Values("Alice", 30),
}
例)Update(um)の場合
import "github.com/stephenafamo/bob/dialect/psql/um"
um.Set("name", "Bob"),
um.Where("id = ?", 1),
}
例)Delete(dm)の場合
import "github.com/stephenafamo/bob/dialect/psql/dm"
dm.Where("id = ?", 1),
}
code:go
Goの型チェックにより、smの修飾子は*psql.SelectQuery用のmodsにだけ追加できる
code:go
mods = append(mods, sm.Where("id = ?", 1)) // OK
mods = append(mods, im.Values(...)) // 型エラー