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"
|
import "database/sql"
|
||||||
|
|
||||||
var get_user_stmt *sql.Stmt
|
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 get_reply_stmt *sql.Stmt
|
||||||
var login_stmt *sql.Stmt
|
var login_stmt *sql.Stmt
|
||||||
var get_password_stmt *sql.Stmt
|
var get_password_stmt *sql.Stmt
|
||||||
|
@ -109,18 +107,6 @@ func gen_mysql() (err error) {
|
||||||
return err
|
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.")
|
log.Print("Preparing get_reply statement.")
|
||||||
get_reply_stmt, err = db.Prepare("SELECT `content`,`createdBy`,`createdAt`,`lastEdit`,`lastEditBy`,`ipaddress`,`likeCount` FROM `replies` WHERE `rid` = ?")
|
get_reply_stmt, err = db.Prepare("SELECT `content`,`createdBy`,`createdAt`,`lastEdit`,`lastEditBy`,`ipaddress`,`likeCount` FROM `replies` WHERE `rid` = ?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
8
mysql.go
8
mysql.go
|
@ -8,6 +8,7 @@ import "strconv"
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
import _ "github.com/go-sql-driver/mysql"
|
import _ "github.com/go-sql-driver/mysql"
|
||||||
|
import "./query_gen/lib"
|
||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var db_version string
|
var db_version string
|
||||||
|
@ -61,6 +62,13 @@ func init_database() (err error) {
|
||||||
return err
|
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.")
|
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))
|
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 {
|
if err != nil {
|
||||||
|
|
|
@ -1,7 +1,37 @@
|
||||||
/* WIP Under Construction */
|
/* WIP Under Construction */
|
||||||
package qgen
|
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
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table == "" {
|
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 {
|
if len(columns) == 0 {
|
||||||
return errors.New("No columns found for SimpleInsert")
|
return "", errors.New("No columns found for SimpleInsert")
|
||||||
}
|
}
|
||||||
if len(fields) == 0 {
|
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 + "`("
|
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]
|
querystr = querystr[0:len(querystr) - 1]
|
||||||
|
|
||||||
adapter.push_statement(name,querystr + ")")
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table == "" {
|
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 {
|
if len(columns) == 0 {
|
||||||
return errors.New("No columns found for SimpleInsert")
|
return "", errors.New("No columns found for SimpleInsert")
|
||||||
}
|
}
|
||||||
if len(fields) == 0 {
|
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 + "`("
|
var querystr string = "REPLACE INTO `" + table + "`("
|
||||||
|
@ -94,7 +94,6 @@ func (adapter *Mysql_Adapter) SimpleReplace(name string, table string, columns s
|
||||||
querystr += "`" + column.Left + "`,"
|
querystr += "`" + column.Left + "`,"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the trailing comma
|
// Remove the trailing comma
|
||||||
querystr = querystr[0:len(querystr) - 1]
|
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]
|
querystr = querystr[0:len(querystr) - 1]
|
||||||
|
|
||||||
adapter.push_statement(name,querystr + ")")
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table == "" {
|
if table == "" {
|
||||||
return errors.New("You need a name for this table")
|
return "", errors.New("You need a name for this table")
|
||||||
}
|
}
|
||||||
if set == "" {
|
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 "
|
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)
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table == "" {
|
if table == "" {
|
||||||
return errors.New("You need a name for this table")
|
return "", errors.New("You need a name for this table")
|
||||||
}
|
}
|
||||||
if where == "" {
|
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"
|
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 += " " + left + " " + loc.Operator + " " + right + " AND "
|
||||||
}
|
}
|
||||||
querystr = querystr[0:len(querystr) - 4]
|
|
||||||
|
|
||||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
querystr = strings.TrimSpace(querystr[0:len(querystr) - 4])
|
||||||
return nil
|
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
|
// 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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table == "" {
|
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 + "`")
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table == "" {
|
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 {
|
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
|
// 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 {
|
for _, column := range colslice {
|
||||||
querystr += "`" + strings.TrimSpace(column) + "`,"
|
querystr += "`" + strings.TrimSpace(column) + "`,"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the trailing comma
|
// Remove the trailing comma
|
||||||
querystr = querystr[0:len(querystr) - 1]
|
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]
|
querystr = querystr[0:len(querystr) - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
querystr = strings.TrimSpace(querystr)
|
||||||
return nil
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table1 == "" {
|
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 == "" {
|
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 {
|
if len(columns) == 0 {
|
||||||
return errors.New("No columns found for SimpleLeftJoin")
|
return "", errors.New("No columns found for SimpleLeftJoin")
|
||||||
}
|
}
|
||||||
if len(joiners) == 0 {
|
if len(joiners) == 0 {
|
||||||
return errors.New("No joiners found for SimpleLeftJoin")
|
return "", errors.New("No joiners found for SimpleLeftJoin")
|
||||||
}
|
}
|
||||||
|
|
||||||
var querystr string = "SELECT "
|
var querystr string = "SELECT "
|
||||||
|
@ -351,25 +350,26 @@ func (adapter *Mysql_Adapter) SimpleLeftJoin(name string, table1 string, table2
|
||||||
querystr = querystr[0:len(querystr) - 1]
|
querystr = querystr[0:len(querystr) - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
querystr = strings.TrimSpace(querystr)
|
||||||
return nil
|
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 == "" {
|
if name == "" {
|
||||||
return errors.New("You need a name for this statement")
|
return "", errors.New("You need a name for this statement")
|
||||||
}
|
}
|
||||||
if table1 == "" {
|
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 == "" {
|
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 {
|
if len(columns) == 0 {
|
||||||
return errors.New("No columns found for SimpleInnerJoin")
|
return "", errors.New("No columns found for SimpleInnerJoin")
|
||||||
}
|
}
|
||||||
if len(joiners) == 0 {
|
if len(joiners) == 0 {
|
||||||
return errors.New("No joiners found for SimpleInnerJoin")
|
return "", errors.New("No joiners found for SimpleInnerJoin")
|
||||||
}
|
}
|
||||||
|
|
||||||
var querystr string = "SELECT "
|
var querystr string = "SELECT "
|
||||||
|
@ -436,8 +436,9 @@ func (adapter *Mysql_Adapter) SimpleInnerJoin(name string, table1 string, table2
|
||||||
querystr = querystr[0:len(querystr) - 1]
|
querystr = querystr[0:len(querystr) - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter.push_statement(name,strings.TrimSpace(querystr))
|
querystr = strings.TrimSpace(querystr)
|
||||||
return nil
|
adapter.push_statement(name,querystr)
|
||||||
|
return querystr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (adapter *Mysql_Adapter) Write() error {
|
func (adapter *Mysql_Adapter) Write() error {
|
||||||
|
|
|
@ -58,14 +58,14 @@ type DB_Setter struct {
|
||||||
|
|
||||||
type DB_Adapter interface {
|
type DB_Adapter interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
SimpleInsert(string,string,string,string) error
|
SimpleInsert(string,string,string,string) (string, error)
|
||||||
SimpleReplace(string,string,string,string) error
|
SimpleReplace(string,string,string,string) (string, error)
|
||||||
SimpleUpdate(string,string,string,string) error
|
SimpleUpdate(string,string,string,string) (string, error)
|
||||||
SimpleDelete(string,string,string) error
|
SimpleDelete(string,string,string) (string, error)
|
||||||
Purge(string,string) error
|
Purge(string,string) (string, error)
|
||||||
SimpleSelect(string,string,string,string,string/*,int,int*/) error
|
SimpleSelect(string,string,string,string,string/*,int,int*/) (string, error)
|
||||||
SimpleLeftJoin(string,string,string,string,string,string,string/*,int,int*/) error
|
SimpleLeftJoin(string,string,string,string,string,string,string/*,int,int*/) (string, error)
|
||||||
SimpleInnerJoin(string,string,string,string,string,string,string/*,int,int*/) error
|
SimpleInnerJoin(string,string,string,string,string,string,string/*,int,int*/) (string, error)
|
||||||
Write() error
|
Write() error
|
||||||
|
|
||||||
// TO-DO: Add a simple query builder
|
// TO-DO: Add a simple query builder
|
||||||
|
|
|
@ -5,12 +5,6 @@ package qgen
|
||||||
import "strings"
|
import "strings"
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
type _statement struct
|
|
||||||
{
|
|
||||||
Name string
|
|
||||||
Body string
|
|
||||||
}
|
|
||||||
|
|
||||||
func _process_columns(colstr string) (columns []DB_Column) {
|
func _process_columns(colstr string) (columns []DB_Column) {
|
||||||
if colstr == "" {
|
if colstr == "" {
|
||||||
return columns
|
return columns
|
||||||
|
|
|
@ -48,10 +48,8 @@ func write_statements(adapter qgen.DB_Adapter) error {
|
||||||
func write_selects(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
|
// 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_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 = ?","")
|
adapter.SimpleSelect("get_reply","replies","content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount","rid = ?","")
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
import "sync"
|
import "sync"
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
|
import "./query_gen/lib"
|
||||||
|
|
||||||
var topics TopicStore
|
var topics TopicStore
|
||||||
|
|
||||||
|
@ -30,7 +32,15 @@ type StaticTopicStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStaticTopicStore(capacity int) *StaticTopicStore {
|
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) {
|
func (sts *StaticTopicStore) Get(id int) (*Topic, error) {
|
||||||
|
@ -161,7 +171,11 @@ type SqlTopicStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSqlTopicStore() *SqlTopicStore {
|
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) {
|
func (sts *SqlTopicStore) Get(id int) (*Topic, error) {
|
||||||
|
|
132
user_store.go
132
user_store.go
|
@ -1,9 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
import "sync"
|
import "sync"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
|
import "./query_gen/lib"
|
||||||
|
|
||||||
var users UserStore
|
var users UserStore
|
||||||
|
|
||||||
|
@ -28,41 +30,50 @@ type StaticUserStore struct {
|
||||||
items map[int]*User
|
items map[int]*User
|
||||||
length int
|
length int
|
||||||
capacity int
|
capacity int
|
||||||
|
get *sql.Stmt
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStaticUserStore(capacity int) *StaticUserStore {
|
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) {
|
func (sus *StaticUserStore) Get(id int) (*User, error) {
|
||||||
sts.RLock()
|
sus.RLock()
|
||||||
item, ok := sts.items[id]
|
item, ok := sus.items[id]
|
||||||
sts.RUnlock()
|
sus.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return item, nil
|
return item, nil
|
||||||
}
|
}
|
||||||
return item, sql.ErrNoRows
|
return item, sql.ErrNoRows
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) GetUnsafe(id int) (*User, error) {
|
func (sus *StaticUserStore) GetUnsafe(id int) (*User, error) {
|
||||||
item, ok := sts.items[id]
|
item, ok := sus.items[id]
|
||||||
if ok {
|
if ok {
|
||||||
return item, nil
|
return item, nil
|
||||||
}
|
}
|
||||||
return item, sql.ErrNoRows
|
return item, sql.ErrNoRows
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) CascadeGet(id int) (*User, error) {
|
func (sus *StaticUserStore) CascadeGet(id int) (*User, error) {
|
||||||
sts.RLock()
|
sus.RLock()
|
||||||
user, ok := sts.items[id]
|
user, ok := sus.items[id]
|
||||||
sts.RUnlock()
|
sus.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
user = &User{ID:id,Loggedin:true}
|
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 != "" {
|
||||||
if user.Avatar[0] == '.' {
|
if user.Avatar[0] == '.' {
|
||||||
|
@ -74,14 +85,14 @@ func (sts *StaticUserStore) CascadeGet(id int) (*User, error) {
|
||||||
user.Tag = groups[user.Group].Tag
|
user.Tag = groups[user.Group].Tag
|
||||||
init_user_perms(user)
|
init_user_perms(user)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
sts.Set(user)
|
sus.Set(user)
|
||||||
}
|
}
|
||||||
return user, err
|
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}
|
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 != "" {
|
||||||
if user.Avatar[0] == '.' {
|
if user.Avatar[0] == '.' {
|
||||||
|
@ -95,11 +106,11 @@ func (sts *StaticUserStore) BypassGet(id int) (*User, error) {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) Load(id int) error {
|
func (sus *StaticUserStore) Load(id int) error {
|
||||||
user := &User{ID:id,Loggedin:true}
|
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 {
|
if err != nil {
|
||||||
sts.Remove(id)
|
sus.Remove(id)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,71 +123,71 @@ func (sts *StaticUserStore) Load(id int) error {
|
||||||
}
|
}
|
||||||
user.Tag = groups[user.Group].Tag
|
user.Tag = groups[user.Group].Tag
|
||||||
init_user_perms(user)
|
init_user_perms(user)
|
||||||
sts.Set(user)
|
sus.Set(user)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) Set(item *User) error {
|
func (sus *StaticUserStore) Set(item *User) error {
|
||||||
sts.Lock()
|
sus.Lock()
|
||||||
user, ok := sts.items[item.ID]
|
user, ok := sus.items[item.ID]
|
||||||
if ok {
|
if ok {
|
||||||
sts.Unlock()
|
sus.Unlock()
|
||||||
*user = *item
|
*user = *item
|
||||||
} else if sts.length >= sts.capacity {
|
} else if sus.length >= sus.capacity {
|
||||||
sts.Unlock()
|
sus.Unlock()
|
||||||
return ErrStoreCapacityOverflow
|
return ErrStoreCapacityOverflow
|
||||||
} else {
|
} else {
|
||||||
sts.items[item.ID] = item
|
sus.items[item.ID] = item
|
||||||
sts.Unlock()
|
sus.Unlock()
|
||||||
sts.length++
|
sus.length++
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) Add(item *User) error {
|
func (sus *StaticUserStore) Add(item *User) error {
|
||||||
if sts.length >= sts.capacity {
|
if sus.length >= sus.capacity {
|
||||||
return ErrStoreCapacityOverflow
|
return ErrStoreCapacityOverflow
|
||||||
}
|
}
|
||||||
sts.Lock()
|
sus.Lock()
|
||||||
sts.items[item.ID] = item
|
sus.items[item.ID] = item
|
||||||
sts.Unlock()
|
sus.Unlock()
|
||||||
sts.length++
|
sus.length++
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) AddUnsafe(item *User) error {
|
func (sus *StaticUserStore) AddUnsafe(item *User) error {
|
||||||
if sts.length >= sts.capacity {
|
if sus.length >= sus.capacity {
|
||||||
return ErrStoreCapacityOverflow
|
return ErrStoreCapacityOverflow
|
||||||
}
|
}
|
||||||
sts.items[item.ID] = item
|
sus.items[item.ID] = item
|
||||||
sts.length++
|
sus.length++
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) Remove(id int) error {
|
func (sus *StaticUserStore) Remove(id int) error {
|
||||||
sts.Lock()
|
sus.Lock()
|
||||||
delete(sts.items,id)
|
delete(sus.items,id)
|
||||||
sts.Unlock()
|
sus.Unlock()
|
||||||
sts.length--
|
sus.length--
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) RemoveUnsafe(id int) error {
|
func (sus *StaticUserStore) RemoveUnsafe(id int) error {
|
||||||
delete(sts.items,id)
|
delete(sus.items,id)
|
||||||
sts.length--
|
sus.length--
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) GetLength() int {
|
func (sus *StaticUserStore) GetLength() int {
|
||||||
return sts.length
|
return sus.length
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) SetCapacity(capacity int) {
|
func (sus *StaticUserStore) SetCapacity(capacity int) {
|
||||||
sts.capacity = capacity
|
sus.capacity = capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *StaticUserStore) GetCapacity() int {
|
func (sus *StaticUserStore) GetCapacity() int {
|
||||||
return sts.capacity
|
return sus.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
//type DynamicUserStore struct {
|
//type DynamicUserStore struct {
|
||||||
|
@ -185,15 +196,20 @@ func (sts *StaticUserStore) GetCapacity() int {
|
||||||
//}
|
//}
|
||||||
|
|
||||||
type SqlUserStore struct {
|
type SqlUserStore struct {
|
||||||
|
get *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSqlUserStore() *SqlUserStore {
|
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) {
|
func (sus *SqlUserStore) Get(id int) (*User, error) {
|
||||||
user := User{ID:id,Loggedin:true}
|
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 != "" {
|
||||||
if user.Avatar[0] == '.' {
|
if user.Avatar[0] == '.' {
|
||||||
|
@ -209,7 +225,7 @@ func (sus *SqlUserStore) Get(id int) (*User, error) {
|
||||||
|
|
||||||
func (sus *SqlUserStore) GetUnsafe(id int) (*User, error) {
|
func (sus *SqlUserStore) GetUnsafe(id int) (*User, error) {
|
||||||
user := User{ID:id,Loggedin:true}
|
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 != "" {
|
||||||
if user.Avatar[0] == '.' {
|
if user.Avatar[0] == '.' {
|
||||||
|
@ -225,7 +241,7 @@ func (sus *SqlUserStore) GetUnsafe(id int) (*User, error) {
|
||||||
|
|
||||||
func (sus *SqlUserStore) CascadeGet(id int) (*User, error) {
|
func (sus *SqlUserStore) CascadeGet(id int) (*User, error) {
|
||||||
user := User{ID:id,Loggedin:true}
|
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 != "" {
|
||||||
if user.Avatar[0] == '.' {
|
if user.Avatar[0] == '.' {
|
||||||
|
@ -241,7 +257,7 @@ func (sus *SqlUserStore) CascadeGet(id int) (*User, error) {
|
||||||
|
|
||||||
func (sus *SqlUserStore) BypassGet(id int) (*User, error) {
|
func (sus *SqlUserStore) BypassGet(id int) (*User, error) {
|
||||||
user := User{ID:id,Loggedin:true}
|
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 != "" {
|
||||||
if user.Avatar[0] == '.' {
|
if user.Avatar[0] == '.' {
|
||||||
|
@ -258,7 +274,7 @@ func (sus *SqlUserStore) BypassGet(id int) (*User, error) {
|
||||||
func (sus *SqlUserStore) Load(id int) error {
|
func (sus *SqlUserStore) Load(id int) error {
|
||||||
user := &User{ID:id}
|
user := &User{ID:id}
|
||||||
// Simplify this into a quick check whether the user exists
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue