get by id

This commit is contained in:
Your Name 2023-11-29 18:06:29 -05:00
parent ebd3dcea87
commit b062c4aea1
1 changed files with 42 additions and 0 deletions

42
main.go
View File

@ -132,6 +132,48 @@ func Get(username string) (*UserData, bool) {
return u, ok
}
func GetById(id int64) (*UserData, bool) {
u := NewUser()
q := fmt.Sprintf("select username, password, id from %s_users where id=?", c.TablePrefix)
err := c.db.Get(u, q, id)
if err != nil {
if err == sql.ErrNoRows {
return nil, false
}
log.Printf("scsusers.Get: %s", err.Error())
return nil, false
}
ok := u.LoadMeta()
return u, ok
}
func GetUsersByMeta(key string, value string) []string {
var u []string
var userids []int64
q := fmt.Sprintf("select user from %s_meta where meta_key=? AND meta_value=?", c.TablePrefix)
err := c.db.Select(&userids, q, key, value)
if err != nil && err != sql.ErrNoRows {
log.Printf("scsusers.getusersByMeta: %s", err)
return nil
}
if len(userids) == 0 {
return nil
}
q = fmt.Sprintf("select username from %s_users where id IN (?)", c.TablePrefix)
query, args, err := sqlx.In(q, userids)
if err != nil {
log.Printf("scsusers.getusersbymeta: %s", err.Error())
return nil
}
query = c.db.Rebind(query)
err = c.db.Select(&u, query, args...)
if err != nil {
log.Printf("scsusers.getusersbymeta: %s", err.Error())
return nil
}
return u
}
func GetUserid(username string) int64 {
var i int64
username = strings.ToLower(username)