xorm

package
v3.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2016 License: BSD-3-Clause, Apache-2.0 Imports: 25 Imported by: 0

README

中文

Xorm is a simple and powerful ORM for Go.

Gitter

Build Status Go Walker Bitdeli Badge

Features

  • Struct <-> Table Mapping Support

  • Chainable APIs

  • Transaction Support

  • Both ORM and raw SQL operation Support

  • Sync database schema Support

  • Query Cache speed up

  • Database Reverse support, See Xorm Tool README

  • Simple cascade loading support

  • Optimistic Locking support

Drivers Support

Drivers for Go's sql package which currently support database/sql includes:

Changelog

  • v0.4.4

    • ql database expriment support
    • tidb database expriment support
    • sql.NullString and etc. field support
    • select ForUpdate support
    • many bugs fixed
  • v0.4.3

    • Json column type support
    • oracle expirement support
    • bug fixed
  • v0.4.2

    • Transaction will auto rollback if not Rollback or Commit be called.
    • Gonic Mapper support
    • bug fixed

More changelogs ...

Installation

If you have gopm installed,

gopm get github.com/go-xorm/xorm

Or

go get github.com/go-xorm/xorm

Documents

Quick Start

  • Create Engine
engine, err := xorm.NewEngine(driverName, dataSourceName)
  • Define a struct and Sync2 table struct to database
type User struct {
    Id int64
    Name string
    Salt string
    Age int
    Passwd string `xorm:"varchar(200)"`
    Created time.Time `xorm:"created"`
    Updated time.Time `xorm:"updated"`
}

err := engine.Sync2(new(User))
  • Query a SQL string, the returned results is []map[string][]byte
results, err := engine.Query("select * from user")
  • Execute a SQL string, the returned results
affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
  • Insert one or multipe records to database
affected, err := engine.Insert(&user)
// INSERT INTO struct () values ()
affected, err := engine.Insert(&user1, &user2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values ()
affected, err := engine.Insert(&users)
// INSERT INTO struct () values (),(),()
affected, err := engine.Insert(&user1, &users)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values (),(),()
  • Query one record from database
has, err := engine.Get(&user)
// SELECT * FROM user LIMIT 1
has, err := engine.Where("name = ?", name).Desc("id").Get(&user)
// SELECT * FROM user WHERE name = ? ORDER BY id DESC LIMIT 1
  • Query multiple records from database, also you can use join and extends
var users []User
err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
// SELECT * FROM user WHERE name = ? AND age > 10 limit 0 offset 10

type Detail struct {
    Id int64
    UserId int64 `xorm:"index"`
}

type UserDetail struct {
    User `xorm:"extends"`
    Detail `xorm:"extends"`
}

var users []UserDetail
err := engine.Table("user").Select("user.*, detail.*")
    Join("INNER", "detail", "detail.user_id = user.id").
    Where("user.name = ?", name).Limit(10, 0).
    Find(&users)
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 0 offset 10
  • Query multiple records and record by record handle, there two methods Iterate and Rows
err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
    user := bean.(*User)
    return nil
})
// SELECT * FROM user

rows, err := engine.Rows(&User{Name:name})
// SELECT * FROM user
defer rows.Close()
bean := new(Struct)
for rows.Next() {
    err = rows.Scan(bean)
}
  • Update one or more records, default will update non-empty and non-zero fields except to use Cols, AllCols and etc.
affected, err := engine.Id(1).Update(&user)
// UPDATE user SET ... Where id = ?

affected, err := engine.Update(&user, &User{Name:name})
// UPDATE user SET ... Where name = ?

var ids = []int64{1, 2, 3}
affected, err := engine.In(ids).Update(&user)
// UPDATE user SET ... Where id IN (?, ?, ?)

// force update indicated columns by Cols
affected, err := engine.Id(1).Cols("age").Update(&User{Name:name, Age: 12})
// UPDATE user SET age = ?, updated=? Where id = ?

// force NOT update indicated columns by Omit
affected, err := engine.Id(1).Omit("name").Update(&User{Name:name, Age: 12})
// UPDATE user SET age = ?, updated=? Where id = ?

affected, err := engine.Id(1).AllCols().Update(&user)
// UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
  • Delete one or more records, Delete MUST has conditon
affected, err := engine.Where(...).Delete(&user)
// DELETE FROM user Where ...
  • Count records
counts, err := engine.Count(&user)
// SELECT count(*) AS total FROM user

Cases

Discuss

Please visit Xorm on Google Groups

Contributing

If you want to pull request, please see CONTRIBUTING

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

Documentation

Overview

Package xorm is a simple and powerful ORM for Go.

Installation

Make sure you have installed Go 1.1+ and then:

go get github.com/go-xorm/xorm

Create Engine

Firstly, we should new an engine for a database

engine, err := xorm.NewEngine(driverName, dataSourceName)

Method NewEngine's parameters is the same as sql.Open. It depends drivers' implementation. Generally, one engine for an application is enough. You can set it as package variable.

Raw Methods

Xorm also support raw sql execution:

1. query a SQL string, the returned results is []map[string][]byte

results, err := engine.Query("select * from user")

2. execute a SQL string, the returned results

affected, err := engine.Exec("update user set .... where ...")

ORM Methods

There are 7 major ORM methods and many helpful methods to use to operate database.

1. Insert one or multipe records to database

