orm

package module
v0.0.0-...-4ec8d7d Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2015 License: MIT Imports: 13 Imported by: 0

README

##About

server-nado

一个数据库ORM.

Please go to http://github.com/server-nado/orm !

代码已经转移到 http://github.com/server-nado/orm !

How to use?

Insert

go get github.com/server-nado/orm

Super database

sqlite3 "github.com/mattn/go-sqlite3" mysql "github.com/go-sql-driver/mysql" postgree "github.com/lib/pq"

##数据库Model 建立方法

//引用模块
import "github.com/ablegao/orm"

//mysql 驱动
import _ "github.com/go-sql-driver/mysql"

//建立连接 
// 参数分别为 名称 , 驱动, 连接字符串
// 注:必须包含一个default 连接, 作为默认连接。
orm.NewDatabase("default" , "mysql" , "user:passwd@ip/database?charset=utf8&parseTime=true")


//建立一个数据模型。 
type UserInfo struct**** {
	orm.Object
	Id int64 `field:"id" auto:"true" index:"pk"`
	Name string `field:"username"`
	Passwd string `field:"password"`
}

更多信息>>

##新增 CacheModel 模型, 支持分布式redis作为数据库缓存。

import "github.com/ablegao/orm"
import _ "github.com/go-sql-driver/mysql"

type userB struct {
	CacheModule
	Uid     int64  `field:"Id" index:"pk" cache:"user" `
	Alias   string `field:"Alias"`
	Money int64  `field:"money"	`
}

func main(){
	orm.CacheConsistent.Add("127.0.0.1:6379")  //添加多个redis服务器
	orm.SetCachePrefix("nado") //默认nado .  将作为redis key 的前缀
	NewDatabase("default", "mysql", "happy:passwd@tcp(127.0.0.1:3306)/mydatabase?charset=utf8&parseTime=true")


	b := new(userB)
	b.Uid = 10000
	err:=b.Objects(b).One()
	if err!= nil {
		panic(err)
	}
	fmt.Println(b.Uid ,b.Alias ,b.Money)

	b.Incrby("Money" , 100)
	fmt.Println(b.Money)
	b.Save() //不执行不会保存到数据库 只会修改redis数据。 


}

Documentation

Overview

Package consistent provides a consistent hashing function.

Consistent hashing is often used to distribute requests to a changing set of servers. For example, say you have some cache servers cacheA, cacheB, and cacheC. You want to decide which cache server to use to look up information on a user.

You could use a typical hash table and hash the user id to one of cacheA, cacheB, or cacheC. But with a typical hash table, if you add or remove a server, almost all keys will get remapped to different results, which basically could bring your service to a grinding halt while the caches get rebuilt.

With a consistent hash, adding or removing a server drastically reduces the number of keys that get remapped.

Read more about consistent hashing on wikipedia: http://en.wikipedia.org/wiki/Consistent_hashing

Index

Constants

This section is empty.

Variables

View Source
var CacheConsistent = NewConsistent()
View Source
var Debug = log.New(os.Stdout, "ORM-DEBUG", log.Lshortfile|log.LstdFlags)
View Source
var ErrEmptyCircle = errors.New("empty circle")

ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.

View Source
var (
	ErrKeyNotExist = errors.New("keys not exists")
)
View Source
var Error = log.New(os.Stdout, "ORM-ERROR", log.Lshortfile|log.LstdFlags)
View Source
var NULL_LIMIT = [2]int{0, 0}
View Source
var (
	RedisServer = map[string]*redis.Client{}
)

Functions

func GetRedisClient

func GetRedisClient(key string) *redis.Client

func NewMarsharlDriverSql

func NewMarsharlDriverSql(driverName string, fun driversqlType)

func SetCachePrefix

func SetCachePrefix(str string)

func SetDebug

func SetDebug(b bool)

func SetDefaultCacheDb

func SetDefaultCacheDb(db int)

Types

type Cache

type Cache interface {
	Set(key string, b []byte) error
	Get(key string) ([]byte, error)
	Keys(key string) ([]string, error)
	//Incy(key string) (int64, error)
	Incrby(key string, n int64) (int64, error)
	Hset(key, field string, b []byte) (bool, error)
	Hmset(key string, maping interface{}) error
	Hget(key, field string) ([]byte, error)
	Hincrby(key, filed string, n int64) (int64, error)
	Exists(key string) (bool, error)
	Del(key string) (bool, error)
}

func GetCacheConn

func GetCacheConn(key interface{}) (address string, c Cache)

type CacheModule

type CacheModule struct {
	Cache
	Object

	CacheFileds []string
	CacheNames  []string
	// contains filtered or unexported fields
}

func CacheMode

func CacheMode(mode Module) (cache *CacheModule)

func (*CacheModule) All

func (self *CacheModule) All() ([]interface{}, error)

func (*CacheModule) AllOnCache

func (self *CacheModule) AllOnCache() ([]interface{}, error)

func (*CacheModule) Ca

