db

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

nolint

nolint

nolint

Index

Constants

View Source
const (
	SearchTypeEquals = iota
	SearchTypeNull
	SearchTypeGE
	SearchTypeLE
	SearchTypeGreater
	SearchTypeLess
	SearchTypeLike
	SearchTypeILike
	SearchTypeArray
	SearchTypeArrayContains
	SearchTypeArrayContained
	SearchTypeArrayIntersect
	SearchTypeJsonbPath
)
View Source
const (
	ErrEmptyValue = "empty"
	ErrMaxLength  = "len"
	ErrWrongValue = "value"
)
View Source
const (
	// common statuses
	StatusEnabled  = 1
	StatusDisabled = 2
	StatusDeleted  = 3
)
View Source
const TableColumns = "t.*"
View Source
const TablePrefix = "t"

Variables

View Source
var (
	StatusFilter        = Filter{Field: "statusId", Value: []int{StatusEnabled, StatusDisabled}, SearchType: SearchTypeArray}
	StatusEnabledFilter = Filter{Field: "statusId", Value: []int{StatusEnabled}, SearchType: SearchTypeArray}
)
View Source
var (
	PagerDefault = Pager{PageSize: defaultMaxLimit}
	PagerNoLimit = Pager{PageSize: defaultNoLimit}
	PagerOne     = Pager{PageSize: 1}
	PagerTwo     = Pager{PageSize: 2}
)
View Source
var Columns = struct {
	VfsFile struct {
		ID, FolderID, Title, Path, Params, IsFavorite, MimeType, FileSize, FileExists, CreatedAt, StatusID string

		Folder string
	}
	VfsFolder struct {
		ID, ParentFolderID, Title, IsFavorite, CreatedAt, StatusID string

		ParentFolder string
	}
	VfsHash struct {
		Hash, Namespace, Extension, FileSize, Width, Height, Blurhash, CreatedAt, IndexedAt, Error string
	}
}{
	VfsFile: struct {
		ID, FolderID, Title, Path, Params, IsFavorite, MimeType, FileSize, FileExists, CreatedAt, StatusID string

		Folder string
	}{
		ID:         "fileId",
		FolderID:   "folderId",
		Title:      "title",
		Path:       "path",
		Params:     "params",
		IsFavorite: "isFavorite",
		MimeType:   "mimeType",
		FileSize:   "fileSize",
		FileExists: "fileExists",
		CreatedAt:  "createdAt",
		StatusID:   "statusId",

		Folder: "Folder",
	},
	VfsFolder: struct {
		ID, ParentFolderID, Title, IsFavorite, CreatedAt, StatusID string

		ParentFolder string
	}{
		ID:             "folderId",
		ParentFolderID: "parentFolderId",
		Title:          "title",
		IsFavorite:     "isFavorite",
		CreatedAt:      "createdAt",
		StatusID:       "statusId",

		ParentFolder: "ParentFolder",
	},
	VfsHash: struct {
		Hash, Namespace, Extension, FileSize, Width, Height, Blurhash, CreatedAt, IndexedAt, Error string
	}{
		Hash:      "hash",
		Namespace: "namespace",
		Extension: "extension",
		FileSize:  "fileSize",
		Width:     "width",
		Height:    "height",
		Blurhash:  "blurhash",
		CreatedAt: "createdAt",
		IndexedAt: "indexedAt",
		Error:     "error",
	},
}
View Source
var Tables = struct {
	VfsFile struct {
		Name, Alias string
	}
	VfsFolder struct {
		Name, Alias string
	}
	VfsHash struct {
		Name, Alias string
	}
}{
	VfsFile: struct {
		Name, Alias string
	}{
		Name:  "vfsFiles",
		Alias: "t",
	},
	VfsFolder: struct {
		Name, Alias string
	}{
		Name:  "vfsFolders",
		Alias: "t",
	},
	VfsHash: struct {
		Name, Alias string
	}{
		Name:  "vfsHashes",
		Alias: "t",
	},
}

Functions

This section is empty.

Types

type DB

type DB struct {
	*pg.DB
	// contains filtered or unexported fields
}

DB stores db connection

func New

func New(db *pg.DB) DB

New is a function that returns DB as wrapper on postgres connection.

func (DB) CopyHashesFromSTDIN

func (db DB) CopyHashesFromSTDIN(tx *pg.Tx, r io.Reader) (int, error)

CopyHashesFromSTDIN fills temporary hashes table with CSV data

func (DB) CreateTempHashesTable

func (db DB) CreateTempHashesTable(ctx context.Context, tx *pg.Tx) error

CreateTempHashesTable creates a temporary table for hashes.

func (*DB) RunInLock

func (db *DB) RunInLock(ctx context.Context, lockName string, fns ...func(*pg.Tx) error) error

RunInLock runs chain of functions in transaction with lock until first error

func (DB) UpsertHashesTable

func (db DB) UpsertHashesTable(ctx context.Context, tx *pg.Tx) (int, time.Duration, error)

UpsertHashesTable upserts data from tempTable into vfsHashes.

func (*DB) Version

func (db *DB) Version() (string, error)

Version is a function that returns Postgres version.

type Filter

type Filter struct {
	Field      string      `json:"field"`             //search field
	Value      interface{} `json:"value,omitempty"`   //search value
	SearchType int         `json:"type,omitempty"`    //search type. see db/filter.go
	Exclude    bool        `json:"exclude,omitempty"` //is this filter should exclude
}

func (Filter) Apply

func (f Filter) Apply(query *orm.Query) *orm.Query

Apply applies filter to go-pg orm

func (Filter) String

func (f Filter) String() string

String prints filter as sql string

type OpFunc

type OpFunc func(query *orm.Query)

OpFunc is a function that applies different options to query.

func EnabledOnly

func EnabledOnly() OpFunc

EnabledOnly is a function that adds "statusId"=1 filter to query.

func WithColumns

func WithColumns(cols ...string) OpFunc

WithColumns is a function that adds user specific columns to query.

func WithJoinedIDs

func WithJoinedIDs(ids []int, tableAlias, column string) OpFunc

WithJoinedIDs adds join VALUES statement for given table and column.

func WithRelations

func WithRelations(rels ...string) OpFunc

WithRelations is a function that adds user specific relations to query.

func WithSort

func WithSort(fields ...SortField) OpFunc

WithColumns is a function that adds uses specific columns to query.

func WithTable

func WithTable(table string) OpFunc

WithTable is a function that adds uses specific table to query.

type Pager

type Pager struct {
	Page     int
	PageSize int
}

func NewPager

func NewPager(page, pageSize int) Pager

NewPager create new Pager. If page and pageSize is zero return PagerDefault

func (Pager) Apply

func (p Pager) Apply(query *orm.Query) *orm.Query

Apply applies options to go-pg orm

func (Pager) Pager

func (p Pager) Pager() *urlstruct.Pager

Pager gets orm.Pages for go-pg

func (Pager) String

func (p Pager) String() (opts string)

String gets sql string from options

type Searcher

type Searcher interface {
	Apply(query *orm.Query) *orm.Query
	Q() applier

	With(condition string, params ...interface{})
	WithApply(a applier)
}

Searcher is interface for every generated filter

type SortDirection

type SortDirection string
const (
	SortAsc            SortDirection = "asc"
	SortAscNullsFirst  SortDirection = "asc nulls first"
	SortAscNullsLast   SortDirection = "asc nulls last"
	SortDesc           SortDirection = "desc"
	SortDescNullsFirst SortDirection = "desc nulls first"
	SortDescNullsLast  SortDirection = "desc nulls last"
)

type SortField

type SortField struct {
	Column    string
	Direction SortDirection
}

func NewSortField

func NewSortField(column string, sortDesc bool) SortField

type VfsFile

type VfsFile struct {
	ID         int            `pg:"fileId,pk"`
	FolderID   int            `pg:"folderId,use_zero"`
	Title      string         `pg:"title,use_zero"`
	Path       string         `pg:"path,use_zero"`
	Params     *VfsFileParams `pg:"params"`
	IsFavorite *bool          `pg:"isFavorite"`
	MimeType   string         `pg:"mimeType,use_zero"`
	FileSize   *int           `pg:"fileSize"`
	FileExists bool           `pg:"fileExists,use_zero"`
	CreatedAt  time.Time      `pg:"createdAt,use_zero"`
	StatusID   int            `pg:"statusId,use_zero"`

	Folder *VfsFolder `pg:"fk:folderId,rel:has-one"`
	// contains filtered or unexported fields
}

