當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。