Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
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
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
)
{
return
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
err
:=
sqliteutil
.
ExecScript
(
conn
,
`
...
...
@@ -137,33 +163,28 @@ func (r *sqliteRegistry) Query(ctx context.Context, hostname string) (osladdr st
defer
r
.
regerr
(
&
err
,
"query"
,
hostname
)
err
=
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
nrow
:=
0
err
:=
sqliteutil
.
Exec
(
conn
,
"SELECT osladdr FROM hosts WHERE hostname = ?"
,
func
(
stmt
*
sqlite
.
Stmt
)
error
{
err
:=
query1
(
conn
,
"SELECT osladdr FROM hosts WHERE hostname = ?"
,
func
(
stmt
*
sqlite
.
Stmt
)
{
osladdr
=
stmt
.
ColumnText
(
0
)
nrow
++
return
nil
},
hostname
)
if
err
!=
nil
{
return
err
}
if
nrow
==
0
{
switch
err
{
case
errNoRows
:
return
errNoHost
}
else
if
nrow
>
1
{
case
errManyRows
:
// hostname is PK - we should not get several results
osladdr
=
""
return
errRegDup
}
return
nil
return
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
{})
{
if
*
errp
==
nil
{
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