2017-06-05 11:57:27 +00:00
/* WIP Under Construction */
2018-10-27 03:21:02 +00:00
package main // import "github.com/Azareal/Gosora/query_gen"
2017-06-05 11:57:27 +00:00
2017-10-16 07:32:58 +00:00
import (
2019-10-06 05:32:08 +00:00
"encoding/json"
2017-10-16 07:32:58 +00:00
"fmt"
"log"
"os"
"runtime/debug"
2018-04-23 08:38:25 +00:00
"strconv"
2017-06-05 11:57:27 +00:00
2019-10-06 05:32:08 +00:00
c "github.com/Azareal/Gosora/common"
2019-10-06 00:34:09 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2017-10-16 07:32:58 +00:00
)
// TODO: Make sure all the errors in this file propagate upwards properly
2017-06-05 11:57:27 +00:00
func main ( ) {
2017-10-16 07:32:58 +00:00
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func ( ) {
2019-10-06 00:34:09 +00:00
if r := recover ( ) ; r != nil {
2017-10-16 07:32:58 +00:00
fmt . Println ( r )
debug . PrintStack ( )
return
}
} ( )
2017-06-05 11:57:27 +00:00
log . Println ( "Running the query generator" )
2019-10-06 11:32:00 +00:00
for _ , a := range qgen . Registry {
log . Printf ( "Building the queries for the %s adapter" , a . GetName ( ) )
qgen . Install . SetAdapterInstance ( a )
2017-11-23 05:37:08 +00:00
qgen . Install . AddPlugins ( NewPrimaryKeySpitter ( ) ) // TODO: Do we really need to fill the spitter for every adapter?
2017-10-16 07:32:58 +00:00
2019-10-06 11:32:00 +00:00
err := writeStatements ( a )
2017-10-16 07:32:58 +00:00
if err != nil {
log . Print ( err )
}
err = qgen . Install . Write ( )
if err != nil {
log . Print ( err )
}
2019-10-06 11:32:00 +00:00
err = a . Write ( )
2017-10-16 07:32:58 +00:00
if err != nil {
log . Print ( err )
}
2017-06-05 11:57:27 +00:00
}
}
2017-09-10 16:57:22 +00:00
// nolint
2019-10-06 11:32:00 +00:00
func writeStatements ( a qgen . Adapter ) ( err error ) {
e := func ( f func ( qgen . Adapter ) error ) {
if err != nil {
return
}
err = f ( a )
2017-10-16 07:32:58 +00:00
}
2019-10-06 11:32:00 +00:00
e ( createTables )
e ( seedTables )
e ( writeSelects )
e ( writeLeftJoins )
e ( writeInnerJoins )
e ( writeInserts )
e ( writeUpdates )
e ( writeDeletes )
e ( writeSimpleCounts )
e ( writeInsertSelects )
e ( writeInsertLeftJoins )
e ( writeInsertInnerJoins )
return err
2017-07-12 11:05:18 +00:00
}
2019-10-06 00:34:09 +00:00
type si = map [ string ] interface { }
2019-10-06 11:32:00 +00:00
func seedTables ( a qgen . Adapter ) error {
2018-12-31 09:03:49 +00:00
qgen . Install . AddIndex ( "topics" , "parentID" , "parentID" )
qgen . Install . AddIndex ( "replies" , "tid" , "tid" )
qgen . Install . AddIndex ( "polls" , "parentID" , "parentID" )
qgen . Install . AddIndex ( "likes" , "targetItem" , "targetItem" )
qgen . Install . AddIndex ( "emails" , "uid" , "uid" )
qgen . Install . AddIndex ( "attachments" , "originID" , "originID" )
qgen . Install . AddIndex ( "attachments" , "path" , "path" )
qgen . Install . AddIndex ( "activity_stream_matches" , "watcher" , "watcher" )
2017-10-14 07:39:22 +00:00
qgen . Install . SimpleInsert ( "sync" , "last_update" , "UTC_TIMESTAMP()" )
qgen . Install . SimpleInsert ( "settings" , "name, content, type, constraints" , "'activation_type','1','list','1-3'" )
qgen . Install . SimpleInsert ( "settings" , "name, content, type" , "'bigpost_min_words','250','int'" )
qgen . Install . SimpleInsert ( "settings" , "name, content, type" , "'megapost_min_words','1000','int'" )
2017-12-25 06:12:19 +00:00
qgen . Install . SimpleInsert ( "settings" , "name, content, type" , "'meta_desc','','html-attribute'" )
2018-07-13 11:27:58 +00:00
qgen . Install . SimpleInsert ( "settings" , "name, content, type" , "'rapid_loading','1','bool'" )
2019-02-28 07:28:17 +00:00
qgen . Install . SimpleInsert ( "settings" , "name, content, type" , "'google_site_verify','','html-attribute'" )
2018-03-17 08:16:43 +00:00
qgen . Install . SimpleInsert ( "themes" , "uname, default" , "'cosora',1" )
2017-10-14 07:39:22 +00:00
qgen . Install . SimpleInsert ( "emails" , "email, uid, validated" , "'admin@localhost',1,1" ) // ? - Use a different default email or let the admin input it during installation?
/ *
The Permissions :
Global Permissions :
BanUsers
ActivateUsers
EditUser
EditUserEmail
EditUserPassword
EditUserGroup
EditUserGroupSuperMod
EditUserGroupAdmin
EditGroup
EditGroupLocalPerms
EditGroupGlobalPerms
EditGroupSuperMod
EditGroupAdmin
ManageForums
EditSettings
ManageThemes
ManagePlugins
ViewAdminLogs
ViewIPs
Non - staff Global Permissions :
UploadFiles
2019-06-09 03:21:48 +00:00
UploadAvatars
2019-10-06 00:34:09 +00:00
UseConvos
// CreateConvo ?
// CreateConvoReply ?
2017-10-14 07:39:22 +00:00
Forum Permissions :
ViewTopic
LikeItem
CreateTopic
EditTopic
DeleteTopic
CreateReply
EditReply
DeleteReply
PinTopic
CloseTopic
2018-01-15 08:24:18 +00:00
MoveTopic
2017-10-14 07:39:22 +00:00
* /
2019-10-06 05:32:08 +00:00
p := func ( perms c . Perms ) string {
jBytes , err := json . Marshal ( perms )
if err != nil {
panic ( err )
}
return string ( jBytes )
}
addGroup := func ( name string , perms c . Perms , mod bool , admin bool , banned bool , tag string ) {
mi , ai , bi := "0" , "0" , "0"
if mod {
mi = "1"
}
if admin {
ai = "1"
}
if banned {
bi = "1"
}
qgen . Install . SimpleInsert ( "users_groups" , "name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag" , ` ' ` + name + ` ',' ` + p ( perms ) + ` ',' { }', ` + mi + ` , ` + ai + ` , ` + bi + ` ," ` + tag + ` " ` )
}
2017-10-14 07:39:22 +00:00
2019-10-06 05:32:08 +00:00
perms := c . AllPerms
perms . EditUserGroupAdmin = false
perms . EditGroupAdmin = false
addGroup ( "Administrator" , perms , true , true , false , "Admin" )
2017-10-14 07:39:22 +00:00
2019-10-06 05:32:08 +00:00
perms = c . Perms { BanUsers : true , ActivateUsers : true , EditUser : true , EditUserEmail : false , EditUserGroup : true , ViewIPs : true , UploadFiles : true , UploadAvatars : true , UseConvos : true , ViewTopic : true , LikeItem : true , CreateTopic : true , EditTopic : true , DeleteTopic : true , CreateReply : true , EditReply : true , DeleteReply : true , PinTopic : true , CloseTopic : true , MoveTopic : true }
addGroup ( "Moderator" , perms , true , false , false , "Mod" )
2017-10-14 07:39:22 +00:00
2019-10-06 05:32:08 +00:00
perms = c . Perms { UploadFiles : true , UploadAvatars : true , UseConvos : true , ViewTopic : true , LikeItem : true , CreateTopic : true , CreateReply : true }
addGroup ( "Member" , perms , false , false , false , "" )
2017-10-14 07:39:22 +00:00
2019-10-06 05:32:08 +00:00
perms = c . Perms { ViewTopic : true }
addGroup ( "Banned" , perms , false , false , true , "" )
addGroup ( "Awaiting Activation" , perms , false , false , false , "" )
addGroup ( "Not Loggedin" , perms , false , false , false , "Guest" )
2017-10-14 07:39:22 +00:00
//
// TODO: Stop processFields() from stripping the spaces in the descriptions in the next commit
2019-05-28 00:07:26 +00:00
qgen . Install . SimpleInsert ( "forums" , "name, active, desc, tmpl" , "'Reports',0,'All the reports go here',''" )
2017-10-14 07:39:22 +00:00
2019-05-28 00:07:26 +00:00
qgen . Install . SimpleInsert ( "forums" , "name, lastTopicID, lastReplyerID, desc, tmpl" , "'General',1,1,'A place for general discussions which don't fit elsewhere',''" )
2017-10-14 07:39:22 +00:00
//
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 1,1,' { "ViewTopic":true,"CreateReply":true,"CreateTopic":true,"PinTopic":true,"CloseTopic":true}' ` )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 2,1,' { "ViewTopic":true,"CreateReply":true,"CloseTopic":true}' ` )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , "3,1,'{}'" )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , "4,1,'{}'" )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , "5,1,'{}'" )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , "6,1,'{}'" )
//
2018-01-15 08:24:18 +00:00
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 1,2,' { "ViewTopic":true,"CreateReply":true,"CreateTopic":true,"LikeItem":true,"EditTopic":true,"DeleteTopic":true,"EditReply":true,"DeleteReply":true,"PinTopic":true,"CloseTopic":true,"MoveTopic":true}' ` )
2017-10-14 07:39:22 +00:00
2018-01-15 08:24:18 +00:00
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 2,2,' { "ViewTopic":true,"CreateReply":true,"CreateTopic":true,"LikeItem":true,"EditTopic":true,"DeleteTopic":true,"EditReply":true,"DeleteReply":true,"PinTopic":true,"CloseTopic":true,"MoveTopic":true}' ` )
2017-10-14 07:39:22 +00:00
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 3,2,' { "ViewTopic":true,"CreateReply":true,"CreateTopic":true,"LikeItem":true}' ` )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 4,2,' { "ViewTopic":true}' ` )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 5,2,' { "ViewTopic":true}' ` )
qgen . Install . SimpleInsert ( "forums_permissions" , "gid, fid, permissions" , ` 6,2,' { "ViewTopic":true}' ` )
//
2017-11-06 16:24:45 +00:00
qgen . Install . SimpleInsert ( "topics" , "title, content, parsed_content, createdAt, lastReplyAt, lastReplyBy, createdBy, parentID, ipaddress" , "'Test Topic','A topic automatically generated by the software.','A topic automatically generated by the software.',UTC_TIMESTAMP(),UTC_TIMESTAMP(),1,1,2,'::1'" )
2017-10-14 07:39:22 +00:00
2017-11-06 16:24:45 +00:00
qgen . Install . SimpleInsert ( "replies" , "tid, content, parsed_content, createdAt, createdBy, lastUpdated, lastEdit, lastEditBy, ipaddress" , "1,'A reply!','A reply!',UTC_TIMESTAMP(),1,UTC_TIMESTAMP(),0,0,'::1'" )
2017-10-14 07:39:22 +00:00
2018-04-22 12:33:56 +00:00
qgen . Install . SimpleInsert ( "menus" , "" , "" )
2018-04-23 21:08:31 +00:00
// Go maps have a random iteration order, so we have to do this, otherwise the schema files will become unstable and harder to audit
2019-10-06 00:34:09 +00:00
order := 0
mOrder := "mid, name, htmlID, cssClass, position, path, aria, tooltip, guestOnly, memberOnly, staffOnly, adminOnly"
addMenuItem := func ( data map [ string ] interface { } ) {
2018-04-23 08:38:25 +00:00
cols , values := qgen . InterfaceMapToInsertStrings ( data , mOrder )
qgen . Install . SimpleInsert ( "menu_items" , cols + ", order" , values + "," + strconv . Itoa ( order ) )
order ++
}
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_forums}" , "htmlID" : "menu_forums" , "position" : "left" , "path" : "/forums/" , "aria" : "{lang.menu_forums_aria}" , "tooltip" : "{lang.menu_forums_tooltip}" } )
2018-04-23 08:38:25 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_topics}" , "htmlID" : "menu_topics" , "cssClass" : "menu_topics" , "position" : "left" , "path" : "/topics/" , "aria" : "{lang.menu_topics_aria}" , "tooltip" : "{lang.menu_topics_tooltip}" } )
2018-04-23 08:38:25 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "htmlID" : "general_alerts" , "cssClass" : "menu_alerts" , "position" : "right" , "tmplName" : "menu_alerts" } )
2018-04-23 08:38:25 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_account}" , "cssClass" : "menu_account" , "position" : "left" , "path" : "/user/edit/" , "aria" : "{lang.menu_account_aria}" , "tooltip" : "{lang.menu_account_tooltip}" , "memberOnly" : true } )
2018-04-22 12:33:56 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_profile}" , "cssClass" : "menu_profile" , "position" : "left" , "path" : "{me.Link}" , "aria" : "{lang.menu_profile_aria}" , "tooltip" : "{lang.menu_profile_tooltip}" , "memberOnly" : true } )
2018-04-22 12:33:56 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_panel}" , "cssClass" : "menu_panel menu_account" , "position" : "left" , "path" : "/panel/" , "aria" : "{lang.menu_panel_aria}" , "tooltip" : "{lang.menu_panel_tooltip}" , "memberOnly" : true , "staffOnly" : true } )
2018-04-23 08:38:25 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_logout}" , "cssClass" : "menu_logout" , "position" : "left" , "path" : "/accounts/logout/?s={me.Session}" , "aria" : "{lang.menu_logout_aria}" , "tooltip" : "{lang.menu_logout_tooltip}" , "memberOnly" : true } )
2018-04-23 08:38:25 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_register}" , "cssClass" : "menu_register" , "position" : "left" , "path" : "/accounts/create/" , "aria" : "{lang.menu_register_aria}" , "tooltip" : "{lang.menu_register_tooltip}" , "guestOnly" : true } )
2018-04-23 08:38:25 +00:00
2019-10-06 00:34:09 +00:00
addMenuItem ( si { "mid" : 1 , "name" : "{lang.menu_login}" , "cssClass" : "menu_login" , "position" : "left" , "path" : "/accounts/login/" , "aria" : "{lang.menu_login_aria}" , "tooltip" : "{lang.menu_login_tooltip}" , "guestOnly" : true } )
2018-04-22 12:33:56 +00:00
2017-07-12 11:05:18 +00:00
return nil
}
2018-12-14 04:08:53 +00:00
// ? - What is this for?
/ * func copyInsertMap ( in map [ string ] interface { } ) ( out map [ string ] interface { } ) {
2018-04-23 08:38:25 +00:00
out = make ( map [ string ] interface { } )
for col , value := range in {
out [ col ] = value
}
return out
2018-12-14 04:08:53 +00:00
} * /
2018-04-23 08:38:25 +00:00
type LitStr string
2019-10-06 00:34:09 +00:00
func writeSelects ( a qgen . Adapter ) error {
b := a . Builder ( )
2017-11-13 00:31:46 +00:00
2017-09-18 17:03:52 +00:00
// Looking for getTopic? Your statement is in another castle
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
//b.Select("isPluginInstalled").Table("plugins").Columns("installed").Where("uname = ?").Parse()
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
b . Select ( "forumEntryExists" ) . Table ( "forums" ) . Columns ( "fid" ) . Where ( "name = ''" ) . Orderby ( "fid ASC" ) . Limit ( "0,1" ) . Parse ( )
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
b . Select ( "groupEntryExists" ) . Table ( "users_groups" ) . Columns ( "gid" ) . Where ( "name = ''" ) . Orderby ( "gid ASC" ) . Limit ( "0,1" ) . Parse ( )
2017-09-10 16:57:22 +00:00
2017-06-10 07:58:15 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeLeftJoins ( a qgen . Adapter ) error {
a . SimpleLeftJoin ( "getForumTopics" , "topics" , "users" , "topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.lastReplyAt, topics.parentID, users.name, users.avatar" , "topics.createdBy = users.uid" , "topics.parentID = ?" , "topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy desc" , "" )
2017-09-10 16:57:22 +00:00
2017-06-12 09:03:14 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeInnerJoins ( a qgen . Adapter ) ( err error ) {
2017-06-10 07:58:15 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeInserts ( a qgen . Adapter ) error {
b := a . Builder ( )
2017-11-13 00:31:46 +00:00
2019-10-06 00:34:09 +00:00
b . Insert ( "addForumPermsToForum" ) . Table ( "forums_permissions" ) . Columns ( "gid,fid,preset,permissions" ) . Fields ( "?,?,?,?" ) . Parse ( )
2017-09-10 16:57:22 +00:00
2017-06-10 07:58:15 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeUpdates ( a qgen . Adapter ) error {
b := a . Builder ( )
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
b . Update ( "updateEmail" ) . Table ( "emails" ) . Set ( "email = ?, uid = ?, validated = ?, token = ?" ) . Where ( "email = ?" ) . Parse ( )
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
b . Update ( "setTempGroup" ) . Table ( "users" ) . Set ( "temp_group = ?" ) . Where ( "uid = ?" ) . Parse ( )
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
b . Update ( "bumpSync" ) . Table ( "sync" ) . Set ( "last_update = UTC_TIMESTAMP()" ) . Parse ( )
2017-09-10 16:57:22 +00:00
2017-06-05 11:57:27 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeDeletes ( a qgen . Adapter ) error {
b := a . Builder ( )
2017-11-13 00:31:46 +00:00
2019-10-06 00:34:09 +00:00
//b.Delete("deleteForumPermsByForum").Table("forums_permissions").Where("fid = ?").Parse()
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
b . Delete ( "deleteActivityStreamMatch" ) . Table ( "activity_stream_matches" ) . Where ( "watcher = ? AND asid = ?" ) . Parse ( )
//b.Delete("deleteActivityStreamMatchesByWatcher").Table("activity_stream_matches").Where("watcher = ?").Parse()
2017-09-10 16:57:22 +00:00
2017-06-12 09:03:14 +00:00
return nil
2017-06-14 07:09:44 +00:00
}
2019-10-06 00:34:09 +00:00
func writeSimpleCounts ( a qgen . Adapter ) error {
2017-06-19 08:06:54 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeInsertSelects ( a qgen . Adapter ) error {
/ * a . SimpleInsertSelect ( "addForumPermsToForumAdmins" ,
2017-10-14 07:39:22 +00:00
qgen . DB_Insert { "forums_permissions" , "gid, fid, preset, permissions" , "" } ,
2017-09-10 16:57:22 +00:00
qgen . DB_Select { "users_groups" , "gid, ? AS fid, ? AS preset, ? AS permissions" , "is_admin = 1" , "" , "" } ,
2017-10-21 00:27:47 +00:00
) * /
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
/ * a . SimpleInsertSelect ( "addForumPermsToForumStaff" ,
2017-10-14 07:39:22 +00:00
qgen . DB_Insert { "forums_permissions" , "gid, fid, preset, permissions" , "" } ,
2017-09-10 16:57:22 +00:00
qgen . DB_Select { "users_groups" , "gid, ? AS fid, ? AS preset, ? AS permissions" , "is_admin = 0 AND is_mod = 1" , "" , "" } ,
2017-10-21 00:27:47 +00:00
) * /
2017-09-10 16:57:22 +00:00
2019-10-06 00:34:09 +00:00
/ * a . SimpleInsertSelect ( "addForumPermsToForumMembers" ,
2017-10-14 07:39:22 +00:00
qgen . DB_Insert { "forums_permissions" , "gid, fid, preset, permissions" , "" } ,
2017-09-10 16:57:22 +00:00
qgen . DB_Select { "users_groups" , "gid, ? AS fid, ? AS preset, ? AS permissions" , "is_admin = 0 AND is_mod = 0 AND is_banned = 0" , "" , "" } ,
2017-10-21 00:27:47 +00:00
) * /
2017-09-10 16:57:22 +00:00
2017-06-19 08:06:54 +00:00
return nil
}
2017-09-10 16:57:22 +00:00
// nolint
2019-10-06 00:34:09 +00:00
func writeInsertLeftJoins ( a qgen . Adapter ) error {
2017-06-19 08:06:54 +00:00
return nil
}
2019-10-06 00:34:09 +00:00
func writeInsertInnerJoins ( a qgen . Adapter ) error {
2017-06-14 07:09:44 +00:00
return nil
}
2017-10-16 07:32:58 +00:00
func writeFile ( 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
}
err = f . Sync ( )
if err != nil {
return err
}
return f . Close ( )
}