From 0a0a2ea2b01be626538acd8297591b78fb5cb2f1 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 1 Jan 2020 11:26:15 +1000 Subject: [PATCH] Optimise insert field identifiers. Optimise db where parse column. --- query_gen/querygen.go | 10 +++++++++- query_gen/utils.go | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/query_gen/querygen.go b/query_gen/querygen.go index 71fd4c20..e0ee5df3 100644 --- a/query_gen/querygen.go +++ b/query_gen/querygen.go @@ -61,9 +61,17 @@ type DBColumn struct { Type int } +const ( + IdenFunc = iota + IdenColumn + IdenString + IdenLiteral +) + type DBField struct { Name string - Type string + //Type string + Type int } type DBWhere struct { diff --git a/query_gen/utils.go b/query_gen/utils.go index 9e00a48d..75569c2a 100644 --- a/query_gen/utils.go +++ b/query_gen/utils.go @@ -108,28 +108,39 @@ func (wh *DBWhere) parseNumber(segment string, i int) int { return i } -func (wh *DBWhere) parseColumn(segment string, i int) int { - var buffer string - for ; i < len(segment); i++ { - ch := segment[i] +func (wh *DBWhere) 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 == '.' || ch == '_': - buffer += string(ch) + //buffer += string(ch) + l++ case ch == '(': - return wh.parseFunction(segment, buffer, i) + var str string + if l != 0 { + str = seg[si : si+l] + } + return wh.parseFunction(seg, str, i) default: i-- - wh.Expr = append(wh.Expr, DBToken{buffer, TokenColumn}) + var str string + if l != 0 { + str = seg[si : si+l] + } + wh.Expr = append(wh.Expr, DBToken{str, TokenColumn}) return i } } return i } -func (wh *DBWhere) parseFunction(segment string, buffer string, i int) int { +func (wh *DBWhere) parseFunction(seg string, buffer string, i int) int { preI := i - i = skipFunctionCall(segment, i-1) - buffer += segment[preI:i] + string(segment[i]) + i = skipFunctionCall(seg, i-1) + buffer += seg[preI:i] + string(seg[i]) wh.Expr = append(wh.Expr, DBToken{buffer, TokenFunc}) return i } @@ -385,17 +396,17 @@ func processFields(fieldStr string) (fields []DBField) { return fields } -func getIdentifierType(iden string) string { +func getIdentifierType(iden string) int { if ('a' <= iden[0] && iden[0] <= 'z') || ('A' <= iden[0] && iden[0] <= 'Z') { if iden[len(iden)-1] == ')' { - return "function" + return IdenFunc } - return "column" + return IdenColumn } if iden[0] == '\'' || iden[0] == '"' { - return "string" + return IdenString } - return "literal" + return IdenLiteral } func getIdentifier(seg string, startOffset int) (out string, i int) {