From 08864edd6cdbbe4752814c9e42f5c3708b547dd8 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 1 Jan 2020 14:15:11 +1000 Subject: [PATCH] Optimise DBWhere methods. Optimise DBSetter methods. --- query_gen/mysql.go | 18 +++--- query_gen/utils.go | 141 +++++++++++++++++++++++++++++++-------------- 2 files changed, 106 insertions(+), 53 deletions(-) diff --git a/query_gen/mysql.go b/query_gen/mysql.go index 9d2ca9f7..ffde1929 100644 --- a/query_gen/mysql.go +++ b/query_gen/mysql.go @@ -82,7 +82,7 @@ func (a *MysqlAdapter) DbVersion() string { return "SELECT VERSION()" } -func (a *MysqlAdapter) DropTable(name string, table string) (string, error) { +func (a *MysqlAdapter) DropTable(name, table string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -206,7 +206,7 @@ func (a *MysqlAdapter) AddColumn(name string, table string, column DBTableColumn } // TODO: Test to make sure everything works here -func (a *MysqlAdapter) AddIndex(name string, table string, iname string, colname string) (string, error) { +func (a *MysqlAdapter) AddIndex(name, table, iname, colname string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -268,7 +268,7 @@ func (a *MysqlAdapter) AddForeignKey(name string, table string, column string, f var silen1 = len("INSERT INTO ``() VALUES () ") -func (a *MysqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) { +func (a *MysqlAdapter) SimpleInsert(name, table, columns, fields string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -323,7 +323,7 @@ func (a *MysqlAdapter) buildColumns(columns string) (q string) { } // ! DEPRECATED -func (a *MysqlAdapter) SimpleReplace(name string, table string, columns string, fields string) (string, error) { +func (a *MysqlAdapter) SimpleReplace(name, table, columns, fields string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -345,7 +345,7 @@ func (a *MysqlAdapter) SimpleReplace(name string, table string, columns string, return q + ")", nil } -func (a *MysqlAdapter) SimpleUpsert(name string, table string, columns string, fields string, where string) (string, error) { +func (a *MysqlAdapter) SimpleUpsert(name, table, columns, fields, where string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -444,7 +444,7 @@ func (a *MysqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) { return q, nil } -func (a *MysqlAdapter) SimpleDelete(name string, table string, where string) (string, error) { +func (a *MysqlAdapter) SimpleDelete(name, table, where string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -497,7 +497,7 @@ func (a *MysqlAdapter) ComplexDelete(b *deletePrebuilder) (string, error) { } // We don't want to accidentally wipe tables, so we'll have a separate method for purging tables instead -func (a *MysqlAdapter) Purge(name string, table string) (string, error) { +func (a *MysqlAdapter) Purge(name, table string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -576,7 +576,7 @@ func (a *MysqlAdapter) buildOrderby(orderby string) (q string) { return q } -func (a *MysqlAdapter) SimpleSelect(name string, table string, columns string, where string, orderby string, limit string) (string, error) { +func (a *MysqlAdapter) SimpleSelect(name, table, columns, where, orderby, limit string) (string, error) { if table == "" { return "", errors.New("You need a name for this table") } @@ -872,7 +872,7 @@ func (a *MysqlAdapter) SimpleInsertInnerJoin(name string, ins DBInsert, sel DBJo return q, nil } -func (a *MysqlAdapter) SimpleCount(name string, table string, where string, limit string) (q string, err error) { +func (a *MysqlAdapter) SimpleCount(name, table, where, limit string) (q string, err error) { if table == "" { return "", errors.New("You need a name for this table") } diff --git a/query_gen/utils.go b/query_gen/utils.go index 75569c2a..d47e16cf 100644 --- a/query_gen/utils.go +++ b/query_gen/utils.go @@ -93,15 +93,22 @@ func processJoiner(joinStr string) (joiner []DBJoiner) { return joiner } -func (wh *DBWhere) parseNumber(segment string, i int) int { - var buffer string - for ; i < len(segment); i++ { - ch := segment[i] +func (wh *DBWhere) parseNumber(seg string, i int) int { + //var buffer string + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] if '0' <= ch && ch <= '9' { - buffer += string(ch) + //buffer += string(ch) + l++ } else { i-- - wh.Expr = append(wh.Expr, DBToken{buffer, TokenNumber}) + var str string + if l != 0 { + str = seg[si : si+l] + } + wh.Expr = append(wh.Expr, DBToken{str, TokenNumber}) return i } } @@ -145,30 +152,44 @@ func (wh *DBWhere) parseFunction(seg string, buffer string, i int) int { return i } -func (wh *DBWhere) parseString(segment string, i int) int { - var buffer string +func (wh *DBWhere) parseString(seg string, i int) int { + //var buffer string i++ - for ; i < len(segment); i++ { - ch := segment[i] + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] if ch != '\'' { - buffer += string(ch) + //buffer += string(ch) + l++ } else { - wh.Expr = append(wh.Expr, DBToken{buffer, TokenString}) + var str string + if l != 0 { + str = seg[si : si+l] + } + wh.Expr = append(wh.Expr, DBToken{str, TokenString}) return i } } return i } -func (wh *DBWhere) parseOperator(segment string, i int) int { - var buffer string - for ; i < len(segment); i++ { - ch := segment[i] +func (wh *DBWhere) parseOperator(seg string, i int) int { + //var buffer string + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] if isOpByte(ch) { - buffer += string(ch) + //buffer += string(ch) + l++ } else { i-- - wh.Expr = append(wh.Expr, DBToken{buffer, TokenOp}) + var str string + if l != 0 { + str = seg[si : si+l] + } + wh.Expr = append(wh.Expr, DBToken{str, TokenOp}) return i } } @@ -228,32 +249,50 @@ func processWhere(whereStr string) (where []DBWhere) { return where } -func (set *DBSetter) parseNumber(segment string, i int) int { - var buffer string - for ; i < len(segment); i++ { - ch := segment[i] +func (set *DBSetter) parseNumber(seg string, i int) int { + //var buffer string + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] if '0' <= ch && ch <= '9' { - buffer += string(ch) + //buffer += string(ch) + l++ } else { - set.Expr = append(set.Expr, DBToken{buffer, TokenNumber}) + var str string + if l != 0 { + str = seg[si : si+l] + } + set.Expr = append(set.Expr, DBToken{str, TokenNumber}) return i } } return i } -func (set *DBSetter) parseColumn(segment string, i int) int { - var buffer string - for ; i < len(segment); i++ { - ch := segment[i] +func (set *DBSetter) parseColumn(seg string, i int) int { + //var buffer string + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] switch { case ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '_': - buffer += string(ch) + //buffer += string(ch) + l++ case ch == '(': - return set.parseFunction(segment, buffer, i) + var str string + if l != 0 { + str = seg[si : si+l] + } + return set.parseFunction(seg, str, i) default: i-- - set.Expr = append(set.Expr, DBToken{buffer, TokenColumn}) + var str string + if l != 0 { + str = seg[si : si+l] + } + set.Expr = append(set.Expr, DBToken{str, TokenColumn}) return i } } @@ -268,30 +307,44 @@ func (set *DBSetter) parseFunction(segment string, buffer string, i int) int { return i } -func (set *DBSetter) parseString(segment string, i int) int { - var buffer string +func (set *DBSetter) parseString(seg string, i int) int { + //var buffer string i++ - for ; i < len(segment); i++ { - char := segment[i] - if char != '\'' { - buffer += string(char) + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] + if ch != '\'' { + //buffer += string(ch) + l++ } else { - set.Expr = append(set.Expr, DBToken{buffer, TokenString}) + var str string + if l != 0 { + str = seg[si : si+l] + } + set.Expr = append(set.Expr, DBToken{str, TokenString}) return i } } return i } -func (set *DBSetter) parseOperator(segment string, i int) int { - var buffer string - for ; i < len(segment); i++ { - ch := segment[i] +func (set *DBSetter) parseOperator(seg string, i int) int { + //var buffer string + si := i + l := 0 + for ; i < len(seg); i++ { + ch := seg[i] if isOpByte(ch) { - buffer += string(ch) + //buffer += string(ch) + l++ } else { i-- - set.Expr = append(set.Expr, DBToken{buffer, TokenOp}) + var str string + if l != 0 { + str = seg[si : si+l] + } + set.Expr = append(set.Expr, DBToken{str, TokenOp}) return i } }