func (VfsFile) Validate

func (vf VfsFile) Validate() (errors map[string]string, valid bool)

type VfsFileParams

type VfsFileParams struct {
	Width  int `json:"width,omitempty"`
	Height int `json:"height,omitempty"`
}

type VfsFileSearch

type VfsFileSearch struct {
	ID            *int
	FolderID      *int
	Title         *string
	Path          *string
	Params        *VfsFileParams
	IsFavorite    *bool
	MimeType      *string
	FileSize      *int
	FileExists    *bool
	CreatedAt     *time.Time
	StatusID      *int
	IDs           []int
	TitleILike    *string
	PathILike     *string
	MimeTypeILike *string
	// contains filtered or unexported fields
}

func (*VfsFileSearch) Apply

func (vfs *VfsFileSearch) Apply(query *orm.Query) *orm.Query

func (*VfsFileSearch) Q

func (vfs *VfsFileSearch) Q() applier

func (*VfsFileSearch) With

func (s *VfsFileSearch) With(condition string, params ...interface{})

func (*VfsFileSearch) WithApply

func (s *VfsFileSearch) WithApply(a applier)

func (*VfsFileSearch) WithQuery

func (vfs *VfsFileSearch) WithQuery(query *string) *VfsFileSearch

type VfsFolder

type VfsFolder struct {
	ID             int       `pg:"folderId,pk"`
	ParentFolderID *int      `pg:"parentFolderId"`
	Title          string    `pg:"title,use_zero"`
	IsFavorite     *bool     `pg:"isFavorite"`
	CreatedAt      time.Time `pg:"createdAt,use_zero"`
	StatusID       int       `pg:"statusId,use_zero"`

	ParentFolder *VfsFolder `pg:"fk:parentFolderId,rel:has-one"`
	// contains filtered or unexported fields
}

func (VfsFolder) Validate

func (vf VfsFolder) Validate() (errors map[string]string, valid bool)

type VfsFolderSearch

type VfsFolderSearch struct {
	ID             *int
	ParentFolderID *int
	Title          *string
	IsFavorite     *bool
	CreatedAt      *time.Time
	StatusID       *int
	IDs            []int
	TitleILike     *string
	// contains filtered or unexported fields
}

func (*VfsFolderSearch) Apply

func (vfs *VfsFolderSearch) Apply(query *orm.Query) *orm.Query

func (*VfsFolderSearch) Q

func (vfs *VfsFolderSearch) Q() applier

func (*VfsFolderSearch) With

func (s *VfsFolderSearch) With(condition string, params ...interface{})

func (*VfsFolderSearch) WithApply

func (s *VfsFolderSearch) WithApply(a applier)

type VfsHash

type VfsHash struct {
	Hash      string     `pg:"hash,pk"`
	Namespace string     `pg:"namespace,pk"`
	Extension string     `pg:"extension,use_zero"`
	FileSize  int        `pg:"fileSize,use_zero"`
	Width     int        `pg:"width,use_zero"`
	Height    int        `pg:"height,use_zero"`
	Blurhash  *string    `pg:"blurhash"`
	CreatedAt time.Time  `pg:"createdAt,use_zero"`
	IndexedAt *time.Time `pg:"indexedAt"`
	Error     string     `pg:"error,use_zero"`
	// contains filtered or unexported fields
}

func (VfsHash) Validate

func (vh VfsHash) Validate() (errors map[string]string, valid bool)

type VfsHashSearch

type VfsHashSearch struct {
	Hash           *string
	Namespace      *string
	Extension      *string
	FileSize       *int
	Width          *int
	Height         *int
	Blurhash       *string
	CreatedAt      *time.Time
	IndexedAt      *time.Time
	Error          *string
	Hashes         []string
	HashILike      *string
	Namespaces     []string
	NamespaceILike *string
	ExtensionILike *string
	BlurhashILike  *string
	ErrorILike     *string
	// contains filtered or unexported fields
}

