bob vs sqlboilerのmodifyの違い
code:bob.go
mods := []bob.Mod*dialect.SelectQuery{}
code:sqlboiler.go
mods := []qm.QueryMod{}
modsをsmで組み立てる
ID検索(例:クエリパラメータを使って)
code:go
mods := []bob.Mod*psql.SelectQuery{}
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"
mods := []bob.Mod*psql.SelectQuery{
sm.Where("id = ?", 1),
sm.OrderBy("created_at DESC"),
}
例)Insert(im)の場合
import "github.com/stephenafamo/bob/dialect/psql/im"
insertMods := []bob.Mod*psql.InsertQuery{
im.Columns("name", "age"),
im.Values("Alice", 30),
}
例)Update(um)の場合
import "github.com/stephenafamo/bob/dialect/psql/um"
updateMods := []bob.Mod*psql.UpdateQuery{
um.Set("name", "Bob"),
um.Where("id = ?", 1),
}
例)Delete(dm)の場合
import "github.com/stephenafamo/bob/dialect/psql/dm"
deleteMods := []bob.Mod*psql.DeleteQuery{
dm.Where("id = ?", 1),
}
code:go
- SELECT: sm (Select Mods) → []bob.Mod*psql.SelectQuery
- INSERT: im (Insert Mods) → []bob.Mod*psql.InsertQuery
- UPDATE: um (Update Mods) → []bob.Mod*psql.UpdateQuery
- DELETE: dm (Delete Mods) → []bob.Mod*psql.DeleteQuery
Goの型チェックにより、smの修飾子は*psql.SelectQuery用のmodsにだけ追加できる
code:go
mods := []bob.Mod*psql.SelectQuery{}
mods = append(mods, sm.Where("id = ?", 1)) // OK
mods = append(mods, im.Values(...)) // 型エラー