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
42db5ca3dedd
Commit
42db5ca3dedd
authored
2 months ago
by
Christophe de Vienne
Browse files
Options
Downloads
Patches
Plain Diff
sql: add a 'Row' api
parent
5d141f571cf0
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
database/sql.go
+14
-0
14 additions, 0 deletions
database/sql.go
database/sql_helper.go
+5
-0
5 additions, 0 deletions
database/sql_helper.go
database/sql_row.go
+44
-0
44 additions, 0 deletions
database/sql_row.go
with
63 additions
and
0 deletions
database/sql.go
+
14
−
0
View file @
42db5ca3
...
@@ -57,6 +57,7 @@
...
@@ -57,6 +57,7 @@
GetContext
(
ctx
context
.
Context
,
dest
interface
{},
query
string
,
args
...
interface
{})
error
GetContext
(
ctx
context
.
Context
,
dest
interface
{},
query
string
,
args
...
interface
{})
error
SelectContext
(
ctx
context
.
Context
,
dest
interface
{},
query
string
,
args
...
interface
{})
error
SelectContext
(
ctx
context
.
Context
,
dest
interface
{},
query
string
,
args
...
interface
{})
error
QueryxContext
(
context
.
Context
,
string
,
...
interface
{})
(
*
sqlx
.
Rows
,
error
)
QueryxContext
(
context
.
Context
,
string
,
...
interface
{})
(
*
sqlx
.
Rows
,
error
)
QueryRowxContext
(
context
.
Context
,
string
,
...
interface
{})
*
sqlx
.
Row
}
}
func
setPlaceHolderFormat
(
query
squirrel
.
Sqlizer
)
squirrel
.
Sqlizer
{
func
setPlaceHolderFormat
(
query
squirrel
.
Sqlizer
)
squirrel
.
Sqlizer
{
...
@@ -135,6 +136,19 @@
...
@@ -135,6 +136,19 @@
return
e
.
QueryxContext
(
ctx
,
sqlQuery
,
args
...
)
return
e
.
QueryxContext
(
ctx
,
sqlQuery
,
args
...
)
}
}
// QueryRowContext runs a squirrel query on the given db or tx and return a single Row.
func
QueryRowContext
(
ctx
context
.
Context
,
e
SQLExecutor
,
query
squirrel
.
Sqlizer
,
log
*
zerolog
.
Logger
)
*
Row
{
query
=
setPlaceHolderFormat
(
query
)
sqlQuery
,
args
,
err
:=
query
.
ToSql
()
if
err
!=
nil
{
return
&
Row
{
err
:
err
}
}
SQLTrace
(
log
,
sqlQuery
,
args
)
return
&
Row
{
Row
:
e
.
QueryRowxContext
(
ctx
,
sqlQuery
,
args
...
)}
}
// ValuesMap returns the values for a list of columns as a map. If a column does
// ValuesMap returns the values for a list of columns as a map. If a column does
// not exits, the corresponding value is set to nil.
// not exits, the corresponding value is set to nil.
func
ValuesMap
(
m
Mapped
,
columns
...
string
)
map
[
string
]
interface
{}
{
func
ValuesMap
(
m
Mapped
,
columns
...
string
)
map
[
string
]
interface
{}
{
...
...
This diff is collapsed.
Click to expand it.
database/sql_helper.go
+
5
−
0
View file @
42db5ca3
...
@@ -123,6 +123,11 @@
...
@@ -123,6 +123,11 @@
return
h
.
sqle
.
QueryxContext
(
h
.
ctx
,
sql
,
args
...
)
return
h
.
sqle
.
QueryxContext
(
h
.
ctx
,
sql
,
args
...
)
}
}
// QueryRow executes a query that returns a single row.
func
(
h
*
SQLHelper
)
QueryRow
(
query
sq
.
Sqlizer
)
*
Row
{
return
QueryRowContext
(
h
.
ctx
,
h
.
sqle
,
query
,
&
h
.
log
)
}
// Insert inserts a Mapped into the db.
// Insert inserts a Mapped into the db.
func
(
h
*
SQLHelper
)
Insert
(
instances
...
Mapped
)
(
sql
.
Result
,
error
)
{
func
(
h
*
SQLHelper
)
Insert
(
instances
...
Mapped
)
(
sql
.
Result
,
error
)
{
query
:=
SQLInsert
(
instances
...
)
query
:=
SQLInsert
(
instances
...
)
...
...
This diff is collapsed.
Click to expand it.
database/sql_row.go
0 → 100644
+
44
−
0
View file @
42db5ca3
package
database
import
(
"database/sql"
"github.com/jmoiron/sqlx"
)
type
Row
struct
{
*
sqlx
.
Row
err
error
}
// Scan is a fixed implementation of sql.Row.Scan, which does not discard the
// underlying error from the internal rows object if it exists.
func
(
r
*
Row
)
Scan
(
dest
...
interface
{})
error
{
if
r
.
err
!=
nil
{
return
r
.
err
}
return
r
.
Row
.
Scan
(
dest
...
)
}
// Columns returns the underlying sql.Rows.Columns(), or the deferred error usually
// returned by Row.Scan()
func
(
r
*
Row
)
Columns
()
([]
string
,
error
)
{
if
r
.
err
!=
nil
{
return
[]
string
{},
r
.
err
}
return
r
.
Row
.
Columns
()
}
// ColumnTypes returns the underlying sql.Rows.ColumnTypes(), or the deferred error
func
(
r
*
Row
)
ColumnTypes
()
([]
*
sql
.
ColumnType
,
error
)
{
if
r
.
err
!=
nil
{
return
[]
*
sql
.
ColumnType
{},
r
.
err
}
return
r
.
Row
.
ColumnTypes
()
}
// Err returns the error encountered while scanning.
func
(
r
*
Row
)
Err
()
error
{
return
r
.
err
}
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