affected, err := engine.Insert(&struct)
// INSERT INTO struct () values ()
affected, err := engine.Insert(&struct1, &struct2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values ()
affected, err := engine.Insert(&sliceOfStruct)
// INSERT INTO struct () values (),(),()
affected, err := engine.Insert(&struct1, &sliceOfStruct2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values (),(),()

2. Query one record from database

has, err := engine.Get(&user)
// SELECT * FROM user LIMIT 1

3. Query multiple records from database

sliceOfStructs := new(Struct)
err := engine.Find(sliceOfStructs)
// SELECT * FROM user

4. Query multiple records and record by record handle, there two methods, one is Iterate, another is Rows

err := engine.Iterate(...)
// SELECT * FROM user

rows, err := engine.Rows(...)
// SELECT * FROM user
defer rows.Close()
bean := new(Struct)
for rows.Next() {
    err = rows.Scan(bean)
}

5. Update one or more records

affected, err := engine.Id(...).Update(&user)
// UPDATE user SET ...

6. Delete one or more records, Delete MUST has conditon

affected, err := engine.Where(...).Delete(&user)
// DELETE FROM user Where ...

7. Count records

counts, err := engine.Count(&user)
// SELECT count(*) AS total FROM user

Conditions

The above 7 methods could use with condition methods chainable. Attention: the above 7 methods should be the last chainable method.

1. Id, In

engine.Id(1).Get(&user) // for single primary key
// SELECT * FROM user WHERE id = 1
engine.Id(core.PK{1, 2}).Get(&user) // for composite primary keys
// SELECT * FROM user WHERE id1 = 1 AND id2 = 2
engine.In("id", 1, 2, 3).Find(&users)
// SELECT * FROM user WHERE id IN (1, 2, 3)
engine.In("id", []int{1, 2, 3})
// SELECT * FROM user WHERE id IN (1, 2, 3)

2. Where, And, Or

engine.Where().And().Or().Find()
// SELECT * FROM user WHERE (.. AND ..) OR ...

3. OrderBy, Asc, Desc

engine.Asc().Desc().Find()
// SELECT * FROM user ORDER BY .. ASC, .. DESC
engine.OrderBy().Find()
// SELECT * FROM user ORDER BY ..

4. Limit, Top

engine.Limit().Find()
// SELECT * FROM user LIMIT .. OFFSET ..
engine.Top(5).Find()
// SELECT TOP 5 * FROM user // for mssql
// SELECT * FROM user LIMIT .. OFFSET 0 //for other databases

5. Sql, let you custom SQL

engine.Sql("select * from user").Find()

6. Cols, Omit, Distinct

engine.Cols("col1, col2").Find()
// SELECT col1, col2 FROM user
engine.Cols("col1", "col2").Where().Update(user)
// UPDATE user set col1 = ?, col2 = ? Where ...
engine.Omit("col1").Find()
// SELECT col2, col3 FROM user
engine.Omit("col1").Insert()
// INSERT INTO table (non-col1) VALUES ()
engine.Distinct("col1").Find()
// SELECT DISTINCT col1 FROM user

7. Join, GroupBy, Having

engine.GroupBy("name").Having("name='xlw'").Find()
//SELECT * FROM user GROUP BY name HAVING name='xlw'
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find()
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id

More usage, please visit http://xorm.io/docs

Index

Constants

View Source
const (
	DEFAULT_LOG_PREFIX = "[xorm]"
	DEFAULT_LOG_FLAG   = log.Ldate | log.Lmicroseconds
	DEFAULT_LOG_LEVEL  = core.LOG_DEBUG
)
View Source
const (
	Version string = "0.4.5.0204"
)

Variables

View Source
var (
	ErrParamsType      error = errors.New("Params type error")
	ErrTableNotFound   error = errors.New("Not found table")
	ErrUnSupportedType error = errors.New("Unsupported type error")
	ErrNotExist        error = errors.New("Not exist error")
	ErrCacheFailed     error = errors.New("Cache failed")
	ErrNeedDeletedCond error = errors.New("Delete need at least one condition")
	ErrNotImplemented  error = errors.New("Not implemented.")
)
View Source
var (
	NULL_TIME time.Time
)

Functions

func Atot

func Atot(s string, tp reflect.Type) (interface{}, error)

Types

type AfterDeleteProcessor

type AfterDeleteProcessor interface {
	AfterDelete()
}

Executed after an object has been deleted

type AfterInsertProcessor

type AfterInsertProcessor interface {
	AfterInsert()
}

Executed after an object is persisted to the database

type AfterSetProcessor

type AfterSetProcessor interface {
	AfterSet(string, Cell)
}

type AfterUpdateProcessor

type AfterUpdateProcessor interface {
	AfterUpdate()
}

Executed after an object has been updated

type BeforeDeleteProcessor

type BeforeDeleteProcessor interface {
	BeforeDelete()
}

Executed before an object is deleted

type BeforeInsertProcessor

type BeforeInsertProcessor interface {
	BeforeInsert()
}

Executed before an object is initially persisted to the database

type BeforeSetProcessor

type BeforeSetProcessor interface {
	BeforeSet(string, Cell)
}

type BeforeUpdateProcessor

type BeforeUpdateProcessor interface {
	BeforeUpdate()
}

Executed before an object is updated

type Cell

type Cell *interface{}

type Engine

type Engine struct {
	ColumnMapper  core.IMapper
	TableMapper   core.IMapper
	TagIdentifier string
	Tables        map[reflect.Type]*core.Table

	Cacher core.Cacher

	ShowSQL bool

	// !nashtsai! TODO ought to deprecate these but having logger to control its log level
	ShowInfo  bool
	ShowErr   bool
	ShowDebug bool
	ShowWarn  bool

	Logger     core.ILogger
	TZLocation *time.Location
	// contains filtered or unexported fields
}

Engine is the major struct of xorm, it means a database manager. Commonly, an application only need one engine

func NewEngine

func NewEngine(driverName string, dataSourceName string) (*Engine, error)

new a db manager according to the parameter. Currently support four drivers

func (*Engine) After

func (engine *Engine) After(closures func(interface{})) *Session

Apply after insert Processor, affected bean is passed to closure arg

func (*Engine) Alias

func (engine *Engine) Alias(alias string) *Session

set the table alias

func (*Engine) AllCols

func (engine *Engine) AllCols() *Session

func (*Engine) Asc

func (engine *Engine) Asc(colNames ...string) *Session

Method Asc will generate "ORDER BY column1,column2 Asc" This method can chainable use.

engine.Desc("name").Asc("age").Find(&users)
// SELECT * FROM user ORDER BY name DESC, age ASC

func (*Engine) AutoIncrStr

func (engine *Engine) AutoIncrStr() string

Database's autoincrement statement

func (*Engine) Before

func (engine *Engine) Before(closures func(interface{})) *Session

Apply before Processor, affected bean is passed to closure arg

func (*Engine) Cascade

func (engine *Engine) Cascade(trueOrFalse ...bool) *Session

use cascade or not

func (*Engine) Charset

func (engine *Engine) Charset(charset string) *Session

set charset when create table, only support mysql now

func (*Engine) ClearCache

func (engine *Engine) ClearCache(beans ...interface{}) error

If enabled cache, clear some tables' cache

func (*Engine) ClearCacheBean

func (engine *Engine) ClearCacheBean(bean interface{}, id string) error

If enabled cache, clear the cache bean

func (*Engine) Clone

func (engine *Engine) Clone() (*Engine, error)

clone an engine

func (*Engine) Close

func (engine *Engine) Close() error

Close the engine

func (*Engine) Cols

func (engine *Engine) Cols(columns ...string) *Session

only use the paramters as select or update columns

func (*Engine) Count

func (engine *Engine) Count(bean interface{}) (int64, error)

Count counts the records. bean's non-empty fields are conditions.

func (*Engine) CreateIndexes

func (engine *Engine) CreateIndexes(bean interface{}) error

create indexes

func (*Engine) CreateTables

func (engine *Engine) CreateTables(beans ...interface{}) error

CreateTables create tabls according bean

func (*Engine) CreateUniques

func (engine *Engine) CreateUniques(bean interface{}) error

create uniques

func (*Engine) DB

func (engine *Engine) DB() *core.DB

func (*Engine) DBMetas

func (engine *Engine) DBMetas() ([]*core.Table, error)

Retrieve all tables, columns, indexes' informations from database.

func (*Engine) DataSourceName

func (engine *Engine) DataSourceName() string

func (*Engine) Decr

func (engine *Engine) Decr(column string, arg ...interface{}) *Session

Method Decr provides a update string like "column = column - ?"

func (*Engine) Delete

func (engine *Engine) Delete(bean interface{}) (int64, error)

Delete records, bean's non-empty fields are conditions

func (*Engine) Desc

func (engine *Engine) Desc(colNames ...string) *Session

Method Desc will generate "ORDER BY column1 DESC, column2 DESC" This will

func (*Engine) Dialect

func (engine *Engine) Dialect() core.Dialect

func (*Engine) Distinct

func (engine *Engine) Distinct(columns ...string) *Session

use for distinct columns. Caution: when you are using cache, distinct will not be cached because cache system need id, but distinct will not provide id

func (*Engine) DriverName

func (engine *Engine) DriverName() string

func (*Engine) DropTables

func (engine *Engine) DropTables(beans ...interface{}) error

func (*Engine) DumpAll

func (engine *Engine) DumpAll(w io.Writer) error

dump database all table structs and data to w

func (*Engine) DumpAllToFile

func (engine *Engine) DumpAllToFile(fp string) error

dump database all table structs and data to a file

func (*Engine) Exec

func (engine *Engine) Exec(sql string, args ...interface{}) (sql.Result, error)

Exec raw sql

func (*Engine) Find

func (engine *Engine) Find(beans interface{}, condiBeans ...interface{}) error

Find retrieve records from table, condiBeans's non-empty fields are conditions. beans could be []Struct, []*Struct, map[int64]Struct map[int64]*Struct

func (*Engine) FormatTime

func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{})

func (*Engine) Get

func (engine *Engine) Get(bean interface{}) (bool, error)

Get retrieve one record from table, bean's non-empty fields are conditions

func (*Engine) GobRegister

func (engine *Engine) GobRegister(v interface{}) *Engine

func (*Engine) GroupBy

func (engine *Engine) GroupBy(keys string) *Session

Generate Group By statement

func (*Engine) Having

func (engine *Engine) Having(conditions string) *Session

Generate Having statement

func (*Engine) Id

func (engine *Engine) Id(id interface{}) *Session

Id mehtod provoide a condition as (id) = ?

func (*Engine) IdOf

func (engine *Engine) IdOf(bean interface{}) core.PK

func (*Engine) IdOfV

func (engine *Engine) IdOfV(rv reflect.Value) core.PK

func (*Engine) Import

func (engine *Engine) Import(r io.Reader) ([]sql.Result, error)

Import SQL DDL file

func (*Engine) ImportFile

func (engine *Engine) ImportFile(ddlPath string) ([]sql.Result, error)

Import SQL DDL file

func (*Engine) In

func (engine *Engine) In(column string, args ...interface{}) *Session

This method will generate "column IN (?, ?)"

func (*Engine) Incr

func (engine *Engine) Incr(column string, arg ...interface{}) *Session

Method Inc provides a update string like "column = column + ?"

func (*Engine) Insert

func (engine *Engine) Insert(beans ...interface{}) (int64, error)

Insert one or more records

func (*Engine) InsertOne

func (engine *Engine) InsertOne(bean interface{}) (int64, error)

Insert only one record

func (*Engine) IsTableEmpty

func (engine *Engine) IsTableEmpty(bean interface{}) (bool, error)

If a table has any reocrd

func (*Engine) IsTableExist

func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error)

If a table is exist

func (*Engine) Iterate

func (engine *Engine) Iterate(bean interface{}, fun IterFunc) error

Iterate record by record handle records from table, bean's non-empty fields are conditions.

func (*Engine) Join

func (engine *Engine) Join(join_operator string, tablename interface{}, condition string) *Session

The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN

func (*Engine) Limit

func (engine *Engine) Limit(limit int, start ...int) *Session

This method will generate "LIMIT start, limit"

func (*Engine) LogDebug

func (engine *Engine) LogDebug(contents ...interface{})

logging debug

func (*Engine) LogDebugf

func (engine *Engine) LogDebugf(format string, contents ...interface{})

func (*Engine) LogError

func (engine *Engine) LogError(contents ...interface{})

func (*Engine) LogErrorf

func (engine *Engine) LogErrorf(format string, contents ...interface{})

func (*Engine) LogInfo

func (engine *Engine) LogInfo(contents ...interface{})

logging info

func (*Engine) LogInfof

func (engine *Engine) LogInfof(format string, contents ...interface{})

func (*Engine) LogSQLExecutionTime

func (engine *Engine) LogSQLExecutionTime(sqlStr string, args interface{}, executionBlock func() (sql.Result, error)) (sql.Result, error)

func (*Engine) LogSQLQueryTime

