good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
471ba7e4
Commit
471ba7e4
authored
Feb 12, 2022
by
José Nieto
Browse files
Options
Downloads
Patches
Plain Diff
optimize prepareQueryForDisplay
parent
78486f21
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
internal/sqlbuilder/builder.go
+37
-18
37 additions, 18 deletions
internal/sqlbuilder/builder.go
internal/sqlbuilder/builder_test.go
+5
-0
5 additions, 0 deletions
internal/sqlbuilder/builder_test.go
internal/sqlbuilder/placeholder_test.go
+63
-0
63 additions, 0 deletions
internal/sqlbuilder/placeholder_test.go
with
105 additions
and
18 deletions
internal/sqlbuilder/builder.go
+
37
−
18
View file @
471ba7e4
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
internal/sqlbuilder/builder_test.go
+
5
−
0
View file @
471ba7e4
...
@@ -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
)}
...
...
This diff is collapsed.
Click to expand it.
internal/sqlbuilder/placeholder_test.go
+
63
−
0
View file @
471ba7e4
...
@@ -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\t
12345"
,
Out
:
"12345"
,
},
{
In
:
"12345
\r\n\t
"
,
Out
:
"12345"
,
},
{
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
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
})
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment