Refactored the query generator subfunctions.

Refactored more prebuilder queries to use the OO methods.
Refactored the router generator.
Added more query generator OO methods.

More work on Cosora.
This commit is contained in:
Azareal 2017-11-12 11:29:23 +00:00
parent f1c418bd13
commit 8ec0534299
10 changed files with 163 additions and 119 deletions

View File

@ -3,6 +3,7 @@ package qgen
import (
"database/sql"
"log"
)
type Accumulator struct {
@ -42,6 +43,7 @@ func (build *Accumulator) recordError(err error) {
}
func (build *Accumulator) prepare(res string, err error) *sql.Stmt {
log.Print("res: ", res)
if err != nil {
build.recordError(err)
return nil

View File

@ -1076,6 +1076,16 @@ func (adapter *MssqlAdapter) Insert(nlist ...string) *insertPrebuilder {
return &insertPrebuilder{name, "", "", "", adapter}
}
func (adapter *MssqlAdapter) Update(nlist ...string) *updatePrebuilder {
name := optString(nlist, "_builder")
return &updatePrebuilder{name, "", "", "", adapter}
}
func (adapter *MssqlAdapter) Delete(nlist ...string) *deletePrebuilder {
name := optString(nlist, "_builder")
return &deletePrebuilder{name, "", "", adapter}
}
func (adapter *MssqlAdapter) Write() error {
var stmts, body string
for _, name := range adapter.BufferOrder {

View File

@ -562,6 +562,16 @@ func (adapter *MysqlAdapter) Insert(nlist ...string) *insertPrebuilder {
return &insertPrebuilder{name, "", "", "", adapter}
}
func (adapter *MysqlAdapter) Update(nlist ...string) *updatePrebuilder {
name := optString(nlist, "_builder")
return &updatePrebuilder{name, "", "", "", adapter}
}
func (adapter *MysqlAdapter) Delete(nlist ...string) *deletePrebuilder {
name := optString(nlist, "_builder")
return &deletePrebuilder{name, "", "", adapter}
}
func (adapter *MysqlAdapter) Write() error {
var stmts, body string
for _, name := range adapter.BufferOrder {

View File

@ -328,6 +328,16 @@ func (adapter *PgsqlAdapter) Insert(nlist ...string) *insertPrebuilder {
return &insertPrebuilder{name, "", "", "", adapter}
}
func (adapter *PgsqlAdapter) Update(nlist ...string) *updatePrebuilder {
name := optString(nlist, "_builder")
return &updatePrebuilder{name, "", "", "", adapter}
}
func (adapter *PgsqlAdapter) Delete(nlist ...string) *deletePrebuilder {
name := optString(nlist, "_builder")
return &deletePrebuilder{name, "", "", adapter}
}
func (adapter *PgsqlAdapter) Write() error {
var stmts, body string
for _, name := range adapter.BufferOrder {

View File

@ -118,6 +118,9 @@ type Adapter interface {
Select(name ...string) *selectPrebuilder
Insert(name ...string) *insertPrebuilder
Update(name ...string) *updatePrebuilder
Delete(name ...string) *deletePrebuilder
Write() error
}

View File

@ -93,6 +93,46 @@ func processJoiner(joinstr string) (joiner []DBJoiner) {
return joiner
}
func parseNumber(tmpWhere *DBWhere, segment string, i int) int {
var buffer string
for ; i < len(segment); i++ {
char := segment[i]
if '0' <= char && char <= '9' {
buffer += string(char)
} else {
i--
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "number"})
return i
}
}
return i
}
func parseColumn(tmpWhere *DBWhere, segment string, i int) int {
var buffer string
for ; i < len(segment); i++ {
char := segment[i]
if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '.' || char == '_' {
buffer += string(char)
} else if char == '(' {
return parseFunction(tmpWhere, segment, buffer, i)
} else {
i--
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "column"})
return i
}
}
return i
}
func parseFunction(tmpWhere *DBWhere, segment string, buffer string, i int) int {
var preI = i
i = skipFunctionCall(segment, i-1)
buffer += segment[preI:i] + string(segment[i])
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "function"})
return i
}
func processWhere(wherestr string) (where []DBWhere) {
if wherestr == "" {
return where
@ -102,20 +142,16 @@ func processWhere(wherestr string) (where []DBWhere) {
var buffer string
var optype int // 0: None, 1: Number, 2: Column, 3: Function, 4: String, 5: Operator
for _, segment := range strings.Split(wherestr, " AND ") {
var tmpWhere DBWhere
var tmpWhere = &DBWhere{[]DBToken{}}
segment += ")"
for i := 0; i < len(segment); i++ {
char := segment[i]
//fmt.Println("optype", optype)
switch optype {
case 0: // unknown
//fmt.Println("case 0:", char, string(char))
if '0' <= char && char <= '9' {
optype = 1
buffer = string(char)
i = parseNumber(tmpWhere, segment, i)
} else if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '_' {
optype = 2
buffer = string(char)
i = parseColumn(tmpWhere, segment, i)
} else if char == '\'' {
optype = 4
buffer = ""
@ -123,51 +159,13 @@ func processWhere(wherestr string) (where []DBWhere) {
optype = 5
buffer = string(char)
} else if char == '?' {
//fmt.Println("Expr:","?")
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{"?", "substitute"})
}
case 1: // number
if '0' <= char && char <= '9' {
buffer += string(char)
} else {
optype = 0
i--
//fmt.Println("Expr:",buffer)
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "number"})
}
case 2: // column
if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '.' || char == '_' {
buffer += string(char)
} else if char == '(' {
optype = 3
i--
} else {
optype = 0
i--
//fmt.Println("Expr:", buffer)
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "column"})
}
case 3: // function
var preI = i
//fmt.Println("buffer", buffer)
//fmt.Println("len(halves)", len(halves[1]))
//fmt.Println("preI", string(halves[1][preI]))
//fmt.Println("msg prior to preI", halves[1][0:preI])
i = skipFunctionCall(segment, i-1)
//fmt.Println("i",i)
//fmt.Println("msg prior to i-1", halves[1][0:i-1])
//fmt.Println("string(i-1)", string(halves[1][i-1]))
//fmt.Println("string(i)", string(halves[1][i]))
buffer += segment[preI:i] + string(segment[i])
//fmt.Println("Expr:",buffer)
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "function"})
optype = 0
case 4: // string
if char != '\'' {
buffer += string(char)
} else {
optype = 0
//fmt.Println("Expr:",buffer)
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "string"})
}
case 5: // operator
@ -176,14 +174,13 @@ func processWhere(wherestr string) (where []DBWhere) {
} else {
optype = 0
i--
//fmt.Println("Expr:",buffer)
tmpWhere.Expr = append(tmpWhere.Expr, DBToken{buffer, "operator"})
}
default:
panic("Bad optype in processWhere")
}
}
where = append(where, tmpWhere)
where = append(where, *tmpWhere)
}
return where
}
@ -222,13 +219,14 @@ func processSet(setstr string) (setter []DBSetter) {
continue
}
tmpSetter.Column = strings.TrimSpace(halves[0])
segment := halves[1] + ")"
halves[1] += ")"
var optype int // 0: None, 1: Number, 2: Column, 3: Function, 4: String, 5: Operator
//fmt.Println("halves[1]",halves[1])
for i := 0; i < len(halves[1]); i++ {
char := halves[1][i]
//fmt.Println("segment", segment)
for i := 0; i < len(segment); i++ {
char := segment[i]
//fmt.Println("optype",optype)
//fmt.Println("Expr:",buffer)
switch optype {
case 0: // unknown
if '0' <= char && char <= '9' {
@ -244,7 +242,6 @@ func processSet(setstr string) (setter []DBSetter) {
optype = 5
buffer = string(char)
} else if char == '?' {
//fmt.Println("Expr:","?")
tmpSetter.Expr = append(tmpSetter.Expr, DBToken{"?", "substitute"})
}
case 1: // number
@ -253,7 +250,6 @@ func processSet(setstr string) (setter []DBSetter) {
} else {
optype = 0
i--
//fmt.Println("Expr:",buffer)
tmpSetter.Expr = append(tmpSetter.Expr, DBToken{buffer, "number"})
}
case 2: // column
@ -265,22 +261,12 @@ func processSet(setstr string) (setter []DBSetter) {
} else {
optype = 0
i--
//fmt.Println("Expr:",buffer)
tmpSetter.Expr = append(tmpSetter.Expr, DBToken{buffer, "column"})
}
case 3: // function
var preI = i
//fmt.Println("buffer",buffer)
//fmt.Println("len(halves)",len(halves[1]))
//fmt.Println("preI",string(halves[1][preI]))
//fmt.Println("msg prior to preI",halves[1][0:preI])
i = skipFunctionCall(halves[1], i-1)
//fmt.Println("i",i)
//fmt.Println("msg prior to i-1",halves[1][0:i-1])
//fmt.Println("string(i-1)",string(halves[1][i-1]))
//fmt.Println("string(i)",string(halves[1][i]))
buffer += halves[1][preI:i] + string(halves[1][i])
//fmt.Println("Expr:",buffer)
i = skipFunctionCall(segment, i-1)
buffer += segment[preI:i] + string(segment[i])
tmpSetter.Expr = append(tmpSetter.Expr, DBToken{buffer, "function"})
optype = 0
case 4: // string
@ -288,7 +274,6 @@ func processSet(setstr string) (setter []DBSetter) {
buffer += string(char)
} else {
optype = 0
//fmt.Println("Expr:", buffer)
tmpSetter.Expr = append(tmpSetter.Expr, DBToken{buffer, "string"})
}
case 5: // operator
@ -297,7 +282,6 @@ func processSet(setstr string) (setter []DBSetter) {
} else {
optype = 0
i--
//fmt.Println("Expr:", buffer)
tmpSetter.Expr = append(tmpSetter.Expr, DBToken{buffer, "operator"})
}
default:

View File

@ -297,19 +297,19 @@ func writeInserts(adapter qgen.Adapter) error {
adapter.Insert("notifyOne").Table("activity_stream_matches").Columns("watcher, asid").Fields("?,?").Parse()
adapter.SimpleInsert("addEmail", "emails", "email, uid, validated, token", "?,?,?,?")
adapter.Insert("addEmail").Table("emails").Columns("email, uid, validated, token").Fields("?,?,?,?").Parse()
adapter.SimpleInsert("addSubscription", "activity_subscriptions", "user, targetID, targetType, level", "?,?,?,2")
adapter.Insert("addSubscription").Table("activity_subscriptions").Columns("user, targetID, targetType, level").Fields("?,?,?,2").Parse()
adapter.SimpleInsert("addForumPermsToForum", "forums_permissions", "gid,fid,preset,permissions", "?,?,?,?")
adapter.Insert("addForumPermsToForum").Table("forums_permissions").Columns("gid,fid,preset,permissions").Fields("?,?,?,?").Parse()
adapter.SimpleInsert("addPlugin", "plugins", "uname, active, installed", "?,?,?")
adapter.Insert("addPlugin").Table("plugins").Columns("uname, active, installed").Fields("?,?,?").Parse()
adapter.SimpleInsert("addTheme", "themes", "uname, default", "?,?")
adapter.Insert("addTheme").Table("themes").Columns("uname, default").Fields("?,?").Parse()
adapter.SimpleInsert("addAttachment", "attachments", "sectionID, sectionTable, originID, originTable, uploadedBy, path", "?,?,?,?,?,?")
adapter.Insert("addAttachment").Table("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path").Fields("?,?,?,?,?,?").Parse()
adapter.SimpleInsert("createWordFilter", "word_filters", "find, replacement", "?,?")
adapter.Insert("createWordFilter").Table("word_filters").Columns("find, replacement").Fields("?,?").Parse()
return nil
}
@ -334,48 +334,48 @@ func writeReplaces(adapter qgen.Adapter) (err error) {
}*/
func writeUpdates(adapter qgen.Adapter) error {
adapter.SimpleUpdate("editReply", "replies", "content = ?, parsed_content = ?", "rid = ?")
adapter.Update("editReply").Table("replies").Set("content = ?, parsed_content = ?").Where("rid = ?").Parse()
adapter.SimpleUpdate("editProfileReply", "users_replies", "content = ?, parsed_content = ?", "rid = ?")
adapter.Update("editProfileReply").Table("users_replies").Set("content = ?, parsed_content = ?").Where("rid = ?").Parse()
adapter.SimpleUpdate("updateSetting", "settings", "content = ?", "name = ?")
adapter.Update("updateSetting").Table("settings").Set("content = ?").Where("name = ?").Parse()
adapter.SimpleUpdate("updatePlugin", "plugins", "active = ?", "uname = ?")
adapter.Update("updatePlugin").Table("plugins").Set("active = ?").Where("uname = ?").Parse()
adapter.SimpleUpdate("updatePluginInstall", "plugins", "installed = ?", "uname = ?")
adapter.Update("updatePluginInstall").Table("plugins").Set("installed = ?").Where("uname = ?").Parse()
adapter.SimpleUpdate("updateTheme", "themes", "default = ?", "uname = ?")
adapter.Update("updateTheme").Table("themes").Set("default = ?").Where("uname = ?").Parse()
adapter.SimpleUpdate("updateForum", "forums", "name = ?, desc = ?, active = ?, preset = ?", "fid = ?")
adapter.Update("updateForum").Table("forums").Set("name = ?, desc = ?, active = ?, preset = ?").Where("fid = ?").Parse()
adapter.SimpleUpdate("updateUser", "users", "name = ?, email = ?, group = ?", "uid = ?")
adapter.Update("updateUser").Table("users").Set("name = ?, email = ?, group = ?").Where("uid = ?").Parse()
adapter.SimpleUpdate("updateGroupPerms", "users_groups", "permissions = ?", "gid = ?")
adapter.Update("updateGroupPerms").Table("users_groups").Set("permissions = ?").Where("gid = ?").Parse()
adapter.SimpleUpdate("updateGroup", "users_groups", "name = ?, tag = ?", "gid = ?")
adapter.Update("updateGroup").Table("users_groups").Set("name = ?, tag = ?").Where("gid = ?").Parse()
adapter.SimpleUpdate("updateEmail", "emails", "email = ?, uid = ?, validated = ?, token = ?", "email = ?")
adapter.Update("updateEmail").Table("emails").Set("email = ?, uid = ?, validated = ?, token = ?").Where("email = ?").Parse()
adapter.SimpleUpdate("verifyEmail", "emails", "validated = 1, token = ''", "email = ?") // Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed?
adapter.Update("verifyEmail").Table("emails").Set("validated = 1, token = ''").Where("email = ?").Parse() // Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed?
adapter.SimpleUpdate("setTempGroup", "users", "temp_group = ?", "uid = ?")
adapter.Update("setTempGroup").Table("users").Set("temp_group = ?").Where("uid = ?").Parse()
adapter.SimpleUpdate("updateWordFilter", "word_filters", "find = ?, replacement = ?", "wfid = ?")
adapter.Update("updateWordFilter").Table("word_filters").Set("find = ?, replacement = ?").Where("wfid = ?").Parse()
adapter.SimpleUpdate("bumpSync", "sync", "last_update = UTC_TIMESTAMP()", "")
adapter.Update("bumpSync").Table("sync").Set("last_update = UTC_TIMESTAMP()").Parse()
return nil
}
func writeDeletes(adapter qgen.Adapter) error {
adapter.SimpleDelete("deleteProfileReply", "users_replies", "rid = ?")
adapter.Delete("deleteProfileReply").Table("users_replies").Where("rid = ?").Parse()
//adapter.SimpleDelete("deleteForumPermsByForum", "forums_permissions", "fid = ?")
//adapter.Delete("deleteForumPermsByForum").Table("forums_permissions").Where("fid = ?").Parse()
adapter.SimpleDelete("deleteActivityStreamMatch", "activity_stream_matches", "watcher = ? AND asid = ?")
//adapter.SimpleDelete("deleteActivityStreamMatchesByWatcher","activity_stream_matches","watcher = ?")
adapter.Delete("deleteActivityStreamMatch").Table("activity_stream_matches").Where("watcher = ? AND asid = ?").Parse()
//adapter.Delete("deleteActivityStreamMatchesByWatcher").Table("activity_stream_matches").Where("watcher = ?").Parse()
adapter.SimpleDelete("deleteWordFilter", "word_filters", "wfid = ?")
adapter.Delete("deleteWordFilter").Table("word_filters").Where("wfid = ?").Parse()
return nil
}

View File

@ -4,7 +4,6 @@ type RouteImpl struct {
Name string
Path string
Vars []string
MemberAction bool
RunBefore []Runnable
Parent *RouteGroup
@ -35,12 +34,19 @@ func (route *RouteImpl) LitBefore(items ...string) *RouteImpl {
func (route *RouteImpl) hasBefore(items ...string) bool {
for _, item := range items {
if route.hasBeforeItem(item) {
return true
}
}
return false
}
func (route *RouteImpl) hasBeforeItem(item string) bool {
for _, before := range route.RunBefore {
if before.Contents == item {
return true
}
}
}
return false
}
@ -49,15 +55,19 @@ func addRouteGroup(routeGroup *RouteGroup) {
}
func blankRoute() *RouteImpl {
return &RouteImpl{"", "", []string{}, false, []Runnable{}, nil}
return &RouteImpl{"", "", []string{}, []Runnable{}, nil}
}
func route(fname string, path string, args ...string) *RouteImpl {
return &RouteImpl{fname, path, args, []Runnable{}, nil}
}
func View(fname string, path string, args ...string) *RouteImpl {
return &RouteImpl{fname, path, args, false, []Runnable{}, nil}
return route(fname, path, args...)
}
func MemberView(fname string, path string, args ...string) *RouteImpl {
route := &RouteImpl{fname, path, args, false, []Runnable{}, nil}
route := route(fname, path, args...)
if !route.hasBefore("SuperModOnly", "AdminOnly") {
route.Before("MemberOnly")
}
@ -65,7 +75,7 @@ func MemberView(fname string, path string, args ...string) *RouteImpl {
}
func ModView(fname string, path string, args ...string) *RouteImpl {
route := &RouteImpl{fname, path, args, false, []Runnable{}, nil}
route := route(fname, path, args...)
if !route.hasBefore("AdminOnly") {
route.Before("SuperModOnly")
}
@ -73,7 +83,7 @@ func ModView(fname string, path string, args ...string) *RouteImpl {
}
func Action(fname string, path string, args ...string) *RouteImpl {
route := &RouteImpl{fname, path, args, true, []Runnable{}, nil}
route := route(fname, path, args...)
route.Before("NoSessionMismatch")
if !route.hasBefore("SuperModOnly", "AdminOnly") {
route.Before("MemberOnly")
@ -82,13 +92,11 @@ func Action(fname string, path string, args ...string) *RouteImpl {
}
func AnonAction(fname string, path string, args ...string) *RouteImpl {
route := &RouteImpl{fname, path, args, false, []Runnable{}, nil}
route.Before("ParseForm")
return route
return route(fname, path, args...).Before("ParseForm")
}
func UploadAction(fname string, path string, args ...string) *RouteImpl {
route := &RouteImpl{fname, path, args, true, []Runnable{}, nil}
route := route(fname, path, args...)
if !route.hasBefore("SuperModOnly", "AdminOnly") {
route.Before("MemberOnly")
}

View File

@ -505,6 +505,7 @@ var profile_0 = []byte(`
<div class="rowitem"><h1>Profile</h1></div>
</header>-->
<div id="profile_left_pane" class="rowmenu">
<div class="topBlock">
<div class="rowitem avatarRow">
<img src="`)
var profile_1 = []byte(`" class="avatar" />
@ -516,6 +517,7 @@ var profile_3 = []byte(`<span class="username">`)
var profile_4 = []byte(`</span>`)
var profile_5 = []byte(`
</div>
</div>
<div class="passiveBlock">
<div class="rowitem passive">
<a class="profile_menu_item">Add Friend</a>

View File

@ -45,10 +45,14 @@ a {
padding-top: 14px;
display: flex;
/*background-color: hsl(0,0%,97%);*/
padding-left: 0px;
padding-right: 0px;
}
#main {
width: 100%;
padding-left: 8px;
padding-right: 8px;
}
.sidebar {
width: 200px;
@ -750,8 +754,19 @@ select, input, textarea {
}
.footer {
border: 1px solid var(--element-border-color);
border-bottom: 2px solid var(--element-border-color);
border-top: 1px solid var(--element-border-color);
padding: 12px;
padding-top: 10px;
padding-bottom: 10px;
margin-top: 14px;
margin-left: -8px;
margin-right: -8px;
}
.footer #poweredBy {
font-size: 17px;
}
.footer #poweredBy span {
font-size: 16px;
}
@media(max-width: 670px) {