func (engine *Engine) LogSQLQueryTime(sqlStr string, args interface{}, executionBlock func() (*core.Stmt, *core.Rows, error)) (*core.Stmt, *core.Rows, error)

func (*Engine) LogWarn

func (engine *Engine) LogWarn(contents ...interface{})

logging warn

func (*Engine) LogWarnf

func (engine *Engine) LogWarnf(format string, contents ...interface{})

func (*Engine) MapCacher

func (engine *Engine) MapCacher(bean interface{}, cacher core.Cacher)

Set a table use a special cacher

func (*Engine) MustCols

func (engine *Engine) MustCols(columns ...string) *Session

func (*Engine) NewDB

func (engine *Engine) NewDB() (*core.DB, error)

NewDB provides an interface to operate database directly

func (*Engine) NewSession

func (engine *Engine) NewSession() *Session

New a session

func (*Engine) NoAutoCondition

func (engine *Engine) NoAutoCondition(no ...bool) *Session

func (*Engine) NoAutoTime

func (engine *Engine) NoAutoTime() *Session

Default if your struct has "created" or "updated" filed tag, the fields will automatically be filled with current time when Insert or Update invoked. Call NoAutoTime if you dont' want to fill automatically.

func (*Engine) NoCache

func (engine *Engine) NoCache() *Session

If you has set default cacher, and you want temporilly stop use cache, you can use NoCache()

func (*Engine) NoCascade

func (engine *Engine) NoCascade() *Session

func (*Engine) NowTime

func (engine *Engine) NowTime(sqlTypeName string) interface{}

func (*Engine) NowTime2

func (engine *Engine) NowTime2(sqlTypeName string) (interface{}, time.Time)

func (*Engine) Nullable

func (engine *Engine) Nullable(columns ...string) *Session

Set null when column is zero-value and nullable for update

func (*Engine) Omit

func (engine *Engine) Omit(columns ...string) *Session

Only not use the paramters as select or update columns

func (*Engine) OrderBy

func (engine *Engine) OrderBy(order string) *Session

Method OrderBy will generate "ORDER BY order"

func (*Engine) Ping

func (engine *Engine) Ping() error

Ping tests if database is alive

func (*Engine) Query

func (engine *Engine) Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error)

Exec a raw sql and return records as []map[string][]byte

func (*Engine) Quote

func (engine *Engine) Quote(sql string) string

Use QuoteStr quote the string sql

func (*Engine) QuoteStr

func (engine *Engine) QuoteStr() string

Engine's database use which charactor as quote. mysql, sqlite use ` and postgres use "

func (*Engine) Rows

func (engine *Engine) Rows(bean interface{}) (*Rows, error)

Return sql.Rows compatible Rows obj, as a forward Iterator object for iterating record by record, bean's non-empty fields are conditions.

func (*Engine) Select

func (engine *Engine) Select(str string) *Session

func (*Engine) SetColumnMapper

func (engine *Engine) SetColumnMapper(mapper core.IMapper)

func (*Engine) SetDefaultCacher

func (engine *Engine) SetDefaultCacher(cacher core.Cacher)

SetDefaltCacher set the default cacher. Xorm's default not enable cacher.

func (*Engine) SetDisableGlobalCache

func (engine *Engine) SetDisableGlobalCache(disable bool)

func (*Engine) SetExpr

func (engine *Engine) SetExpr(column string, expression string) *Session

Method SetExpr provides a update string like "column = {expression}"

func (*Engine) SetLogger

func (engine *Engine) SetLogger(logger core.ILogger)

func (*Engine) SetMapper

func (engine *Engine) SetMapper(mapper core.IMapper)

func (*Engine) SetMaxConns

func (engine *Engine) SetMaxConns(conns int)

@Deprecated

func (*Engine) SetMaxIdleConns

