sqlcommon

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package sqlcommon contains utility functions shared among all SQL data stores.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindLatestAuthorizationModel added in v1.5.0

func FindLatestAuthorizationModel(
	ctx context.Context,
	dbInfo *DBInfo,
	store string,
) (*openfgav1.AuthorizationModel, error)

FindLatestAuthorizationModel reads the latest authorization model corresponding to the store

func HandleSQLError

func HandleSQLError(err error, args ...interface{}) error

HandleSQLError processes an SQL error and converts it into a more specific error type based on the nature of the SQL error.

func IsReady added in v1.3.4

func IsReady(ctx context.Context, db *sql.DB) (storage.ReadinessStatus, error)

IsReady returns true if the connection to the datastore is successful and the datastore has the latest migration applied.

func ReadAuthorizationModel added in v1.3.3

func ReadAuthorizationModel(
	ctx context.Context,
	dbInfo *DBInfo,
	store, modelID string,
) (*openfgav1.AuthorizationModel, error)

ReadAuthorizationModel reads the model corresponding to store and model ID.

func Write

func Write(
	ctx context.Context,
	dbInfo *DBInfo,
	store string,
	deletes storage.Deletes,
	writes storage.Writes,
	now time.Time,
) error

Write provides the common method for writing to database across sql storage.

func WriteAuthorizationModel added in v1.3.3

func WriteAuthorizationModel(
	ctx context.Context,
	dbInfo *DBInfo,
	store string,
	model *openfgav1.AuthorizationModel,
) error

WriteAuthorizationModel writes an authorization model for the given store.

Types

type Config

type Config struct {
	Username               string
	Password               string
	Logger                 logger.Logger
	MaxTuplesPerWriteField int
	MaxTypesPerModelField  int

	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration

	ExportMetrics bool
}

Config defines the configuration parameters for setting up and managing a sql connection.

func NewConfig

func NewConfig(opts ...DatastoreOption) *Config

NewConfig creates a new Config instance with default values and applies any provided DatastoreOption modifications.

type ContToken

type ContToken struct {
	Ulid       string `json:"ulid"`
	ObjectType string `json:"ObjectType"`
}

ContToken represents a continuation token structure used in pagination.

func NewContToken

func NewContToken(ulid, objectType string) *ContToken

NewContToken creates a new instance of ContToken with the provided ULID and object type.

func UnmarshallContToken

func UnmarshallContToken(from string) (*ContToken, error)

UnmarshallContToken takes a string representation of a continuation token and attempts to unmarshal it into a ContToken struct.

type DBInfo

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

DBInfo encapsulates DB information for use in common method.

func NewDBInfo

func NewDBInfo(db *sql.DB, stbl sq.StatementBuilderType, sqlTime interface{}) *DBInfo

NewDBInfo constructs a DBInfo object.

type DatastoreOption

type DatastoreOption func(*Config)

DatastoreOption defines a function type used for configuring a Config object.

func WithConnMaxIdleTime

func WithConnMaxIdleTime(d time.Duration) DatastoreOption

WithConnMaxIdleTime returns a DatastoreOption that sets the maximum idle time for a connection in the Config.

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) DatastoreOption

WithConnMaxLifetime returns a DatastoreOption that sets the maximum lifetime for a connection in the Config.

func WithLogger

func WithLogger(l logger.Logger) DatastoreOption

WithLogger returns a DatastoreOption that sets the Logger in the Config.

func WithMaxIdleConns

func WithMaxIdleConns(c int) DatastoreOption

WithMaxIdleConns returns a DatastoreOption that sets the maximum number of idle connections in the Config.

func WithMaxOpenConns

func WithMaxOpenConns(c int) DatastoreOption

WithMaxOpenConns returns a DatastoreOption that sets the maximum number of open connections in the Config.

func WithMaxTuplesPerWrite

func WithMaxTuplesPerWrite(maxTuples int) DatastoreOption

WithMaxTuplesPerWrite returns a DatastoreOption that sets the maximum number of tuples per write in the Config.

func WithMaxTypesPerAuthorizationModel

func WithMaxTypesPerAuthorizationModel(maxTypes int) DatastoreOption

WithMaxTypesPerAuthorizationModel returns a DatastoreOption that sets the maximum number of types per authorization model in the Config.

func WithMetrics added in v1.3.5

func WithMetrics() DatastoreOption

WithMetrics returns a DatastoreOption that enables the export of metrics in the Config.

func WithPassword added in v1.1.0

func WithPassword(password string) DatastoreOption

WithPassword returns a DatastoreOption that sets the password in the Config.

func WithUsername added in v1.1.0

func WithUsername(username string) DatastoreOption

WithUsername returns a DatastoreOption that sets the username in the Config.

type SQLTupleIterator

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

SQLTupleIterator is a struct that implements the storage.TupleIterator interface for iterating over tuples fetched from a SQL database.

func NewSQLTupleIterator

func NewSQLTupleIterator(rows *sql.Rows) *SQLTupleIterator

NewSQLTupleIterator returns a SQL tuple iterator.

func (*SQLTupleIterator) Next

Next will return the next available item.

func (*SQLTupleIterator) Stop

func (t *SQLTupleIterator) Stop()

Stop terminates iteration.

func (*SQLTupleIterator) ToArray

func (t *SQLTupleIterator) ToArray(
	opts storage.PaginationOptions,
) ([]*openfgav1.Tuple, []byte, error)

ToArray converts the tupleIterator to an []*openfgav1.Tuple and a possibly empty continuation token. If the continuation token exists it is the ulid of the last element of the returned array.

Jump to

Keyboard shortcuts

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