statping/database/database.go

520 lines
13 KiB
Go
Raw Normal View History

2020-02-22 23:52:05 +00:00
package database
2020-01-26 21:01:43 +00:00
import (
"database/sql"
2020-02-22 23:52:05 +00:00
"github.com/hunterlong/statping/types"
2020-01-26 21:01:43 +00:00
"github.com/jinzhu/gorm"
2020-02-22 04:40:05 +00:00
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2020-01-26 21:01:43 +00:00
"net/http"
2020-02-21 05:36:23 +00:00
"strings"
2020-01-26 21:01:43 +00:00
"time"
)
2020-02-22 23:52:05 +00:00
const (
TIME_NANO = "2006-01-02T15:04:05Z"
TIME = "2006-01-02 15:04:05"
CHART_TIME = "2006-01-02T15:04:05.999999-07:00"
TIME_DAY = "2006-01-02"
)
2020-02-24 05:53:15 +00:00
var (
database Database
)
2020-01-26 21:01:43 +00:00
// Database is an interface which DB implements
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
RowsAffected() int64
Since(time.Time) Database
Between(time.Time, time.Time) Database
2020-02-22 04:40:05 +00:00
SelectByTime(string) string
2020-02-21 05:36:23 +00:00
MultipleSelects(args ...string) Database
2020-02-22 23:52:05 +00:00
FormatTime(t time.Time) string
ParseTime(t string) (time.Time, error)
2020-02-22 04:40:05 +00:00
2020-02-25 07:41:28 +00:00
Requests(*http.Request, isObject) Database
2020-02-24 05:53:15 +00:00
GroupQuery(query *GroupQuery, by By) GroupByer
2020-02-25 07:41:28 +00:00
Objects
}
type Objects interface {
Services() Database
Users() Database
Groups() Database
Incidents() Database
IncidentUpdates() Database
Hits() Database
Failures() Database
Checkins() Database
CheckinHits() Database
Messages() Database
Integrations() Database
}
func (d *Db) Services() Database {
return d.Model(&types.Service{})
}
func (d *Db) Users() Database {
return d.Model(&types.User{})
}
func (d *Db) Groups() Database {
return d.Model(&types.Group{})
2020-02-24 05:53:15 +00:00
}
2020-02-25 07:41:28 +00:00
func (d *Db) Incidents() Database {
return d.Model(&types.Incident{})
}
func (d *Db) IncidentUpdates() Database {
return d.Model(&types.IncidentUpdate{})
}
func (d *Db) Hits() Database {
return d.Model(&types.Hit{})
}
func (d *Db) Integrations() Database {
return d.Model(&types.Integration{})
}
func (d *Db) Failures() Database {
return d.Model(&types.Failure{})
}
func (d *Db) Checkins() Database {
return d.Model(&types.Checkin{})
}
func (d *Db) CheckinHits() Database {
return d.Model(&types.CheckinHit{})
}
func (d *Db) Messages() Database {
return d.Model(&types.Message{})
}
func (it *Db) Requests(r *http.Request, o isObject) Database {
g := ParseQueries(r, o)
2020-02-24 05:53:15 +00:00
return g.db
2020-02-21 05:36:23 +00:00
}
func (it *Db) MultipleSelects(args ...string) Database {
joined := strings.Join(args, ", ")
return it.Select(joined)
}
2020-02-19 04:07:22 +00:00
type Db struct {
Database *gorm.DB
2020-02-21 05:36:23 +00:00
Type string
2020-01-26 21:01:43 +00:00
}
// Openw is a drop-in replacement for Open()
func Openw(dialect string, args ...interface{}) (db Database, err error) {
gormdb, err := gorm.Open(dialect, args...)
2020-02-24 05:53:15 +00:00
if err != nil {
return nil, err
}
database = Wrap(gormdb)
return database, err
2020-01-26 21:01:43 +00:00
}
// Wrap wraps gorm.DB in an interface
func Wrap(db *gorm.DB) Database {
2020-02-21 05:36:23 +00:00
return &Db{
Database: db,
Type: db.Dialect().GetName(),
}
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Close() error {
return it.Database.Close()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) DB() *sql.DB {
return it.Database.DB()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) New() Database {
return Wrap(it.Database.New())
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) NewScope(value interface{}) *gorm.Scope {
return it.Database.NewScope(value)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) CommonDB() gorm.SQLCommon {
return it.Database.CommonDB()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Callback() *gorm.Callback {
return it.Database.Callback()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) SetLogger(log gorm.Logger) {
it.Database.SetLogger(log)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) LogMode(enable bool) Database {
return Wrap(it.Database.LogMode(enable))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) SingularTable(enable bool) {
it.Database.SingularTable(enable)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Where(query interface{}, args ...interface{}) Database {
return Wrap(it.Database.Where(query, args...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Or(query interface{}, args ...interface{}) Database {
return Wrap(it.Database.Or(query, args...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Not(query interface{}, args ...interface{}) Database {
return Wrap(it.Database.Not(query, args...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Limit(value int) Database {
return Wrap(it.Database.Limit(value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Offset(value int) Database {
return Wrap(it.Database.Offset(value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Order(value string, reorder ...bool) Database {
return Wrap(it.Database.Order(value, reorder...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Select(query interface{}, args ...interface{}) Database {
return Wrap(it.Database.Select(query, args...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Omit(columns ...string) Database {
return Wrap(it.Database.Omit(columns...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Group(query string) Database {
return Wrap(it.Database.Group(query))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Having(query string, values ...interface{}) Database {
return Wrap(it.Database.Having(query, values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Joins(query string, args ...interface{}) Database {
return Wrap(it.Database.Joins(query, args...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Scopes(funcs ...func(*gorm.DB) *gorm.DB) Database {
return Wrap(it.Database.Scopes(funcs...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Unscoped() Database {
return Wrap(it.Database.Unscoped())
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Attrs(attrs ...interface{}) Database {
return Wrap(it.Database.Attrs(attrs...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Assign(attrs ...interface{}) Database {
return Wrap(it.Database.Assign(attrs...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) First(out interface{}, where ...interface{}) Database {
return Wrap(it.Database.First(out, where...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Last(out interface{}, where ...interface{}) Database {
return Wrap(it.Database.Last(out, where...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Find(out interface{}, where ...interface{}) Database {
return Wrap(it.Database.Find(out, where...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Scan(dest interface{}) Database {
return Wrap(it.Database.Scan(dest))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Row() *sql.Row {
return it.Database.Row()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Rows() (*sql.Rows, error) {
return it.Database.Rows()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) ScanRows(rows *sql.Rows, result interface{}) error {
return it.Database.ScanRows(rows, result)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Pluck(column string, value interface{}) Database {
return Wrap(it.Database.Pluck(column, value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Count(value interface{}) Database {
return Wrap(it.Database.Count(value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Related(value interface{}, foreignKeys ...string) Database {
return Wrap(it.Database.Related(value, foreignKeys...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) FirstOrInit(out interface{}, where ...interface{}) Database {
return Wrap(it.Database.FirstOrInit(out, where...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) FirstOrCreate(out interface{}, where ...interface{}) Database {
return Wrap(it.Database.FirstOrCreate(out, where...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Update(attrs ...interface{}) Database {
return Wrap(it.Database.Update(attrs...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Updates(values interface{}, ignoreProtectedAttrs ...bool) Database {
return Wrap(it.Database.Updates(values, ignoreProtectedAttrs...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) UpdateColumn(attrs ...interface{}) Database {
return Wrap(it.Database.UpdateColumn(attrs...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) UpdateColumns(values interface{}) Database {
return Wrap(it.Database.UpdateColumns(values))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Save(value interface{}) Database {
return Wrap(it.Database.Save(value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Create(value interface{}) Database {
return Wrap(it.Database.Create(value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Delete(value interface{}, where ...interface{}) Database {
return Wrap(it.Database.Delete(value, where...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Raw(sql string, values ...interface{}) Database {
return Wrap(it.Database.Raw(sql, values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Exec(sql string, values ...interface{}) Database {
return Wrap(it.Database.Exec(sql, values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Model(value interface{}) Database {
return Wrap(it.Database.Model(value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Table(name string) Database {
return Wrap(it.Database.Table(name))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Debug() Database {
return Wrap(it.Database.Debug())
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Begin() Database {
return Wrap(it.Database.Begin())
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Commit() Database {
return Wrap(it.Database.Commit())
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Rollback() Database {
return Wrap(it.Database.Rollback())
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) NewRecord(value interface{}) bool {
return it.Database.NewRecord(value)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) RecordNotFound() bool {
return it.Database.RecordNotFound()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) CreateTable(values ...interface{}) Database {
return Wrap(it.Database.CreateTable(values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) DropTable(values ...interface{}) Database {
return Wrap(it.Database.DropTable(values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) DropTableIfExists(values ...interface{}) Database {
return Wrap(it.Database.DropTableIfExists(values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) HasTable(value interface{}) bool {
return it.Database.HasTable(value)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) AutoMigrate(values ...interface{}) Database {
return Wrap(it.Database.AutoMigrate(values...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) ModifyColumn(column string, typ string) Database {
return Wrap(it.Database.ModifyColumn(column, typ))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) DropColumn(column string) Database {
return Wrap(it.Database.DropColumn(column))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) AddIndex(indexName string, columns ...string) Database {
return Wrap(it.Database.AddIndex(indexName, columns...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) AddUniqueIndex(indexName string, columns ...string) Database {
return Wrap(it.Database.AddUniqueIndex(indexName, columns...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) RemoveIndex(indexName string) Database {
return Wrap(it.Database.RemoveIndex(indexName))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Association(column string) *gorm.Association {
return it.Database.Association(column)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Preload(column string, conditions ...interface{}) Database {
return Wrap(it.Database.Preload(column, conditions...))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Set(name string, value interface{}) Database {
return Wrap(it.Database.Set(name, value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) InstantSet(name string, value interface{}) Database {
return Wrap(it.Database.InstantSet(name, value))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Get(name string) (interface{}, bool) {
return it.Database.Get(name)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface) {
it.Database.SetJoinTableHandler(source, column, handler)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) AddForeignKey(field string, dest string, onDelete string, onUpdate string) Database {
return Wrap(it.Database.AddForeignKey(field, dest, onDelete, onUpdate))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) AddError(err error) error {
return it.Database.AddError(err)
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) GetErrors() (errors []error) {
return it.Database.GetErrors()
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) RowsAffected() int64 {
return it.Database.RowsAffected
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Error() error {
return it.Database.Error
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Since(ago time.Time) Database {
2020-02-22 23:52:05 +00:00
return it.Where("created_at > ?", it.FormatTime(ago))
2020-01-26 21:01:43 +00:00
}
2020-02-19 04:07:22 +00:00
func (it *Db) Between(t1 time.Time, t2 time.Time) Database {
2020-02-22 23:52:05 +00:00
return it.Where("created_at BETWEEN ? AND ?", it.FormatTime(t1), it.FormatTime(t2))
2020-01-26 21:01:43 +00:00
}
2020-02-21 05:36:23 +00:00
type TimeValue struct {
2020-02-24 05:53:15 +00:00
Timeframe string `json:"timeframe"`
Amount float64 `json:"amount"`
2020-02-21 05:36:23 +00:00
}