db

package module
v0.0.0-...-764e792 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 10 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DropAll

func DropAll(schemaURL string) error

DropAll drops all tables in the database schemaURL is the path to the directory containing the migration files e.g. file://../db/migration

Returns an error if the drop failed

func Finalize

func Finalize() error

func MigrateIfNeeded

func MigrateIfNeeded(schemaURL string) (bool, error)

MigrateIfNeeded checks if the database schema needs to be migrated and performs the migration if needed. schemaURL is the path to the directory containing the migration files e.g. file://../db/migration

Returns true if the migration was performed, false if no migration was needed

Types

type Account

type Account struct {
	ID int32
	// 3 characters, case-sensitive
	Prefix   string
	Username string
	Email    string
	// Hashed password
	HashedPassword string
	// A flag that enables/disables the account and its urls
	Enabled bool
	// Timestamp of creation
	CreatedAt sql.NullTime
	// Timestamp of last update
	UpdatedAt sql.NullTime
}

Table holding Account information

type AccountRepository

type AccountRepository interface {
	CheckPrefixExists(ctx context.Context, prefix string) (bool, error)
	CheckUsernameExists(ctx context.Context, username string) (bool, error)
	GetAccountByPrefix(ctx context.Context, prefix string) (Account, error)
	GetAccountByUsername(ctx context.Context, username string) (Account, error)
	InsertNewAccount(ctx context.Context, arg InsertNewAccountParams) error
	UpdateAccountStatusByUsername(ctx context.Context, arg UpdateAccountStatusByUsernameParams) error
}

type CheckShortUrlKeyExistsParams

type CheckShortUrlKeyExistsParams struct {
	ShortUrlKey sql.NullString
	AccountID   int32
}

type Click

type Click struct {
	ID     int32
	LinkID int32
	// Timestamp of click
	ClickDateTime sql.NullTime
	UserAgent     sql.NullString
	IpAddress     sql.NullString
}

Table holding click information

type ClickRepository

type ClickRepository interface {
	InsertNewClick(ctx context.Context, arg InsertNewClickParams) error
}

type DBConf

type DBConf struct {
	DriverName     string
	DataSourceName string
	MaxIdleConns   int
	MaxOpenConns   int
	MaxIdleTime    time.Duration
	MaxLifeTime    time.Duration
}

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

type GetLinkByAccountIDAndShortURLKeyParams

type GetLinkByAccountIDAndShortURLKeyParams struct {
	AccountID   int32
	ShortUrlKey sql.NullString
}

type InsertNewAccountParams

type InsertNewAccountParams struct {
	Prefix         string
	Username       string
	Email          string
	HashedPassword string
}

type InsertNewClickParams

type InsertNewClickParams struct {
	LinkID    int32
	UserAgent sql.NullString
	IpAddress sql.NullString
}

type InsertNewLinkParams

type InsertNewLinkParams struct {
	ShortUrlKey sql.NullString
	AccountID   int32
	LongUrl     string
}
type Link struct {
	ID int32
	// 6 characters, case-sensitive
	ShortUrlKey sql.NullString
	AccountID   int32
	LongUrl     string
	// A flag to enable/disable the url redirection
	Enabled bool
	// A flag that enable/disable url tracking on redirection
	TrackingEnabled bool
	// Timestamp of creation
	CreatedAt sql.NullTime
	// Timestamp of last update
	UpdatedAt sql.NullTime
}

Table holding Link information

type LinkRepository

type LinkRepository interface {
	CheckShortUrlKeyExists(ctx context.Context, arg CheckShortUrlKeyExistsParams) (bool, error)
	GetLinkByAccountIDAndShortURLKey(ctx context.Context, arg GetLinkByAccountIDAndShortURLKeyParams) (Link, error)
	InsertNewLink(ctx context.Context, arg InsertNewLinkParams) error
	UpdateLinkLongURL(ctx context.Context, arg UpdateLinkLongURLParams) error
	UpdateLinkStatus(ctx context.Context, arg UpdateLinkStatusParams) error
	UpdateLinkTrackingStatus(ctx context.Context, arg UpdateLinkTrackingStatusParams) error
}

type Queries

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

func New

func New(db DBTX) *Queries

func (*Queries) CheckPrefixExists

func (q *Queries) CheckPrefixExists(ctx context.Context, prefix string) (bool, error)

func (*Queries) CheckShortUrlKeyExists

func (q *Queries) CheckShortUrlKeyExists(ctx context.Context, arg CheckShortUrlKeyExistsParams) (bool, error)

func (*Queries) CheckUsernameExists

func (q *Queries) CheckUsernameExists(ctx context.Context, username string) (bool, error)

func (*Queries) GetAccountByPrefix

func (q *Queries) GetAccountByPrefix(ctx context.Context, prefix string) (Account, error)

func (*Queries) GetAccountByUsername

func (q *Queries) GetAccountByUsername(ctx context.Context, username string) (Account, error)

func (*Queries) GetLinkByAccountIDAndShortURLKey

func (q *Queries) GetLinkByAccountIDAndShortURLKey(ctx context.Context, arg GetLinkByAccountIDAndShortURLKeyParams) (Link, error)

func (*Queries) InsertNewAccount

func (q *Queries) InsertNewAccount(ctx context.Context, arg InsertNewAccountParams) error

func (*Queries) InsertNewClick

func (q *Queries) InsertNewClick(ctx context.Context, arg InsertNewClickParams) error
func (q *Queries) InsertNewLink(ctx context.Context, arg InsertNewLinkParams) error

func (*Queries) UpdateAccountStatusByUsername

func (q *Queries) UpdateAccountStatusByUsername(ctx context.Context, arg UpdateAccountStatusByUsernameParams) error

func (*Queries) UpdateLinkLongURL

func (q *Queries) UpdateLinkLongURL(ctx context.Context, arg UpdateLinkLongURLParams) error

func (*Queries) UpdateLinkStatus

func (q *Queries) UpdateLinkStatus(ctx context.Context, arg UpdateLinkStatusParams) error

func (*Queries) UpdateLinkTrackingStatus

func (q *Queries) UpdateLinkTrackingStatus(ctx context.Context, arg UpdateLinkTrackingStatusParams) error

func (*Queries) WithTx

func (q *Queries) WithTx(tx *sql.Tx) *Queries

type SQLStore

type SQLStore struct {
	*Queries
	DB *sql.DB
}

func GetStoreInstance

func GetStoreInstance(conf DBConf) (*SQLStore, error)

GetStoreInstance returns a singleton instance of the SQLStore If the instance is not initialized, it will initialize it with the provided configuration If the instance is already initialized, it will return the existing instance

func (*SQLStore) Transactional

func (store *SQLStore) Transactional(ctx context.Context, fn func(queries *Queries) error) error

type Store

type Store interface {
	AccountRepository
	LinkRepository
	ClickRepository
	Transactional(ctx context.Context, fn func(queries *Queries) error) error
}

type UpdateAccountStatusByUsernameParams

type UpdateAccountStatusByUsernameParams struct {
	Enabled  bool
	Username string
}

type UpdateLinkLongURLParams

type UpdateLinkLongURLParams struct {
	LongUrl string
	ID      int32
}

type UpdateLinkStatusParams

type UpdateLinkStatusParams struct {
	Enabled bool
	ID      int32
}

type UpdateLinkTrackingStatusParams

type UpdateLinkTrackingStatusParams struct {
	TrackingEnabled bool
	ID              int32
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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