Documentation ¶
Overview ¶
Package protoql includes common types that do not need to be generated, imported by generated code in protoc-gen-go-queries. These include interface types that database connections must implement, concrete types that the generated code uses, and any common errors that we may return.
Index ¶
Constants ¶
const NoRowsMsg = "protoql: no rows remaining"
NoRowsMsg is the message that ErrNoRows returns from its Error() method.
Variables ¶
ErrNoRows is the error that protoql uses when there are no rows left. This will be used to signal the end of results from an iterator in multi-row queries, or to signal that no rows were found in single-row queries.
Note: this variable may be altered by wrapper implementations (e.g. protopgx) in order to wrap multiple error values. For example, protopgx's init() will ensure that errors.Is(protoql.ErrNoRows, sql.ErrNoRows) and errors.Is(protoql.ErrNoRows, pgx.ErrNoRows) will both return true.
Functions ¶
This section is empty.
Types ¶
type Querier ¶
type Querier interface { Query(context.Context, string, ...any) (Rows, error) QueryRow(context.Context, string, ...any) Row Exec(context.Context, string, ...any) (Result, error) }
Querier is any type which can run queries in a database (i.e. a connection or a transaction).
type Result ¶
type Result interface {
RowsAffected() int
}
Result is the type required for write-only (i.e. zero-result) queries in protoql generated code.
type RowsIterator ¶
type RowsIterator[T any] struct { // contains filtered or unexported fields }
RowsIterator is an iterator that may be used to iterate over a result set. T is the type of data that will be returned for each row.
func NewRowsIterator ¶
func NewRowsIterator[T any](ctx context.Context, rows Rows, scan func(Rows) (T, error)) *RowsIterator[T]
NewRowsIterator returns an iterator to iterate over a set of rows. This is used in generated code to return specific row iterator types.
A goroutine will be kicked off to monitor ctx.Done(), calling rows.Close() when ctx.Done() is closed. This works around a common gotcha in go code where a missing `rows.Close()` causes SQL connection leaks. In generated code, ctx.Done will be checked to ensure that it is non-nil to prevent leaked goroutines.
To prevent goroutine leaks in long-running contexts, the goroutine will also be freed when either Close() is called or Next() returns an error (including ErrNoRows).
func (*RowsIterator[T]) Close ¶
func (i *RowsIterator[T]) Close()
Close closes the rows. It is a good practice to 'defer rows.Close()' in calling code.
To work around common gotchas, a missed `defer rows.Close()` will be caught when the constructor's context.Context is canceled. However, this is just a failsafe - callers should still `defer rows.Close()` (and test for that call).
func (*RowsIterator[T]) Err ¶
func (i *RowsIterator[T]) Err() error
Err returns the error (if any) that occurred while scanning.
func (*RowsIterator[T]) Next ¶
func (i *RowsIterator[T]) Next() (T, error)
Next scans and returns the next row from the result set.
If there was an error scanning, it will return the error and the rows will be closed.
If there was no error scanning but no rows are left, it returns ErrNoRows.
func (*RowsIterator[T]) Scan ¶
func (i *RowsIterator[T]) Scan() (T, error)
Scan returns the current row from the result set. This will return the value that was last returned by Next().
Most SQL driver implementations will error if Next() has not been called prior to Scan().
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
Package option contains the generated go code for protoql options.
|
Package option contains the generated go code for protoql options. |
Package protopgx includes wrapper logic to make a pgx connection type implement protoql.Store for use in generated code.
|
Package protopgx includes wrapper logic to make a pgx connection type implement protoql.Store for use in generated code. |
Package sample contains samples of .proto files and the resulting generated go code to run SQL queries using the options present in those .proto files.
|
Package sample contains samples of .proto files and the resulting generated go code to run SQL queries using the options present in those .proto files. |