ormd

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2020 License: MIT Imports: 20 Imported by: 0

README

MGEs.ORMd

MGEs框架下,基于jinzhu/gorm的简单生命周期封装。

依赖

  • MGEs.Base^0.3.1

  • jinzhu/gorm^1.9.8

  • @Attention:主程序必须调用workflow.Lifecycle_Start,否则服务不会运行!

安装

go get gitlab.com/MGEs/ORMd

例子

package main

import (
	"github.com/jinzhu/gorm"

	"gitlab.com/MGEs/Base/workflow"
	ormd "gitlab.com/MGEs/ORMd"
)

var ormClient *gorm.DB

func init(){
	if cfg, err := ormd.NewConfig(ormd.ConfigEntry{
		Comment:    "SSO",
		Mode:       ormd.ConfigMode_Flag | ormd.ConfigMode_File,
		FlagPrefix: "-ormd",
		FileEntry:  []string{"api", "ormd"},
	}); err != nil {
		workflow.Throw(err, workflow.TlPanic)
	} else if iErr := cfg.Setup(func(db *gorm.DB) []error {
		ormClient = db
		ormClient.LogMode(true)
		return nil
	}); iErr != nil {
		workflow.Throw(iErr, workflow.TlPanic)
	}
}

func main(){
	if err := workflow.Lifecycle_Start(); err != nil {
        panic(err)
    }
}

Licensing

The MIT License (MIT)

Copyright (c) 2018 - 2020 Kitagawa Kenta.

Documentation

Index

Constants

View Source
const (
	ConfigMode_Internal = 1 << (8 - 1 - iota)
	ConfigMode_Flag
	ConfigMode_File
)
View Source
const (
	EVT_LCSubscriber = "ORMd.BootUp"
)

Variables

View Source
var (
	EM_ORMd_EmptyConfig = workflow.NewMask(
		"ORMd:EmptyConfig",
		"ORMd:缺少关键的配置字段",
	)
	EM_ORMd_FailedToRegisterParameters = workflow.NewMask(
		"ORMd:FailedToRegisterParameters",
		"ORMd:注册参数失败",
	)
	EM_ORMd_FailedToGetDriverContext = workflow.NewMask(
		"ORMd:FailedToGetDriverContext",
		"ORMd:未能成功获取驱动的上下文对象",
	)
	EM_ORMd_UndefinedDriverName = workflow.NewMask(
		"ORMd:UndefinedDriverName",
		"ORMd:未定义驱动名称",
	)
	EM_ORMd_UnsupportedDriver = workflow.NewMask(
		"ORMd:UnsupportedDriver",
		"ORMd:不受支持的驱动名称{{name}}",
	)
	EM_ORMd_FailedToRegisterEvent = workflow.NewMask(
		"ORMd:FailedToRegisterEvent",
		"ORMd:注册事件失败",
	)
	EM_ORMd_FailedToCreateInstance = workflow.NewMask(
		"ORMd:FailedToCreateInstance",
		"ORMd:实例化数据库配置失败",
	)
	EM_ORMd_FailedToConfigInstance = workflow.NewMask(
		"ORMd:FailedToConfigInstance",
		"ORMd:配置数据库实例失败",
	)
	EM_ORMd_InstanceCreated = workflow.NewMask(
		"ORMd:InstanceCreated",
		"[ORMd] 已创建链接 {{name}}",
	)
	EM_ORMd_InstanceClosed = workflow.NewMask(
		"ORMd:InstanceClosed",
		"[ORMd] 已关闭链接 {{name}}",
	)
)
View Source
var (
	EM_ORMd_FailedToMigrate = workflow.NewMask(
		"ORMd:FailedToMigrate",
		"ORMd:同步数据结构失败",
	)
	EM_ORMd_FailedToGetInstance = workflow.NewMask(
		"ORMd:FailedToGetInstance",
		"ORMd:获取数据库实例失败",
	)
	EM_ORMd_FailedToListModule = workflow.NewMask(
		"ORMd:FailedToListModule",
		"ORMd:未能成功列举数据模型",
	)
	EM_ORMd_UndefinedColumn = workflow.NewMask(
		"ORMd:UndefinedColumn",
		"ORMd:未定义的列: {{table}}.{{column}}",
	)
	Err_ORMd_UninitializedInstance = errors.New(
		"ORMd:UninitializedInstance",
	)
)
View Source
var (
	EM_Logger_Other = workflow.NewMask(
		"ORMd:Message",
		"[ORMd] {{message}}",
	)
	EM_Logger_Sql_Params = workflow.NewMask(
		"ORMd:SQL",
		"[ORMd][SQL] {{duration}}, {{affected}} rows affected\n{{source}}\n{{sql}}{{params}}",
	)
)
View Source
var DriverList = []string{"mysql", "postgres"}

Functions

func GetTableColumnName

func GetTableColumnName(db *gorm.DB, table interface{}, column string, quote bool) string

func GetTableColumnReference

func GetTableColumnReference(db *gorm.DB, table interface{}, column string) string

Types

type Config

type Config struct {
	Comment    string
	DriverName string
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(entry ConfigEntry) (*Config, error)

func (*Config) GetDSN

func (c *Config) GetDSN() (string, error)

func (*Config) GetDriverSection

func (c *Config) GetDriverSection(name string) (Config_DriverSection, error)

func (*Config) Setup

func (c *Config) Setup(setup func(*gorm.DB) []error) error

type ConfigEntry

type ConfigEntry struct {
	Comment    string
	Mode       ConfigMode
	FlagPrefix string
	FlagGroup  *flags.Group
	FileEntry  []string
}

type ConfigMode

type ConfigMode uint8

type Config_DriverSection

type Config_DriverSection interface {
	GetDSN() (string, error)
}

type Config_Mysql

type Config_Mysql struct {
	*mysql.Config
}

func NewConfig_Mysql

func NewConfig_Mysql() *Config_Mysql

func (*Config_Mysql) GetDSN

func (c *Config_Mysql) GetDSN() (string, error)

type Config_Postgres

type Config_Postgres struct {
	User   string
	Passwd string
	Addr   string
	DBName string
	Params map[string]string
}

func NewConfig_Postgres

func NewConfig_Postgres() *Config_Postgres

func (*Config_Postgres) GetDSN

func (c *Config_Postgres) GetDSN() (string, error)

type Errors

type Errors []error

func ErrorOccurred

func ErrorOccurred(err error) Errors

func (Errors) RecordNotFound

func (e Errors) RecordNotFound() bool

type Logger

type Logger struct{}

func NewLogger

func NewLogger() *Logger

func (Logger) Print

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

type Table added in v0.2.3

type Table interface {
	TableName() string
	MigrateTo(db *gorm.DB) (e error)
}

type TableField

type TableField interface {
	fmt.Stringer
	GetDBName(db *gorm.DB, quote bool) string
	Editable() bool
	Filterable() bool
	Sortable() bool
	Preloadable() bool
}

Jump to

Keyboard shortcuts

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