From f5190e83ba6d68816677a1aa0a014b561a1a843f Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 5 Nov 2017 03:58:19 +0000 Subject: [PATCH] More MySQL generator refactoring. --- gen_mysql.go | 16 ++-- query_gen/lib/mysql.go | 161 ++++------------------------------------- 2 files changed, 24 insertions(+), 153 deletions(-) diff --git a/gen_mysql.go b/gen_mysql.go index 5c2b51b2..b68dc088 100644 --- a/gen_mysql.go +++ b/gen_mysql.go @@ -333,49 +333,49 @@ func _gen_mysql() (err error) { } log.Print("Preparing getTopicRepliesOffset statement.") - getTopicRepliesOffsetStmt, 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 `replies`.`tid` = ? ORDER BY replies.rid ASC LIMIT ?,?") + getTopicRepliesOffsetStmt, 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 `replies`.`tid` = ? ORDER BY replies.rid ASC LIMIT ?,?") if err != nil { return err } log.Print("Preparing getTopicList statement.") - getTopicListStmt, 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") + getTopicListStmt, 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 getTopicUser statement.") - getTopicUserStmt, 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` = ?") + getTopicUserStmt, 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 { return err } log.Print("Preparing getTopicByReply statement.") - getTopicByReplyStmt, err = db.Prepare("SELECT `topics`.`tid`,`topics`.`title`,`topics`.`content`,`topics`.`createdBy`,`topics`.`createdAt`,`topics`.`is_closed`,`topics`.`sticky`,`topics`.`parentID`,`topics`.`ipaddress`,`topics`.`postCount`,`topics`.`likeCount`,`topics`.`data` FROM `replies` LEFT JOIN `topics` ON `replies`.`tid` = `topics`.`tid` WHERE `rid` = ?") + getTopicByReplyStmt, err = db.Prepare("SELECT `topics`.`tid`, `topics`.`title`, `topics`.`content`, `topics`.`createdBy`, `topics`.`createdAt`, `topics`.`is_closed`, `topics`.`sticky`, `topics`.`parentID`, `topics`.`ipaddress`, `topics`.`postCount`, `topics`.`likeCount`, `topics`.`data` FROM `replies` LEFT JOIN `topics` ON `replies`.`tid` = `topics`.`tid` WHERE `rid` = ?") if err != nil { return err } log.Print("Preparing getTopicReplies statement.") - getTopicRepliesStmt, 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` FROM `replies` LEFT JOIN `users` ON `replies`.`createdBy` = `users`.`uid` WHERE `tid` = ?") + getTopicRepliesStmt, 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` FROM `replies` LEFT JOIN `users` ON `replies`.`createdBy` = `users`.`uid` WHERE `tid` = ?") if err != nil { return err } log.Print("Preparing getForumTopics statement.") - getForumTopicsStmt, err = db.Prepare("SELECT `topics`.`tid`,`topics`.`title`,`topics`.`content`,`topics`.`createdBy`,`topics`.`is_closed`,`topics`.`sticky`,`topics`.`createdAt`,`topics`.`lastReplyAt`,`topics`.`parentID`,`users`.`name`,`users`.`avatar` FROM `topics` LEFT JOIN `users` ON `topics`.`createdBy` = `users`.`uid` WHERE `topics`.`parentID` = ? ORDER BY topics.sticky DESC,topics.lastReplyAt DESC,topics.createdBy DESC") + getForumTopicsStmt, err = db.Prepare("SELECT `topics`.`tid`, `topics`.`title`, `topics`.`content`, `topics`.`createdBy`, `topics`.`is_closed`, `topics`.`sticky`, `topics`.`createdAt`, `topics`.`lastReplyAt`, `topics`.`parentID`, `users`.`name`, `users`.`avatar` FROM `topics` LEFT JOIN `users` ON `topics`.`createdBy` = `users`.`uid` WHERE `topics`.`parentID` = ? ORDER BY topics.sticky DESC,topics.lastReplyAt DESC,topics.createdBy DESC") if err != nil { return err } log.Print("Preparing getProfileReplies statement.") - getProfileRepliesStmt, err = db.Prepare("SELECT `users_replies`.`rid`,`users_replies`.`content`,`users_replies`.`createdBy`,`users_replies`.`createdAt`,`users_replies`.`lastEdit`,`users_replies`.`lastEditBy`,`users`.`avatar`,`users`.`name`,`users`.`group` FROM `users_replies` LEFT JOIN `users` ON `users_replies`.`createdBy` = `users`.`uid` WHERE `users_replies`.`uid` = ?") + getProfileRepliesStmt, err = db.Prepare("SELECT `users_replies`.`rid`, `users_replies`.`content`, `users_replies`.`createdBy`, `users_replies`.`createdAt`, `users_replies`.`lastEdit`, `users_replies`.`lastEditBy`, `users`.`avatar`, `users`.`name`, `users`.`group` FROM `users_replies` LEFT JOIN `users` ON `users_replies`.`createdBy` = `users`.`uid` WHERE `users_replies`.`uid` = ?") if err != nil { return err } log.Print("Preparing getWatchers statement.") - getWatchersStmt, err = db.Prepare("SELECT `activity_subscriptions`.`user` FROM `activity_stream` INNER JOIN `activity_subscriptions` ON `activity_subscriptions`.`targetType` = `activity_stream`.`elementType` AND `activity_subscriptions`.`targetID` = `activity_stream`.`elementID` AND `activity_subscriptions`.`user` != `activity_stream`.`actor` WHERE `asid` = ?") + getWatchersStmt, err = db.Prepare("SELECT `activity_subscriptions`.`user` FROM `activity_stream` INNER JOIN `activity_subscriptions` ON `activity_subscriptions`.`targetType` = `activity_stream`.`elementType` AND `activity_subscriptions`.`targetID` = `activity_stream`.`elementID` AND `activity_subscriptions`.`user` != `activity_stream`.`actor` WHERE `asid` = ?") if err != nil { return err } diff --git a/query_gen/lib/mysql.go b/query_gen/lib/mysql.go index b5b5b0a8..908c73c9 100644 --- a/query_gen/lib/mysql.go +++ b/query_gen/lib/mysql.go @@ -389,34 +389,12 @@ func (adapter *MysqlAdapter) SimpleLeftJoin(name string, table1 string, table2 s return "", errors.New("No joiners found for SimpleLeftJoin") } - var querystr = "SELECT " - - for _, column := range processColumns(columns) { - var source, alias string - - // Escape the column names, just in case we've used a reserved keyword - 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] - whereStr, err := adapter.buildJoinWhere(where) if err != nil { - return querystr, err + return "", err } - querystr += " FROM `" + table1 + "` LEFT JOIN `" + table2 + "` ON " + adapter.buildJoiners(joiners) + whereStr + adapter.buildOrderby(orderby) + adapter.buildLimit(limit) + var querystr = "SELECT" + adapter.buildJoinColumns(columns) + " FROM `" + table1 + "` LEFT JOIN `" + table2 + "` ON " + adapter.buildJoiners(joiners) + whereStr + adapter.buildOrderby(orderby) + adapter.buildLimit(limit) querystr = strings.TrimSpace(querystr) adapter.pushStatement(name, "select", querystr) @@ -440,35 +418,12 @@ func (adapter *MysqlAdapter) SimpleInnerJoin(name string, table1 string, table2 return "", errors.New("No joiners found for SimpleInnerJoin") } - var querystr = "SELECT " - - for _, column := range processColumns(columns) { - var source, alias string - - // Escape the column names, just in case we've used a reserved keyword - 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] - whereStr, err := adapter.buildJoinWhere(where) if err != nil { - return querystr, err + return "", err } - querystr += " FROM `" + table1 + "` INNER JOIN `" + table2 + "` ON " + adapter.buildJoiners(joiners) + whereStr + adapter.buildOrderby(orderby) + adapter.buildLimit(limit) + var querystr = "SELECT " + adapter.buildJoinColumns(columns) + " FROM `" + table1 + "` INNER JOIN `" + table2 + "` ON " + adapter.buildJoiners(joiners) + whereStr + adapter.buildOrderby(orderby) + adapter.buildLimit(limit) querystr = strings.TrimSpace(querystr) adapter.pushStatement(name, "select", querystr) @@ -476,44 +431,12 @@ func (adapter *MysqlAdapter) SimpleInnerJoin(name string, table1 string, table2 } func (adapter *MysqlAdapter) SimpleInsertSelect(name string, ins DB_Insert, sel DB_Select) (string, error) { - /* Insert Portion */ - var querystr = "INSERT INTO `" + ins.Table + "`(" - - // Escape the column names, just in case we've used a reserved keyword - for _, column := range processColumns(ins.Columns) { - if column.Type == "function" { - querystr += column.Left + "," - } else { - querystr += "`" + column.Left + "`," - } - } - querystr = querystr[0:len(querystr)-1] + ") SELECT" - - /* Select Portion */ - - for _, column := range processColumns(sel.Columns) { - var source, alias string - - // Escape the column names, just in case we've used a reserved keyword - if column.Type == "function" || column.Type == "substitute" { - source = column.Left - } else { - source = "`" + column.Left + "`" - } - - if column.Alias != "" { - alias = " AS `" + column.Alias + "`" - } - querystr += " " + source + alias + "," - } - querystr = querystr[0 : len(querystr)-1] - whereStr, err := adapter.buildWhere(sel.Where) if err != nil { - return querystr, err + return "", err } - querystr += " FROM `" + sel.Table + "`" + whereStr + adapter.buildOrderby(sel.Orderby) + adapter.buildLimit(sel.Limit) + var querystr = "INSERT INTO `" + ins.Table + "`(" + adapter.buildColumns(ins.Columns) + ") SELECT" + adapter.buildJoinColumns(sel.Columns) + " FROM `" + sel.Table + "`" + whereStr + adapter.buildOrderby(sel.Orderby) + adapter.buildLimit(sel.Limit) querystr = strings.TrimSpace(querystr) adapter.pushStatement(name, "insert", querystr) @@ -521,46 +444,12 @@ func (adapter *MysqlAdapter) SimpleInsertSelect(name string, ins DB_Insert, sel } func (adapter *MysqlAdapter) SimpleInsertLeftJoin(name string, ins DB_Insert, sel DB_Join) (string, error) { - /* Insert Portion */ - var querystr = "INSERT INTO `" + ins.Table + "`(" - - // Escape the column names, just in case we've used a reserved keyword - for _, column := range processColumns(ins.Columns) { - if column.Type == "function" { - querystr += column.Left + "," - } else { - querystr += "`" + column.Left + "`," - } - } - querystr = querystr[0:len(querystr)-1] + ") SELECT" - - /* Select Portion */ - - for _, column := range processColumns(sel.Columns) { - var source, alias string - - // Escape the column names, just in case we've used a reserved keyword - 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 + "," - } - querystr = querystr[0 : len(querystr)-1] - whereStr, err := adapter.buildJoinWhere(sel.Where) if err != nil { - return querystr, err + return "", err } - querystr += " FROM `" + sel.Table1 + "` LEFT JOIN `" + sel.Table2 + "` ON " + adapter.buildJoiners(sel.Joiners) + whereStr + adapter.buildOrderby(sel.Orderby) + adapter.buildLimit(sel.Limit) + var querystr = "INSERT INTO `" + ins.Table + "`(" + adapter.buildColumns(ins.Columns) + ") SELECT" + adapter.buildJoinColumns(sel.Columns) + " FROM `" + sel.Table1 + "` LEFT JOIN `" + sel.Table2 + "` ON " + adapter.buildJoiners(sel.Joiners) + whereStr + adapter.buildOrderby(sel.Orderby) + adapter.buildLimit(sel.Limit) querystr = strings.TrimSpace(querystr) adapter.pushStatement(name, "insert", querystr) @@ -612,23 +501,8 @@ func (adapter *MysqlAdapter) buildLimit(limit string) (querystr string) { return querystr } -func (adapter *MysqlAdapter) SimpleInsertInnerJoin(name string, ins DB_Insert, sel DB_Join) (string, error) { - /* Insert Portion */ - var querystr = "INSERT INTO `" + ins.Table + "`(" - - // Escape the column names, just in case we've used a reserved keyword - for _, column := range processColumns(ins.Columns) { - if column.Type == "function" { - querystr += column.Left + "," - } else { - querystr += "`" + column.Left + "`," - } - } - querystr = querystr[0:len(querystr)-1] + ") SELECT" - - /* Select Portion */ - - for _, column := range processColumns(sel.Columns) { +func (adapter *MysqlAdapter) buildJoinColumns(columns string) (querystr string) { + for _, column := range processColumns(columns) { var source, alias string // Escape the column names, just in case we've used a reserved keyword @@ -645,14 +519,16 @@ func (adapter *MysqlAdapter) SimpleInsertInnerJoin(name string, ins DB_Insert, s } querystr += " " + source + alias + "," } - querystr = querystr[0 : len(querystr)-1] + return querystr[0 : len(querystr)-1] +} +func (adapter *MysqlAdapter) SimpleInsertInnerJoin(name string, ins DB_Insert, sel DB_Join) (string, error) { whereStr, err := adapter.buildJoinWhere(sel.Where) if err != nil { - return querystr, err + return "", err } - querystr += " FROM `" + sel.Table1 + "` INNER JOIN `" + sel.Table2 + "` ON " + adapter.buildJoiners(sel.Joiners) + whereStr + adapter.buildOrderby(sel.Orderby) + adapter.buildLimit(sel.Limit) + var querystr = "INSERT INTO `" + ins.Table + "`(" + adapter.buildColumns(ins.Columns) + ") SELECT" + adapter.buildJoinColumns(sel.Columns) + " FROM `" + sel.Table1 + "` INNER JOIN `" + sel.Table2 + "` ON " + adapter.buildJoiners(sel.Joiners) + whereStr + adapter.buildOrderby(sel.Orderby) + adapter.buildLimit(sel.Limit) querystr = strings.TrimSpace(querystr) adapter.pushStatement(name, "insert", querystr) @@ -667,17 +543,12 @@ func (adapter *MysqlAdapter) SimpleCount(name string, table string, where string return "", errors.New("You need a name for this table") } - querystr = "SELECT COUNT(*) AS `count` FROM `" + table + "`" whereStr, err := adapter.buildWhere(where) if err != nil { return "", err } - querystr += whereStr - - if limit != "" { - querystr += " LIMIT " + limit - } + querystr = "SELECT COUNT(*) AS `count` FROM `" + table + "`" + whereStr + adapter.buildLimit(limit) querystr = strings.TrimSpace(querystr) adapter.pushStatement(name, "select", querystr) return querystr, nil