1
0
Fork 0

Save org members function

This commit is contained in:
Gregory Eremin 2015-03-19 19:50:18 +07:00
parent fe9776cc35
commit 6bb0de0c78
1 changed files with 38 additions and 0 deletions

38
db/members.go Normal file
View File

@ -0,0 +1,38 @@
package db
import (
"fmt"
"strconv"
"strings"
"time"
)
func SaveOrgMembers(org uint64, members []uint64) {
defer measure("SaveOrgMembers", time.Now())
tx := db.MustBegin()
var ids = []string{}
for _, id := range members {
ids = append(ids, strconv.FormatUint(id, 10))
}
tx.MustExec(fmt.Sprintf(`
delete from org_members
where
org_id = %d and
user_id not in (%s)
`, org, strings.Join(ids, ", ")))
var values = []string{}
for _, id := range members {
values = append(values, fmt.Sprintf("(%d, %d)", org, id))
}
tx.MustExec(`
insert into org_members (org_id, user_id)
values ` + strings.Join(values, ", ") + `
on duplicate key update org_id = org_id
`)
if err := tx.Commit(); err != nil {
panic(err)
}
}