当前位置: 首页>>代码示例>>Golang>>正文


Golang Tx.Queryx方法代码示例

本文整理汇总了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
}
开发者ID:croll,项目名称:arkeogis-server,代码行数:33,代码来源:user.go

示例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...)
}
开发者ID:BrianHarringtonUTSC,项目名称:uTeach,代码行数:7,代码来源:base.go

示例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
}
开发者ID:croll,项目名称:arkeogis-server,代码行数:33,代码来源:user.go

示例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
}
开发者ID:croll,项目名称:arkeogis-server,代码行数:31,代码来源:user.go

示例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
}
开发者ID:upwrd,项目名称:sift,代码行数:7,代码来源:db.go

示例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
}
开发者ID:upwrd,项目名称:sift,代码行数:7,代码来源:db.go

示例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
}
开发者ID:upwrd,项目名称:sift,代码行数:24,代码来源:db.go


注:本文中的github.com/jmoiron/sqlx.Tx.Queryx方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。