move ScanAll/ScanSingle in sq package

This commit is contained in:
Mike Schwörer 2022-12-11 03:14:42 +01:00
parent 26cd1533b4
commit 98b1e8bd80
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
9 changed files with 21 additions and 61 deletions

View File

@ -8,7 +8,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/rs/zerolog v1.28.0
github.com/swaggo/swag v1.8.7
gogs.mikescher.com/BlackForestBytes/goext v0.0.33
gogs.mikescher.com/BlackForestBytes/goext v0.0.34
)
require (

View File

@ -104,6 +104,8 @@ gogs.mikescher.com/BlackForestBytes/goext v0.0.32 h1:DJoRBNhq4rrOBXA/nD6WEm7L3vy
gogs.mikescher.com/BlackForestBytes/goext v0.0.32/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g=
gogs.mikescher.com/BlackForestBytes/goext v0.0.33 h1:NQRgsEs2j8eY9V45Ynq84+F0FgBfvapOGv4JZMh0eaI=
gogs.mikescher.com/BlackForestBytes/goext v0.0.33/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g=
gogs.mikescher.com/BlackForestBytes/goext v0.0.34 h1:fi6nA+7vDiAbIjs+meIo/jGXw4rig/nrjF/QNWSKN08=
gogs.mikescher.com/BlackForestBytes/goext v0.0.34/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=

View File

@ -3,6 +3,7 @@ package models
import (
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -67,7 +68,7 @@ func (c ChannelDB) Model() Channel {
}
func DecodeChannel(r *sqlx.Rows) (Channel, error) {
data, err := scanSingle[ChannelDB](r)
data, err := sq.ScanSingle[ChannelDB](r, true)
if err != nil {
return Channel{}, err
}
@ -75,7 +76,7 @@ func DecodeChannel(r *sqlx.Rows) (Channel, error) {
}
func DecodeChannels(r *sqlx.Rows) ([]Channel, error) {
data, err := scanAll[ChannelDB](r)
data, err := sq.ScanAll[ChannelDB](r, true)
if err != nil {
return nil, err
}

View File

@ -3,6 +3,7 @@ package models
import (
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -68,7 +69,7 @@ func (c ClientDB) Model() Client {
}
func DecodeClient(r *sqlx.Rows) (Client, error) {
data, err := scanSingle[ClientDB](r)
data, err := sq.ScanSingle[ClientDB](r, true)
if err != nil {
return Client{}, err
}
@ -76,7 +77,7 @@ func DecodeClient(r *sqlx.Rows) (Client, error) {
}
func DecodeClients(r *sqlx.Rows) ([]Client, error) {
data, err := scanAll[ClientDB](r)
data, err := sq.ScanAll[ClientDB](r, true)
if err != nil {
return nil, err
}

View File

@ -3,6 +3,7 @@ package models
import (
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -88,7 +89,7 @@ func (d DeliveryDB) Model() Delivery {
}
func DecodeDelivery(r *sqlx.Rows) (Delivery, error) {
data, err := scanSingle[DeliveryDB](r)
data, err := sq.ScanSingle[DeliveryDB](r, true)
if err != nil {
return Delivery{}, err
}
@ -96,7 +97,7 @@ func DecodeDelivery(r *sqlx.Rows) (Delivery, error) {
}
func DecodeDeliveries(r *sqlx.Rows) ([]Delivery, error) {
data, err := scanAll[DeliveryDB](r)
data, err := sq.ScanAll[DeliveryDB](r, true)
if err != nil {
return nil, err
}

View File

@ -3,6 +3,7 @@ package models
import (
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -142,7 +143,7 @@ func (m MessageDB) Model() Message {
}
func DecodeMessage(r *sqlx.Rows) (Message, error) {
data, err := scanSingle[MessageDB](r)
data, err := sq.ScanSingle[MessageDB](r, true)
if err != nil {
return Message{}, err
}
@ -150,7 +151,7 @@ func DecodeMessage(r *sqlx.Rows) (Message, error) {
}
func DecodeMessages(r *sqlx.Rows) ([]Message, error) {
data, err := scanAll[MessageDB](r)
data, err := sq.ScanAll[MessageDB](r, true)
if err != nil {
return nil, err
}

View File

@ -3,6 +3,7 @@ package models
import (
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -61,7 +62,7 @@ func (s SubscriptionDB) Model() Subscription {
}
func DecodeSubscription(r *sqlx.Rows) (Subscription, error) {
data, err := scanSingle[SubscriptionDB](r)
data, err := sq.ScanSingle[SubscriptionDB](r, true)
if err != nil {
return Subscription{}, err
}
@ -69,7 +70,7 @@ func DecodeSubscription(r *sqlx.Rows) (Subscription, error) {
}
func DecodeSubscriptions(r *sqlx.Rows) ([]Subscription, error) {
data, err := scanAll[SubscriptionDB](r)
data, err := sq.ScanAll[SubscriptionDB](r, true)
if err != nil {
return nil, err
}

View File

@ -4,6 +4,7 @@ import (
scn "blackforestbytes.com/simplecloudnotifier"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@ -160,7 +161,7 @@ func (u UserDB) Model() User {
}
func DecodeUser(r *sqlx.Rows) (User, error) {
data, err := scanSingle[UserDB](r)
data, err := sq.ScanSingle[UserDB](r, true)
if err != nil {
return User{}, err
}
@ -168,7 +169,7 @@ func DecodeUser(r *sqlx.Rows) (User, error) {
}
func DecodeUsers(r *sqlx.Rows) ([]User, error) {
data, err := scanAll[UserDB](r)
data, err := sq.ScanAll[UserDB](r, true)
if err != nil {
return nil, err
}

View File

@ -1,9 +1,6 @@
package models
import (
"database/sql"
"errors"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"time"
)
@ -22,48 +19,3 @@ func timeOptFromMilli(millis *int64) *time.Time {
}
return langext.Ptr(time.UnixMilli(*millis))
}
func scanSingle[TData any](rows *sqlx.Rows) (TData, error) {
if rows.Next() {
var data TData
err := rows.StructScan(&data)
if err != nil {
return *new(TData), err
}
if rows.Next() {
_ = rows.Close()
return *new(TData), errors.New("sql returned more than onw row")
}
err = rows.Close()
if err != nil {
return *new(TData), err
}
return data, nil
} else {
_ = rows.Close()
return *new(TData), sql.ErrNoRows
}
}
func scanAll[TData any](rows *sqlx.Rows) ([]TData, error) {
res := make([]TData, 0)
for rows.Next() {
var data TData
err := rows.StructScan(&data)
if err != nil {
return nil, err
}
res = append(res, data)
}
err := rows.Close()
if err != nil {
return nil, err
}
return res, nil
}
//TODO move scanAll+scanSingle into sq package (?)
//TODO als add convenient methods:
// - QueryScanSingle[T any](..)
// - QueryScanMulti[T any](..)