Optimise insert field identifiers.

Optimise db where parse column.
This commit is contained in:
Azareal 2020-01-01 11:26:15 +10:00
parent 35ddc89009
commit 0a0a2ea2b0
2 changed files with 35 additions and 16 deletions

View File

@ -61,9 +61,17 @@ type DBColumn struct {
Type int Type int
} }
const (
IdenFunc = iota
IdenColumn
IdenString
IdenLiteral
)
type DBField struct { type DBField struct {
Name string Name string
Type string //Type string
Type int
} }
type DBWhere struct { type DBWhere struct {

View File

@ -108,28 +108,39 @@ func (wh *DBWhere) parseNumber(segment string, i int) int {
return i return i
} }
func (wh *DBWhere) parseColumn(segment string, i int) int { func (wh *DBWhere) parseColumn(seg string, i int) int {
var buffer string //var buffer string
for ; i < len(segment); i++ { si := i
ch := segment[i] l := 0
for ; i < len(seg); i++ {
ch := seg[i]
switch { switch {
case ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '.' || ch == '_': case ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '.' || ch == '_':
buffer += string(ch) //buffer += string(ch)
l++
case ch == '(': 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: default:
i-- 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
} }
} }
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 preI := i
i = skipFunctionCall(segment, i-1) i = skipFunctionCall(seg, i-1)
buffer += segment[preI:i] + string(segment[i]) buffer += seg[preI:i] + string(seg[i])
wh.Expr = append(wh.Expr, DBToken{buffer, TokenFunc}) wh.Expr = append(wh.Expr, DBToken{buffer, TokenFunc})
return i return i
} }
@ -385,17 +396,17 @@ func processFields(fieldStr string) (fields []DBField) {
return fields 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 ('a' <= iden[0] && iden[0] <= 'z') || ('A' <= iden[0] && iden[0] <= 'Z') {
if iden[len(iden)-1] == ')' { if iden[len(iden)-1] == ')' {
return "function" return IdenFunc
} }
return "column" return IdenColumn
} }
if iden[0] == '\'' || iden[0] == '"' { if iden[0] == '\'' || iden[0] == '"' {
return "string" return IdenString
} }
return "literal" return IdenLiteral
} }
func getIdentifier(seg string, startOffset int) (out string, i int) { func getIdentifier(seg string, startOffset int) (out string, i int) {