Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-orusapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
orus-io
go-orusapi
Commits
938b5b90d386
Commit
938b5b90d386
authored
3 years ago
by
Christophe de Vienne
Browse files
Options
Downloads
Patches
Plain Diff
Add a 'ArrayContains' operator ('@>' in PG)
parent
d9d9948c52fa
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#28178
passed
3 years ago
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
database/array_contains.go
+70
-0
70 additions, 0 deletions
database/array_contains.go
scripts/generate_db_helpers.go
+4
-0
4 additions, 0 deletions
scripts/generate_db_helpers.go
with
74 additions
and
0 deletions
database/array_contains.go
0 → 100644
+
70
−
0
View file @
938b5b90
package
database
import
(
"database/sql/driver"
"fmt"
"sort"
"strings"
"github.com/jackc/pgtype"
)
type
ArrayContains
map
[
string
]
interface
{}
func
(
ac
ArrayContains
)
ToSQL
()
(
sql
string
,
args
[]
interface
{},
err
error
)
{
if
len
(
ac
)
==
0
{
// Empty Sql{} evaluates to true.
sql
=
"(1=1)"
return
}
sortedKeys
:=
getSortedKeys
(
ac
)
var
exprs
[]
string
for
_
,
key
:=
range
sortedKeys
{
var
expr
string
val
:=
ac
[
key
]
switch
v
:=
val
.
(
type
)
{
case
driver
.
Valuer
:
if
val
,
err
=
v
.
Value
();
err
!=
nil
{
return
}
case
[]
string
:
var
data
pgtype
.
TextArray
if
err
=
data
.
Set
(
v
);
err
!=
nil
{
return
}
if
val
,
err
=
data
.
Value
();
err
!=
nil
{
return
}
case
string
:
var
data
pgtype
.
TextArray
if
err
=
data
.
Set
([]
string
{
v
});
err
!=
nil
{
return
}
if
val
,
err
=
data
.
Value
();
err
!=
nil
{
return
}
}
if
val
==
nil
{
panic
(
"cannot handle NULL values"
)
}
else
{
expr
=
fmt
.
Sprintf
(
"%s @> ?"
,
key
)
args
=
append
(
args
,
val
)
}
exprs
=
append
(
exprs
,
expr
)
}
sql
=
strings
.
Join
(
exprs
,
" AND "
)
return
}
func
getSortedKeys
(
exp
map
[
string
]
interface
{})
[]
string
{
sortedKeys
:=
make
([]
string
,
0
,
len
(
exp
))
for
k
:=
range
exp
{
sortedKeys
=
append
(
sortedKeys
,
k
)
}
sort
.
Strings
(
sortedKeys
)
return
sortedKeys
}
This diff is collapsed.
Click to expand it.
scripts/generate_db_helpers.go
+
4
−
0
View file @
938b5b90
...
...
@@ -344,6 +344,10 @@
return squirrel.NotLike{c.Sql(): value}
}
func (c Column) ArrayContains(value interface{}) database.ArrayContains{
return database.ArrayContains{c.Sql(): value}
}
func (c Column) As(name string) Column {
c.alias = name
return c
...
...
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