pgxiter

package module
v0.0.0-...-8e7eeb1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2024 License: MIT Imports: 5 Imported by: 0

README

pgxiter

A cursor based iterator for pgx

Go Reference

Getting Started

You can use these examples to get started.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor is a wrapper around pgx.Cursor.

func (*Cursor) Close

func (r *Cursor) Close()

Close implements pgx.Rows.

func (*Cursor) CommandTag

func (r *Cursor) CommandTag() pgconn.CommandTag

CommandTag implements pgx.Rows.

func (*Cursor) Conn

func (r *Cursor) Conn() *pgx.Conn

Conn implements pgx.Rows.

func (*Cursor) Err

func (r *Cursor) Err() error

Err implements pgx.Rows.

func (*Cursor) FieldDescriptions

func (r *Cursor) FieldDescriptions() []pgconn.FieldDescription

FieldDescriptions implements pgx.Rows.

func (*Cursor) Next

func (r *Cursor) Next() bool

Next implements pgx.Rows.

func (*Cursor) RawValues

func (r *Cursor) RawValues() [][]byte

RawValues implements pgx.Rows.

func (*Cursor) Scan

func (r *Cursor) Scan(dest ...any) error

Scan implements pgx.Rows.

func (*Cursor) Values

func (r *Cursor) Values() ([]any, error)

Values implements pgx.Rows.

type Querier

type Querier struct {
	// Capacity is the maximum number of rows to fetch for each iteration.
	Capacity int
	// Querier is the interface that wraps the Query method.
	Querier Queryable
}

Querier represents a PostgreSQL cursor querier.

Example
config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
	panic(err)
}

conn, err := pgxpool.NewWithConfig(context.TODO(), config)
if err != nil {
	panic(err)
}
// close the pool
defer conn.Close()

querier := &pgxiter.Querier{Querier: conn}
// start the cursor
rows, err := querier.Query(context.TODO(), "SELECT * FROM user")
if err != nil {
	panic(err)
}
// close the cursor
defer rows.Close()

// User represents a user.
type User struct {
	Name     string `db:"name"`
	Password string `db:"password"`
}

for rows.Next() {
	user, err := pgx.RowToStructByName[User](rows)
	if err != nil {
		panic(err)
	}

	fmt.Println(user.Name)
}
Output:

func (*Querier) Query

func (c *Querier) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)

Query executes a query that returns rows.

type Queryable

type Queryable interface {
	// Begin starts a pseudo nested transaction.
	Begin(ctx context.Context) (pgx.Tx, error)
	// Exec executes a query that doesn't return rows.
	Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)
	// Query executes a query that returns rows.
	Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)
}

Queryable is the interface that wraps the Query method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL