adm

package module
v0.0.0-...-e5bac01 Latest Latest
Warning

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

Go to latest
Published: May 6, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

README

Go ADM - Active Data Model

Go语言ORM,暂时只支持MySQL,本来并不打算做成ORM,不过实在是没办法。

目前封装包含了:

  1. 数据库配置 - XmlConfig
  2. 连接器 - Connection
  3. 查询结果集 - ResultSet, ResultRow, ExecResult
  4. 查询构造器 - QueryBuilder
  5. 数据模型和事件 - Model, ModelEvents
  6. 数据模型映射 - ModelMapping
  7. 0.1.0 分支说明

已经将对Model的反射集中到ModelMapping中(mapping.go),并且go-agi库的type_conv.go中增加了大量的类型转换的方法(尤其是AnyTo*开头的函数,详情请看:type_conv.go)。

mapping.go定义的是数据模型的反射逻辑。

model.go则是外部对数据模型的操作。

修改后的数据装箱的性能成倍提升了,具体多少就不说了,我比较满意。

目前可用度已达到80%了,除了缓存部分的接口设计以外,不会再增加新的接口和库的规模。

[2015.05.07] 0.1.0分支已经实现Model面向对象化操作,详情请参考0.1.0 分支说明

[2015.05.06] 推送一个0.1.0的分支,0.1.0的Model改为一个结构,原来的接口改名为ModelInterface,暂不作为主分支推送,详细可参考0.1.0 分支说明

[2015.05.02] 重构model.go部分的代码,分离Model的数据操作,和ModelMapping(模型反射)两个区块,代码可控性更强,结构更清晰。

[2015.05.01] 初步实现ORM,大量hardcode来实现的反射

[2015.04.29] 初步完成数据库查询构造器、数据库配置加载、数据库连接器的功能。

[2015.04.28] 本库诞生……

需要基于go-agi这个库。

go get git.oschina.net/janpoem/go-agi
go get git.oschina.net/janpoem/go-adm

详细的使用说明,请查看Wiki

主分支的问题,基本在分支0.1.0已经解决,包括Model面向对象化的操作,详情请参阅0.1.0 分支说明

Documentation

Index

Constants

View Source
const (
	TagColumn = "col"
	TagPk     = "pk"
	TagOn     = "on"

	TagValNothing  = ""
	TagValPkAi     = "ai"
	TagValPkNormal = "nor"

	OnCreate = "create"
	OnUpdate = "update"
)
View Source
const (
	EventBreak    = false
	EventContinue = true
)
View Source
const (
	MARRY_AND = iota
	MARRY_OR
)
View Source
const (
	SQL_AND          = "AND"
	SQL_OR           = "OR"
	SQL_ASC          = "ASC"
	SQL_DESC         = "DESC"
	SQL_AS           = "AS"
	SQL_NONE         = ""
	SQL_QUERY        = "?"
	SQL_COL_SPR      = ","
	SQL_QUERY_HOLDER = SQL_COL_SPR + SQL_QUERY
	SQL_IN           = "IN"
	SQL_NOT_IN       = "NOT IN"
)
View Source
const (
	T_STR = iota
	T_INT
	T_FLOAT
)

Variables

This section is empty.

Functions

func Asc

func Asc(field string) string

func AssignMap

func AssignMap(obj Model, m map[string]interface{})

func AssignMapByField

func AssignMapByField(obj Model, m map[string]interface{})

func AssignSlice

func AssignSlice(obj Model, values []interface{})

func AssignStruct

func AssignStruct(obj Model, target interface{})

func Col

func Col(args ...string) string

Col("name") => "name" Col("name", "f1") => "name as f1"

func Desc

func Desc(field string) string

func Export

func Export(obj Model) map[string]interface{}

func InToSql

func InToSql(field string, len int, negative bool) string

func LoadConf

func LoadConf(path string)

func Marry

func Marry(marry int) string

func MkQueryHolder

func MkQueryHolder(size int) string

func PkValid

func PkValid(obj Model) bool

判断是否有有效主键值

Types

type Conditions

type Conditions struct {
	SQL    string
	Params []interface{}
}

func (*Conditions) And

func (this *Conditions) And(sql string, params ...interface{}) *Conditions

func (*Conditions) IsEmpty

func (this *Conditions) IsEmpty() bool

func (*Conditions) Push

func (this *Conditions) Push(marry int, sql string, params ...interface{}) *Conditions

func (*Conditions) PushParams

func (this *Conditions) PushParams(params ...interface{}) *Conditions

func (*Conditions) PushSQL

func (this *Conditions) PushSQL(marry int, sql string) *Conditions

type Connection

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

func Conn

func Conn(name string) *Connection

func (*Connection) Delete

func (this *Connection) Delete(query *QueryBuilder) *ExecResult

func (*Connection) Exec

func (this *Connection) Exec(t QueryType, sql string, params ...interface{}) *ExecResult

func (*Connection) Find

func (this *Connection) Find(query *QueryBuilder) *ResultSet

func (*Connection) GetDB

func (this *Connection) GetDB() (*sql.DB, error)

func (*Connection) GetErr

func (this *Connection) GetErr() error

func (*Connection) GetName

func (this *Connection) GetName() string

func (*Connection) HasErr

func (this *Connection) HasErr() bool

func (*Connection) Insert

func (this *Connection) Insert(query *QueryBuilder) *ExecResult

func (*Connection) IsConn

func (this *Connection) IsConn() bool

func (*Connection) Query

func (this *Connection) Query(sql string, params ...interface{}) *ResultSet

func (*Connection) Update

func (this *Connection) Update(query *QueryBuilder) *ExecResult

type EventSignal

type EventSignal bool

type ExecResult

type ExecResult struct {
	QueryType    QueryType
	LastInsertId int64
	RowsAffected int64
	Error        error
}

func NewExecResult

func NewExecResult(t QueryType, rs sql.Result, err error) *ExecResult

func (*ExecResult) ExecError

func (this *ExecResult) ExecError() error

func (*ExecResult) ExecHasError

func (this *ExecResult) ExecHasError() bool

func (*ExecResult) IsCreate

func (this *ExecResult) IsCreate() bool

func (*ExecResult) IsDelete

func (this *ExecResult) IsDelete() bool

func (*ExecResult) IsSave

func (this *ExecResult) IsSave() bool

func (*ExecResult) IsSuccess

func (this *ExecResult) IsSuccess() bool

func (*ExecResult) IsUpdate

func (this *ExecResult) IsUpdate() bool

type Model

type Model interface {
	GetRow() *ResultRow
	Conn() *Connection
	Table() string
	IsExists() bool
	IsNew() bool
	// Event Api
	OnBind() // 增加一个OnBind接口
	BeforeUpdate() EventSignal
	BeforeCreate() EventSignal
	BeforeSave() EventSignal
	BeforeDestroy() EventSignal
	AfterUpdate()
	AfterCreate()
	AfterSave()
	AfterDestroy()
}

func Bind

func Bind(row *ResultRow, obj Model) Model

type ModelColumn

type ModelColumn struct {
	// 结构的属性名
	Name string
	// 数据库存储的字段名
	Column string
	// 该字段在Model的属性集合中的Index,用于从一个Model实例中快速获取该字段
	Index int
}

type ModelEvents

type ModelEvents struct {
}

func (*ModelEvents) AfterCreate

func (this *ModelEvents) AfterCreate()

func (*ModelEvents) AfterDestroy

func (this *ModelEvents) AfterDestroy()

func (*ModelEvents) AfterSave

func (this *ModelEvents) AfterSave()

func (*ModelEvents) AfterUpdate

func (this *ModelEvents) AfterUpdate()

func (*ModelEvents) BeforeCreate

func (this *ModelEvents) BeforeCreate() EventSignal

func (*ModelEvents) BeforeDestroy

func (this *ModelEvents) BeforeDestroy() EventSignal

func (*ModelEvents) BeforeSave

func (this *ModelEvents) BeforeSave() EventSignal

func (*ModelEvents) BeforeUpdate

func (this *ModelEvents) BeforeUpdate() EventSignal

func (*ModelEvents) OnBind

func (this *ModelEvents) OnBind()

type ModelMapping

type ModelMapping struct {
	// 主键的字段,实际上指向的是一个ModelColumn
	Pk *ModelColumn
	// 主键的模式
	PkMode PkMode
	// 映射的字段
	Columns []*ModelColumn
	// 字段的总数
	Size int
}

func GetModelMapping

func GetModelMapping(obj Model) *ModelMapping

type Ope

type Ope int8
const (
	OpeNone Ope = iota
	OpeCreate
	OpeUpdate
	OpeSelect
	OpeDelete
)

type PkMode

type PkMode int8
const (
	PkNone PkMode = iota
	PkAi
	PkNormal
)

type QueryBuilder

type QueryBuilder struct {

	//////////////////////////////////////////
	// 关联的字段,只在Select和Insert中有效
	Columns []string
	// 要操作的目标的表
	// select时,为查询的表,不包含join的表
	// insert, update, delete为操作的主表
	TargetTable string
	// 查询条件
	Conditions *Conditions
	// limit, 0 -> offset, 1 -> limit
	LimitOffset [2]int
	// 排序字段
	OrderColumns []string
	// GROUP BY
	GroupColumn string
	//////////////////////////////////////////
	// Insert
	InsertData [][]interface{}

	//////////////////////////////////////////
	// Update
	UpdateData map[string]interface{}
	// contains filtered or unexported fields
}

func Delete

func Delete(table string) *QueryBuilder

func Insert

func Insert(table string) *QueryBuilder

func Select

func Select(columns ...string) *QueryBuilder

构造一个select查询 Select还是恢复查询字段的参数模式

func Update

func Update(table string) *QueryBuilder

func (*QueryBuilder) ClearValues

func (this *QueryBuilder) ClearValues() *QueryBuilder

func (*QueryBuilder) Cols

func (this *QueryBuilder) Cols(columns ...string) *QueryBuilder

追加字段

func (*QueryBuilder) Find

func (this *QueryBuilder) Find(obj Model) *ResultSet

func (*QueryBuilder) FindOne

func (this *QueryBuilder) FindOne(obj Model) *ResultSet

将FindOne绑定到QueryBuilder,这样更符合语义化 adm.Select().Where("id > 100").FindOne()

func (*QueryBuilder) GetExecSQLParams

func (this *QueryBuilder) GetExecSQLParams() (string, []interface{})

func (*QueryBuilder) GetInsertSize

func (this *QueryBuilder) GetInsertSize() int

func (*QueryBuilder) GetJoinColumns

func (this *QueryBuilder) GetJoinColumns() string

func (*QueryBuilder) GetParams

func (this *QueryBuilder) GetParams() []interface{}

go 1.4.2 这里返回一个nil会自动转为一个 [] 空数组

func (*QueryBuilder) GetQueryName

func (this *QueryBuilder) GetQueryName() string

func (*QueryBuilder) GetQueryType

func (this *QueryBuilder) GetQueryType() QueryType

func (*QueryBuilder) GetSQL

func (this *QueryBuilder) GetSQL() string

func (*QueryBuilder) Group

func (this *QueryBuilder) Group(column string) *QueryBuilder

暂时只支持一个字段的Group

func (*QueryBuilder) HasConds

func (this *QueryBuilder) HasConds() bool

func (*QueryBuilder) In

func (this *QueryBuilder) In(field string, values ...interface{}) *QueryBuilder

func (*QueryBuilder) Limit

func (this *QueryBuilder) Limit(limit int) *QueryBuilder

func (*QueryBuilder) NotIn

func (this *QueryBuilder) NotIn(field string, values ...interface{}) *QueryBuilder

func (*QueryBuilder) Offset

func (this *QueryBuilder) Offset(offset int, limit int) *QueryBuilder

func (*QueryBuilder) OrIn

func (this *QueryBuilder) OrIn(field string, values ...interface{}) *QueryBuilder

func (*QueryBuilder) OrNotIn

func (this *QueryBuilder) OrNotIn(field string, values ...interface{}) *QueryBuilder

func (*QueryBuilder) OrWhere

func (this *QueryBuilder) OrWhere(sql string, params ...interface{}) *QueryBuilder

func (*QueryBuilder) Order

func (this *QueryBuilder) Order(args ...string) *QueryBuilder

func (*QueryBuilder) Set

func (this *QueryBuilder) Set(key string, value interface{}) *QueryBuilder

func (*QueryBuilder) SetCols

func (this *QueryBuilder) SetCols(columns ...string) *QueryBuilder

直接覆盖现有的所有字段

func (*QueryBuilder) SetMap

func (this *QueryBuilder) SetMap(values map[string]interface{}) *QueryBuilder

func (*QueryBuilder) Table

func (this *QueryBuilder) Table(args ...string) *QueryBuilder

修改目标表

func (*QueryBuilder) Values

func (this *QueryBuilder) Values(values ...interface{}) *QueryBuilder

func (*QueryBuilder) Verify

func (this *QueryBuilder) Verify() error

func (*QueryBuilder) Where

func (this *QueryBuilder) Where(sql string, params ...interface{}) *QueryBuilder

type QueryType

type QueryType int8
const (
	QUERY_SELECT QueryType = iota
	QUERY_INSERT
	QUERY_UPDATE
	QUERY_DELETE
)

type ResultRow

type ResultRow struct {
	Error error
	// contains filtered or unexported fields
}