func (self *CacheModule) Ca(key interface{}) *CacheModule

func (*CacheModule) Count

func (self *CacheModule) Count() (int64, error)

func (*CacheModule) CountOnCache

func (self *CacheModule) CountOnCache() (int64, error)

func (*CacheModule) Db

func (self *CacheModule) Db(name string) *CacheModule

func (*CacheModule) DeleteOnCache

func (self *CacheModule) DeleteOnCache() error

func (*CacheModule) Filter

func (self *CacheModule) Filter(name string, val interface{}) *CacheModule

func (*CacheModule) GetCacheKey

func (self *CacheModule) GetCacheKey() string

func (*CacheModule) Incrby

func (self *CacheModule) Incrby(key string, val int64) (ret int64, err error)

func (*CacheModule) Incry

func (self *CacheModule) Incry(key string) (val int64, err error)

func (*CacheModule) Limit

func (self *CacheModule) Limit(page, step int) *CacheModule

func (*CacheModule) Objects

func (self *CacheModule) Objects(mode Module) *CacheModule

func (*CacheModule) One

func (self *CacheModule) One() error

func (*CacheModule) OneOnCache

func (self *CacheModule) OneOnCache() error

func (*CacheModule) Orderby

func (self *CacheModule) Orderby(order ...string) *CacheModule

func (*CacheModule) Save

func (self *CacheModule) Save() (isnew bool, id int64, err error)

func (*CacheModule) SaveToCache

func (self *CacheModule) SaveToCache() error

func (*CacheModule) Set

func (self *CacheModule) Set(key string, val interface{}) (err error)

type CacheModuleInteerface

type CacheModuleInteerface interface {
	Objects(Module) CacheModuleInteerface
	Ca(interface{}) CacheModuleInteerface //一致性hash 默认处理方式
	Db(string) CacheModuleInteerface      //数据库连接
	Filter(name string, val interface{}) CacheModuleInteerface
	GetCacheKey() string
	Incrby(string, int64) (int64, error)
	Incry(string) (int64, error)
	Set(string, interface{}) error
	Save() (bool, int64, error)
	One() error
	SaveToCache() error
	All() ([]interface{}, error)
	AllCache() ([]interface{}, error)
}

type Consistent

type Consistent struct {
	NumberOfReplicas int

	sync.RWMutex
	// contains filtered or unexported fields
}

Consistent holds the information about the members of the consistent hash circle.

func NewConsistent

func NewConsistent() *Consistent

New creates a new Consistent object with a default setting of 20 replicas for each entry.

To change the number of replicas, set NumberOfReplicas before adding entries.

func (*Consistent) Add

func (c *Consistent) Add(elt string)

Add inserts a string element in the consistent hash.

func (*Consistent) Get

func (c *Consistent) Get(name string) (string, error)

Get returns an element close to where name hashes to in the circle.

func (Consistent) GetCircle

func (c Consistent) GetCircle() map[uint32]string

func (*Consistent) GetN

func (c *Consistent) GetN(name string, n int) ([]string, error)

GetN returns the N closest distinct elements to the name input in the circle.

func (*Consistent) GetTwo

func (c *Consistent) GetTwo(name string) (string, string, error)

GetTwo returns the two closest distinct elements to the name input in the circle.

func (*Consistent) Members

func (c *Consistent) Members() []string

func (*Consistent) Remove

func (c *Consistent) Remove(elt string)

Remove removes an element from the hash.

func (*Consistent) Set

func (c *Consistent) Set(elts []string)

Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed.

type Database

type Database struct {
	*sql.DB
	Name           string
	DriverName     string
	DataSourceName string
}

func NewDatabase

func NewDatabase(name, driverName, dataSourceName string) (database *Database, err error)

func (*Database) Conn

func (self *Database) Conn() (err error)

type Module

type Module interface {
	GetTableName() string
}

type ModuleToSql

type ModuleToSql interface {
	Select() (sql string, val []interface{})
	Delete() (sql string, val []interface{})
	Update() (sql string, val []interface{})
	Insert() (sql string, val []interface{})
	Count() (sql string, val []interface{})
	Instance(ParamsInterface)
}

type MysqlModeToSql

type MysqlModeToSql struct {
	Params ParamsInterface
}

func (MysqlModeToSql) Count

func (self MysqlModeToSql) Count() (sql string, val []interface{})

func (MysqlModeToSql) Delete

func (self MysqlModeToSql) Delete() (sql string, val []interface{})

func (MysqlModeToSql) Insert

func (self MysqlModeToSql) Insert() (sql string, val []interface{})

func (MysqlModeToSql) Instance

func (self MysqlModeToSql) Instance(param ParamsInterface)

func (MysqlModeToSql) Select

func (self MysqlModeToSql) Select() (sql string, val []interface{})

func (MysqlModeToSql) Update

func (self MysqlModeToSql) Update() (sql string, val []interface{})

type Object

type Object struct {
	sync.RWMutex
	Params
	// contains filtered or unexported fields
}

