goform

package
v0.0.0-...-40c044f Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Slave  *gorm.DB
	Master *gorm.DB
)
View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
	if len(values) > 1 {
		var (
			sql             string
			formattedValues []string
			level           = values[0]
			currentTime     = "\n[" + time.Now().Local().Format("2006-01-02 15:04:05.000000") + "]"
			source          = fmt.Sprintf("source: (%v)", values[1])
		)

		messages = []interface{}{source, currentTime}

		if level == "sql" {

			messages = append(messages, fmt.Sprintf(" took: [%.2fms]  \n", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))

			for _, value := range values[4].([]interface{}) {
				indirectValue := reflect.Indirect(reflect.ValueOf(value))
				if indirectValue.IsValid() {
					value = indirectValue.Interface()
					if t, ok := value.(time.Time); ok {
						formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
					} else if b, ok := value.([]byte); ok {
						if str := string(b); isPrintable(str) {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
						} else {
							formattedValues = append(formattedValues, "'<binary>'")
						}
					} else if r, ok := value.(driver.Valuer); ok {
						if value, err := r.Value(); err == nil && value != nil {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						} else {
							formattedValues = append(formattedValues, "NULL")
						}
					} else {
						formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
					}
				} else {
					formattedValues = append(formattedValues, "NULL")
				}
			}

			if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
				sql = values[3].(string)
				for index, value := range formattedValues {
					placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
					sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
				}
			} else {
				formattedValuesLength := len(formattedValues)
				for index, value := range sqlRegexp.Split(values[3].(string), -1) {
					sql += value
					if index < formattedValuesLength {
						sql += formattedValues[index]
					}
				}
			}

			messages = append(messages, sql)
			messages = append(messages, fmt.Sprintf(" \n[%v] ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned "))
		} else {

			messages = append(messages, values[2:]...)

		}
	}

	return
}

LogFormatter ... 自定义日志格式化

Functions

func Initialize

func Initialize()

Types

type BaseModel

type BaseModel struct {
	ID        uint      `gorm:"primary_key"`
	CreatedAt *JSONTime `gorm:"index;type:timestamptz" json:",omitempty"`
	UpdatedAt *JSONTime `gorm:"index;type:timestamptz" json:",omitempty"`
	DeletedAt *JSONTime `gorm:"index;type:timestamptz" json:"-"`
	Version   uint      `gorm:"not null;index;default:1" json:",omitempty"`
}

BaseModel ...

type Conf

type Conf struct {
	ShowSQL bool
	Master  Database
	Slave   Database `yaml:",flow"`
}

Conf All Database configuration Settings

type Database

type Database struct {
	Type     string `yaml:",omitempty"`
	Address  string
	Port     int
	User     string `yaml:",omitempty"`
	Password string `yaml:",omitempty"`
	DB       string `yaml:",omitempty"`
}

Database configuration Settings

type JSONTime

type JSONTime time.Time

JSONTime ...

func Todatetime

func Todatetime(in string) (JSONTime, error)

Todatetime ...

func (*JSONTime) GobDecode

func (p *JSONTime) GobDecode(data []byte) error

GobDecode implements the gob.GobDecoder interface.

func (JSONTime) GobEncode

func (p JSONTime) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface.

func (JSONTime) MarshalJSON

func (p JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*JSONTime) Scan

func (p *JSONTime) Scan(v interface{}) error

Scan valueof time.Time

func (JSONTime) String

func (p JSONTime) String() string

String ...

func (*JSONTime) UnmarshalJSON

func (p *JSONTime) UnmarshalJSON(data []byte) error

UnmarshalJSON ...

func (JSONTime) Value

func (p JSONTime) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

type LogWriter

type LogWriter interface {
	Println(v ...interface{})
}

LogWriter log writer interface

type Logger

type Logger struct {
	LogWriter
}

Logger default logger

func (Logger) Print

func (logger Logger) Print(values ...interface{})

Print format & print log

Jump to

Keyboard shortcuts

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