func (engine *Engine) SetMaxIdleConns(conns int)

SetMaxIdleConns

func (*Engine) SetMaxOpenConns

func (engine *Engine) SetMaxOpenConns(conns int)

SetMaxOpenConns is only available for go 1.2+

func (*Engine) SetTableMapper

func (engine *Engine) SetTableMapper(mapper core.IMapper)

func (*Engine) Sql

func (engine *Engine) Sql(querystring string, args ...interface{}) *Session

Sql method let's you manualy write raw sql and operate For example:

engine.Sql("select * from user").Find(&users)

This code will execute "select * from user" and set the records to users

func (*Engine) SqlType

func (engine *Engine) SqlType(c *core.Column) string

A simple wrapper to dialect's core.SqlType method

func (*Engine) StoreEngine

func (engine *Engine) StoreEngine(storeEngine string) *Session

set store engine when create table, only support mysql now

func (*Engine) SupportInsertMany

func (engine *Engine) SupportInsertMany() bool

If engine's database support batch insert records like "insert into user values (name, age), (name, age)". When the return is ture, then engine.Insert(&users) will generate batch sql and exeute.

func (*Engine) Sync

func (engine *Engine) Sync(beans ...interface{}) error

Sync the new struct changes to database, this method will automatically add table, column, index, unique. but will not delete or change anything. If you change some field, you should change the database manually.

func (*Engine) Sync2

func (engine *Engine) Sync2(beans ...interface{}) error

func (*Engine) TZTime

func (engine *Engine) TZTime(t time.Time) time.Time

func (*Engine) Table

func (engine *Engine) Table(tableNameOrBean interface{}) *Session

Temporarily change the Get, Find, Update's table

func (*Engine) TableInfo

func (engine *Engine) TableInfo(bean interface{}) *core.Table

func (*Engine) Unscoped

func (engine *Engine) Unscoped() *Session

Always disable struct tag "deleted"

func (*Engine) Update

func (engine *Engine) Update(bean interface{}, condiBeans ...interface{}) (int64, error)

Update records, bean's non-empty fields are updated contents, condiBean' non-empty filds are conditions CAUTION:

1.bool will defaultly be updated content nor conditions
 You should call UseBool if you have bool to use.
2.float32 & float64 may be not inexact as conditions

func (*Engine) UseBool

func (engine *Engine) UseBool(columns ...string) *Session

Xorm automatically retrieve condition according struct, but if struct has bool field, it will ignore them. So use UseBool to tell system to do not ignore them. If no paramters, it will use all the bool field of struct, or it will use paramters's columns

func (*Engine) Where

func (engine *Engine) Where(querystring string, args ...interface{}) *Session

Where method provide a condition query

type IterFunc

type IterFunc func(idx int, bean interface{}) error

IterFunc only use by Iterate

type LRUCacher

type LRUCacher struct {

	// maxSize    int
	MaxElementSize int
	Expired        time.Duration
	GcInterval     time.Duration
	// contains filtered or unexported fields
}

func NewLRUCacher

func NewLRUCacher(store core.CacheStore, maxElementSize int) *LRUCacher

func NewLRUCacher2

func NewLRUCacher2(store core.CacheStore, expired time.Duration, maxElementSize int) *LRUCacher

func (*LRUCacher) ClearBeans

func (m *LRUCacher) ClearBeans(tableName string)

func (*LRUCacher) ClearIds

func (m *LRUCacher) ClearIds(tableName string)

func (*LRUCacher) DelBean

func (m *LRUCacher) DelBean(tableName string, id string)

func (*LRUCacher) DelIds

func (m *LRUCacher) DelIds(tableName, sql string)

func (*LRUCacher) GC

func (m *LRUCacher) GC()

GC check ids lit and sql list to remove all element expired

func (*LRUCacher) GetBean

func (m *LRUCacher) GetBean(tableName string, id string) interface{}

Get bean according tableName and id from cache

func (*LRUCacher) GetIds

func (m *LRUCacher) GetIds(tableName, sql string) interface{}

Get all bean's ids according to sql and parameter from cache

func (*LRUCacher) PutBean

func (m *LRUCacher) PutBean(tableName string, id string, obj interface{})

func (*LRUCacher) PutIds

func (m *LRUCacher) PutIds(tableName, sql string, ids interface{})

func (*LRUCacher) RunGC

func (m *LRUCacher) RunGC()

RunGC run once every m.GcInterval

type MemoryStore

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

memory store

func NewMemoryStore

func NewMemoryStore() *MemoryStore

func (*MemoryStore) Del

func (s *MemoryStore) Del(key string) error

func (*MemoryStore) Get

func (s *MemoryStore) Get(key string) (interface{}, error)

func (*MemoryStore) Put

func (s *MemoryStore) Put(key string, value interface{}) error

type Rows

type Rows struct {
	NoTypeCheck bool
	// contains filtered or unexported fields
}

func (*Rows) Close

func (rows *Rows) Close() error

close session if session.IsAutoClose is true, and claimed any opened resources

func (*Rows) Err

func (rows *Rows) Err() error

Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.

func (*Rows) Next

func (rows *Rows) Next() bool

move cursor to next record, return false if end has reached

func (*Rows) Scan

func (rows *Rows) Scan(bean interface{}) error

scan row record to bean properties

type Session

type Session struct {
	Engine                 *Engine
	Tx                     *core.Tx
	Statement              Statement
	IsAutoCommit           bool
	IsCommitedOrRollbacked bool
	TransType              string
	IsAutoClose            bool

	// Automatically reset the statement after operations that execute a SQL
	// query such as Count(), Find(), Get(), ...
	AutoResetStatement bool
	// contains filtered or unexported fields
}

