good morning!!!!

Skip to content
Snippets Groups Projects
Commit 471ba7e4 authored by José Nieto's avatar José Nieto
Browse files

optimize prepareQueryForDisplay

parent 78486f21
Branches
Tags
No related merge requests found
...@@ -29,7 +29,6 @@ import ( ...@@ -29,7 +29,6 @@ import (
"fmt" "fmt"
"log" "log"
"reflect" "reflect"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
...@@ -72,10 +71,6 @@ type fieldValue struct { ...@@ -72,10 +71,6 @@ type fieldValue struct {
values []interface{} values []interface{}
} }
var (
reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
)
var ( var (
sqlPlaceholder = exql.RawValue(`?`) sqlPlaceholder = exql.RawValue(`?`)
) )
...@@ -347,11 +342,10 @@ func Map(item interface{}, options *MapOptions) ([]string, []interface{}, error) ...@@ -347,11 +342,10 @@ func Map(item interface{}, options *MapOptions) ([]string, []interface{}, error)
} }
func columnFragments(columns []interface{}) ([]exql.Fragment, []interface{}, error) { func columnFragments(columns []interface{}) ([]exql.Fragment, []interface{}, error) {
l := len(columns) f := make([]exql.Fragment, len(columns))
f := make([]exql.Fragment, l)
args := []interface{}{} args := []interface{}{}
for i := 0; i < l; i++ { for i := range columns {
switch v := columns[i].(type) { switch v := columns[i].(type) {
case compilable: case compilable:
c, err := v.Compile() c, err := v.Compile()
...@@ -393,19 +387,44 @@ func columnFragments(columns []interface{}) ([]exql.Fragment, []interface{}, err ...@@ -393,19 +387,44 @@ func columnFragments(columns []interface{}) ([]exql.Fragment, []interface{}, err
return f, args, nil return f, args, nil
} }
func prepareQueryForDisplay(in string) (out string) { func prepareQueryForDisplay(in string) string {
j := 1 out := make([]byte, 0, len(in))
for i := range in {
if in[i] == '?' { offset := 0
out = out + "$" + strconv.Itoa(j) whitespace := true
j++ 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 { } 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, ` `) out = append(out, '$')
return strings.TrimSpace(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 { func (iter *iterator) NextScan(dst ...interface{}) error {
......
...@@ -2,6 +2,7 @@ package sqlbuilder ...@@ -2,6 +2,7 @@ package sqlbuilder
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"testing" "testing"
...@@ -9,6 +10,10 @@ import ( ...@@ -9,6 +10,10 @@ import (
db "github.com/upper/db/v4" db "github.com/upper/db/v4"
) )
var (
reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
)
func TestSelect(t *testing.T) { func TestSelect(t *testing.T) {
b := &sqlBuilder{t: newTemplateWithUtils(&testTemplate)} b := &sqlBuilder{t: newTemplateWithUtils(&testTemplate)}
......
...@@ -7,6 +7,69 @@ import ( ...@@ -7,6 +7,69 @@ import (
db "github.com/upper/db/v4" 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) { func TestPlaceholderSimple(t *testing.T) {
{ {
ret, _ := Preprocess("?", []interface{}{1}) 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