Documentation
¶
Overview ¶
Package psql is the Postgresql storage layer of Qvain.
Index ¶
- Constants
- Variables
- func NewError(text string) error
- type BatchManager
- func (b *BatchManager) Commit() error
- func (b *BatchManager) Create(dataset *models.Dataset) error
- func (b *BatchManager) CreateWithMetadata(dataset *models.Dataset) error
- func (b *BatchManager) Delete(id uuid.UUID) error
- func (b *BatchManager) Rollback()
- func (b *BatchManager) Update(id uuid.UUID, blob []byte) error
- func (b *BatchManager) UpdateSynced(id uuid.UUID) error
- func (b *BatchManager) Upsert(data *models.Dataset) error
- type DB
- func (db *DB) BatchStore(datasets []*models.Dataset) error
- func (psql *DB) Begin() (*Tx, error)
- func (db *DB) ChangeOwnerTo(id uuid.UUID, uid uuid.UUID) error
- func (psql *DB) Check() error
- func (db *DB) CheckOwner(id uuid.UUID, owner uuid.UUID) (err error)
- func (db *DB) Clone(id uuid.UUID, newid uuid.UUID, blob []byte) error
- func (psql *DB) Connect() (*pgx.Conn, error)
- func (db *DB) CountDatasets(filter *DatasetFilter) (json.RawMessage, error)
- func (db *DB) Create(dataset *models.Dataset) error
- func (db *DB) CreateWithMetadata(dataset *models.Dataset) error
- func (db *DB) Delete(id uuid.UUID, owner *uuid.UUID) error
- func (db *DB) ExportAsJson(id uuid.UUID) (json.RawMessage, error)
- func (db *DB) Get(id uuid.UUID) (*models.Dataset, error)
- func (db *DB) GetAllForUid(uid uuid.UUID) ([]*models.Dataset, error)
- func (db *DB) GetIdentityForUid(svc string, uid uuid.UUID) (id string, err error)
- func (db *DB) GetLastSync(uid uuid.UUID) (time.Time, error)
- func (db *DB) GetUidForIdentity(svc, id string) (uid uuid.UUID, err error)
- func (db *DB) GetWithOwner(id uuid.UUID, owner uuid.UUID) (*models.Dataset, error)
- func (psql *DB) InitPool() (err error)
- func (db *DB) ListAllForUid(uid uuid.UUID) ([]*models.Dataset, error)
- func (psql *DB) Log(plevel pgx.LogLevel, msg string, data map[string]interface{})
- func (db *DB) LookupByFairdataIdentifier(fdid string) (uuid.UUID, error)
- func (db *DB) LookupByQvainId(id uuid.UUID) (bool, error)
- func (psql *DB) MustConnect() *pgx.Conn
- func (db *DB) NewBatch() (*BatchManager, error)
- func (db *DB) NewBatchForUser(uid uuid.UUID) (*BatchManager, error)
- func (db *DB) Patch(id uuid.UUID, blob []byte) error
- func (db *DB) PatchWithOwner(id uuid.UUID, blob []byte, owner uuid.UUID) error
- func (db *DB) RegisterIdentity(svc, id string) (uid uuid.UUID, isNew bool, err error)
- func (psql *DB) SetLogger(logger zerolog.Logger)
- func (db *DB) SmartGetWithOwner(id uuid.UUID, owner uuid.UUID) (*models.Dataset, error)
- func (db *DB) SmartUpdateWithOwner(id uuid.UUID, blob []byte, owner uuid.UUID) error
- func (db *DB) StoreNewVersion(id uuid.UUID, basedOn uuid.UUID, created time.Time, blob []byte) error
- func (db *DB) StorePublished(id uuid.UUID, blob []byte, synced time.Time) error
- func (db *DB) Update(id uuid.UUID, blob []byte) error
- func (db *DB) UpdateWithOwner(id uuid.UUID, blob []byte, owner uuid.UUID) error
- func (psql *DB) Version() (string, error)
- func (db *DB) ViewDatasetInfoByIdentifier(identifierType string, identifier string) (json.RawMessage, error)
- func (db *DB) ViewDatasetWithOwner(id uuid.UUID, owner uuid.UUID, svc string) (json.RawMessage, error)
- func (db *DB) ViewDatasetsByOwner(owner uuid.UUID) (json.RawMessage, error)
- func (db *DB) ViewVersions(owner uuid.UUID, dataset uuid.UUID) (json.RawMessage, error)
- func (db *DB) WithTransaction(f func(tx *Tx) error) error
- type DatabaseError
- type DatasetFilter
- type TimeFilter
- type Tx
- type WhereBuilder
Constants ¶
const ( CompareEq = iota // == CompareLe = iota // <= CompareGe = iota // >= CompareLt = iota // < CompareGt = iota // > )
Constants for comparison types.
const DefaultPoolAcquireTimeout = 10 * time.Second
DefaultPoolAcquireTimeout is the duration pgx waits for a connection to become available from the pool.
Variables ¶
var ( ErrExists = NewError("exists") ErrNotFound = NewError("not found") ErrNotOwner = NewError("not owner") ErrInvalidJson = NewError("invalid json") ErrNotImplemented = NewError("not implemented") )
Errors exported by the database layer.
var ( ErrTemporary = NewError("temporary database error") ErrTimeout = NewError("database timeout") ErrConnection = NewError("database connection error") )
Errors from the underlying database connection.
var ComparisonSuffixes = map[string]int{ "": CompareEq, "_eq": CompareEq, "_le": CompareLe, "_ge": CompareGe, "_lt": CompareLt, "_gt": CompareGt, }
ComparisonSuffixes are used to determine the type of comparison used in a query parameter.
var DatasetFilterGroupByPaths = map[string]string{
"schema": `schema`,
"organization": `blob->>'metadata_provider_org' as organization`,
"access_type": `blob#>>'{"research_dataset","access_rights","access_type","identifier"}' as access_type`,
"date_created": `date_trunc('day', created$tz) as created`,
}
DatasetFilterGroupByPaths provides paths/values for grouping datasets. Keys contained in this map are the only valid options for group_by.
var TimeZoneRegex = regexp.MustCompile(`(Z|([\+-]\d\d:\d\d))$`)
TimeZoneRegex is used to determine if a date string ends with a timezone offset.
Functions ¶
Types ¶
type BatchManager ¶
type BatchManager struct {
// contains filtered or unexported fields
}
func (*BatchManager) Commit ¶
func (b *BatchManager) Commit() error
func (*BatchManager) CreateWithMetadata ¶
func (b *BatchManager) CreateWithMetadata(dataset *models.Dataset) error
func (*BatchManager) Rollback ¶
func (b *BatchManager) Rollback()
func (*BatchManager) UpdateSynced ¶
func (b *BatchManager) UpdateSynced(id uuid.UUID) error
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB holds the database methods and configuration.
func NewPoolService ¶
NewPoolService creates a psql service from a connection string and initialises the connection pool.
func NewPoolServiceFromEnv ¶
NewPoolService creates a psql service using environment variables and initialises the connection pool.
func NewService ¶
NewService returns a database handle configured with the given connection string. It does not try to connect.
func (*DB) BatchStore ¶
BatchStore takes a list of datasets and stores them as new datasets.
func (*DB) ChangeOwnerTo ¶
ChangeOwnerTo updates a dataset's owner.
func (*DB) CheckOwner ¶
CheckOwner calls tx.CheckOwner to check if the record exists and is owner by the given user.
func (*DB) CountDatasets ¶
func (db *DB) CountDatasets(filter *DatasetFilter) (json.RawMessage, error)
CountDatasets gives the number of datasets matching the DatasetFilter. Returns a json object containing the count. If grouping is enabled, returns a json array containing the grouped results.
func (*DB) Create ¶
Create creates a new dataset. It is a convenience wrapper for the Create method on transactions.
func (*DB) CreateWithMetadata ¶
CreateWithMetadata creates a new dataset with extra metadata instead of default values.
func (*DB) ExportAsJson ¶
func (*DB) GetAllForUid ¶
GetAllForUid returns all datasets for a given user.
func (*DB) GetIdentityForUid ¶
GetIdentityForUid gets the identity for a given uid and service.
func (*DB) GetUidForIdentity ¶
GetUidForIdentity gets the application uid for a given external service and identity.
Note that identities need not be unique, though those used for login ought to be.
func (*DB) GetWithOwner ¶
GetWithOwner retrieves a dataset from the database if the owner matches.
func (*DB) ListAllForUid ¶
ListAllForUid returns the list of datasets for a given user.
func (*DB) LookupByFairdataIdentifier ¶
LookupByFairdataIdentifier returns the Qvain id for a given Fairdata identifier.
func (*DB) MustConnect ¶
MustConnect returns a single database conn and panics on failure.
func (*DB) NewBatch ¶
func (db *DB) NewBatch() (*BatchManager, error)
func (*DB) NewBatchForUser ¶
func (db *DB) NewBatchForUser(uid uuid.UUID) (*BatchManager, error)
func (*DB) PatchWithOwner ¶
PatchWithOwner patches a dataset JSON blob with ownership checks.
func (*DB) RegisterIdentity ¶
RegisterIdentity takes an external service and identity and gets the corresponding application uid, creating it if necessary. Returns either a UUID uid and a boolean indicating if the account has been newly created or an error.
Note that while external ids are not guaranteed to be unique and might refer to multiple application users, this function allows only unique registrations as it creates real (login) users mapped to external accounts.
func (*DB) SetLogger ¶
SetLogger assigns a zerolog logger to the database service. It is not safe to call this function after initialisation.
func (*DB) SmartGetWithOwner ¶
func (*DB) SmartUpdateWithOwner ¶
func (*DB) StoreNewVersion ¶
func (db *DB) StoreNewVersion(id uuid.UUID, basedOn uuid.UUID, created time.Time, blob []byte) error
StoreNewVersion wraps a StoreNewVersion transaction.
func (*DB) StorePublished ¶
StorePublished saves a published dataset to the database and marks it as published. TODO: handle empty blob
func (*DB) UpdateWithOwner ¶
UpdateWithOwner updates a dataset with ownership checks.
func (*DB) ViewDatasetInfoByIdentifier ¶ added in v0.10.3
func (db *DB) ViewDatasetInfoByIdentifier(identifierType string, identifier string) (json.RawMessage, error)
ViewDatasetInfoByIdentifier gives basic information for a single dataset with a given identifier.
func (*DB) ViewDatasetWithOwner ¶
func (*DB) ViewDatasetsByOwner ¶
ViewDatasetsByOwner builds a JSON array with the datasets for a given owner.
func (*DB) ViewVersions ¶
ViewVersions returns a (JSON) array with existing versions for a given dataset and owner.
type DatabaseError ¶
type DatabaseError struct {
// contains filtered or unexported fields
}
DatabaseError is a trivial implementation of error.
func (*DatabaseError) Error ¶
func (e *DatabaseError) Error() string
Error satisfies Go's Error interface.
type DatasetFilter ¶
type DatasetFilter struct { OnlyDrafts bool // only drafts are counted OnlyPublished bool // only published are counted OnlyAtt bool // only datasets in ATT catalog are counted OnlyIda bool // only datasets in IDA catalog are counted DateCreated []TimeFilter // filters by creation date User string // filter by user (metadata_provider_user) Organization string // filter by organization (metadata_provider_org) GroupBy string // group values, see DatasetFilterGroupByPaths for valid options // options for testing, not currently exposed in the stats API QvainOwner string // qvain id of owner GroupTimeZone string // time zone used in grouping dates, supported values are "" (local) and "UTC" }
DatasetFilter is used to specify filtering options for datasets.
func (*DatasetFilter) GroupByPath ¶
func (filter *DatasetFilter) GroupByPath() string
GroupByPath returns data path to use in GROUP BY statement.
func (*DatasetFilter) Where ¶
func (filter *DatasetFilter) Where() (string, []interface{})
Where returns the WHERE statement for the filter.
type TimeFilter ¶
TimeFilter contains data for time-based filtering. The timefilter represents a comparison performed on a time range defined by: start ≤ time < end
func ParseTimeFilter ¶
func ParseTimeFilter(suffix string, timeString string) TimeFilter
ParseTimeFilter parses a time string for a given query suffix.
The suffix is used to determine the comparison used. For "param", the options are: - param equal - param_eq equal - param_le less than or equal - param_ge greater than or equal - param_lt less than - param_gt greater than
The function accepts RFC3339 dates. In addition, the date can be truncated to desired precision and timezone offset is optional. If timezone offset is not specified, the local timezone is assumed. - year: 2019 - month: 2019-08 - date: 2019-08-27 - hours: 2019-08-27T13 - minutes: 2019-08-27T13:30 - seconds: 2019-08-27T13:30:00 Comparisons will be made with the used precision, e.g. equality for a date without hours given will represent a 24 hour range starting from the date.
Timezone offset is either Z for UTC, or +/-hh:mm. Both escaped and unscaped query parameters should work, and space can be used instead of +.
func (*TimeFilter) IsZero ¶
func (t *TimeFilter) IsZero() bool
IsZero tells if the time contained has the zero time value.
func (*TimeFilter) Start ¶
func (t *TimeFilter) Start() time.Time
Start is the start of the time range.
type Tx ¶
func (*Tx) CheckOwner ¶
CheckOwner returns an error if the record is not owned by the given user.
type WhereBuilder ¶
type WhereBuilder struct {
// contains filtered or unexported fields
}
WhereBuilder is a helper object for creating SQL WHERE statements.
func (*WhereBuilder) MaybeAdd ¶
func (w *WhereBuilder) MaybeAdd(enabled bool, query string)
MaybeAdd adds query to conditions if enabled is true.
func (*WhereBuilder) MaybeAddString ¶
func (w *WhereBuilder) MaybeAddString(s string, query string)
MaybeAddString adds query to conditions if s is non-empty. The s argument is added as a query parameter and $ in the query string is replaced with its position.
func (*WhereBuilder) MaybeAddTimeFilter ¶
func (w *WhereBuilder) MaybeAddTimeFilter(filter TimeFilter, query string)
MaybeAddTimeFilter adds a time condition if !TimeFilter.IsZero(). Time comparisons are performed against the query argument.
func (*WhereBuilder) Where ¶
func (w *WhereBuilder) Where() (string, []interface{})
Where returns the WHERE statement.