Struct Session keep a pointer to sql.DB and provides all execution of all kind of database operations.

func (*Session) After

func (session *Session) After(closures func(interface{})) *Session

Apply after Processor, affected bean is passed to closure arg

func (*Session) Alias

func (session *Session) Alias(alias string) *Session

set the table alias

func (*Session) AllCols

func (session *Session) AllCols() *Session

func (*Session) And

func (session *Session) And(querystring string, args ...interface{}) *Session

Method Where provides custom query condition.

func (*Session) Asc

func (session *Session) Asc(colNames ...string) *Session

Method Asc provide asc order by query condition, the input parameters are columns.

func (*Session) Before

func (session *Session) Before(closures func(interface{})) *Session

Apply before Processor, affected bean is passed to closure arg

func (*Session) Begin

func (session *Session) Begin() error

Begin a transaction

func (*Session) Cascade

func (session *Session) Cascade(trueOrFalse ...bool) *Session

Method Cascade indicates if loading sub Struct

func (*Session) Charset

func (session *Session) Charset(charset string) *Session

Method Charset is only avialble mysql dialect currently

func (*Session) Close

func (session *Session) Close()

Method Close release the connection from pool

func (*Session) Cols

func (session *Session) Cols(columns ...string) *Session

Method Cols provides some columns to special

func (*Session) Commit

func (session *Session) Commit() error

When using transaction, Commit will commit all operations.

func (*Session) Count

func (session *Session) Count(bean interface{}) (int64, error)

Count counts the records. bean's non-empty fields are conditions.

func (*Session) CreateIndexes

func (session *Session) CreateIndexes(bean interface{}) error

create indexes

func (*Session) CreateTable

func (session *Session) CreateTable(bean interface{}) error

this function create a table according a bean

func (*Session) CreateUniques

func (session *Session) CreateUniques(bean interface{}) error

create uniques

func (*Session) DB

func (session *Session) DB() *core.DB

func (*Session) Decr

func (session *Session) Decr(column string, arg ...interface{}) *Session

Method Decr provides a query string like "count = count - 1"

func (*Session) Delete

func (session *Session) Delete(bean interface{}) (int64, error)

Delete records, bean's non-empty fields are conditions

func (*Session) Desc

func (session *Session) Desc(colNames ...string) *Session

Method Desc provide desc order by query condition, the input parameters are columns.

func (*Session) Distinct

func (session *Session) Distinct(columns ...string) *Session

use for distinct columns. Caution: when you are using cache, distinct will not be cached because cache system need id, but distinct will not provide id

func (*Session) DropIndexes

func (session *Session) DropIndexes(bean interface{}) error

drop indexes

func (*Session) DropTable

func (session *Session) DropTable(beanOrTableName interface{}) error

drop table will drop table if exist, if drop failed, it will return error

func (*Session) Exec

func (session *Session) Exec(sqlStr string, args ...interface{}) (sql.Result, error)

Exec raw sql

func (*Session) Find

func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) error

Find retrieve records from table, condiBeans's non-empty fields are conditions. beans could be []Struct, []*Struct, map[int64]Struct map[int64]*Struct

func (*Session) ForUpdate

func (session *Session) ForUpdate() *Session

Set Read/Write locking for UPDATE

func (*Session) Get

func (session *Session) Get(bean interface{}) (bool, error)

get retrieve one record from database, bean's non-empty fields will be as conditions

func (*Session) GroupBy

func (session *Session) GroupBy(keys string) *Session

Generate Group By statement

func (*Session) Having

func (session *Session) Having(conditions string) *Session

Generate Having statement

func (*Session) Id

func (session *Session) Id(id interface{}) *Session

Method Id provides converting id as a query condition

func (*Session) In

func (session *Session) In(column string, args ...interface{}) *Session

Method In provides a query string like "id in (1, 2, 3)"

func (*Session) Incr

func (session *Session) Incr(column string, arg ...interface{}) *Session

Method In provides a query string like "count = count + 1"

func (*Session) Init

func (session *Session) Init()

Method Init reset the session as the init status.

func (*Session) Insert

func (session *Session) Insert(beans ...interface{}) (int64, error)

insert one or more beans

func (*Session) InsertMulti

func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error)

Insert multiple records

func (*Session) InsertOne

func (session *Session) InsertOne(bean interface{}) (int64, error)

Method InsertOne insert only one struct into database as a record. The in parameter bean must a struct or a point to struct. The return parameter is inserted and error

func (*Session) IsTableEmpty

func (session *Session) IsTableEmpty(bean interface{}) (bool, error)

func (*Session) IsTableExist

func (session *Session) IsTableExist(beanOrTableName interface{}) (bool, error)

func (*Session) Iterate

func (session *Session) Iterate(bean interface{}, fun IterFunc) error

Iterate record by record handle records from table, condiBeans's non-empty fields are conditions. beans could be []Struct, []*Struct, map[int64]Struct map[int64]*Struct

func (*Session) Join

func (session *Session) Join(join_operator string, tablename interface{}, condition string) *Session

The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN

func (*Session) LastSQL

func (session *Session) LastSQL() (string, []interface{})

LastSQL returns last query information

func (*Session) Limit

func (session *Session) Limit(limit int, start ...int) *Session

Method Limit provide limit and offset query condition

func (*Session) MustCols

func (session *Session) MustCols(columns ...string) *Session

func (*Session) NoAutoCondition

func (session *Session) NoAutoCondition(no ...bool) *Session

