Stop using globals in the topic store and the user store.
Added the inline query builder.
This commit is contained in:
parent
af09013a25
commit
93b4b269ed
14
gen_mysql.go
14
gen_mysql.go
|
@ -7,8 +7,6 @@ import "log"
|
|||
import "database/sql"
|
||||
|
||||
var get_user_stmt *sql.Stmt
|
||||
var get_full_user_stmt *sql.Stmt
|
||||
var get_topic_stmt *sql.Stmt
|
||||
var get_reply_stmt *sql.Stmt
|
||||
var login_stmt *sql.Stmt
|
||||
var get_password_stmt *sql.Stmt
|
||||
|
@ -109,18 +107,6 @@ func gen_mysql() (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
log.Print("Preparing get_full_user statement.")
|
||||
get_full_user_stmt, err = db.Prepare("SELECT `name`,`group`,`is_super_admin`,`session`,`email`,`avatar`,`message`,`url_prefix`,`url_name`,`level`,`score`,`last_ip` FROM `users` WHERE `uid` = ?")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print("Preparing get_topic statement.")
|
||||
get_topic_stmt, err = db.Prepare("SELECT `title`,`content`,`createdBy`,`createdAt`,`is_closed`,`sticky`,`parentID`,`ipaddress`,`postCount`,`likeCount`,`data` FROM `topics` WHERE `tid` = ?")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print("Preparing get_reply statement.")
|
||||
get_reply_stmt, err = db.Prepare("SELECT `content`,`createdBy`,`createdAt`,`lastEdit`,`lastEditBy`,`ipaddress`,`likeCount` FROM `replies` WHERE `rid` = ?")
|
||||
if err != nil {
|
||||
|
|
8
mysql.go
8
mysql.go
|
@ -8,6 +8,7 @@ import "strconv"
|
|||
import "encoding/json"
|
||||
import "database/sql"
|
||||
import _ "github.com/go-sql-driver/mysql"
|
||||
import "./query_gen/lib"
|
||||
|
||||
var db *sql.DB
|
||||
var db_version string
|
||||
|
@ -61,6 +62,13 @@ func init_database() (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
// Ready the query builder
|
||||
qgen.Builder.SetConn(db)
|
||||
err = qgen.Builder.SetAdapter("mysql")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print("Preparing get_topic_replies_offset statement.")
|
||||
get_topic_replies_offset_stmt, err = db.Prepare("select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name, users.group, users.url_prefix, users.url_name, users.level, replies.ipaddress, replies.likeCount, replies.actionType from replies left join users on replies.createdBy = users.uid where tid = ? limit ?, " + strconv.Itoa(items_per_page))
|
||||
if err != nil {
|
||||
|
|
|
@ -1,7 +1,37 @@
|
|||
/* WIP Under Construction */
|
||||
package qgen
|
||||
import "database/sql"
|
||||
|
||||
type Builder struct
|
||||
{
|
||||
|
||||
var Builder *builder
|
||||
|
||||
func init() {
|
||||
Builder = &builder{conn:nil}
|
||||
}
|
||||
|
||||
// A set of wrappers around the generator methods, so that we can use this inline in Gosora
|
||||
type builder struct
|
||||
{
|
||||
conn *sql.DB
|
||||
adapter DB_Adapter
|
||||
}
|
||||
|
||||
func (build *builder) SetConn(conn *sql.DB) {
|
||||
build.conn = conn
|
||||
}
|
||||
|
||||
func (build *builder) SetAdapter(name string) error {
|
||||
adap, err := GetAdapter(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
build.adapter = adap
|
||||
return nil
|
||||
}
|
||||
|
||||
func (build *builder) SimpleSelect(table string, columns string, where string, orderby string/*, offset int, maxCount int*/) (stmt *sql.Stmt, err error) {
|
||||
res, err := build.adapter.SimpleSelect("_builder", table, columns, where, orderby /*, offset, maxCount*/)
|
||||
if err != nil {
|
||||
return stmt, err
|
||||
}
|
||||
return build.conn.Prepare(res)
|
||||
}
|
||||
|
|
|
@ -32,18 +32,18 @@ func (adapter *Mysql_Adapter) GetStmts() map[string]string {
|
|||
return adapter.Buffer
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleInsert(name string, table string, columns string, fields string) error {
|
||||
func (adapter *Mysql_Adapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table == "" {
|
||||
return errors.New("You need a name for this table")
|
||||
return "", errors.New("You need a name for this table")
|
||||
}
|
||||
if len(columns) == 0 {
|
||||
return errors.New("No columns found for SimpleInsert")
|
||||
return "", errors.New("No columns found for SimpleInsert")
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
return errors.New("No input data found for SimpleInsert")
|
||||
return "", errors.New("No input data found for SimpleInsert")
|
||||
}
|
||||
|
||||
var querystr string = "INSERT INTO `" + table + "`("
|
||||
|
@ -67,21 +67,21 @@ func (adapter *Mysql_Adapter) SimpleInsert(name string, table string, columns st
|
|||
querystr = querystr[0:len(querystr) - 1]
|
||||
|
||||
adapter.push_statement(name,querystr + ")")
|
||||
return nil
|
||||
return querystr + ")", nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleReplace(name string, table string, columns string, fields string) error {
|
||||
func (adapter *Mysql_Adapter) SimpleReplace(name string, table string, columns string, fields string) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table == "" {
|
||||
return errors.New("You need a name for this table")
|
||||
return "", errors.New("You need a name for this table")
|
||||
}
|
||||
if len(columns) == 0 {
|
||||
return errors.New("No columns found for SimpleInsert")
|
||||
return "", errors.New("No columns found for SimpleInsert")
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
return errors.New("No input data found for SimpleInsert")
|
||||
return "", errors.New("No input data found for SimpleInsert")
|
||||
}
|
||||
|
||||
var querystr string = "REPLACE INTO `" + table + "`("
|
||||
|
@ -94,7 +94,6 @@ func (adapter *Mysql_Adapter) SimpleReplace(name string, table string, columns s
|
|||
querystr += "`" + column.Left + "`,"
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the trailing comma
|
||||
querystr = querystr[0:len(querystr) - 1]
|
||||
|
||||
|
@ -105,18 +104,18 @@ func (adapter *Mysql_Adapter) SimpleReplace(name string, table string, columns s
|
|||
querystr = querystr[0:len(querystr) - 1]
|
||||
|
||||
adapter.push_statement(name,querystr + ")")
|
||||
return nil
|
||||
return querystr + ")", nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleUpdate(name string, table string, set string, where string) error {
|
||||
func (adapter *Mysql_Adapter) SimpleUpdate(name string, table string, set string, where string) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table == "" {
|
||||
return errors.New("You need a name for this table")
|
||||
return "", errors.New("You need a name for this table")
|
||||
}
|
||||
if set == "" {
|
||||
return errors.New("You need to set data in this update statement")
|
||||
return "", errors.New("You need to set data in this update statement")
|
||||
}
|
||||
|
||||
var querystr string = "UPDATE `" + table + "` SET "
|
||||
|
@ -161,18 +160,18 @@ func (adapter *Mysql_Adapter) SimpleUpdate(name string, table string, set string
|
|||
}
|
||||
|
||||
adapter.push_statement(name,querystr)
|
||||
return nil
|
||||
return querystr, nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleDelete(name string, table string, where string) error {
|
||||
func (adapter *Mysql_Adapter) SimpleDelete(name string, table string, where string) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table == "" {
|
||||
return errors.New("You need a name for this table")
|
||||
return "", errors.New("You need a name for this table")
|
||||
}
|
||||
if where == "" {
|
||||
return errors.New("You need to specify what data you want to delete")
|
||||
return "", errors.New("You need to specify what data you want to delete")
|
||||
}
|
||||
|
||||
var querystr string = "DELETE FROM `" + table + "` WHERE"
|
||||
|
@ -193,33 +192,33 @@ func (adapter *Mysql_Adapter) SimpleDelete(name string, table string, where stri
|
|||
|
||||
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
||||
}
|
||||
querystr = querystr[0:len(querystr) - 4]
|
||||
|
||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
||||
return nil
|
||||
querystr = strings.TrimSpace(querystr[0:len(querystr) - 4])
|
||||
adapter.push_statement(name,querystr)
|
||||
return querystr, nil
|
||||
}
|
||||
|
||||
// We don't want to accidentally wipe tables, so we'll have a seperate method for purging tables instead
|
||||
func (adapter *Mysql_Adapter) Purge(name string, table string) error {
|
||||
func (adapter *Mysql_Adapter) Purge(name string, table string) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table == "" {
|
||||
return errors.New("You need a name for this table")
|
||||
return "", errors.New("You need a name for this table")
|
||||
}
|
||||
adapter.push_statement(name,"DELETE FROM `" + table + "`")
|
||||
return nil
|
||||
return "DELETE FROM `" + table + "`", nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleSelect(name string, table string, columns string, where string, orderby string/*, offset int, maxCount int*/) error {
|
||||
func (adapter *Mysql_Adapter) SimpleSelect(name string, table string, columns string, where string, orderby string/*, offset int, maxCount int*/) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table == "" {
|
||||
return errors.New("You need a name for this table")
|
||||
return "", errors.New("You need a name for this table")
|
||||
}
|
||||
if len(columns) == 0 {
|
||||
return errors.New("No columns found for SimpleSelect")
|
||||
return "", errors.New("No columns found for SimpleSelect")
|
||||
}
|
||||
|
||||
// Slice up the user friendly strings into something easier to process
|
||||
|
@ -231,7 +230,6 @@ func (adapter *Mysql_Adapter) SimpleSelect(name string, table string, columns st
|
|||
for _, column := range colslice {
|
||||
querystr += "`" + strings.TrimSpace(column) + "`,"
|
||||
}
|
||||
|
||||
// Remove the trailing comma
|
||||
querystr = querystr[0:len(querystr) - 1]
|
||||
|
||||
|
@ -266,25 +264,26 @@ func (adapter *Mysql_Adapter) SimpleSelect(name string, table string, columns st
|
|||
querystr = querystr[0:len(querystr) - 1]
|
||||
}
|
||||
|
||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
||||
return nil
|
||||
querystr = strings.TrimSpace(querystr)
|
||||
adapter.push_statement(name,querystr)
|
||||
return querystr, nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleLeftJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string/*, offset int, maxCount int*/) error {
|
||||
func (adapter *Mysql_Adapter) SimpleLeftJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string/*, offset int, maxCount int*/) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table1 == "" {
|
||||
return errors.New("You need a name for the left table")
|
||||
return "", errors.New("You need a name for the left table")
|
||||
}
|
||||
if table2 == "" {
|
||||
return errors.New("You need a name for the right table")
|
||||
return "", errors.New("You need a name for the right table")
|
||||
}
|
||||
if len(columns) == 0 {
|
||||
return errors.New("No columns found for SimpleLeftJoin")
|
||||
return "", errors.New("No columns found for SimpleLeftJoin")
|
||||
}
|
||||
if len(joiners) == 0 {
|
||||
return errors.New("No joiners found for SimpleLeftJoin")
|
||||
return "", errors.New("No joiners found for SimpleLeftJoin")
|
||||
}
|
||||
|
||||
var querystr string = "SELECT "
|
||||
|
@ -351,25 +350,26 @@ func (adapter *Mysql_Adapter) SimpleLeftJoin(name string, table1 string, table2
|
|||
querystr = querystr[0:len(querystr) - 1]
|
||||
}
|
||||
|
||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
||||
return nil
|
||||
querystr = strings.TrimSpace(querystr)
|
||||
adapter.push_statement(name,querystr)
|
||||
return querystr, nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) SimpleInnerJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string/*, offset int, maxCount int*/) error {
|
||||
func (adapter *Mysql_Adapter) SimpleInnerJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string/*, offset int, maxCount int*/) (string, error) {
|
||||
if name == "" {
|
||||
return errors.New("You need a name for this statement")
|
||||
return "", errors.New("You need a name for this statement")
|
||||
}
|
||||
if table1 == "" {
|
||||
return errors.New("You need a name for the left table")
|
||||
return "", errors.New("You need a name for the left table")
|
||||
}
|
||||
if table2 == "" {
|
||||
return errors.New("You need a name for the right table")
|
||||
return "", errors.New("You need a name for the right table")
|
||||
}
|
||||
if len(columns) == 0 {
|
||||
return errors.New("No columns found for SimpleInnerJoin")
|
||||
return "", errors.New("No columns found for SimpleInnerJoin")
|
||||
}
|
||||
if len(joiners) == 0 {
|
||||
return errors.New("No joiners found for SimpleInnerJoin")
|
||||
return "", errors.New("No joiners found for SimpleInnerJoin")
|
||||
}
|
||||
|
||||
var querystr string = "SELECT "
|
||||
|
@ -436,8 +436,9 @@ func (adapter *Mysql_Adapter) SimpleInnerJoin(name string, table1 string, table2
|
|||
querystr = querystr[0:len(querystr) - 1]
|
||||
}
|
||||
|
||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
||||
return nil
|
||||
querystr = strings.TrimSpace(querystr)
|
||||
adapter.push_statement(name,querystr)
|
||||
return querystr, nil
|
||||
}
|
||||
|
||||
func (adapter *Mysql_Adapter) Write() error {
|
||||
|
|
|
@ -58,14 +58,14 @@ type DB_Setter struct {
|
|||
|
||||
type DB_Adapter interface {
|
||||
GetName() string
|
||||
SimpleInsert(string,string,string,string) error
|
||||
SimpleReplace(string,string,string,string) error
|
||||
SimpleUpdate(string,string,string,string) error
|
||||
SimpleDelete(string,string,string) error
|
||||
Purge(string,string) error
|
||||
SimpleSelect(string,string,string,string,string/*,int,int*/) error
|
||||
SimpleLeftJoin(string,string,string,string,string,string,string/*,int,int*/) error
|
||||
SimpleInnerJoin(string,string,string,string,string,string,string/*,int,int*/) error
|
||||
SimpleInsert(string,string,string,string) (string, error)
|
||||
SimpleReplace(string,string,string,string) (string, error)
|
||||
SimpleUpdate(string,string,string,string) (string, error)
|
||||
SimpleDelete(string,string,string) (string, error)
|
||||
Purge(string,string) (string, error)
|
||||
SimpleSelect(string,string,string,string,string/*,int,int*/) (string, error)
|
||||
SimpleLeftJoin(string,string,string,string,string,string,string/*,int,int*/) (string, error)
|
||||
SimpleInnerJoin(string,string,string,string,string,string,string/*,int,int*/) (string, error)
|
||||
Write() error
|
||||
|
||||
// TO-DO: Add a simple query builder
|
||||
|
|
|
@ -5,12 +5,6 @@ package qgen
|
|||
import "strings"
|
||||
import "os"
|
||||
|
||||
type _statement struct
|
||||
{
|
||||
Name string
|
||||
Body string
|
||||
}
|
||||
|
||||
func _process_columns(colstr string) (columns []DB_Column) {
|
||||
if colstr == "" {
|
||||
return columns
|
||||
|
|
|
@ -48,10 +48,8 @@ func write_statements(adapter qgen.DB_Adapter) error {
|
|||
func write_selects(adapter qgen.DB_Adapter) error {
|
||||
// url_prefix and url_name will be removed from this query in a later commit
|
||||
adapter.SimpleSelect("get_user","users","name, group, is_super_admin, avatar, message, url_prefix, url_name, level","uid = ?","")
|
||||
|
||||
adapter.SimpleSelect("get_full_user","users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?","")
|
||||
|
||||
adapter.SimpleSelect("get_topic","topics","title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data","tid = ?","")
|
||||
// Looking for get_topic? Your statement is in another castle
|
||||
|
||||
adapter.SimpleSelect("get_reply","replies","content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount","rid = ?","")
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package main
|
||||
|
||||
import "log"
|
||||
import "sync"
|
||||
import "database/sql"
|
||||
import "./query_gen/lib"
|
||||
|
||||
var topics TopicStore
|
||||
|
||||
|
@ -30,7 +32,15 @@ type StaticTopicStore struct {
|
|||
}
|
||||
|
||||
func NewStaticTopicStore(capacity int) *StaticTopicStore {
|
||||
return &StaticTopicStore{items:make(map[int]*Topic),capacity:capacity,get:get_topic_stmt}
|
||||
stmt, err := qgen.Builder.SimpleSelect("topics","title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data","tid = ?","")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &StaticTopicStore{
|
||||
items:make(map[int]*Topic),
|
||||
capacity:capacity,
|
||||
get:stmt,
|
||||
}
|
||||
}
|
||||
|
||||
func (sts *StaticTopicStore) Get(id int) (*Topic, error) {
|
||||
|
@ -161,7 +171,11 @@ type SqlTopicStore struct {
|
|||
}
|
||||
|
||||
func NewSqlTopicStore() *SqlTopicStore {
|
||||
return &SqlTopicStore{get_topic_stmt}
|
||||
stmt, err := qgen.Builder.SimpleSelect("topics","title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data","tid = ?","")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &SqlTopicStore{stmt}
|
||||
}
|
||||
|
||||
func (sts *SqlTopicStore) Get(id int) (*Topic, error) {
|
||||
|
|
132
user_store.go
132
user_store.go
|
@ -1,9 +1,11 @@
|
|||
package main
|
||||
|
||||
import "log"
|
||||
import "sync"
|
||||
import "strings"
|
||||
import "strconv"
|
||||
import "database/sql"
|
||||
import "./query_gen/lib"
|
||||
|
||||
var users UserStore
|
||||
|
||||
|
@ -28,41 +30,50 @@ type StaticUserStore struct {
|
|||
items map[int]*User
|
||||
length int
|
||||
capacity int
|
||||
get *sql.Stmt
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func NewStaticUserStore(capacity int) *StaticUserStore {
|
||||
return &StaticUserStore{items:make(map[int]*User),capacity:capacity}
|
||||
stmt, err := qgen.Builder.SimpleSelect("users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?","")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &StaticUserStore{
|
||||
items:make(map[int]*User),
|
||||
capacity:capacity,
|
||||
get:stmt,
|
||||
}
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) Get(id int) (*User, error) {
|
||||
sts.RLock()
|
||||
item, ok := sts.items[id]
|
||||
sts.RUnlock()
|
||||
func (sus *StaticUserStore) Get(id int) (*User, error) {
|
||||
sus.RLock()
|
||||
item, ok := sus.items[id]
|
||||
sus.RUnlock()
|
||||
if ok {
|
||||
return item, nil
|
||||
}
|
||||
return item, sql.ErrNoRows
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) GetUnsafe(id int) (*User, error) {
|
||||
item, ok := sts.items[id]
|
||||
func (sus *StaticUserStore) GetUnsafe(id int) (*User, error) {
|
||||
item, ok := sus.items[id]
|
||||
if ok {
|
||||
return item, nil
|
||||
}
|
||||
return item, sql.ErrNoRows
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) CascadeGet(id int) (*User, error) {
|
||||
sts.RLock()
|
||||
user, ok := sts.items[id]
|
||||
sts.RUnlock()
|
||||
func (sus *StaticUserStore) CascadeGet(id int) (*User, error) {
|
||||
sus.RLock()
|
||||
user, ok := sus.items[id]
|
||||
sus.RUnlock()
|
||||
if ok {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
user = &User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
|
||||
if user.Avatar != "" {
|
||||
if user.Avatar[0] == '.' {
|
||||
|
@ -74,14 +85,14 @@ func (sts *StaticUserStore) CascadeGet(id int) (*User, error) {
|
|||
user.Tag = groups[user.Group].Tag
|
||||
init_user_perms(user)
|
||||
if err == nil {
|
||||
sts.Set(user)
|
||||
sus.Set(user)
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) BypassGet(id int) (*User, error) {
|
||||
func (sus *StaticUserStore) BypassGet(id int) (*User, error) {
|
||||
user := &User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
|
||||
if user.Avatar != "" {
|
||||
if user.Avatar[0] == '.' {
|
||||
|
@ -95,11 +106,11 @@ func (sts *StaticUserStore) BypassGet(id int) (*User, error) {
|
|||
return user, err
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) Load(id int) error {
|
||||
func (sus *StaticUserStore) Load(id int) error {
|
||||
user := &User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
if err != nil {
|
||||
sts.Remove(id)
|
||||
sus.Remove(id)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -112,71 +123,71 @@ func (sts *StaticUserStore) Load(id int) error {
|
|||
}
|
||||
user.Tag = groups[user.Group].Tag
|
||||
init_user_perms(user)
|
||||
sts.Set(user)
|
||||
sus.Set(user)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) Set(item *User) error {
|
||||
sts.Lock()
|
||||
user, ok := sts.items[item.ID]
|
||||
func (sus *StaticUserStore) Set(item *User) error {
|
||||
sus.Lock()
|
||||
user, ok := sus.items[item.ID]
|
||||
if ok {
|
||||
sts.Unlock()
|
||||
sus.Unlock()
|
||||
*user = *item
|
||||
} else if sts.length >= sts.capacity {
|
||||
sts.Unlock()
|
||||
} else if sus.length >= sus.capacity {
|
||||
sus.Unlock()
|
||||
return ErrStoreCapacityOverflow
|
||||
} else {
|
||||
sts.items[item.ID] = item
|
||||
sts.Unlock()
|
||||
sts.length++
|
||||
sus.items[item.ID] = item
|
||||
sus.Unlock()
|
||||
sus.length++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) Add(item *User) error {
|
||||
if sts.length >= sts.capacity {
|
||||
func (sus *StaticUserStore) Add(item *User) error {
|
||||
if sus.length >= sus.capacity {
|
||||
return ErrStoreCapacityOverflow
|
||||
}
|
||||
sts.Lock()
|
||||
sts.items[item.ID] = item
|
||||
sts.Unlock()
|
||||
sts.length++
|
||||
sus.Lock()
|
||||
sus.items[item.ID] = item
|
||||
sus.Unlock()
|
||||
sus.length++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) AddUnsafe(item *User) error {
|
||||
if sts.length >= sts.capacity {
|
||||
func (sus *StaticUserStore) AddUnsafe(item *User) error {
|
||||
if sus.length >= sus.capacity {
|
||||
return ErrStoreCapacityOverflow
|
||||
}
|
||||
sts.items[item.ID] = item
|
||||
sts.length++
|
||||
sus.items[item.ID] = item
|
||||
sus.length++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) Remove(id int) error {
|
||||
sts.Lock()
|
||||
delete(sts.items,id)
|
||||
sts.Unlock()
|
||||
sts.length--
|
||||
func (sus *StaticUserStore) Remove(id int) error {
|
||||
sus.Lock()
|
||||
delete(sus.items,id)
|
||||
sus.Unlock()
|
||||
sus.length--
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) RemoveUnsafe(id int) error {
|
||||
delete(sts.items,id)
|
||||
sts.length--
|
||||
func (sus *StaticUserStore) RemoveUnsafe(id int) error {
|
||||
delete(sus.items,id)
|
||||
sus.length--
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) GetLength() int {
|
||||
return sts.length
|
||||
func (sus *StaticUserStore) GetLength() int {
|
||||
return sus.length
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) SetCapacity(capacity int) {
|
||||
sts.capacity = capacity
|
||||
func (sus *StaticUserStore) SetCapacity(capacity int) {
|
||||
sus.capacity = capacity
|
||||
}
|
||||
|
||||
func (sts *StaticUserStore) GetCapacity() int {
|
||||
return sts.capacity
|
||||
func (sus *StaticUserStore) GetCapacity() int {
|
||||
return sus.capacity
|
||||
}
|
||||
|
||||
//type DynamicUserStore struct {
|
||||
|
@ -185,15 +196,20 @@ func (sts *StaticUserStore) GetCapacity() int {
|
|||
//}
|
||||
|
||||
type SqlUserStore struct {
|
||||
get *sql.Stmt
|
||||
}
|
||||
|
||||
func NewSqlUserStore() *SqlUserStore {
|
||||
return &SqlUserStore{}
|
||||
stmt, err := qgen.Builder.SimpleSelect("users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?","")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &SqlUserStore{stmt}
|
||||
}
|
||||
|
||||
func (sus *SqlUserStore) Get(id int) (*User, error) {
|
||||
user := User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
|
||||
if user.Avatar != "" {
|
||||
if user.Avatar[0] == '.' {
|
||||
|
@ -209,7 +225,7 @@ func (sus *SqlUserStore) Get(id int) (*User, error) {
|
|||
|
||||
func (sus *SqlUserStore) GetUnsafe(id int) (*User, error) {
|
||||
user := User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
|
||||
if user.Avatar != "" {
|
||||
if user.Avatar[0] == '.' {
|
||||
|
@ -225,7 +241,7 @@ func (sus *SqlUserStore) GetUnsafe(id int) (*User, error) {
|
|||
|
||||
func (sus *SqlUserStore) CascadeGet(id int) (*User, error) {
|
||||
user := User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
|
||||
if user.Avatar != "" {
|
||||
if user.Avatar[0] == '.' {
|
||||
|
@ -241,7 +257,7 @@ func (sus *SqlUserStore) CascadeGet(id int) (*User, error) {
|
|||
|
||||
func (sus *SqlUserStore) BypassGet(id int) (*User, error) {
|
||||
user := User{ID:id,Loggedin:true}
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
|
||||
if user.Avatar != "" {
|
||||
if user.Avatar[0] == '.' {
|
||||
|
@ -258,7 +274,7 @@ func (sus *SqlUserStore) BypassGet(id int) (*User, error) {
|
|||
func (sus *SqlUserStore) Load(id int) error {
|
||||
user := &User{ID:id}
|
||||
// Simplify this into a quick check whether the user exists
|
||||
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
err := sus.get.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue