本文整理汇总了Golang中github.com/jmoiron/sqlx.Tx.Queryx方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.Queryx方法的具体用法?Golang Tx.Queryx怎么用?Golang Tx.Queryx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jmoiron/sqlx.Tx
的用法示例。
在下文中一共展示了Tx.Queryx方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetUsers
// SetUsers of the group
func (g *Group) SetUsers(tx *sqlx.Tx, users []User) error {
ids := make([]string, len(users)+1)
for i, user := range users {
ids[i] = fmt.Sprintf("%d", user.Id)
}
ids[len(users)] = "-1" // empty list is a problem
_, err := tx.NamedExec("DELETE FROM \"user__group\" WHERE group_id=:id AND user_id NOT IN ("+strings.Join(ids, ",")+")", g)
if err != nil {
return err
}
rows, err := tx.Queryx("SELECT user_id FROM user__group WHERE group_id = $1 AND user_id IN ("+strings.Join(ids, ",")+")", g.Id)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var id string
rows.Scan(&id)
ids = removeString(ids, id)
}
ids = removeString(ids, "-1")
for _, userid := range ids {
_, err := tx.Exec("INSERT INTO user__group (group_id, user_id) VALUES ($1, $2)", g.Id, userid)
if err != nil {
return err
}
}
return nil
}
示例2: query
func (b *Base) query(tx *sqlx.Tx, query string, args ...interface{}) (*sqlx.Rows, error) {
if tx != nil {
return tx.Queryx(query, args...)
}
return b.db.Queryx(query, args...)
}
示例3: SetCompanies
// SetCompanies set user companies
func (u *User) SetCompanies(tx *sqlx.Tx, companies []Company) error {
ids := make([]string, len(companies)+1)
for i, company := range companies {
ids[i] = fmt.Sprintf("%d", company.Id)
}
ids[len(companies)] = "-1" // empty list is a problem
_, err := tx.NamedExec("DELETE FROM \"user__company\" WHERE user_id=:id AND company_id NOT IN ("+strings.Join(ids, ",")+")", u)
if err != nil {
return err
}
rows, err := tx.Queryx("SELECT company_id FROM user__company WHERE user_id = $1 AND company_id IN ("+strings.Join(ids, ",")+")", u.Id)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var id string
rows.Scan(&id)
ids = removeString(ids, id)
}
ids = removeString(ids, "-1")
for _, companyid := range ids {
_, err := tx.Exec("INSERT INTO user__company (user_id, company_id) VALUES ($1, $2)", u.Id, companyid)
if err != nil {
return err
}
}
return nil
}
示例4: SetGroups
// SetGroups set user groups
func (u *User) SetGroups(tx *sqlx.Tx, groups []Group) error {
ids := make([]string, len(groups))
for i, group := range groups {
ids[i] = fmt.Sprintf("%d", group.Id)
}
_, err := tx.NamedExec("DELETE FROM \"user__group\" WHERE user_id=:id AND group_id NOT IN ("+strings.Join(ids, ",")+")", u)
if err != nil {
return err
}
rows, err := tx.Queryx("SELECT group_id FROM user__group WHERE user_id = $1 AND group_id IN ("+strings.Join(ids, ",")+")", u.Id)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var id string
rows.Scan(&id)
ids = removeString(ids, id)
}
for _, groupid := range ids {
_, err := tx.Exec("INSERT INTO user__group (user_id, group_id) VALUES ($1, $2)", u.Id, groupid)
if err != nil {
return err
}
}
return nil
}
示例5: deleteMediaPlayerTx
func deleteMediaPlayerTx(tx *sqlx.Tx, compID int64) error {
if _, err := tx.Queryx("DELETE FROM media_player_state WHERE id=?", compID); err != nil {
return fmt.Errorf("error deleteing from media_player_state: %v", err)
}
//TODO: also delete media_player_stats
return nil
}
示例6: deleteLightEmitterTx
func deleteLightEmitterTx(tx *sqlx.Tx, compID int64) error {
if _, err := tx.Queryx("DELETE FROM light_emitter_state WHERE id=?", compID); err != nil {
return fmt.Errorf("error deleteing from light_emitter_state: %v", err)
}
//TODO: also delete light_emitter_stats
return nil
}
示例7: deleteComponentTx
func deleteComponentTx(tx *sqlx.Tx, deviceID types.DeviceID, compName string) error {
// Get the base component, if it exists
if comp, found := getBaseComponentTx(tx, deviceID, compName); found {
// delete the specific component info (changes by type)
switch comp.Type {
default:
return fmt.Errorf("unhandled component type: %T", comp)
case types.LightEmitter{}.Type():
if err := deleteLightEmitterTx(tx, comp.ID); err != nil {
return fmt.Errorf("could not delete light emitter: %v", err)
}
case types.MediaPlayer{}.Type():
if err := deleteMediaPlayerTx(tx, comp.ID); err != nil {
return fmt.Errorf("could not delete media player: %v", err)
}
}
// delete the base component
if _, err := tx.Queryx("DELETE FROM component WHERE device_id=? AND name=?", deviceID, compName); err != nil {
return fmt.Errorf("error deleting base component: %v", err)
}
}
return nil
}