func (*Session) NoAutoTime

func (session *Session) NoAutoTime() *Session

Method NoAutoTime means do not automatically give created field and updated field the current time on the current session temporarily

func (*Session) NoCache

func (session *Session) NoCache() *Session

Method NoCache ask this session do not retrieve data from cache system and get data from database directly.

func (*Session) NoCascade

func (session *Session) NoCascade() *Session

func (*Session) Nullable

func (session *Session) Nullable(columns ...string) *Session

Set null when column is zero-value and nullable for update

func (*Session) Omit

func (session *Session) Omit(columns ...string) *Session

Only not use the paramters as select or update columns

func (*Session) Or

func (session *Session) Or(querystring string, args ...interface{}) *Session

Method Where provides custom query condition.

func (*Session) OrderBy

func (session *Session) OrderBy(order string) *Session

Method OrderBy provide order by query condition, the input parameter is the content after order by on a sql statement.

func (*Session) Ping

func (session *Session) Ping() error

Test if database is ok

func (*Session) Prepare

func (session *Session) Prepare() *Session

Prepare

func (*Session) Query

func (session *Session) Query(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error)

Exec a raw sql and return records as []map[string][]byte

func (*Session) Rollback

func (session *Session) Rollback() error

When using transaction, you can rollback if any error

func (*Session) Rows

func (session *Session) Rows(bean interface{}) (*Rows, error)

Return sql.Rows compatible Rows obj, as a forward Iterator object for iterating record by record, bean's non-empty fields are conditions.

func (*Session) Select

func (session *Session) Select(str string) *Session

Method Cols provides some columns to special

func (*Session) SetExpr

func (session *Session) SetExpr(column string, expression string) *Session

Method SetExpr provides a query string like "column = {expression}"

func (*Session) Sql

func (session *Session) Sql(querystring string, args ...interface{}) *Session

Method Sql provides raw sql input parameter. When you have a complex SQL statement and cannot use Where, Id, In and etc. Methods to describe, you can use Sql.

func (*Session) StoreEngine

func (session *Session) StoreEngine(storeEngine string) *Session

Method StoreEngine is only avialble mysql dialect currently

func (*Session) Sync2

func (s *Session) Sync2(beans ...interface{}) error

func (*Session) Table

func (session *Session) Table(tableNameOrBean interface{}) *Session

Method core.Table can input a string or pointer to struct for special a table to operate.

func (*Session) Unscoped

func (session *Session) Unscoped() *Session

Always disable struct tag "deleted"

func (*Session) Update

func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error)

Update records, bean's non-empty fields are updated contents, condiBean' non-empty filds are conditions CAUTION:

1.bool will defaultly be updated content nor conditions
 You should call UseBool if you have bool to use.
2.float32 & float64 may be not inexact as conditions

func (*Session) UseBool

func (session *Session) UseBool(columns ...string) *Session

Xorm automatically retrieve condition according struct, but if struct has bool field, it will ignore them. So use UseBool to tell system to do not ignore them. If no paramters, it will use all the bool field of struct, or it will use paramters's columns

func (*Session) Where

func (session *Session) Where(querystring string, args ...interface{}) *Session

Method Where provides custom query condition.

type SimpleLogger

type SimpleLogger struct {
	DEBUG *log.Logger
	ERR   *log.Logger
	INFO  *log.Logger
	WARN  *log.Logger
	// contains filtered or unexported fields
}

func NewSimpleLogger

func NewSimpleLogger(out io.Writer) *SimpleLogger

func NewSimpleLogger2

func NewSimpleLogger2(out io.Writer, prefix string, flag int) *SimpleLogger

func NewSimpleLogger3

func NewSimpleLogger3(out io.Writer, prefix string, flag int, l core.LogLevel) *SimpleLogger

func (*SimpleLogger) Debug

func (s *SimpleLogger) Debug(v ...interface{}) (err error)

func (*SimpleLogger) Debugf

func (s *SimpleLogger) Debugf(format string, v ...interface{}) (err error)

func (*SimpleLogger) Err

func (s *SimpleLogger) Err(v ...interface{}) (err error)

func (*SimpleLogger) Errf

func (s *SimpleLogger) Errf(format string, v ...interface{}) (err error)

func (*SimpleLogger) Info

func (s *SimpleLogger) Info(v ...interface{}) (err error)

func (*SimpleLogger) Infof

func (s *SimpleLogger) Infof(format string, v ...interface{}) (err error)

func (*SimpleLogger) Level

func (s *SimpleLogger) Level() core.LogLevel

func (*SimpleLogger) SetLevel

func (s *SimpleLogger) SetLevel(l core.LogLevel) (err error)

func (*SimpleLogger) Warning

func (s *SimpleLogger) Warning(v ...interface{}) (err error)

func (*SimpleLogger) Warningf

func (s *SimpleLogger) Warningf(format string, v ...interface{}) (err error)

type Statement

type Statement struct {
	RefTable   *core.Table
	Engine     *Engine
	Start      int
	LimitN     int
	WhereStr   string
	IdParam    *core.PK
	Params     []interface{}
	OrderStr   string
	JoinStr    string
	GroupByStr string
	HavingStr  string
	ColumnStr  string

	OmitStr      string
	ConditionStr string
	AltTableName string
	RawSQL       string
	RawParams    []interface{}
	UseCascade   bool
	UseAutoJoin  bool
	StoreEngine  string
	Charset      string
	BeanArgs     []interface{}
	UseCache     bool
	UseAutoTime  bool

	IsDistinct  bool
	IsForUpdate bool
	TableAlias  string
	// contains filtered or unexported fields
}

statement save all the sql info for executing SQL

func (*Statement) Alias

func (statement *Statement) Alias(alias string) *Statement

Alias set the table alias

func (*Statement) AllCols

func (statement *Statement) AllCols() *Statement

Update use only: update all columns

func (*Statement) And

func (statement *Statement) And(querystring string, args ...interface{}) *Statement

And add Where & and statment

func (*Statement) Asc

func (statement *Statement) Asc(colNames ...string) *Statement

Method Asc provide asc order by query condition, the input parameters are columns.

func (*Statement) Cols

func (statement *Statement) Cols(columns ...string) *Statement

Generate "col1, col2" statement

func (*Statement) Decr

func (statement *Statement) Decr(column string, arg ...interface{}) *Statement

Decr Generate "Update ... Set column = column - arg" statment

func (*Statement) Desc

func (statement *Statement) Desc(colNames ...string) *Statement

func (*Statement) Distinct

func (statement *Statement) Distinct(columns ...string) *Statement

Generate "Distince col1, col2 " statment

func (*Statement) ForUpdate

func (statement *Statement) ForUpdate() *Statement

Generate "SELECT ... FOR UPDATE" statment

func (*Statement) GroupBy

func (statement *Statement) GroupBy(keys string) *Statement

Generate "Group By keys" statement

func (*Statement) Having

func (statement *Statement) Having(conditions string) *Statement

Generate "Having conditions" statement

func (*Statement) Id

func (statement *Statement) Id(id interface{}) *Statement

Generate "where id = ? " statment or for composite key "where key1 = ? and key2 = ?"

func (*Statement) In

func (statement *Statement) In(column string, args ...interface{}) *Statement

Generate "Where column IN (?) " statment

func (*Statement) Incr

func (statement *Statement) Incr(column string, arg ...interface{}) *Statement

Incr Generate "Update ... Set column = column + arg" statment

func (*Statement) Init

func (statement *Statement) Init()

init

func (*Statement) Join

func (statement *Statement) Join(join_operator string, tablename interface{}, condition string) *Statement

The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN

func (*Statement) JoinColumns

func (statement *Statement) JoinColumns(cols []*core.Column, includeTableName bool) string

func (*Statement) Limit

func (statement *Statement) Limit(limit int, start ...int) *Statement

Generate LIMIT start, limit statement

func (*Statement) MustCols

func (statement *Statement) MustCols(columns ...string) *Statement

Update use only: must update columns

func (*Statement) NoAutoCondition

func (statement *Statement) NoAutoCondition(no ...bool) *Statement

NoAutoCondition if you do not want convert bean's field as query condition, then use this function

func (*Statement) Nullable

func (statement *Statement) Nullable(columns ...string)

Update use only: update columns to null when value is nullable and zero-value

func (*Statement) Omit

func (statement *Statement) Omit(columns ...string)

do not use the columns

func (*Statement) Or

func (statement *Statement) Or(querystring string, args ...interface{}) *Statement

Or add Where & Or statment

func (*Statement) OrderBy

func (statement *Statement) OrderBy(order string) *Statement

Generate "Order By order" statement

func (*Statement) Select

func (s *Statement) Select(str string) *Statement

replace select

func (*Statement) SetExpr

func (statement *Statement) SetExpr(column string, expression string) *Statement

SetExpr Generate "Update ... Set column = {expression}" statment

func (*Statement) Sql

func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement

Sql add the raw sql statement

func (*Statement) Table

func (statement *Statement) Table(tableNameOrBean interface{}) *Statement

Table tempororily set table name, the parameter could be a string or a pointer of struct

func (*Statement) TableName

func (statement *Statement) TableName() string

return current tableName

func (*Statement) Top

func (statement *Statement) Top(limit int) *Statement

Generate LIMIT limit statement

func (*Statement) Unscoped

func (statement *Statement) Unscoped() *Statement

Always disable struct tag "deleted"

func (*Statement) UseBool

func (statement *Statement) UseBool(columns ...string) *Statement

indicates that use bool fields as update contents and query contiditions

func (*Statement) Where

func (statement *Statement) Where(querystring string, args ...interface{}) *Statement

Where add Where statment

type SyslogLogger

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

func NewSyslogLogger

func NewSyslogLogger(w *syslog.Writer) *SyslogLogger

func (*SyslogLogger) Debug

func (s *SyslogLogger) Debug(v ...interface{}) (err error)

func (*SyslogLogger) Debugf

func (s *SyslogLogger) Debugf(format string, v ...interface{}) (err error)

func (*SyslogLogger) Err

func (s *SyslogLogger) Err(v ...interface{}) (err error)

func (*SyslogLogger) Errf

func (s *SyslogLogger) Errf(format string, v ...interface{}) (err error)

func (*SyslogLogger) Info

func (s *SyslogLogger) Info(v ...interface{}) (err error)

func (*SyslogLogger) Infof

func (s *SyslogLogger) Infof(format string, v ...interface{}) (err error)

func (*SyslogLogger) Level

func (s *SyslogLogger) Level() core.LogLevel

func (*SyslogLogger) SetLevel

func (s *SyslogLogger) SetLevel(l core.LogLevel) (err error)

SetLevel always return error, as current log/syslog package doesn't allow to set priority level after syslog.Writer created

func (*SyslogLogger) Warning

func (s *SyslogLogger) Warning(v ...interface{}) (err error)

func (*SyslogLogger) Warningf

func (s *SyslogLogger) Warningf(format string, v ...interface{}) (err error)

type TableName

type TableName interface {
	TableName() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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