database

package
v0.0.0-...-1285e68 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2022 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ByCount   = By("COUNT(id) as amount")
	ByAverage = func(column string, multiplier int) By {
		switch database.DbType() {
		case "mysql":
			return By(fmt.Sprintf("CAST(AVG(%s) as UNSIGNED INT) as amount", column))
		case "postgres":
			return By(fmt.Sprintf("cast(AVG(%s) as int) as amount", column))
		default:
			return By(fmt.Sprintf("cast(AVG(%s) as int) as amount", column))
		}
	}
)

Functions

func Available

func Available(db Database) bool

func Close

func Close(db Database) error

func Maintenance

func Maintenance()

Maintenance will automatically delete old records from 'failures' and 'hits' this function is currently set to delete records 7+ days old every 60 minutes env: REMOVE_AFTER - golang duration parsed time for deleting records older than REMOVE_AFTER duration from now env: CLEANUP_INTERVAL - golang duration parsed time for checking old records routine

func Routine

func Routine()

Types

type By

type By string

func (By) String

func (b By) String() string

type Database

type Database interface {
	Close() error
	DB() *sql.DB
	New() Database
	NewScope(value interface{}) *gorm.Scope
	CommonDB() gorm.SQLCommon
	Callback() *gorm.Callback
	SetLogger(l gorm.Logger)
	LogMode(enable bool) Database
	SingularTable(enable bool)
	Where(query interface{}, args ...interface{}) Database
	Or(query interface{}, args ...interface{}) Database
	Not(query interface{}, args ...interface{}) Database
	Limit(value int) Database
	Offset(value int) Database
	Order(value string, reorder ...bool) Database
	Select(query interface{}, args ...interface{}) Database
	Omit(columns ...string) Database
	Group(query string) Database
	Having(query string, values ...interface{}) Database
	Joins(query string, args ...interface{}) Database
	Scopes(funcs ...func(*gorm.DB) *gorm.DB) Database
	Unscoped() Database
	Attrs(attrs ...interface{}) Database
	Assign(attrs ...interface{}) Database
	First(out interface{}, where ...interface{}) Database
	Last(out interface{}, where ...interface{}) Database
	Find(out interface{}, where ...interface{}) Database
	Scan(dest interface{}) Database
	Row() *sql.Row
	Rows() (*sql.Rows, error)
	ScanRows(rows *sql.Rows, result interface{}) error
	Pluck(column string, value interface{}) Database
	Count(value interface{}) Database
	Related(value interface{}, foreignKeys ...string) Database
	FirstOrInit(out interface{}, where ...interface{}) Database
	FirstOrCreate(out interface{}, where ...interface{}) Database
	Update(attrs ...interface{}) Database
	Updates(values interface{}, ignoreProtectedAttrs ...bool) Database
	UpdateColumn(attrs ...interface{}) Database
	UpdateColumns(values interface{}) Database
	Save(value interface{}) Database
	Create(value interface{}) Database
	Delete(value interface{}, where ...interface{}) Database
	Raw(sql string, values ...interface{}) Database
	Exec(sql string, values ...interface{}) Database
	Model(value interface{}) Database
	Table(name string) Database
	Debug() Database
	Begin() Database
	Commit() Database
	Rollback() Database
	NewRecord(value interface{}) bool
	RecordNotFound() bool
	CreateTable(values ...interface{}) Database
	DropTable(values ...interface{}) Database
	DropTableIfExists(values ...interface{}) Database
	HasTable(value interface{}) bool
	AutoMigrate(values ...interface{}) Database
	ModifyColumn(column string, typ string) Database
	DropColumn(column string) Database
	AddIndex(indexName string, column ...string) Database
	AddUniqueIndex(indexName string, column ...string) Database
	RemoveIndex(indexName string) Database
	AddForeignKey(field string, dest string, onDelete string, onUpdate string) Database
	Association(column string) *gorm.Association
	Preload(column string, conditions ...interface{}) Database
	Set(name string, value interface{}) Database
	InstantSet(name string, value interface{}) Database
	Get(name string) (value interface{}, ok bool)
	SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface)
	AddError(err error) error
	GetErrors() (errors []error)

	// extra
	Error() error
	Status() int
	RowsAffected() int64

	Since(time.Time) Database
	Between(time.Time, time.Time) Database

	SelectByTime(time.Duration) string
	MultipleSelects(args ...string) Database

	FormatTime(t time.Time) string
	ParseTime(t string) (time.Time, error)
	DbType() string
	GormDB() *gorm.DB
	ChunkSize() int
}

Database is an interface which DB implements

func Begin

func Begin(db Database, model interface{}) Database

func LogMode

func LogMode(db Database, b bool) Database

func OpenTester

func OpenTester() (Database, error)

func Openw

