From 98b1e8bd806b091c6ea6c09fa30c6f1577f7c6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 11 Dec 2022 03:14:42 +0100 Subject: [PATCH] move ScanAll/ScanSingle in sq package --- server/go.mod | 2 +- server/go.sum | 2 ++ server/models/channel.go | 5 ++-- server/models/client.go | 5 ++-- server/models/delivery.go | 5 ++-- server/models/message.go | 5 ++-- server/models/subscription.go | 5 ++-- server/models/user.go | 5 ++-- server/models/utils.go | 48 ----------------------------------- 9 files changed, 21 insertions(+), 61 deletions(-) diff --git a/server/go.mod b/server/go.mod index f3c27ee..4d94432 100644 --- a/server/go.mod +++ b/server/go.mod @@ -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 ( diff --git a/server/go.sum b/server/go.sum index a12ebc3..cd660db 100644 --- a/server/go.sum +++ b/server/go.sum @@ -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= diff --git a/server/models/channel.go b/server/models/channel.go index 8e4c408..af19dde 100644 --- a/server/models/channel.go +++ b/server/models/channel.go @@ -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 } diff --git a/server/models/client.go b/server/models/client.go index f2b62ab..242d059 100644 --- a/server/models/client.go +++ b/server/models/client.go @@ -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 } diff --git a/server/models/delivery.go b/server/models/delivery.go index 2e1512a..dd09a98 100644 --- a/server/models/delivery.go +++ b/server/models/delivery.go @@ -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 } diff --git a/server/models/message.go b/server/models/message.go index 1c5031b..0b133bc 100644 --- a/server/models/message.go +++ b/server/models/message.go @@ -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 } diff --git a/server/models/subscription.go b/server/models/subscription.go index b2eda95..c889779 100644 --- a/server/models/subscription.go +++ b/server/models/subscription.go @@ -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 } diff --git a/server/models/user.go b/server/models/user.go index 856f487..46a1d78 100644 --- a/server/models/user.go +++ b/server/models/user.go @@ -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 } diff --git a/server/models/utils.go b/server/models/utils.go index af6dbd8..051705c 100644 --- a/server/models/utils.go +++ b/server/models/utils.go @@ -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](..)