70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package scsusers
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
type Metadata struct {
|
|
MetaKey string `db:"meta_key"`
|
|
MetaValue string `db:"meta_value"`
|
|
}
|
|
|
|
func (u *UserData) LoadMeta() bool {
|
|
q := fmt.Sprintf("select meta_key, meta_value, id from %s_meta where user=?", c.TablePrefix)
|
|
rows, err := c.db.Query(q, u.UserID)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
log.Printf("scsuser.Get: select: %s", err.Error())
|
|
return false
|
|
}
|
|
|
|
for rows.Next() {
|
|
var m metadata
|
|
rows.Scan(&m.Key, &m.Value, &m.ID)
|
|
u.Meta[m.Key] = m
|
|
}
|
|
return true
|
|
|
|
}
|
|
|
|
func (u *UserData) Get(key string) (string, bool) {
|
|
tmp, ok := u.Meta[key]
|
|
return tmp.Value, ok
|
|
}
|
|
|
|
func (u *UserData) Delete(key string) {
|
|
tmp, ok := u.Meta[key]
|
|
if ok {
|
|
_, err := c.db.Query(fmt.Sprintf("delete from %s_meta where id=?", c.TablePrefix), tmp.ID)
|
|
if err != nil {
|
|
log.Printf("scsauth: set: delete: %s", err.Error())
|
|
}
|
|
delete(u.Meta, key)
|
|
}
|
|
}
|
|
|
|
func (u *UserData) Set(key, value string) error {
|
|
tmp, ok := u.Meta[key]
|
|
if ok {
|
|
_, err := c.db.Query(fmt.Sprintf("delete from %s_meta where id=?", c.TablePrefix), tmp.ID)
|
|
if err != nil {
|
|
log.Printf("scsauth: set: delete: %s", err.Error())
|
|
return err
|
|
}
|
|
}
|
|
var insertid int64
|
|
err := c.db.Get(&insertid, fmt.Sprintf("insert into %s_meta (user, meta_key, meta_value) VALUES (?,?,?) returning id", c.TablePrefix), u.UserID, key, value)
|
|
if err != nil {
|
|
log.Printf("scsauth: set: insert: %s", err.Error())
|
|
return err
|
|
}
|
|
var m metadata
|
|
m.Key = key
|
|
m.Value = value
|
|
m.ID = insertid
|
|
u.Meta[key] = m
|
|
log.Printf("%#v", u)
|
|
return nil
|
|
}
|