good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit f79575d1 authored by José Nieto's avatar José Nieto Committed by GitHub
Browse files

Merge pull request #649 from upper/refactor-memory-and-speed-optimizations

optimize `prepareQueryForDisplay`
parents 78486f21 471ba7e4
Branches
Tags v4.5.2
No related merge requests found
......@@ -29,7 +29,6 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
......@@ -72,10 +71,6 @@ type fieldValue struct {
values []interface{}
}
var (
reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
)
var (
sqlPlaceholder = exql.RawValue(`?`)
)
......@@ -347,11 +342,10 @@ func Map(item interface{}, options *MapOptions) ([]string, []interface{}, error)
}
func columnFragments(columns []interface{}) ([]exql.Fragment, []interface{}, error) {
l := len(columns)
f := make([]exql.Fragment, l)
f := make([]exql.Fragment, len(columns))
args := []interface{}{}
for i := 0; i < l; i++ {
for i := range columns {
switch v := columns[i].(type) {
case compilable:
c, err := v.Compile()
......@@ -393,19 +387,44 @@ func columnFragments(columns []interface{}) ([]exql.Fragment, []interface{}, err
return f, args, nil
}
func prepareQueryForDisplay(in string) (out string) {
j := 1
for i := range in {
if in[i] == '?' {
out = out + "$" + strconv.Itoa(j)
j++
func prepareQueryForDisplay(in string) string {
out := make([]byte, 0, len(in))
offset := 0
whitespace := true
placeholders := 1
for i := 0; i < len(in); i++ {
if in[i] == ' ' || in[i] == '\r' || in[i] == '\n' || in[i] == '\t' {
if whitespace {
offset = i
} else {
out = out + string(in[i])
whitespace = true
out = append(out, in[offset:i]...)
offset = i
}
continue
}
if whitespace {
whitespace = false
if len(out) > 0 {
out = append(out, ' ')
}
offset = i
}
if in[i] == '?' {
out = append(out, in[offset:i]...)
offset = i + 1
out = reInvisibleChars.ReplaceAllString(out, ` `)
return strings.TrimSpace(out)
out = append(out, '$')
out = append(out, strconv.Itoa(placeholders)...)
placeholders++
}
}
if !whitespace {
out = append(out, in[offset:len(in)]...)
}
return string(out)
}
func (iter *iterator) NextScan(dst ...interface{}) error {
......
......@@ -2,6 +2,7 @@ package sqlbuilder
import (
"fmt"
"regexp"
"strings"
"testing"
......@@ -9,6 +10,10 @@ import (
db "github.com/upper/db/v4"
)
var (
reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
)
func TestSelect(t *testing.T) {
b := &sqlBuilder{t: newTemplateWithUtils(&testTemplate)}
......
......@@ -7,6 +7,69 @@ import (
db "github.com/upper/db/v4"
)
func TestPrepareForDisplay(t *testing.T) {
samples := []struct {
In string
Out string
}{
{
In: "12345",
Out: "12345",
},
{
In: "\r\n\t12345",
Out: "12345",
},
{
In: "12345\r\n\t",
Out: "12345",
},
{
In: "\r\n\t1\r2\n3\t4\r5\r\n\t",
Out: "1 2 3 4 5",
},
{
In: "\r\n \t 1\r 2\n 3\t 4\r 5\r \n\t",
Out: "1 2 3 4 5",
},
{
In: "\r\n \t 11\r 22\n 33\t 44 \r 55",
Out: "11 22 33 44 55",
},
{
In: "11\r 22\n 33\t 44 \r 55",
Out: "11 22 33 44 55",
},
{
In: "1 2 3 4 5",
Out: "1 2 3 4 5",
},
{
In: "?",
Out: "$1",
},
{
In: "? ?",
Out: "$1 $2",
},
{
In: "? ? ?",
Out: "$1 $2 $3",
},
{
In: " ? ? ? ",
Out: "$1 $2 $3",
},
{
In: "???",
Out: "$1$2$3",
},
}
for _, sample := range samples {
assert.Equal(t, sample.Out, prepareQueryForDisplay(sample.In))
}
}
func TestPlaceholderSimple(t *testing.T) {
{
ret, _ := Preprocess("?", []interface{}{1})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment