Documentation
¶
Overview ¶
Package gorm
An ORM framework implementation in Go(based on sgen).
::: quickstart :::
package main
import (
"database/sql" "fmt" . "github.com/billcoding/gorm" "github.com/billcoding/sgen" _ "github.com/go-sql-driver/mysql"
)
const (
testDriverName = "mysql" testDSN = "root:oGMyxw4auP6F6Sn1ENxMVTa1kCc=@tcp(192.168.0.252:3310)/test"
)
var (
testDB, _ = sql.Open(testDriverName, testDSN)
)
type userModel struct {
ID int `orm:"pk{T} column{id} insertIgnore{T} definition{id int not null auto_increment comment 'ID'}"`
Name string `orm:"pk{F} column{name} definition{name varchar(50) not null default 'hello world' comment 'Name'}"`
Age int `orm:"pk{F} column{age} definition{age int not null default '20' comment 'Age'}"`
Address string `orm:"pk{F} column{address} definition{address varchar(100) not null comment 'Address'}"`
Phone string `orm:"pk{F} column{phone} definition{phone varchar(11) not null default '13900000000' comment 'Phone'}"`
CreateTime string `orm:"pk{F} column{create_time} insertIgnore{T} updateIgnore{T} definition{create_time datetime not null default current_timestamp comment 'CreateTime'}"`
XYZ string `orm:"pk{F} column{xyz} definition{xyz varchar(50) not null default 'xyz' comment 'XYZ'}"`
}
func (u *userModel) MetaData() *ModelMeta {
return &ModelMeta{
Migrate: true,
Comment: "The userModel Table",
IndexDefinitions: []sgen.Ge{
sgen.IndexDefinition(false, sgen.P("idx_name"), sgen.C("name")),
},
InsertIgnores: []sgen.C{"id", "create_time"},
}
}
func main() {
DS(testDB)
Register(new(userModel))
o := New(new(userModel))
count, err := o.Select().Count(&userModel{ID: 1})
if err != nil {
fmt.Println(err)
} else {
fmt.Println("count = ", count)
}
}
Index ¶
- Variables
- func DS(db *sql.DB)
- func DSWithName(name string, db *sql.DB)
- func New(model Model) *orm
- func NewWithDS(model Model, ds string) *orm
- func NewWithTx(model Model, tx *sql.Tx, autoCommit bool) *orm
- func NullBool(b bool) sql.NullBool
- func NullBoolPtr(b bool) *sql.NullBool
- func NullByte(b byte) sql.NullByte
- func NullBytePtr(b byte) *sql.NullByte
- func NullFloat64(f float64) sql.NullFloat64
- func NullFloat64Ptr(f float64) *sql.NullFloat64
- func NullInt16(i int16) sql.NullInt16
- func NullInt16Ptr(i int16) *sql.NullInt16
- func NullInt32(i int32) sql.NullInt32
- func NullInt32Ptr(i int32) *sql.NullInt32
- func NullInt64(i int64) sql.NullInt64
- func NullInt64Ptr(i int64) *sql.NullInt64
- func NullString(str string) sql.NullString
- func NullStringPtr(str string) *sql.NullString
- func NullTime(t time.Time) sql.NullTime
- func NullTimePtr(t time.Time) *sql.NullTime
- func Register(model Model)
- func SetConfig(config *Config)
- type Config
- type ExecHooker
- type Model
- type ModelMeta
- type Pagination
- type Strategy
Constants ¶
This section is empty.
Variables ¶
View Source
var ( Configuration = &Config{ Migrate: false, TableNameStrategy: Underline, ColumnNameStrategy: Underline, Logger: logrus.StandardLogger(), InsertHookers: make([]ExecHooker, 0), DeleteHookers: make([]ExecHooker, 0), UpdateHookers: make([]ExecHooker, 0), SelectHookers: make([]ExecHooker, 0), } )
View Source
var (
MySQLPagination = &mysqlPagination{}
)
Functions ¶
func DSWithName ¶
func NullBoolPtr ¶
func NullBytePtr ¶
func NullFloat64 ¶
func NullFloat64(f float64) sql.NullFloat64
func NullFloat64Ptr ¶
func NullFloat64Ptr(f float64) *sql.NullFloat64
func NullInt16Ptr ¶
func NullInt32Ptr ¶
func NullInt64Ptr ¶
func NullString ¶
func NullString(str string) sql.NullString
func NullStringPtr ¶
func NullStringPtr(str string) *sql.NullString
Types ¶
type Config ¶
type Config struct {
// Migrate defines migrate create or update table in the database
Migrate bool
// TableNameStrategy defines binding table name for Model struct
TableNameStrategy Strategy
// TableNameStrategy defines binding table's column name for Model's Fields
ColumnNameStrategy Strategy
// Logger defines logger for gorm
Logger *logrus.Logger
InsertHookers []ExecHooker
DeleteHookers []ExecHooker
UpdateHookers []ExecHooker
SelectHookers []ExecHooker
}
Config defines configuration struct
type ExecHooker ¶
type ExecHooker interface {
BeforeExec(model Model, sqlStr *string, ps *[]interface{})
AfterExec(model Model, sqlStr string, ps []interface{}, err error)
}
ExecHooker defines exec hooker before and after exec SQL
type Model ¶
type Model interface {
MetaData() *ModelMeta
}
Model defines standard Model interface and implements to marked a Model struct
type ModelMeta ¶
type ModelMeta struct {
// Migrate defines migrate create or update table in the database
Migrate bool
// Table defines table name for the Model
// If Table is empty, set from Configuration's TableNameStrategy
Table string
// Comment defines table comment for the Model
Comment string
// Table defines column name Strategy for the Model's Fields
// see strategy.Default, strategy.Underline, strategy.CamelCase,
ColumnNameStrategy Strategy
// DB defines DS name from dsMap, default: `_` or `master`
DS string
// PrimaryKeyColumns defines Table primary key columns
PrimaryKeyColumns []sgen.C
// ColumnDefinitions defines Table column definitions
// Use sgen.ColumnDefinition to build
ColumnDefinitions []sgen.Ge
// Indexes defines Table index definitions
// Use sgen.IndexDefinition to build
IndexDefinitions []sgen.Ge
// InsertIgnores defines Table insert ignore columns
InsertIgnores []sgen.C
// UpdateIgnores defines Table update ignore columns
UpdateIgnores []sgen.C
}
ModelMeta defines Model struct meta info
type Pagination ¶
Pagination defines Pagination plugin
type Strategy ¶
type Strategy int
const ( // Default defines default not changed // Model:User => Table:User // Field:UserName => Column:UserName Default Strategy = 1 + iota // Underline defines to transform a underline style name // Model:UserModel => Table:user_model // Field:UserName => Column:user_name Underline // CamelCase defines to transform a camelcase style name // Model:UserModel => Table:userModel // Field:UserName => Column:userName CamelCase )
Click to show internal directories.
Click to hide internal directories.