Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
d4509066
Commit
d4509066
authored
May 08, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
52adb309
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
13 deletions
+34
-13
go/xcommon/xnet/lonet/registry_sqlite.go
go/xcommon/xnet/lonet/registry_sqlite.go
+34
-13
No files found.
go/xcommon/xnet/lonet/registry_sqlite.go
View file @
d4509066
...
@@ -90,6 +90,32 @@ func (r *sqliteRegistry) withConn(ctx context.Context, f func(*sqlite.Conn) erro
...
@@ -90,6 +90,32 @@ func (r *sqliteRegistry) withConn(ctx context.Context, f func(*sqlite.Conn) erro
return
f
(
conn
)
return
f
(
conn
)
}
}
var
errNoRows
=
errors
.
New
(
"query1: empty result"
)
var
errManyRows
=
errors
.
New
(
"query1: multiple results"
)
// query1 is like sqliteutil.Exec but checks that exactly 1 row is returned.
//
// if query results in no rows - errNoRows is returned.
// if query results in more than 1 row - errManyRows is returned.
func
query1
(
conn
*
sqlite
.
Conn
,
query
string
,
resultf
func
(
stmt
*
sqlite
.
Stmt
),
argv
...
interface
{})
error
{
nrow
:=
0
err
:=
sqliteutil
.
Exec
(
conn
,
query
,
func
(
stmt
*
sqlite
.
Stmt
)
error
{
if
nrow
==
1
{
return
errManyRows
}
nrow
++
resultf
(
stmt
)
return
nil
},
argv
...
)
if
err
!=
nil
{
return
err
}
if
nrow
==
0
{
return
errNoRows
}
return
nil
}
func
(
r
*
sqliteRegistry
)
setup
(
ctx
context
.
Context
)
(
err
error
)
{
func
(
r
*
sqliteRegistry
)
setup
(
ctx
context
.
Context
)
(
err
error
)
{
return
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
return
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
err
:=
sqliteutil
.
ExecScript
(
conn
,
`
err
:=
sqliteutil
.
ExecScript
(
conn
,
`
...
@@ -137,33 +163,28 @@ func (r *sqliteRegistry) Query(ctx context.Context, hostname string) (osladdr st
...
@@ -137,33 +163,28 @@ func (r *sqliteRegistry) Query(ctx context.Context, hostname string) (osladdr st
defer
r
.
regerr
(
&
err
,
"query"
,
hostname
)
defer
r
.
regerr
(
&
err
,
"query"
,
hostname
)
err
=
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
err
=
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
nrow
:=
0
err
:=
query1
(
conn
,
"SELECT osladdr FROM hosts WHERE hostname = ?"
,
err
:=
sqliteutil
.
Exec
(
conn
,
"SELECT osladdr FROM hosts WHERE hostname = ?"
,
func
(
stmt
*
sqlite
.
Stmt
)
{
func
(
stmt
*
sqlite
.
Stmt
)
error
{
osladdr
=
stmt
.
ColumnText
(
0
)
osladdr
=
stmt
.
ColumnText
(
0
)
nrow
++
return
nil
},
hostname
)
},
hostname
)
if
err
!=
nil
{
switch
err
{
return
err
case
errNoRows
:
}
if
nrow
==
0
{
return
errNoHost
return
errNoHost
}
else
if
nrow
>
1
{
case
errManyRows
:
// hostname is PK - we should not get several results
// hostname is PK - we should not get several results
osladdr
=
""
osladdr
=
""
return
errRegDup
return
errRegDup
}
}
return
nil
return
err
})
})
return
osladdr
,
err
return
osladdr
,
err
}
}
// regerr is syntatic sugar to wrap !nil *errp into registryError.
// regerr is synta
c
tic sugar to wrap !nil *errp into registryError.
func
(
r
*
sqliteRegistry
)
regerr
(
errp
*
error
,
op
string
,
args
...
interface
{})
{
func
(
r
*
sqliteRegistry
)
regerr
(
errp
*
error
,
op
string
,
args
...
interface
{})
{
if
*
errp
==
nil
{
if
*
errp
==
nil
{
return
return
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment