xgorm

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2021 License: MIT Imports: 14 Imported by: 0

README

xgorm

Dependencies

  • github.com/Aoi-hosizora/ahlib
  • github.com/jinzhu/gorm
  • github.com/go-sql-driver/mysql
  • github.com/mattn/go-sqlite3 (cgo)
  • github.com/lib/pq
  • github.com/sirupsen/logrus

Documents

Types
  • type GormTime struct
  • type GormTime2 struct
  • type PropertyValue struct
  • type PropertyDict map
  • type ILogger interface
  • type SilenceLogger struct
  • type LogrusLogger struct
  • type LoggerLogger struct
Variables
  • None
Constants
  • const DefaultDeletedAtTimestamp string
  • const MySQLDuplicateEntryErrno int
  • const SQLiteUniqueConstraintErrno int
  • const PostgreSQLUniqueViolationErrno string
Functions
  • func HookDeletedAt(db *gorm.DB, deletedAtTimestamp string)
  • func IsMySQL(db *gorm.DB) bool
  • func IsSQLite(db *gorm.DB) bool
  • func IsPostgreSQL(db *gorm.DB) bool
  • func IsMySQLDuplicateEntryError(err error) bool
  • func IsSQLiteUniqueConstraintError(err error) bool
  • func IsPostgreSQLUniqueViolationError(err error) bool
  • func QueryErr(rdb *gorm.DB) (xstatus.DbStatus, error)
  • func CreateErr(rdb *gorm.DB) (xstatus.DbStatus, error)
  • func UpdateErr(rdb *gorm.DB) (xstatus.DbStatus, error)
  • func DeleteErr(rdb *gorm.DB) (xstatus.DbStatus, error)
  • func NewPropertyValue(reverse bool, destinations ...string) *PropertyValue
  • func GenerateOrderByExp(source string, dict PropertyDict) string
  • func NewSilenceLogger() *SilenceLogger
  • func NewLogrusLogger(logger *logrus.Logger) *LogrusLogger
  • func NewLoggerLogger(logger logrus.StdLogger) *LoggerLogger
Methods
  • func (p *PropertyValue) Destinations() []string
  • func (p *PropertyValue) Reverse() bool
  • func (g *SilenceLogger) Print(...interface{})
  • func (g *LogrusLogger) Print(v ...interface{})
  • func (g *LoggerLogger) Print(v ...interface{})

Documentation

Index

Constants

View Source
const (
	MySQLDuplicateEntryErrno       = 1062      // MySQLDuplicateEntryErrno is MySQL's ER_DUP_ENTRY errno.
	SQLiteUniqueConstraintErrno    = 19 | 8<<8 // SQLiteUniqueConstraintErrno is SQLite's CONSTRAINT_UNIQUE extended errno.
	PostgreSQLUniqueViolationErrno = "23505"   // PostgreSQLUniqueViolationErrno is PostgreSQL's unique_violation errno.
)

Reference from http://go-database-sql.org/errors.html.

MySQL: https://github.com/VividCortex/mysqlerr/blob/master/mysqlerr.go and https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.htm, SQLite: https://github.com/mattn/go-sqlite3/blob/master/error.go and http://www.sqlite.org/c3ref/c_abort_rollback.html, PostgreSQL: https://github.com/lib/pq/blob/master/error.go and https://www.postgresql.org/docs/10/errcodes-appendix.html.

View Source
const (

	// DefaultDeletedAtTimestamp represents the default value of GormTime.DeletedAt.
	DefaultDeletedAtTimestamp = "1970-01-01 00:00:01"
)

Variables

This section is empty.

Functions

func CreateErr

func CreateErr(rdb *gorm.DB) (xstatus.DbStatus, error)

CreateErr checks gorm.DB create result, will only return xstatus.DbExisted, xstatus.DbFailed and xstatus.DbSuccess.

func DeleteErr

func DeleteErr(rdb *gorm.DB) (xstatus.DbStatus, error)

DeleteErr checks gorm.DB delete result, will only return xstatus.DbFailed, xstatus.DbNotFound and xstatus.DbSuccess.

func GenerateOrderByExp

func GenerateOrderByExp(source string, dict PropertyDict) string

GenerateOrderByExp returns a generated orderBy expresion by given source dto order string (split by ",", such as "name desc, age asc") and PropertyDict. The generated expression is in mysql-sql and neo4j-cypher style, that is "xx ASC", "xx DESC".

func HookDeletedAt

func HookDeletedAt(db *gorm.DB, deletedAtTimestamp string)

HookDeletedAt hooks gorm.DB to replace the soft-delete callback (including query, row_query, update, delete) using the new deletedAt timestamp.

func IsMySQL

func IsMySQL(db *gorm.DB) bool

IsMySQL checks if the dialect of given gorm.DB is "mysql".

func IsMySQLDuplicateEntryError

func IsMySQLDuplicateEntryError(err error) bool

IsMySQLDuplicateEntryError checks if err is MySQL's ER_DUP_ENTRY error.

func IsPostgreSQL

func IsPostgreSQL(db *gorm.DB) bool

IsPostgreSQL checks if the dialect of given gorm.DB is "postgres".

func IsPostgreSQLUniqueViolationError

func IsPostgreSQLUniqueViolationError(err error) bool

IsPostgreSQLUniqueViolationError checks if err is PostgreSQL's unique_violation error.

func IsSQLite

func IsSQLite(db *gorm.DB) bool

IsSQLite checks if the dialect of given gorm.DB is "sqlite3".

func IsSQLiteUniqueConstraintError

func IsSQLiteUniqueConstraintError(err error) bool

IsSQLiteUniqueConstraintError checks if err is SQLite's ErrConstraintUnique error.

func QueryErr

func QueryErr(rdb *gorm.DB) (xstatus.DbStatus, error)

QueryErr checks gorm.DB query result, will only return xstatus.DbNotFound, xstatus.DbFailed and xstatus.DbSuccess.

func UpdateErr

func UpdateErr(rdb *gorm.DB) (xstatus.DbStatus, error)

UpdateErr checks gorm.DB update result, will only return xstatus.DbExisted, xstatus.DbFailed, xstatus.DbNotFound and xstatus.DbSuccess.

Types

type GormTime

type GormTime struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index" gorm:"default:'1970-01-01 00:00:01'"`
}

GormTime represents a structure of CreatedAt, UpdatedAt, DeletedAt (defaults to "1970-01-01 00:00:01"), is a replacement of gorm.Model.

type GormTime2

type GormTime2 struct {
	CreatedAt time.Time
	UpdatedAt time.Time
}

GormTime2 represents a structure of CreatedAt, UpdatedAt, which allow you to customize the DeletedAt field, is a replacement of gorm.Model.

type ILogger

type ILogger interface {
	Print(v ...interface{})
}

ILogger represents gorm's internal logger interface.

type LoggerLogger

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

LoggerLogger represents a gorm's logger, used to log "SQL" and "INFO" message to logrus.StdLogger.

func NewLoggerLogger

func NewLoggerLogger(logger logrus.StdLogger) *LoggerLogger

NewLoggerLogger creates a new LoggerLogger using given logrus.StdLogger. Example:

db, err := gorm.Open("mysql", dsl)
db.LogMode(true) // must be true
l := log.New(os.Stderr, "", log.LstdFlags)
db.SetLogger(xgorm.NewLoggerLogger(l))

func (*LoggerLogger) Print

func (g *LoggerLogger) Print(v ...interface{})

Print logs to logrus.StdLogger, see gorm.LogFormatter for details.

type LogrusLogger

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

LogrusLogger represents a gorm's logger, used to log "SQL" and "INFO" message to logrus.Logger.

func NewLogrusLogger

func NewLogrusLogger(logger *logrus.Logger) *LogrusLogger

NewLogrusLogger creates a new LogrusLogger using given logrus.Logger. Example:

db, err := gorm.Open("mysql", dsl)
db.LogMode(true) // must be true
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{})
db.SetLogger(xgorm.NewLogrusLogger(l))

func (*LogrusLogger) Print

func (g *LogrusLogger) Print(v ...interface{})

Print logs to logrus.Logger, see gorm.LogFormatter for details.

type PropertyDict

type PropertyDict = orderby.PropertyDict

PropertyDict represents a DTO-PO PropertyValue dictionary, used in GenerateOrderByExp.

type PropertyValue

type PropertyValue = orderby.PropertyValue

PropertyValue represents a PO entity's property mapping rule.

func NewPropertyValue

func NewPropertyValue(reverse bool, destinations ...string) *PropertyValue

NewPropertyValue creates a PropertyValue by given reverse and destinations.

type SilenceLogger

type SilenceLogger struct{}

SilenceLogger represents a gorm's logger, used to hide "SQL" and "INFO" logs. Note that `gorm.DB.LogMode(false)` will only hide "SQL" message.

func NewSilenceLogger

func NewSilenceLogger() *SilenceLogger

NewSilenceLogger creates a new SilenceLogger. Example:

db, err := gorm.Open("mysql", dsl)
db.LogMode(true) // both true and false are ok
db.SetLogger(xgorm.NewSilenceLogger())

func (*SilenceLogger) Print

func (g *SilenceLogger) Print(...interface{})

Print does nothing for log.

Jump to

Keyboard shortcuts

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