Optimise DBWhere methods.

Optimise DBSetter methods.
This commit is contained in:
Azareal 2020-01-01 14:15:11 +10:00
parent 0a0a2ea2b0
commit 08864edd6c
2 changed files with 106 additions and 53 deletions

View File

@ -82,7 +82,7 @@ func (a *MysqlAdapter) DbVersion() string {
return "SELECT VERSION()" return "SELECT VERSION()"
} }
func (a *MysqlAdapter) DropTable(name string, table string) (string, error) { func (a *MysqlAdapter) DropTable(name, table string) (string, error) {
if table == "" { if table == "" {
return "", errors.New("You need a name for this 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 // 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 == "" { if table == "" {
return "", errors.New("You need a name for this 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 () ") 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 == "" { if table == "" {
return "", errors.New("You need a name for this table") return "", errors.New("You need a name for this table")
} }
@ -323,7 +323,7 @@ func (a *MysqlAdapter) buildColumns(columns string) (q string) {
} }
// ! DEPRECATED // ! 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 == "" { if table == "" {
return "", errors.New("You need a name for this 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 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 == "" { if table == "" {
return "", errors.New("You need a name for this 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 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 == "" { if table == "" {
return "", errors.New("You need a name for this 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 // 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 == "" { if table == "" {
return "", errors.New("You need a name for this 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 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 == "" { if table == "" {
return "", errors.New("You need a name for this 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 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 == "" { if table == "" {
return "", errors.New("You need a name for this table") return "", errors.New("You need a name for this table")
} }

View File

@ -93,15 +93,22 @@ func processJoiner(joinStr string) (joiner []DBJoiner) {
return joiner return joiner
} }
func (wh *DBWhere) parseNumber(segment string, i int) int { func (wh *DBWhere) parseNumber(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]
if '0' <= ch && ch <= '9' { if '0' <= ch && ch <= '9' {
buffer += string(ch) //buffer += string(ch)
l++
} else { } else {
i-- 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 return i
} }
} }
@ -145,30 +152,44 @@ func (wh *DBWhere) parseFunction(seg string, buffer string, i int) int {
return i return i
} }
func (wh *DBWhere) parseString(segment string, i int) int { func (wh *DBWhere) parseString(seg string, i int) int {
var buffer string //var buffer string
i++ i++
for ; i < len(segment); i++ { si := i
ch := segment[i] l := 0
for ; i < len(seg); i++ {
ch := seg[i]
if ch != '\'' { if ch != '\'' {
buffer += string(ch) //buffer += string(ch)
l++
} else { } 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
} }
} }
return i return i
} }
func (wh *DBWhere) parseOperator(segment string, i int) int { func (wh *DBWhere) parseOperator(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]
if isOpByte(ch) { if isOpByte(ch) {
buffer += string(ch) //buffer += string(ch)
l++
} else { } else {
i-- 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 return i
} }
} }
@ -228,32 +249,50 @@ func processWhere(whereStr string) (where []DBWhere) {
return where return where
} }
func (set *DBSetter) parseNumber(segment string, i int) int { func (set *DBSetter) parseNumber(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]
if '0' <= ch && ch <= '9' { if '0' <= ch && ch <= '9' {
buffer += string(ch) //buffer += string(ch)
l++
} else { } 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
} }
} }
return i return i
} }
func (set *DBSetter) parseColumn(segment string, i int) int { func (set *DBSetter) 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 == '_': case ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '_':
buffer += string(ch) //buffer += string(ch)
l++
case ch == '(': 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: default:
i-- 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 return i
} }
} }
@ -268,30 +307,44 @@ func (set *DBSetter) parseFunction(segment string, buffer string, i int) int {
return i return i
} }
func (set *DBSetter) parseString(segment string, i int) int { func (set *DBSetter) parseString(seg string, i int) int {
var buffer string //var buffer string
i++ i++
for ; i < len(segment); i++ { si := i
char := segment[i] l := 0
if char != '\'' { for ; i < len(seg); i++ {
buffer += string(char) ch := seg[i]
if ch != '\'' {
//buffer += string(ch)
l++
} else { } 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
} }
} }
return i return i
} }
func (set *DBSetter) parseOperator(segment string, i int) int { func (set *DBSetter) parseOperator(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]
if isOpByte(ch) { if isOpByte(ch) {
buffer += string(ch) //buffer += string(ch)
l++
} else { } else {
i-- 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 return i
} }
} }