mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-13 16:35:53 +00:00
chore(database): moving scan() -> pgx.RowToStructByNameLax()
This commit is contained in:
@@ -87,7 +87,7 @@ type InstanceRepository interface {
|
|||||||
// Member() MemberRepository
|
// Member() MemberRepository
|
||||||
|
|
||||||
Get(ctx context.Context, opts ...database.Condition) (*Instance, error)
|
Get(ctx context.Context, opts ...database.Condition) (*Instance, error)
|
||||||
List(ctx context.Context, opts ...database.Condition) ([]*Instance, error)
|
List(ctx context.Context, opts ...database.Condition) ([]Instance, error)
|
||||||
|
|
||||||
Create(ctx context.Context, instance *Instance) error
|
Create(ctx context.Context, instance *Instance) error
|
||||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
||||||
|
@@ -2,6 +2,8 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pool is a connection pool. e.g. pgxpool
|
// Pool is a connection pool. e.g. pgxpool
|
||||||
@@ -57,8 +59,5 @@ type Row interface {
|
|||||||
|
|
||||||
// Rows is an abstraction of sql.Rows.
|
// Rows is an abstraction of sql.Rows.
|
||||||
type Rows interface {
|
type Rows interface {
|
||||||
Row
|
pgx.Rows
|
||||||
Next() bool
|
|
||||||
Close() error
|
|
||||||
Err() error
|
|
||||||
}
|
}
|
||||||
|
@@ -9,10 +9,3 @@ import (
|
|||||||
var _ database.Rows = (*Rows)(nil)
|
var _ database.Rows = (*Rows)(nil)
|
||||||
|
|
||||||
type Rows struct{ pgx.Rows }
|
type Rows struct{ pgx.Rows }
|
||||||
|
|
||||||
// Close implements [database.Rows].
|
|
||||||
// Subtle: this method shadows the method (Rows).Close of Rows.Rows.
|
|
||||||
func (r *Rows) Close() error {
|
|
||||||
r.Rows.Close()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgconn"
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
"github.com/zitadel/zitadel/backend/v3/domain"
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||||
@@ -42,11 +43,17 @@ func (i *instance) Get(ctx context.Context, opts ...database.Condition) (*domain
|
|||||||
andCondition := database.And(opts...)
|
andCondition := database.And(opts...)
|
||||||
i.writeCondition(&builder, andCondition)
|
i.writeCondition(&builder, andCondition)
|
||||||
|
|
||||||
return scanInstance(i.client.QueryRow(ctx, builder.String(), builder.Args()...))
|
rows, err := i.client.Query(ctx, builder.String(), builder.Args()...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanInstance(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
// List implements [domain.InstanceRepository].
|
// List implements [domain.InstanceRepository].
|
||||||
func (i *instance) List(ctx context.Context, opts ...database.Condition) ([]*domain.Instance, error) {
|
func (i *instance) List(ctx context.Context, opts ...database.Condition) ([]domain.Instance, error) {
|
||||||
builder := database.StatementBuilder{}
|
builder := database.StatementBuilder{}
|
||||||
|
|
||||||
builder.WriteString(queryInstanceStmt)
|
builder.WriteString(queryInstanceStmt)
|
||||||
@@ -214,20 +221,8 @@ func (i *instance) writeCondition(
|
|||||||
condition.Write(builder)
|
condition.Write(builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanInstance(scanner database.Scanner) (*domain.Instance, error) {
|
func scanInstance(rows database.Rows) (*domain.Instance, error) {
|
||||||
var instance domain.Instance
|
instance, err := pgx.CollectOneRow[domain.Instance](rows, pgx.RowToStructByNameLax[domain.Instance])
|
||||||
err := scanner.Scan(
|
|
||||||
&instance.ID,
|
|
||||||
&instance.Name,
|
|
||||||
&instance.DefaultOrgID,
|
|
||||||
&instance.IAMProjectID,
|
|
||||||
&instance.ConsoleClientID,
|
|
||||||
&instance.ConsoleAppID,
|
|
||||||
&instance.DefaultLanguage,
|
|
||||||
&instance.CreatedAt,
|
|
||||||
&instance.UpdatedAt,
|
|
||||||
&instance.DeletedAt,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if no results returned, this is not a error
|
// if no results returned, this is not a error
|
||||||
// it just means the instance was not found
|
// it just means the instance was not found
|
||||||
@@ -241,29 +236,16 @@ func scanInstance(scanner database.Scanner) (*domain.Instance, error) {
|
|||||||
return &instance, nil
|
return &instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanInstances(rows database.Rows) ([]*domain.Instance, error) {
|
func scanInstances(rows database.Rows) ([]domain.Instance, error) {
|
||||||
instances := make([]*domain.Instance, 0)
|
instances, err := pgx.CollectRows[domain.Instance](rows, pgx.RowToStructByNameLax[domain.Instance])
|
||||||
for rows.Next() {
|
if err != nil {
|
||||||
|
// if no results returned, this is not a error
|
||||||
var instance domain.Instance
|
// it just means the instance was not found
|
||||||
err := rows.Scan(
|
// the caller should check if the returned instance is nil
|
||||||
&instance.ID,
|
if err.Error() == "no rows in result set" {
|
||||||
&instance.Name,
|
return nil, nil
|
||||||
&instance.DefaultOrgID,
|
|
||||||
&instance.IAMProjectID,
|
|
||||||
&instance.ConsoleClientID,
|
|
||||||
&instance.ConsoleAppID,
|
|
||||||
&instance.DefaultLanguage,
|
|
||||||
&instance.CreatedAt,
|
|
||||||
&instance.UpdatedAt,
|
|
||||||
&instance.DeletedAt,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
return nil, err
|
||||||
instances = append(instances, &instance)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return instances, nil
|
return instances, nil
|
||||||
}
|
}
|
||||||
|
@@ -58,13 +58,8 @@ func (u *user) List(ctx context.Context, opts ...database.QueryOption) (users []
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer rows.Close()
|
||||||
closeErr := rows.Close()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = closeErr
|
|
||||||
}()
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
user, err := scanUser(rows)
|
user, err := scanUser(rows)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user