Example #1
0
// 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
}
Example #2
0
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...)
}
Example #3
0
// 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
}
Example #4
0
// 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
}
Example #5
0
File: db.go Project: upwrd/sift
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
}
Example #6
0
File: db.go Project: upwrd/sift
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
}
Example #7
0
File: db.go Project: upwrd/sift
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
}