func Openw(dialect string, args ...interface{}) (db Database, err error)

Openw is a drop-in replacement for Open()

func Wrap

func Wrap(db *gorm.DB) Database

Wrap wraps gorm.DB in an interface

type Db

type Db struct {
	Database *gorm.DB
	Type     string
	ReadOnly bool
}

func (*Db) AddError

func (it *Db) AddError(err error) error

func (*Db) AddForeignKey

func (it *Db) AddForeignKey(field string, dest string, onDelete string, onUpdate string) Database

func (*Db) AddIndex

func (it *Db) AddIndex(indexName string, columns ...string) Database

func (*Db) AddUniqueIndex

func (it *Db) AddUniqueIndex(indexName string, columns ...string) Database

func (*Db) Assign

func (it *Db) Assign(attrs ...interface{}) Database

func (*Db) Association

func (it *Db) Association(column string) *gorm.Association

func (*Db) Attrs

func (it *Db) Attrs(attrs ...interface{}) Database

func (*Db) AutoMigrate

func (it *Db) AutoMigrate(values ...interface{}) Database

func (*Db) Begin

func (it *Db) Begin() Database

func (*Db) Between

func (it *Db) Between(t1 time.Time, t2 time.Time) Database

func (*Db) Callback

func (it *Db) Callback() *gorm.Callback

func (*Db) ChunkSize

func (it *Db) ChunkSize() int

func (*Db) Close

func (it *Db) Close() error

func (*Db) Commit

func (it *Db) Commit() Database

func (*Db) CommonDB

func (it *Db) CommonDB() gorm.SQLCommon

func (*Db) Count

func (it *Db) Count(value interface{}) Database

func (*Db) Create

func (it *Db) Create(value interface{}) Database

func (*Db) CreateTable

func (it *Db) CreateTable(values ...interface{}) Database

func (*Db) DB

func (it *Db) DB() *sql.DB

func (*Db) DbType

func (it *Db) DbType() string

func (*Db) Debug

func (it *Db) Debug() Database

func (*Db) Delete

func (it *Db) Delete(value interface{}, where ...interface{}) Database

func (*Db) DropColumn

func (it *Db) DropColumn(column string) Database

func (*Db) DropTable

func (it *Db) DropTable(values ...interface{}) Database

func (*Db) DropTableIfExists

func (it *Db) DropTableIfExists(values ...interface{}) Database

func (*Db) Error

func (it *Db) Error() error

func (*Db) Exec

func (it *Db) Exec(sql string, values ...interface{}) Database

func (*Db) Find

func (it *Db) Find(out interface{}, where ...interface{}) Database

func (*Db) First

func (it *Db) First(out interface{}, where ...interface{}) Database

func (*Db) FirstOrCreate

func (it *Db) FirstOrCreate(out interface{}, where ...interface{}) Database

func (*Db) FirstOrInit

func (it *Db) FirstOrInit(out interface{}, where ...interface{}) Database

func (*Db) FormatTime

func (it *Db) FormatTime(t time.Time) string

FormatTime returns the timestamp in the same format as the DATETIME column in database

func (*Db) Get

func (it *Db) Get(name string) (interface{}, bool)

func (*Db) GetErrors

func (it *Db) GetErrors() (errors []error)

func (*Db) GormDB

func (it *Db) GormDB() *gorm.DB

func (*Db) Group

func (it *Db) Group(query string) Database

func (*Db) HasTable

func (it *Db) HasTable(value interface{}) bool

func (*Db) Having

func (it *Db) Having(query string, values ...interface{}) Database

func (*Db) InstantSet

func (it *Db) InstantSet(name string, value interface{}) Database

func (*Db) Joins

func (it *Db) Joins(query string, args ...interface{}) Database

func (*Db) Last

func (it *Db) Last(out interface{}, where ...interface{}) Database

func (*Db) Limit

func (it *Db) Limit(value int) Database

func (*Db) LogMode

func (it *Db) LogMode(enable bool) Database

func (*Db) Loggable

func (it *Db) Loggable() bool

func (*Db) Model

func (it *Db) Model(value interface{}) Database

func (*Db) ModifyColumn

func (it *Db) ModifyColumn(column string, typ string) Database

func (*Db) MultipleSelects

func (it *Db) MultipleSelects(args ...string) Database

func (*Db) New

func (it *Db) New() Database

func (*Db) NewRecord

func (it *Db) NewRecord(value interface{}) bool

func (*Db) NewScope

func (it *Db) NewScope(value interface{}) *gorm.Scope

func (*Db) Not

func (it *Db) Not(query interface{}, args ...interface{}) Database

func (*Db) Offset

func (it *Db) Offset(value int) Database

func (*Db) Omit

func (it *Db) Omit(columns ...string) Database

func (*Db) Or