func (*VfsHashSearch) Apply

func (vhs *VfsHashSearch) Apply(query *orm.Query) *orm.Query

func (*VfsHashSearch) Q

func (vhs *VfsHashSearch) Q() applier

func (*VfsHashSearch) With

func (s *VfsHashSearch) With(condition string, params ...interface{})

func (*VfsHashSearch) WithApply

func (s *VfsHashSearch) WithApply(a applier)

type VfsRepo

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

func NewVfsRepo

func NewVfsRepo(db orm.DB) VfsRepo

NewVfsRepo returns new repository

func (VfsRepo) AddVfsFile

func (vr VfsRepo) AddVfsFile(ctx context.Context, vfsFile *VfsFile, ops ...OpFunc) (*VfsFile, error)

AddVfsFile adds VfsFile to DB.

func (VfsRepo) AddVfsFolder

func (vr VfsRepo) AddVfsFolder(ctx context.Context, vfsFolder *VfsFolder, ops ...OpFunc) (*VfsFolder, error)

AddVfsFolder adds VfsFolder to DB.

func (VfsRepo) AddVfsHash

func (vr VfsRepo) AddVfsHash(ctx context.Context, vfsHash *VfsHash, ops ...OpFunc) (*VfsHash, error)

AddVfsHash adds VfsHash to DB.

func (VfsRepo) CountVfsFiles

func (vr VfsRepo) CountVfsFiles(ctx context.Context, search *VfsFileSearch, ops ...OpFunc) (int, error)

CountVfsFiles returns count

func (VfsRepo) CountVfsFolders

func (vr VfsRepo) CountVfsFolders(ctx context.Context, search *VfsFolderSearch, ops ...OpFunc) (int, error)

CountVfsFolders returns count

func (VfsRepo) CountVfsHashes

func (vr VfsRepo) CountVfsHashes(ctx context.Context, search *VfsHashSearch, ops ...OpFunc) (int, error)

CountVfsHashes returns count

func (VfsRepo) DefaultVfsFileSort

func (vr VfsRepo) DefaultVfsFileSort() OpFunc

DefaultVfsFileSort returns default sort.

func (VfsRepo) DefaultVfsFolderSort

func (vr VfsRepo) DefaultVfsFolderSort() OpFunc

DefaultVfsFolderSort returns default sort.

func (VfsRepo) DefaultVfsHashSort

func (vr VfsRepo) DefaultVfsHashSort() OpFunc

DefaultVfsHashSort returns default sort.

func (VfsRepo) DeleteVfsFile

func (vr VfsRepo) DeleteVfsFile(ctx context.Context, id int) (deleted bool, err error)

DeleteVfsFile set statusId to deleted in DB.

func (VfsRepo) DeleteVfsFiles

func (vr VfsRepo) DeleteVfsFiles(ctx context.Context, fileIDs []int64) (bool, error)

func (VfsRepo) DeleteVfsFolder

func (vr VfsRepo) DeleteVfsFolder(ctx context.Context, id int) (deleted bool, err error)

DeleteVfsFolder set statusId to deleted in DB.

func (VfsRepo) DeleteVfsHash

func (vr VfsRepo) DeleteVfsHash(ctx context.Context, hash string, namespace string) (deleted bool, err error)

DeleteVfsHash deletes VfsHash from DB.

func (VfsRepo) FolderBranch

func (vr VfsRepo) FolderBranch(ctx context.Context, folderId int) (list []VfsFolder, err error)

func (VfsRepo) FullVfsFile

func (vr VfsRepo) FullVfsFile() OpFunc

FullVfsFile returns full joins with all columns

func (VfsRepo) FullVfsFolder

func (vr VfsRepo) FullVfsFolder() OpFunc

FullVfsFolder returns full joins with all columns

func (VfsRepo) FullVfsHash

func (vr VfsRepo) FullVfsHash() OpFunc

FullVfsHash returns full joins with all columns

func (VfsRepo) HashesForUpdate

func (vr VfsRepo) HashesForUpdate(ctx context.Context, limit uint64) (list []VfsHash, err error)

func (VfsRepo) NextFileID