func Objects

func Objects(mode Module) Object

func (*Object) All

func (self *Object) All() ([]interface{}, error)

查找数据

func (*Object) Change

func (self *Object) Change(name string, val interface{}) *Object

修改数据 name 结构字段名称 val 结构数据

func (*Object) Count

func (self *Object) Count() (int64, error)

计算数量

func (*Object) Db

func (self *Object) Db(name string) *Object

选择数据库

func (*Object) Delete

func (self *Object) Delete() (int64, error)

删除数据

func (*Object) Filter

func (self *Object) Filter(name string, val interface{}) *Object

条件筛选 name 结构字段名称 val 需要过滤的数据值

func (*Object) FilterOr

func (self *Object) FilterOr(name string, val interface{}) *Object

func (*Object) Filters

func (self *Object) Filters(filters map[string]interface{}) *Object

Filter 的一次传入版本 , 不建议使用 , 因为map 循序不可控

func (*Object) Limit

func (self *Object) Limit(page, steq int) *Object

分页支持

func (*Object) Objects

func (self *Object) Objects(mode Module) *Object

func (*Object) One

func (self *Object) One() error

提取一个数据

func (*Object) Orderby

func (self *Object) Orderby(names ...string) *Object

Order by 排序 , Field__asc Field__desc

func (*Object) Save

func (self *Object) Save() (bool, int64, error)

更新活添加

type Params

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

*

传参解析

*

func (*Params) All

func (self *Params) All() (rows *sql.Rows, err error)

func (*Params) Change

func (self *Params) Change(name string, val interface{})

添加修改

func (*Params) Count

func (self *Params) Count() (int64, error)

func (*Params) Db

func (self *Params) Db(name string) *Params

func (*Params) Delete

func (self *Params) Delete() (res sql.Result, err error)

func (*Params) Filter

func (self *Params) Filter(name string, val interface{}) *Params

func (*Params) FilterOr

func (self *Params) FilterOr(name string, val interface{}) *Params

func (Params) GetFields

func (self Params) GetFields() []string

func (Params) GetLimit

func (self Params) GetLimit() [2]int

func (Params) GetOr

func (self Params) GetOr() []ParmaField

func (Params) GetOrLen

func (self Params) GetOrLen() int

func (Params) GetOrder

func (self Params) GetOrder() []string

func (Params) GetSet

func (self Params) GetSet() []ParmaField

func (Params) GetSetLen

func (self Params) GetSetLen() int

func (Params) GetTableName

func (self Params) GetTableName() string

func (Params) GetWhere

func (self Params) GetWhere() []ParmaField

func (Params) GetWhereLen

func (self Params) GetWhereLen() int

func (*Params) Init

func (self *Params) Init()

func (*Params) Limit

func (self *Params) Limit(page, step int) *Params

func (*Params) One

func (self *Params) One(vals ...interface{}) error

func (*Params) Save

func (self *Params) Save() (bool, int64, error)

func (*Params) SetField

func (self *Params) SetField(fields ...string)

func (*Params) SetTable

func (self *Params) SetTable(tbname string)

type ParamsInterface

type ParamsInterface interface {
	GetOrLen() int
	GetWhereLen() int
	GetSetLen() int
	GetOr() []ParmaField
	GetWhere() []ParmaField
	GetSet() []ParmaField
	GetFields() []string
	GetOrder() []string
	GetLimit() [2]int
	GetTableName() string
}

type ParmaField

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

type PostgressModeToSql

type PostgressModeToSql struct {
	Params ParamsInterface
}

func (PostgressModeToSql) Count

func (self PostgressModeToSql) Count() (sql string, val []interface{})

func (PostgressModeToSql) Delete

func (self PostgressModeToSql) Delete() (sql string, val []interface{})

func (PostgressModeToSql) Insert

func (self PostgressModeToSql) Insert() (sql string, val []interface{})

func (PostgressModeToSql) Instance

func (self PostgressModeToSql) Instance(param ParamsInterface)

func (PostgressModeToSql) Select

func (self PostgressModeToSql) Select() (sql string, val []interface{})

func (PostgressModeToSql) Update

func (self PostgressModeToSql) Update() (sql string, val []interface{})

type SqliteModeToSql

type SqliteModeToSql struct {
	Params ParamsInterface
}

func (SqliteModeToSql) Count

func (self SqliteModeToSql) Count() (sql string, val []interface{})

func (SqliteModeToSql) Delete

func (self SqliteModeToSql) Delete() (sql string, val []interface{})

func (SqliteModeToSql) Insert

func (self SqliteModeToSql) Insert() (sql string, val []interface{})

func (SqliteModeToSql) Instance

func (self SqliteModeToSql) Instance(param ParamsInterface)

func (SqliteModeToSql) Select

func (self SqliteModeToSql) Select() (sql string, val []interface{})

func (SqliteModeToSql) Update

func (self SqliteModeToSql) Update() (sql string, val []interface{})

Jump to

Keyboard shortcuts

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