func (it *Db) Or(query interface{}, args ...interface{}) Database

func (*Db) Order

func (it *Db) Order(value string, reorder ...bool) Database

func (*Db) ParseTime

func (it *Db) ParseTime(t string) (time.Time, error)

func (*Db) Pluck

func (it *Db) Pluck(column string, value interface{}) Database

func (*Db) Preload

func (it *Db) Preload(column string, conditions ...interface{}) Database

func (*Db) Raw

func (it *Db) Raw(sql string, values ...interface{}) Database

func (*Db) RecordNotFound

func (it *Db) RecordNotFound() bool

func (*Db) Related

func (it *Db) Related(value interface{}, foreignKeys ...string) Database

func (*Db) RemoveIndex

func (it *Db) RemoveIndex(indexName string) Database

func (*Db) Rollback

func (it *Db) Rollback() Database

func (*Db) Row

func (it *Db) Row() *sql.Row

func (*Db) Rows

func (it *Db) Rows() (*sql.Rows, error)

func (*Db) RowsAffected

func (it *Db) RowsAffected() int64

func (*Db) Save

func (it *Db) Save(value interface{}) Database

func (*Db) Scan

func (it *Db) Scan(dest interface{}) Database

func (*Db) ScanRows

func (it *Db) ScanRows(rows *sql.Rows, result interface{}) error

func (*Db) Scopes

func (it *Db) Scopes(funcs ...func(*gorm.DB) *gorm.DB) Database

func (*Db) Select

func (it *Db) Select(query interface{}, args ...interface{}) Database

func (*Db) SelectByTime

func (it *Db) SelectByTime(increment time.Duration) string

SelectByTime returns an SQL query that will group "created_at" column by x seconds and returns as "timeframe"

func (*Db) Set

func (it *Db) Set(name string, value interface{}) Database

func (*Db) SetJoinTableHandler

func (it *Db) SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface)

func (*Db) SetLogger

func (it *Db) SetLogger(log gorm.Logger)

func (*Db) Since

func (it *Db) Since(ago time.Time) Database

func (*Db) SingularTable

func (it *Db) SingularTable(enable bool)

func (*Db) Status

func (it *Db) Status() int

func (*Db) Table

func (it *Db) Table(name string) Database

func (*Db) Unscoped

func (it *Db) Unscoped() Database

func (*Db) Update

func (it *Db) Update(attrs ...interface{}) Database

func (*Db) UpdateColumn

func (it *Db) UpdateColumn(attrs ...interface{}) Database

func (*Db) UpdateColumns

func (it *Db) UpdateColumns(values interface{}) Database

func (*Db) Updates

func (it *Db) Updates(values interface{}, ignoreProtectedAttrs ...bool) Database

func (*Db) Where

func (it *Db) Where(query interface{}, args ...interface{}) Database

type DbObject

type DbObject interface {
	Create() error
	Update() error
	Delete() error
}

type GroupBy

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

type GroupByer

type GroupByer interface {
	ToTimeValue() (*TimeVar, error)
}

type GroupQuery

type GroupQuery struct {
	Start     time.Time
	End       time.Time
	Group     time.Duration
	Order     string
	Limit     int
	Offset    int
	FillEmpty bool
	// contains filtered or unexported fields
}

func ParseQueries

func ParseQueries(r *http.Request, o isObject) (*GroupQuery, error)

func ParseQueriesForTable

func ParseQueriesForTable(r *http.Request, o isObject, whereTable string) (*GroupQuery, error)

func ParseRequest

func ParseRequest(r *http.Request) (*GroupQuery, error)

func (GroupQuery) Database

func (b GroupQuery) Database() Database

func (GroupQuery) Find

func (b GroupQuery) Find(data interface{}) error

func (*GroupQuery) GraphData

func (b *GroupQuery) GraphData(by By) ([]*TimeValue, error)

GraphData will return all hits or failures

func (*GroupQuery) ToTimeValue

func (b *GroupQuery) ToTimeValue() (*TimeVar, error)

ToTimeValue will format the SQL rows into a JSON format for the API. [{"timestamp": "2006-01-02T15:04:05Z", "amount": 468293}] TODO redo this entire function, use better SQL query to group by time

type Sampler

type Sampler interface {
	Sample() DbObject
}

type TimeGroup

type TimeGroup interface {
}

type TimeValue

type TimeValue struct {
	Timeframe string `json:"timeframe"`
	Amount    int64  `json:"amount"`
}

type TimeVar

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

func (*TimeVar) FillMissing

func (t *TimeVar) FillMissing(current, end time.Time) ([]*TimeValue, error)

func (*TimeVar) ToValues

func (t *TimeVar) ToValues() ([]*TimeValue, error)

Jump to

Keyboard shortcuts

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