func (vr VfsRepo) NextFileID() (int, error)

func (VfsRepo) OneVfsFile

func (vr VfsRepo) OneVfsFile(ctx context.Context, search *VfsFileSearch, ops ...OpFunc) (*VfsFile, error)

OneVfsFile is a function that returns one VfsFile by filters. It could return pg.ErrMultiRows.

func (VfsRepo) OneVfsFolder

func (vr VfsRepo) OneVfsFolder(ctx context.Context, search *VfsFolderSearch, ops ...OpFunc) (*VfsFolder, error)

OneVfsFolder is a function that returns one VfsFolder by filters. It could return pg.ErrMultiRows.

func (VfsRepo) OneVfsHash

func (vr VfsRepo) OneVfsHash(ctx context.Context, search *VfsHashSearch, ops ...OpFunc) (*VfsHash, error)

OneVfsHash is a function that returns one VfsHash by filters. It could return pg.ErrMultiRows.

func (VfsRepo) SaveVfsHash

func (vr VfsRepo) SaveVfsHash(ctx context.Context, hash *VfsHash) (err error)

SaveVfsHash checks hash in DB and adds it if hash was not found.

func (VfsRepo) UpdateFilesFolder

func (vr VfsRepo) UpdateFilesFolder(ctx context.Context, fileIDs []int64, newFolderId int) (bool, error)

func (VfsRepo) UpdateVfsFile

func (vr VfsRepo) UpdateVfsFile(ctx context.Context, vfsFile *VfsFile, ops ...OpFunc) (bool, error)

UpdateVfsFile updates VfsFile in DB.

func (VfsRepo) UpdateVfsFolder

func (vr VfsRepo) UpdateVfsFolder(ctx context.Context, vfsFolder *VfsFolder, ops ...OpFunc) (bool, error)

UpdateVfsFolder updates VfsFolder in DB.

func (VfsRepo) UpdateVfsHash

func (vr VfsRepo) UpdateVfsHash(ctx context.Context, vfsHash *VfsHash, ops ...OpFunc) (bool, error)

UpdateVfsHash updates VfsHash in DB.

func (VfsRepo) VfsFileByID

func (vr VfsRepo) VfsFileByID(ctx context.Context, id int, ops ...OpFunc) (*VfsFile, error)

VfsFileByID is a function that returns VfsFile by ID(s) or nil.

func (VfsRepo) VfsFilesByFilters

func (vr VfsRepo) VfsFilesByFilters(ctx context.Context, search *VfsFileSearch, pager Pager, ops ...OpFunc) (vfsFiles []VfsFile, err error)

VfsFilesByFilters returns VfsFile list.

func (VfsRepo) VfsFolderByID

func (vr VfsRepo) VfsFolderByID(ctx context.Context, id int, ops ...OpFunc) (*VfsFolder, error)

VfsFolderByID is a function that returns VfsFolder by ID(s) or nil.

func (VfsRepo) VfsFoldersByFilters

func (vr VfsRepo) VfsFoldersByFilters(ctx context.Context, search *VfsFolderSearch, pager Pager, ops ...OpFunc) (vfsFolders []VfsFolder, err error)

VfsFoldersByFilters returns VfsFolder list.

func (VfsRepo) VfsHashByID

func (vr VfsRepo) VfsHashByID(ctx context.Context, hash string, namespace string, ops ...OpFunc) (*VfsHash, error)

VfsHashByID is a function that returns VfsHash by ID(s) or nil.

func (VfsRepo) VfsHashesByFilters

func (vr VfsRepo) VfsHashesByFilters(ctx context.Context, search *VfsHashSearch, pager Pager, ops ...OpFunc) (vfsHashes []VfsHash, err error)

VfsHashesByFilters returns VfsHash list.

func (VfsRepo) WithEnabledOnly

func (vr VfsRepo) WithEnabledOnly() VfsRepo

WithEnabledOnly is a function that adds "statusId"=1 as base filter.

func (VfsRepo) WithTransaction

func (vr VfsRepo) WithTransaction(tx *pg.Tx) VfsRepo

WithTransaction is a function that wraps VfsRepo with pg.Tx transaction.

Jump to

Keyboard shortcuts

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