Documentation
¶
Overview ¶
Package sql provides a SQL database storage implementation.
Index ¶
- type DB
- type SQLXWrapper
- type Storage
- func (s *Storage) Close(_ context.Context)
- func (s *Storage) Connect(ctx context.Context) error
- func (s *Storage) CreateEvent(ctx context.Context, event *types.Event) (*types.Event, error)
- func (s *Storage) DeleteEvent(ctx context.Context, id uuid.UUID) error
- func (s *Storage) DeleteOldEvents(ctx context.Context, date time.Time) (int64, error)
- func (s *Storage) GetAllUserEvents(ctx context.Context, userID string) ([]*types.Event, error)
- func (s *Storage) GetEvent(ctx context.Context, id uuid.UUID) (*types.Event, error)
- func (s *Storage) GetEventsForDay(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)
- func (s *Storage) GetEventsForMonth(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)
- func (s *Storage) GetEventsForNotification(ctx context.Context) ([]*types.Event, error)
- func (s *Storage) GetEventsForPeriod(ctx context.Context, dateStart, dateEnd time.Time, userID *string) ([]*types.Event, error)
- func (s *Storage) GetEventsForWeek(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)
- func (s *Storage) UpdateEvent(ctx context.Context, id uuid.UUID, data *types.EventData) (*types.Event, error)
- func (s *Storage) UpdateNotifiedEvents(ctx context.Context, notifiedEvents []uuid.UUID) (int64, error)
- type StorageOption
- type Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB interface {
ConnectContext(ctx context.Context, driverName, dataSourceName string) (*sqlx.DB, error)
BeginTxx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
Close()
}
DB defines the methods of sqlx.DB used in storage/sql.
type SQLXWrapper ¶
type SQLXWrapper struct {
// contains filtered or unexported fields
}
func (*SQLXWrapper) ConnectContext ¶
func (w *SQLXWrapper) ConnectContext(ctx context.Context, driverName, dataSourceName string) (*sqlx.DB, error)
ConnectContext implements DB.ConnectContext.
func (*SQLXWrapper) SetDB ¶
func (w *SQLXWrapper) SetDB(db *sqlx.DB)
SetDB implements DB.SetDB for testing purposes.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage represents a SQL database storage.
func NewStorage ¶
func NewStorage(timeout time.Duration, driver, host, port, user, password, dbname string, opts ...StorageOption, ) (*Storage, error)
NewStorage creates a new Storage instance based on the given args.
If the arguments are empty, it returns an error.
The function constructs a DSN based on the given arguments and the default driver. No connection is established upon the call.
Currently supported drivers are "postgres" or "postgresql".
func (*Storage) Close ¶
Close closes the connection to the database. Method is safe to call multiple times. No errors are returned.
func (*Storage) Connect ¶
Connect connects to the database.
If the connection is successful, it pings the database to check if the connection is alive. If any error occurs during the connection or pinging, it returns an error.
func (*Storage) CreateEvent ¶
CreateEvent creates a new event in the database. Method uses context with timeout set for Storage.
Returns a wrapped ErrNoData error if no data passed.
If the query is successful but the given ID is already present in the DB, it returns ErrDataExists.
Method uses transaction to ensure the atomicity of the operation over DB.
func (*Storage) DeleteEvent ¶
DeleteEvent deletes the event with the given ID from the database. Method uses context with timeout set for Storage.
If the query is successful but the given ID is not present in the DB, it returns ErrNotExists.
Method uses transaction to ensure the atomicity of the operation over DB.
func (*Storage) DeleteOldEvents ¶
DeleteOldEvents deletes all events older than the given date from the database. Returns the number of deleted events and nil on success, 0 and any error otherwise.
func (*Storage) GetAllUserEvents ¶
GetAllUserEvents retrieves all events for a given user ID from the database. The method uses a transaction with a context and timeouts as configured in Storage.
Returns a slice of Event pointers and nil on success, or nil and any error encountered during the transaction. If no events for the given user ID are found, it returns (nil, ErrEventNotFound).
func (*Storage) GetEvent ¶
GetEvent retrieves an event with the specified ID from the database. The method uses a transaction with a context and timeouts as configured in Storage.
Returns a pointer to the Event and nil on success, or nil and any error encountered during the transaction. If no event with the given ID is found, it returns (nil, ErrEventNotFound).
func (*Storage) GetEventsForDay ¶
func (s *Storage) GetEventsForDay(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)
GetEventsForDay retrieves all events occurring on the specified date from the database. The method uses a transaction with a context and timeouts as configured in Storage.
It fetches events where the datetime falls within the start and end of the given date, ordered by datetime in ascending order. Method truncates any given date to the start of the day.
It accepts an optional userID parameter to filter events by user ID.
Returns a slice of Event pointers and nil on success. If no events are found, it returns (nil, ErrEventNotFound). Returns nil and any error encountered during the transaction or query execution.
func (*Storage) GetEventsForMonth ¶
func (s *Storage) GetEventsForMonth(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)
GetEventsForMonth retrieves all events occurring on the calendar month of the specified date from the database. The method uses a transaction with a context and timeouts as configured in Storage.
It fetches events where the datetime falls within the start and end of the calendar month of the given date, ordered by datetime in ascending order. Method truncates any given date to the start of the calendar month.
It accepts an optional userID parameter to filter events by user ID.
Returns a slice of Event pointers and nil on success. If no events are found, it returns (nil, ErrEventNotFound). Returns nil and any error encountered during the transaction or query execution.
func (*Storage) GetEventsForNotification ¶
GetEventsForNotification retrieves all events, which require notification. The method uses a transaction with a context and timeouts as configured in Storage.
Returns a slice of Event pointers and nil on success, or nil and any error encountered during the transaction. If no events for the given user ID are found, it returns (nil, ErrEventNotFound).
func (*Storage) GetEventsForPeriod ¶
func (s *Storage) GetEventsForPeriod(ctx context.Context, dateStart, dateEnd time.Time, userID *string, ) ([]*types.Event, error)
GetEventsForPeriod retrieves all events occurring on the given period from the database. The method uses a transaction with a context and timeouts as configured in Storage.
It fetches events where the datetime falls within the start and end of the given period, ordered by datetime in ascending order.
It accepts an optional userID parameter to filter events by user ID.
Returns a slice of Event pointers and nil on success. If no events are found, it returns (nil, ErrEventNotFound). Returns nil and any error encountered during the transaction or query execution.
func (*Storage) GetEventsForWeek ¶
func (s *Storage) GetEventsForWeek(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)
GetEventsForWeek retrieves all events occurring on the calendar week of the specified date from the database. The method uses a transaction with a context and timeouts as configured in Storage.
It fetches events where the datetime falls within the start and end of the calendar week of the given date, ordered by datetime in ascending order. Method truncates any given date to the start of the calendar week.
It accepts an optional userID parameter to filter events by user ID.
Returns a slice of Event pointers and nil on success. If no events are found, it returns (nil, ErrEventNotFound). Returns nil and any error encountered during the transaction or query execution.
func (*Storage) UpdateEvent ¶
func (s *Storage) UpdateEvent(ctx context.Context, id uuid.UUID, data *types.EventData) (*types.Event, error)
UpdateEvent updates the event with the given ID in the database. Method uses context with timeout set for Storage.
Returns a wrapped ErrNoData error if no data passed.
If the query is successful but the given ID is not present in the DB, it returns ErrNotExists.
Method uses transaction to ensure the atomicity of the operation over DB.
func (*Storage) UpdateNotifiedEvents ¶
func (s *Storage) UpdateNotifiedEvents(ctx context.Context, notifiedEvents []uuid.UUID) (int64, error)
UpdateNotifiedEvents updates the events with the given IDs in the database as notified ones.
If some of the IDs are not found in the DB, they will be ignored.
Returns the number of updated events and nil on success, 0 and any error otherwise.
type StorageOption ¶
type StorageOption func(s *Storage)
StorageOption defines a function that allows to configure underlying Storage DB on construction. Use it for testing purposes to inject a custom DB implementation.
func WithDB ¶
func WithDB(db DB) StorageOption
WithDB allows to inject a custom DB implementation (for testing).
type Tx ¶
type Tx interface {
GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Commit() error
Rollback() error
}
Tx defines the methods of sqlx.Tx used in storage/sql.