func (*ResultRow) Bool

func (this *ResultRow) Bool(field string) bool

数据库一般都会习惯用0, 1来表达值false or true 也有的时候,通过 0 => 强制等价于false

func (*ResultRow) Compare

func (this *ResultRow) Compare(values map[string]interface{}) map[string]interface{}

func (*ResultRow) Fetch

func (this *ResultRow) Fetch(obj Model) Model

func (*ResultRow) Float

func (this *ResultRow) Float(field string) float64

func (*ResultRow) Get

func (this *ResultRow) Get(f string) []byte

默认返回byte

func (*ResultRow) GetRow

func (this *ResultRow) GetRow() *ResultRow

func (*ResultRow) Int

func (this *ResultRow) Int(field string) int

func (*ResultRow) Int64

func (this *ResultRow) Int64(field string) int64

func (*ResultRow) IntTime

func (this *ResultRow) IntTime(field string) time.Time

用整型表达的时间值,因为我们已经习惯了用整型值来表达时间 不过为了预留配对标准的时间戳、Data、Time,暂时不占用Time的方法名 这个方法还是应该只返回一个结果

func (*ResultRow) IsExists

func (this *ResultRow) IsExists() bool

func (*ResultRow) IsNew

func (this *ResultRow) IsNew() bool

func (*ResultRow) Round

func (this *ResultRow) Round(field string, places int) float64

func (*ResultRow) Str

func (this *ResultRow) Str(f string) string

type ResultSet

type ResultSet struct {
	Columns []string

	Error error
	// contains filtered or unexported fields
}

func NewQueryResult

func NewQueryResult(rawRows *sql.Rows, err error) *ResultSet

func (*ResultSet) AddRow

func (this *ResultSet) AddRow(rawBytes [][]byte, error error)

func (*ResultSet) ColumnOf

func (this *ResultSet) ColumnOf(field string) int

func (*ResultSet) Count

func (this *ResultSet) Count() int

func (*ResultSet) Each

func (this *ResultSet) Each(fn func(int, *ResultRow)) *ResultSet

func (*ResultSet) GetErr

func (this *ResultSet) GetErr() error

func (*ResultSet) HasErr

func (this *ResultSet) HasErr() bool

func (*ResultSet) IsEmpty

func (this *ResultSet) IsEmpty() bool

func (*ResultSet) Row

func (this *ResultSet) Row(index int) *ResultRow

func (*ResultSet) Rows

func (this *ResultSet) Rows() []*ResultRow

type State

type State int8
const (
	StateFail State = iota - 1
	StateNothing
	StateUpdated
)

func Create

func Create(obj Model) State

创建,需要补充两个字段,自增主键,和创建时间

func Destroy

func Destroy(obj Model) State

func Save

func Save(obj Model) State

func Upgrade

func Upgrade(obj Model) State

更新操作,数据实例 本身已经持有数据,更新操作需要将本身 实例 所持有的数据和数据源进行差异化比较 目前比较使用字符串的比较

func (State) Fail

func (s State) Fail() bool

func (State) Nothing

func (s State) Nothing() bool

func (State) Success

func (s State) Success() bool

func (State) Updated

func (s State) Updated() bool

type XmlConfig

type XmlConfig struct {
	XMLName xml.Name `xml:"databases"`
	Dbs     []XmlDb  `xml:"database"`
}

type XmlDb

type XmlDb struct {
	XMLName  xml.Name   `xml:"database"`
	DSN      string     `xml:"dsn,attr"`
	Name     string     `xml:"name,attr"`
	Host     string     `xml:"host"`
	Port     int        `xml:"port"`
	Db       string     `xml:"db"`
	User     string     `xml:"user"`
	Password string     `xml:"password"`
	MaxConn  int        `xml:"maxConn"`
	MaxIdle  int        `xml:"maxIdle"`
	Charset  string     `xml:"charset"`
	Options  []XmlDbOps `xml:"option"`
}

func GetConf

func GetConf(name string) *XmlDb

func (*XmlDb) GetCharset

func (this *XmlDb) GetCharset() string

func (*XmlDb) GetConnDSN

func (this *XmlDb) GetConnDSN() string

func (*XmlDb) GetOpsStr

func (this *XmlDb) GetOpsStr() string

func (*XmlDb) GetPort

func (this *XmlDb) GetPort() int

type XmlDbOps

type XmlDbOps struct {
	XMLName xml.Name `xml:"option"`
	Name    string   `xml:"name,attr"`
	Value   string   `xml:"value,attr"`
}

Jump to

Keyboard shortcuts

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