The Advanced Forum Editor is now accessible via the Forum Manager.

Renamed data.sql to mysql.sql
Added a composite primary key to the forums_permissions table.
Moved five more queries to the query generator.
Revamped the query generator. Yesterday's commit was more of a prototype. Still lots to be done to facilitate all the queries.
Added support for simple left joins to the query generator.
Removed .smaller
Fixed an issue with the permissions not saving with the Advanced Forum Editor.

Remind me to keep all generators away from Atom.
This commit is contained in:
Azareal 2017-06-06 09:47:33 +01:00
parent e099dfd40e
commit 93d00b0337
11 changed files with 251 additions and 131 deletions

View File

@ -11,6 +11,11 @@ var get_reply_stmt *sql.Stmt
var login_stmt *sql.Stmt
var get_password_stmt *sql.Stmt
var username_exists_stmt *sql.Stmt
var get_settings_stmt *sql.Stmt
var get_setting_stmt *sql.Stmt
var get_full_setting_stmt *sql.Stmt
var is_plugin_active_stmt *sql.Stmt
var get_topic_list_stmt *sql.Stmt
func gen_mysql() (err error) {
if debug {
@ -59,5 +64,35 @@ func gen_mysql() (err error) {
return err
}
log.Print("Preparing get_settings statement.")
get_settings_stmt, err = db.Prepare("SELECT `name`,`content`,`type` FROM settings")
if err != nil {
return err
}
log.Print("Preparing get_setting statement.")
get_setting_stmt, err = db.Prepare("SELECT `content`,`type` FROM settings WHERE `name` = ?")
if err != nil {
return err
}
log.Print("Preparing get_full_setting statement.")
get_full_setting_stmt, err = db.Prepare("SELECT `name`,`type`,`constraints` FROM settings WHERE `name` = ?")
if err != nil {
return err
}
log.Print("Preparing is_plugin_active statement.")
is_plugin_active_stmt, err = db.Prepare("SELECT `active` FROM plugins WHERE `uname` = ?")
if err != nil {
return err
}
log.Print("Preparing get_topic_list statement.")
get_topic_list_stmt, err = db.Prepare("SELECT `topics`.`tid`,`topics`.`title`,`topics`.`content`,`topics`.`createdBy`,`topics`.`is_closed`,`topics`.`sticky`,`topics`.`createdAt`,`topics`.`parentID`,`users`.`name`,`users`.`avatar` FROM topics LEFT JOIN users ON `topics`.`createdBy`=`users`.`uid` ORDER BY topics.sticky DESC,topics.lastReplyAt DESC,topics.createdBy DESC")
if err != nil {
return err
}
return nil
}

View File

@ -1,25 +1,22 @@
/* Copyright Azareal 2017 - 2018 */
package main
import "fmt"
import "os"
import "bytes"
import "bufio"
import "strconv"
import "io/ioutil"
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import (
"fmt"
"os"
"bytes"
"bufio"
"strconv"
"io/ioutil"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
var scanner *bufio.Scanner
var db_host string
var db_username string
var db_password string
var db_name string
var db_host, db_username, db_password, db_name string
//var db_collation string = "utf8mb4_general_ci"
var db_port string = "3306"
var site_name string
var site_url string
var server_port string
var site_name, site_url, server_port string
var default_host string = "localhost"
var default_username string = "root"
@ -79,7 +76,7 @@ func main() {
fmt.Println("Successfully connected to the database")
fmt.Println("Opening the database seed file")
sqlContents, err := ioutil.ReadFile("./data.sql")
sqlContents, err := ioutil.ReadFile("./mysql.sql")
if err != nil {
fmt.Println(err)
fmt.Println("Aborting installation...")

View File

@ -12,7 +12,6 @@ var db *sql.DB
var db_version string
var db_collation string = "utf8mb4_general_ci"
var get_topic_list_stmt *sql.Stmt
var get_topic_user_stmt *sql.Stmt
var get_topic_by_reply_stmt *sql.Stmt
var get_topic_replies_stmt *sql.Stmt
@ -119,12 +118,6 @@ func init_database() (err error) {
return err
}
log.Print("Preparing get_topic_list statement.")
get_topic_list_stmt, err = db.Prepare("select topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.parentID, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid order by topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy DESC")
if err != nil {
return err
}
log.Print("Preparing get_topic_user statement.")
get_topic_user_stmt, err = db.Prepare("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.url_prefix, users.url_name, users.level from topics left join users ON topics.createdBy = users.uid where tid = ?")
if err != nil {

View File

@ -62,7 +62,8 @@ CREATE TABLE `forums_permissions`(
`fid` int not null,
`gid` int not null,
`preset` varchar(100) DEFAULT '' not null,
`permissions` text not null
`permissions` text not null,
primary key(fid, gid)
);
CREATE TABLE `topics`(

View File

@ -471,7 +471,7 @@ func route_panel_forums_edit_perms_submit(w http.ResponseWriter, r *http.Request
return
}
gid, err := strconv.Atoi("gid")
gid, err := strconv.Atoi(r.PostFormValue("gid"))
if err != nil {
LocalErrorJSQ("Invalid Group ID",w,r,user,is_js)
return
@ -496,6 +496,13 @@ func route_panel_forums_edit_perms_submit(w http.ResponseWriter, r *http.Request
return
}
permupdate_mutex.Unlock()
_, err = update_forum_stmt.Exec(forums[fid].Name,forums[fid].Desc,forums[fid].Active,"",fid)
if err != nil {
InternalErrorJSQ(err,w,r,is_js)
return
}
forums[fid].Preset = ""
}
if is_js == "0" {
@ -516,7 +523,7 @@ func route_panel_settings(w http.ResponseWriter, r *http.Request){
}
var settingList map[string]interface{} = make(map[string]interface{})
rows, err := db.Query("select name, content, type from settings")
rows, err := get_settings_stmt.Query()
if err != nil {
InternalError(err,w,r)
return
@ -570,7 +577,7 @@ func route_panel_setting(w http.ResponseWriter, r *http.Request, sname string){
}
setting := Setting{sname,"","",""}
err := db.QueryRow("select content, type from settings where name = ?", setting.Name).Scan(&setting.Content,&setting.Type)
err := get_setting_stmt.QueryRow(setting.Name).Scan(&setting.Content,&setting.Type)
if err == sql.ErrNoRows {
LocalError("The setting you want to edit doesn't exist.",w,r,user)
return
@ -627,11 +634,10 @@ func route_panel_setting_edit(w http.ResponseWriter, r *http.Request, sname stri
return
}
var stype string
var sconstraints string
var stype, sconstraints string
scontent := r.PostFormValue("setting-value")
err = db.QueryRow("select name, type, constraints from settings where name = ?", sname).Scan(&sname, &stype, &sconstraints)
err = get_full_setting_stmt.QueryRow(sname).Scan(&sname, &stype, &sconstraints)
if err == sql.ErrNoRows {
LocalError("The setting you want to edit doesn't exist.",w,r,user)
return
@ -702,7 +708,7 @@ func route_panel_plugins_activate(w http.ResponseWriter, r *http.Request, uname
}
var active bool
err := db.QueryRow("select active from plugins where uname = ?", uname).Scan(&active)
err := is_plugin_active_stmt.QueryRow(uname).Scan(&active)
if err != nil && err != sql.ErrNoRows {
InternalError(err,w,r)
return

View File

@ -36,16 +36,12 @@ function load_alerts(menu_alerts)
}
}
if(mmsg.length > 46) mmsg = mmsg.substring(0,43) + "...";
else if(mmsg.length > 35) size_dial = " smaller"; //9px
else size_dial = ""; // 10px
if("avatar" in msg)
{
alist += "<div class='alertItem withAvatar' style='background-image:url(\""+msg.avatar+"\");'><a class='text"+size_dial+"' href=\""+msg.path+"\">"+mmsg+"</a></div>";
alist += "<div class='alertItem withAvatar' style='background-image:url(\""+msg.avatar+"\");'><a class='text' href=\""+msg.path+"\">"+mmsg+"</a></div>";
anyAvatar = true
}
else alist += "<div class='alertItem'><a href=\""+msg.path+"\" class='text"+size_dial+"'>"+mmsg+"</a></div>";
else alist += "<div class='alertItem'><a href=\""+msg.path+"\" class='text'>"+mmsg+"</a></div>";
//console.log(msg);
//console.log(mmsg);
}
@ -219,6 +215,7 @@ $(document).ready(function(){
var block_parent = $(this).closest('.editable_parent');
//console.log(block_parent);
block_parent.find('.hide_on_edit').hide();
block_parent.find('.show_on_edit').show();
block_parent.find('.editable_block').show();
block_parent.find('.editable_block').each(function(){
var field_name = this.getAttribute("data-field");
@ -246,7 +243,6 @@ $(document).ready(function(){
else if(field_type=="hidden") {}
else this.innerHTML = "<input name='"+field_name+"' value='"+this.textContent+"' type='text'/>";
});
block_parent.find('.show_on_edit').eq(0).show();
// Remove any handlers already attached to the submitter
$(".submit_edit").unbind("click");

View File

@ -1,7 +1,7 @@
/* WIP Under Construction */
package main
import "fmt"
//import "fmt"
import "strings"
import "log"
import "os"
@ -9,6 +9,14 @@ import "os"
var db_registry []DB_Adapter
var blank_order []DB_Order
type DB_Column struct
{
Table string
Left string // Could be a function or a column, so I'm naming this Left
Alias string // aka AS Blah, if it's present
Type string // function or column
}
type DB_Where struct
{
Left string
@ -20,8 +28,10 @@ type DB_Where struct
type DB_Joiner struct
{
Left string
Right string
LeftTable string
LeftColumn string
RightTable string
RightColumn string
}
type DB_Order struct
@ -35,8 +45,8 @@ type DB_Adapter interface {
simple_insert(string,string,string,[]string,[]bool) error
//simple_replace(string,string,[]string,[]string,[]bool) error
simple_update() error
simple_select(string,string,string,string,[]DB_Order/*,int,int*/) error
simple_left_join(string,string,string,string,[]DB_Joiner,string,[]DB_Order/*,int,int*/) error
simple_select(string,string,string,string,string/*,int,int*/) error
simple_left_join(string,string,string,string,string,string,string/*,int,int*/) error
write() error
// TO-DO: Add a simple query builder
}
@ -52,46 +62,108 @@ func main() {
func write_statements(adapter DB_Adapter) error {
// url_prefix and url_name will be removed from this query in a later commit
adapter.simple_select("get_user","users","name, group, is_super_admin, avatar, message, url_prefix, url_name, level","uid = ?",blank_order)
adapter.simple_select("get_user","users","name, group, is_super_admin, avatar, message, url_prefix, url_name, level","uid = ?","")
adapter.simple_select("get_full_user","users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?",blank_order)
adapter.simple_select("get_full_user","users","name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip","uid = ?","")
adapter.simple_select("get_topic","topics","title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount","tid = ?",blank_order)
adapter.simple_select("get_topic","topics","title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount","tid = ?","")
adapter.simple_select("get_reply","replies","content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount","rid = ?",blank_order)
adapter.simple_select("get_reply","replies","content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount","rid = ?","")
adapter.simple_select("login","users","uid, name, password, salt","name = ?",blank_order)
adapter.simple_select("login","users","uid, name, password, salt","name = ?","")
adapter.simple_select("get_password","users","password,salt","uid = ?",blank_order)
adapter.simple_select("get_password","users","password,salt","uid = ?","")
adapter.simple_select("username_exists","users","name","name = ?",blank_order)
adapter.simple_select("username_exists","users","name","name = ?","")
adapter.simple_select("get_settings","settings","name, content, type","","")
adapter.simple_select("get_setting","settings","content, type","name = ?","")
adapter.simple_select("get_full_setting","settings","name, type, constraints","name = ?","")
adapter.simple_select("is_plugin_active","plugins","active","uname = ?","")
adapter.simple_left_join("get_topic_list","topics","users","topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.parentID, users.name, users.avatar","topics.createdBy = users.uid","","topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy DESC")
/*
get_topic_list_stmt, err = db.Prepare("select topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.parentID, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid order by topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy DESC")
// A visual reference for me to glance at while I design this thing
*/
//func (adapter *Mysql_Adapter) simple_left_join(name string, table1 string, table2 string, columns string, joiners []DB_Joiner, where []DB_Where, orderby []DB_Order/*, offset int, maxCount int*/) error {
/*adapter.simple_left_join("get_topic_list","topics","users",
"topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.parentID, users.name, users.avatar",
[]DB_Joiner{}
)*/
return nil
}
func write_file(name string, content string) (err error) {
f, err := os.Create(name)
if err != nil {
return err
func _process_columns(colstr string) (columns []DB_Column) {
if colstr == "" {
return columns
}
_, err = f.WriteString(content)
if err != nil {
return err
colstr = strings.Replace(colstr," as "," AS ",-1)
for _, segment := range strings.Split(colstr,",") {
var outcol DB_Column
dothalves := strings.Split(strings.TrimSpace(segment),".")
var halves []string
if len(dothalves) == 2 {
outcol.Table = dothalves[0]
halves = strings.Split(dothalves[1]," AS ")
} else {
halves = strings.Split(dothalves[0]," AS ")
}
f.Sync()
f.Close()
return
halves[0] = strings.TrimSpace(halves[0])
if len(halves) == 2 {
outcol.Alias = strings.TrimSpace(halves[1])
}
if halves[0][len(halves[0]) - 1] == ')' {
outcol.Type = "function"
} else {
outcol.Type = "column"
}
outcol.Left = halves[0]
columns = append(columns,outcol)
}
return columns
}
func _process_orderby(orderstr string) (order []DB_Order) {
if orderstr == "" {
return order
}
for _, segment := range strings.Split(orderstr,",") {
var outorder DB_Order
halves := strings.Split(strings.TrimSpace(segment)," ")
if len(halves) != 2 {
continue
}
outorder.Column = halves[0]
outorder.Order = strings.ToLower(halves[1])
order = append(order,outorder)
}
return order
}
func _process_joiner(joinstr string) (joiner []DB_Joiner) {
if joinstr == "" {
return joiner
}
joinstr = strings.Replace(joinstr," on "," ON ",-1)
joinstr = strings.Replace(joinstr," and "," AND ",-1)
for _, segment := range strings.Split(joinstr," AND ") {
var outjoin DB_Joiner
halves := strings.Split(segment,"=")
if len(halves) != 2 {
continue
}
left_column := strings.Split(halves[0],".")
right_column := strings.Split(halves[1],".")
outjoin.LeftTable = strings.TrimSpace(left_column[0])
outjoin.RightTable = strings.TrimSpace(right_column[0])
outjoin.LeftColumn = strings.TrimSpace(left_column[1])
outjoin.RightColumn = strings.TrimSpace(right_column[1])
joiner = append(joiner,outjoin)
}
return joiner
}
func _process_where(wherestr string) (where []DB_Where) {
@ -127,7 +199,7 @@ func _get_identifier_type(identifier string) string {
}
func _get_identifier(segment string, startOffset int) (out string, i int) {
fmt.Println("entering _get_identifier")
//fmt.Println("entering _get_identifier")
segment = strings.TrimSpace(segment)
segment += " " // Avoid overflow bugs with slicing
for i = startOffset; i < len(segment); i++ {
@ -136,10 +208,10 @@ func _get_identifier(segment string, startOffset int) (out string, i int) {
return strings.TrimSpace(segment[startOffset:i]), (i - 1)
}
if segment[i] == ' ' && i != startOffset {
fmt.Println("segment[startOffset:i]",segment[startOffset:i])
fmt.Println("startOffset",startOffset)
fmt.Println("segment[startOffset]",string(segment[startOffset]))
fmt.Println("i",i)
//fmt.Println("segment[startOffset:i]",segment[startOffset:i])
//fmt.Println("startOffset",startOffset)
//fmt.Println("segment[startOffset]",string(segment[startOffset]))
//fmt.Println("i",i)
return strings.TrimSpace(segment[startOffset:i]), (i - 1)
}
}
@ -171,3 +243,17 @@ func _skip_function_call(segment string, i int) int {
}
return i
}
func write_file(name string, content string) (err error) {
f, err := os.Create(name)
if err != nil {
return err
}
_, err = f.WriteString(content)
if err != nil {
return err
}
f.Sync()
f.Close()
return
}

View File

@ -1,7 +1,7 @@
/* WIP Under Construction */
package main
import "fmt"
//import "fmt"
import "strings"
import "errors"
@ -67,7 +67,7 @@ func (adapter *Mysql_Adapter) simple_update() error {
return nil
}
func (adapter *Mysql_Adapter) simple_select(name string, table string, columns string, where string, orderby []DB_Order/*, offset int, maxCount int*/) error {
func (adapter *Mysql_Adapter) simple_select(name string, table string, columns string, where string, orderby string/*, offset int, maxCount int*/) error {
if name == "" {
return errors.New("You need a name for this statement")
}
@ -94,8 +94,8 @@ func (adapter *Mysql_Adapter) simple_select(name string, table string, columns s
querystr += " FROM " + table
if len(where) != 0 {
querystr += " WHERE"
fmt.Println("where",where)
fmt.Println("_process_where(where)",_process_where(where))
//fmt.Println("where",where)
//fmt.Println("_process_where(where)",_process_where(where))
for _, loc := range _process_where(where) {
var lquote, rquote string
if loc.LeftType == "column" {
@ -104,25 +104,25 @@ func (adapter *Mysql_Adapter) simple_select(name string, table string, columns s
if loc.RightType == "column" {
rquote = "`"
}
querystr += " " + lquote + loc.Left + lquote + loc.Operator + " " + rquote + loc.Right + rquote + " AND "
}
querystr += " " + lquote + loc.Left + lquote + " " + loc.Operator + " " + rquote + loc.Right + rquote + " AND "
}
// Remove the trailing AND
querystr = querystr[0:len(querystr) - 4]
}
if len(orderby) != 0 {
querystr += " ORDER BY "
for _, column := range orderby {
querystr += column.Column + " " + column.Order + ","
}
for _, column := range _process_orderby(orderby) {
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
}
querystr = querystr[0:len(querystr) - 1]
}
adapter.write_statement(name,querystr)
adapter.write_statement(name,strings.TrimSpace(querystr))
return nil
}
func (adapter *Mysql_Adapter) simple_left_join(name string, table1 string, table2 string, columns string, joiners []DB_Joiner, where string, orderby []DB_Order/*, offset int, maxCount int*/) error {
func (adapter *Mysql_Adapter) simple_left_join(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string/*, offset int, maxCount int*/) error {
if name == "" {
return errors.New("You need a name for this statement")
}
@ -139,22 +139,32 @@ func (adapter *Mysql_Adapter) simple_left_join(name string, table1 string, table
return errors.New("No joiners found for simple_left_join")
}
// Slice up the user friendly strings into something easier to process
var colslice []string = strings.Split(strings.TrimSpace(columns),",")
var querystr string = "SELECT "
for _, column := range _process_columns(columns) {
var source, alias string
// Escape the column names, just in case we've used a reserved keyword
for _, column := range colslice {
querystr += "`" + strings.TrimSpace(column) + "`,"
if column.Table != "" {
source = "`" + column.Table + "`.`" + column.Left + "`"
} else if column.Type == "function" {
source = column.Left
} else {
source = "`" + column.Left + "`"
}
if column.Alias != "" {
alias = " AS `" + column.Alias + "`"
}
querystr += source + alias + ","
}
// Remove the trailing comma
querystr = querystr[0:len(querystr) - 1]
querystr += " FROM " + table1 + " LEFT JOIN " + table2 + " ON "
for _, joiner := range joiners {
querystr += "`" + joiner.Left + "`=`" + joiner.Right + "` AND "
for _, joiner := range _process_joiner(joiners) {
querystr += "`" + joiner.LeftTable + "`.`" + joiner.LeftColumn + "`=`" + joiner.RightTable + "`.`" + joiner.RightColumn + "` AND "
}
// Remove the trailing AND
querystr = querystr[0:len(querystr) - 4]
@ -169,20 +179,20 @@ func (adapter *Mysql_Adapter) simple_left_join(name string, table1 string, table
if loc.RightType == "column" {
rquote = "`"
}
querystr += " " + lquote + loc.Left + lquote + loc.Operator + " " + rquote + loc.Right + rquote + " AND "
}
querystr += " " + lquote + loc.Left + lquote + " " + loc.Operator + " " + rquote + loc.Right + rquote + " AND "
}
querystr = querystr[0:len(querystr) - 3]
}
if len(orderby) != 0 {
querystr += " ORDER BY "
for _, column := range orderby {
querystr += column.Column + " " + column.Order + ","
}
for _, column := range _process_orderby(orderby) {
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
}
querystr = querystr[0:len(querystr) - 1]
}
adapter.write_statement(name,querystr)
adapter.write_statement(name,strings.TrimSpace(querystr))
return nil
}

View File

@ -141,7 +141,6 @@ li:hover {
width: 100%;
color: black;
}
.alertItem .text.smaller { font-size: 9px; }
#footer {
clear: left;

View File

@ -136,7 +136,6 @@ li:hover {
width: 100%;
color: black;
}
.alertItem .text.smaller { font-size: 9px; }
hr { color: silver; border: 1px solid silver; }

View File

@ -636,7 +636,7 @@ button.username {
li a { font-size: 14px; }
ul { height: 26px; }
.menu_left, .menu_right { padding-right: 7px; }
.menu_create_topic { display: none; }
.menu_create_topic, .hide_on_mobile { display: none; }
.menu_alerts {
padding-left: 4px;
@ -646,7 +646,6 @@ button.username {
}
.selectedAlert .alertList { top: 33px; }
.hide_on_mobile { display: none; }
.prev_button, .next_button { top: auto;bottom: 5px; }
.colstack_grid { grid-template-columns: none; grid-gap: 8px; }
.grid_istat { margin-bottom: 0px; }
@ -672,7 +671,6 @@ button.username {
font-weight: bold;
white-space: normal;
}
.alertItem .text.smaller { font-size: 9px; }
.post_container { overflow: visible !important; }
.post_item {