2013-05-03 07:26:51 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2013-09-28 15:14:42 +00:00
|
|
|
"errors"
|
2013-06-24 04:08:58 +00:00
|
|
|
"fmt"
|
2013-08-08 05:24:38 +00:00
|
|
|
"io"
|
2013-05-03 07:26:51 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-06-16 03:05:16 +00:00
|
|
|
"sync"
|
2013-05-03 07:26:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-08-08 05:24:38 +00:00
|
|
|
POSTGRES = "postgres"
|
|
|
|
SQLITE = "sqlite3"
|
|
|
|
MYSQL = "mysql"
|
|
|
|
MYMYSQL = "mymysql"
|
2013-05-03 07:26:51 +00:00
|
|
|
)
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// a dialect is a driver's wrapper
|
2013-06-04 08:56:59 +00:00
|
|
|
type dialect interface {
|
2013-10-12 15:16:51 +00:00
|
|
|
Init(DriverName, DataSourceName string) error
|
2013-06-04 08:56:59 +00:00
|
|
|
SqlType(t *Column) string
|
2013-07-03 03:49:29 +00:00
|
|
|
SupportInsertMany() bool
|
2013-08-08 05:24:38 +00:00
|
|
|
QuoteStr() string
|
|
|
|
AutoIncrStr() string
|
|
|
|
SupportEngine() bool
|
|
|
|
SupportCharset() bool
|
2013-09-26 07:19:39 +00:00
|
|
|
IndexOnTable() bool
|
2013-09-28 15:14:42 +00:00
|
|
|
IndexCheckSql(tableName, idxName string) (string, []interface{})
|
|
|
|
TableCheckSql(tableName string) (string, []interface{})
|
|
|
|
ColumnCheckSql(tableName, colName string) (string, []interface{})
|
2013-10-12 15:16:51 +00:00
|
|
|
|
2013-10-27 01:10:20 +00:00
|
|
|
GetColumns(tableName string) ([]string, map[string]*Column, error)
|
2013-10-12 15:16:51 +00:00
|
|
|
GetTables() ([]*Table, error)
|
|
|
|
GetIndexes(tableName string) (map[string]*Index, error)
|
2013-06-04 08:56:59 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
type Engine struct {
|
2013-07-03 03:49:29 +00:00
|
|
|
Mapper IMapper
|
|
|
|
TagIdentifier string
|
|
|
|
DriverName string
|
|
|
|
DataSourceName string
|
2013-10-12 15:16:51 +00:00
|
|
|
dialect dialect
|
2013-07-03 03:49:29 +00:00
|
|
|
Tables map[reflect.Type]*Table
|
|
|
|
mutex *sync.Mutex
|
|
|
|
ShowSQL bool
|
2013-09-23 02:20:45 +00:00
|
|
|
ShowErr bool
|
|
|
|
ShowDebug bool
|
2013-10-04 16:44:43 +00:00
|
|
|
ShowWarn bool
|
2013-08-29 09:26:33 +00:00
|
|
|
Pool IConnectPool
|
2013-08-08 05:24:38 +00:00
|
|
|
Filters []Filter
|
|
|
|
Logger io.Writer
|
2013-09-17 09:36:34 +00:00
|
|
|
Cacher Cacher
|
|
|
|
UseCache bool
|
2013-07-03 03:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// 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.
|
2013-07-03 03:49:29 +00:00
|
|
|
func (engine *Engine) SupportInsertMany() bool {
|
2013-10-12 15:16:51 +00:00
|
|
|
return engine.dialect.SupportInsertMany()
|
2013-07-03 03:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Engine's database use which charactor as quote.
|
|
|
|
// mysql, sqlite use ` and postgres use "
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) QuoteStr() string {
|
2013-10-12 15:16:51 +00:00
|
|
|
return engine.dialect.QuoteStr()
|
2013-07-03 03:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Use QuoteStr quote the string sql
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) Quote(sql string) string {
|
2013-10-12 15:16:51 +00:00
|
|
|
return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr()
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// A simple wrapper to dialect's SqlType method
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) SqlType(c *Column) string {
|
2013-10-12 15:16:51 +00:00
|
|
|
return engine.dialect.SqlType(c)
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Database's autoincrement statement
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) AutoIncrStr() string {
|
2013-10-12 15:16:51 +00:00
|
|
|
return engine.dialect.AutoIncrStr()
|
2013-07-03 03:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Set engine's pool, the pool default is Go's standard library's connection pool.
|
2013-07-03 03:49:29 +00:00
|
|
|
func (engine *Engine) SetPool(pool IConnectPool) error {
|
2013-08-29 09:26:33 +00:00
|
|
|
engine.Pool = pool
|
|
|
|
return engine.Pool.Init(engine)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// SetMaxConns is only available for go 1.2+
|
2013-09-01 02:37:46 +00:00
|
|
|
func (engine *Engine) SetMaxConns(conns int) {
|
|
|
|
engine.Pool.SetMaxConns(conns)
|
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// SetDefaltCacher set the default cacher. Xorm's default not enable cacher.
|
2013-09-17 09:36:34 +00:00
|
|
|
func (engine *Engine) SetDefaultCacher(cacher Cacher) {
|
|
|
|
if cacher == nil {
|
|
|
|
engine.UseCache = false
|
|
|
|
} else {
|
|
|
|
engine.UseCache = true
|
|
|
|
engine.Cacher = cacher
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// If you has set default cacher, and you want temporilly stop use cache,
|
|
|
|
// you can use NoCache()
|
2013-09-22 09:32:23 +00:00
|
|
|
func (engine *Engine) NoCache() *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
|
|
|
return session.NoCache()
|
2013-09-17 09:36:34 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Set a table use a special cacher
|
2013-09-17 09:36:34 +00:00
|
|
|
func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-09-17 09:36:34 +00:00
|
|
|
engine.AutoMapType(t)
|
|
|
|
engine.Tables[t].Cacher = cacher
|
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// OpenDB provides a interface to operate database directly.
|
2013-09-30 06:45:34 +00:00
|
|
|
func (engine *Engine) OpenDB() (*sql.DB, error) {
|
|
|
|
return sql.Open(engine.DriverName, engine.DataSourceName)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// New a session
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) NewSession() *Session {
|
|
|
|
session := &Session{Engine: engine}
|
2013-05-03 07:26:51 +00:00
|
|
|
session.Init()
|
2013-06-16 03:05:16 +00:00
|
|
|
return session
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Close the engine
|
2013-07-03 03:49:29 +00:00
|
|
|
func (engine *Engine) Close() error {
|
2013-08-29 09:26:33 +00:00
|
|
|
return engine.Pool.Close(engine)
|
2013-07-03 03:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// Test method is deprecated, use Ping() method.
|
2013-06-12 13:51:39 +00:00
|
|
|
func (engine *Engine) Test() error {
|
2013-11-06 07:36:38 +00:00
|
|
|
return engine.Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping tests if database is alive.
|
|
|
|
func (engine *Engine) Ping() error {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
2013-08-08 05:24:38 +00:00
|
|
|
engine.LogSQL("PING DATABASE", engine.DriverName)
|
|
|
|
return session.Ping()
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// logging sql
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) LogSQL(contents ...interface{}) {
|
2013-07-03 03:49:29 +00:00
|
|
|
if engine.ShowSQL {
|
2013-08-08 05:24:38 +00:00
|
|
|
io.WriteString(engine.Logger, fmt.Sprintln(contents...))
|
2013-07-03 03:49:29 +00:00
|
|
|
}
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// logging error
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) LogError(contents ...interface{}) {
|
2013-09-23 02:20:45 +00:00
|
|
|
if engine.ShowErr {
|
|
|
|
io.WriteString(engine.Logger, fmt.Sprintln(contents...))
|
|
|
|
}
|
2013-06-16 03:05:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// logging debug
|
2013-09-23 15:59:42 +00:00
|
|
|
func (engine *Engine) LogDebug(contents ...interface{}) {
|
|
|
|
if engine.ShowDebug {
|
|
|
|
io.WriteString(engine.Logger, fmt.Sprintln(contents...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// logging warn
|
2013-10-04 16:44:43 +00:00
|
|
|
func (engine *Engine) LogWarn(contents ...interface{}) {
|
|
|
|
if engine.ShowWarn {
|
|
|
|
io.WriteString(engine.Logger, fmt.Sprintln(contents...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// execute sql
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Sql(querystring, args...)
|
|
|
|
}
|
|
|
|
|
2013-09-17 09:36:34 +00:00
|
|
|
func (engine *Engine) NoAutoTime() *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
|
|
|
return session.NoAutoTime()
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// retrieve all tables, columns, indexes' informations from database.
|
2013-10-12 15:16:51 +00:00
|
|
|
func (engine *Engine) DBMetas() ([]*Table, error) {
|
|
|
|
tables, err := engine.dialect.GetTables()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
2013-10-27 01:10:20 +00:00
|
|
|
colSeq, cols, err := engine.dialect.GetColumns(table.Name)
|
2013-10-12 15:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
table.Columns = cols
|
2013-10-27 01:10:20 +00:00
|
|
|
table.ColumnsSeq = colSeq
|
2013-10-12 15:16:51 +00:00
|
|
|
|
|
|
|
indexes, err := engine.dialect.GetIndexes(table.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
table.Indexes = indexes
|
2013-10-13 15:57:57 +00:00
|
|
|
|
|
|
|
for _, index := range indexes {
|
|
|
|
for _, name := range index.Cols {
|
|
|
|
if col, ok := table.Columns[name]; ok {
|
|
|
|
col.Indexes[index.Name] = true
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Unkonwn col " + name + " in indexes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-12 15:16:51 +00:00
|
|
|
}
|
|
|
|
return tables, nil
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// use cascade or not
|
2013-07-17 17:26:14 +00:00
|
|
|
func (engine *Engine) Cascade(trueOrFalse ...bool) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Cascade(trueOrFalse...)
|
2013-06-12 13:51:39 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// Where method provide a condition query
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Where(querystring string, args ...interface{}) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Where(querystring, args...)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// Id mehtod provoide a condition as (id) = ?
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Id(id int64) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Id(id)
|
2013-05-09 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// set charset when create table, only support mysql now
|
2013-07-27 04:24:38 +00:00
|
|
|
func (engine *Engine) Charset(charset string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-27 04:24:38 +00:00
|
|
|
return session.Charset(charset)
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:36:38 +00:00
|
|
|
// set store engine when create table, only support mysql now
|
2013-07-27 04:24:38 +00:00
|
|
|
func (engine *Engine) StoreEngine(storeEngine string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-27 04:24:38 +00:00
|
|
|
return session.StoreEngine(storeEngine)
|
|
|
|
}
|
|
|
|
|
2013-11-14 15:07:33 +00:00
|
|
|
func (engine *Engine) Distinct(columns ...string) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
|
|
|
return session.Distinct(columns...)
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) Cols(columns ...string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-08-08 05:24:38 +00:00
|
|
|
return session.Cols(columns...)
|
|
|
|
}
|
|
|
|
|
2013-11-15 02:16:08 +00:00
|
|
|
func (engine *Engine) UseBool(columns ...string) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
|
|
|
return session.UseBool(columns...)
|
|
|
|
}
|
|
|
|
|
2013-10-17 04:50:46 +00:00
|
|
|
func (engine *Engine) Omit(columns ...string) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
|
|
|
return session.Omit(columns...)
|
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) In(column string, args ...interface{}) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.In(column, args...)
|
2013-05-11 08:27:17 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 05:18:02 +00:00
|
|
|
func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-08-29 05:18:02 +00:00
|
|
|
return session.Table(tableNameOrBean)
|
2013-05-19 05:25:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Limit(limit int, start ...int) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Limit(limit, start...)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 02:06:32 +00:00
|
|
|
func (engine *Engine) Desc(colNames ...string) *Session {
|
2013-09-02 01:54:37 +00:00
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
2013-09-02 02:06:32 +00:00
|
|
|
return session.Desc(colNames...)
|
2013-09-02 01:54:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 02:06:32 +00:00
|
|
|
func (engine *Engine) Asc(colNames ...string) *Session {
|
2013-09-02 01:54:37 +00:00
|
|
|
session := engine.NewSession()
|
|
|
|
session.IsAutoClose = true
|
2013-09-02 02:06:32 +00:00
|
|
|
return session.Asc(colNames...)
|
2013-09-02 01:54:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) OrderBy(order string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.OrderBy(order)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Join(join_operator, tablename, condition string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Join(join_operator, tablename, condition)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) GroupBy(keys string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.GroupBy(keys)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Having(conditions string) *Session {
|
|
|
|
session := engine.NewSession()
|
2013-09-02 01:54:37 +00:00
|
|
|
session.IsAutoClose = true
|
2013-07-17 17:26:14 +00:00
|
|
|
return session.Having(conditions)
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
//
|
2013-05-13 11:56:38 +00:00
|
|
|
func (engine *Engine) AutoMapType(t reflect.Type) *Table {
|
2013-06-16 03:05:16 +00:00
|
|
|
engine.mutex.Lock()
|
|
|
|
defer engine.mutex.Unlock()
|
2013-05-13 11:56:38 +00:00
|
|
|
table, ok := engine.Tables[t]
|
|
|
|
if !ok {
|
|
|
|
table = engine.MapType(t)
|
2013-06-16 07:10:35 +00:00
|
|
|
engine.Tables[t] = table
|
2013-05-13 11:56:38 +00:00
|
|
|
}
|
2013-06-16 03:05:16 +00:00
|
|
|
return table
|
2013-05-13 11:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (engine *Engine) AutoMap(bean interface{}) *Table {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-05-13 11:56:38 +00:00
|
|
|
return engine.AutoMapType(t)
|
|
|
|
}
|
|
|
|
|
2013-09-17 09:36:34 +00:00
|
|
|
func (engine *Engine) newTable() *Table {
|
2013-10-04 16:44:43 +00:00
|
|
|
table := &Table{}
|
|
|
|
table.Indexes = make(map[string]*Index)
|
2013-09-17 09:36:34 +00:00
|
|
|
table.Columns = make(map[string]*Column)
|
|
|
|
table.ColumnsSeq = make([]string, 0)
|
|
|
|
table.Cacher = engine.Cacher
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) MapType(t reflect.Type) *Table {
|
2013-09-17 09:36:34 +00:00
|
|
|
table := engine.newTable()
|
2013-09-01 03:01:10 +00:00
|
|
|
table.Name = engine.Mapper.Obj2Table(t.Name())
|
|
|
|
table.Type = t
|
|
|
|
|
2013-08-08 16:03:33 +00:00
|
|
|
var idFieldColName string
|
2013-05-03 07:26:51 +00:00
|
|
|
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
tag := t.Field(i).Tag
|
2013-05-19 05:25:52 +00:00
|
|
|
ormTagStr := tag.Get(engine.TagIdentifier)
|
2013-08-08 16:03:33 +00:00
|
|
|
var col *Column
|
2013-05-03 07:26:51 +00:00
|
|
|
fieldType := t.Field(i).Type
|
|
|
|
|
|
|
|
if ormTagStr != "" {
|
2013-08-08 16:03:33 +00:00
|
|
|
col = &Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
|
2013-10-12 15:16:51 +00:00
|
|
|
IsAutoIncrement: false, MapType: TWOSIDES, Indexes: make(map[string]bool)}
|
2013-05-03 07:26:51 +00:00
|
|
|
tags := strings.Split(ormTagStr, " ")
|
2013-08-08 16:03:33 +00:00
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
if len(tags) > 0 {
|
|
|
|
if tags[0] == "-" {
|
|
|
|
continue
|
|
|
|
}
|
2013-08-08 16:03:33 +00:00
|
|
|
if (strings.ToUpper(tags[0]) == "EXTENDS") &&
|
|
|
|
(fieldType.Kind() == reflect.Struct) {
|
2013-06-24 04:08:58 +00:00
|
|
|
parentTable := engine.MapType(fieldType)
|
|
|
|
for name, col := range parentTable.Columns {
|
|
|
|
col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName)
|
|
|
|
table.Columns[name] = col
|
2013-09-01 03:01:10 +00:00
|
|
|
table.ColumnsSeq = append(table.ColumnsSeq, name)
|
2013-06-24 04:08:58 +00:00
|
|
|
}
|
2013-08-08 16:03:33 +00:00
|
|
|
|
|
|
|
table.PrimaryKey = parentTable.PrimaryKey
|
|
|
|
continue
|
2013-06-24 04:08:58 +00:00
|
|
|
}
|
2013-10-12 15:16:51 +00:00
|
|
|
var indexType int
|
|
|
|
var indexName string
|
2013-05-03 07:26:51 +00:00
|
|
|
for j, key := range tags {
|
2013-08-08 16:03:33 +00:00
|
|
|
k := strings.ToUpper(key)
|
2013-05-13 05:24:45 +00:00
|
|
|
switch {
|
2013-06-24 04:08:58 +00:00
|
|
|
case k == "<-":
|
|
|
|
col.MapType = ONLYFROMDB
|
|
|
|
case k == "->":
|
|
|
|
col.MapType = ONLYTODB
|
2013-08-08 16:03:33 +00:00
|
|
|
case k == "PK":
|
2013-05-03 07:26:51 +00:00
|
|
|
col.IsPrimaryKey = true
|
2013-05-13 11:56:38 +00:00
|
|
|
col.Nullable = false
|
2013-08-08 16:03:33 +00:00
|
|
|
case k == "NULL":
|
|
|
|
col.Nullable = (strings.ToUpper(tags[j-1]) != "NOT")
|
|
|
|
case k == "AUTOINCR":
|
2013-05-06 08:01:17 +00:00
|
|
|
col.IsAutoIncrement = true
|
2013-08-08 16:03:33 +00:00
|
|
|
case k == "DEFAULT":
|
2013-05-03 07:26:51 +00:00
|
|
|
col.Default = tags[j+1]
|
2013-09-02 14:50:40 +00:00
|
|
|
case k == "CREATED":
|
|
|
|
col.IsCreated = true
|
2013-11-06 07:36:38 +00:00
|
|
|
case k == "VERSION":
|
|
|
|
col.IsVersion = true
|
|
|
|
col.Default = "1"
|
2013-09-02 14:50:40 +00:00
|
|
|
case k == "UPDATED":
|
|
|
|
col.IsUpdated = true
|
2013-10-04 16:44:43 +00:00
|
|
|
case strings.HasPrefix(k, "INDEX(") && strings.HasSuffix(k, ")"):
|
2013-10-12 15:16:51 +00:00
|
|
|
indexType = IndexType
|
|
|
|
indexName = k[len("INDEX")+1 : len(k)-1]
|
2013-10-04 16:44:43 +00:00
|
|
|
case k == "INDEX":
|
2013-10-12 15:16:51 +00:00
|
|
|
indexType = IndexType
|
2013-10-04 16:44:43 +00:00
|
|
|
case strings.HasPrefix(k, "UNIQUE(") && strings.HasSuffix(k, ")"):
|
2013-10-12 15:16:51 +00:00
|
|
|
indexName = k[len("UNIQUE")+1 : len(k)-1]
|
|
|
|
indexType = UniqueType
|
2013-10-04 16:44:43 +00:00
|
|
|
case k == "UNIQUE":
|
2013-10-12 15:16:51 +00:00
|
|
|
indexType = UniqueType
|
2013-10-04 16:44:43 +00:00
|
|
|
case k == "NOTNULL":
|
|
|
|
col.Nullable = false
|
2013-08-08 16:03:33 +00:00
|
|
|
case k == "NOT":
|
2013-05-03 07:26:51 +00:00
|
|
|
default:
|
2013-09-02 14:50:40 +00:00
|
|
|
if strings.HasPrefix(k, "'") && strings.HasSuffix(k, "'") {
|
|
|
|
if key != col.Default {
|
|
|
|
col.Name = key[1 : len(key)-1]
|
|
|
|
}
|
|
|
|
} else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
|
2013-08-08 16:03:33 +00:00
|
|
|
fs := strings.Split(k, "(")
|
|
|
|
if _, ok := sqlTypes[fs[0]]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
col.SQLType = SQLType{fs[0], 0, 0}
|
|
|
|
fs2 := strings.Split(fs[1][0:len(fs[1])-1], ",")
|
|
|
|
if len(fs2) == 2 {
|
|
|
|
col.Length, _ = strconv.Atoi(fs2[0])
|
|
|
|
col.Length2, _ = strconv.Atoi(fs2[1])
|
|
|
|
} else if len(fs2) == 1 {
|
|
|
|
col.Length, _ = strconv.Atoi(fs2[0])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := sqlTypes[k]; ok {
|
|
|
|
col.SQLType = SQLType{k, 0, 0}
|
2013-09-02 14:50:40 +00:00
|
|
|
} else if key != col.Default {
|
2013-08-08 16:03:33 +00:00
|
|
|
col.Name = key
|
|
|
|
}
|
2013-05-13 05:24:45 +00:00
|
|
|
}
|
2013-08-08 16:03:33 +00:00
|
|
|
engine.SqlType(col)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if col.SQLType.Name == "" {
|
|
|
|
col.SQLType = Type2SQLType(fieldType)
|
2013-05-13 05:24:45 +00:00
|
|
|
}
|
|
|
|
if col.Length == 0 {
|
2013-05-03 07:26:51 +00:00
|
|
|
col.Length = col.SQLType.DefaultLength
|
2013-05-13 05:24:45 +00:00
|
|
|
}
|
|
|
|
if col.Length2 == 0 {
|
2013-05-06 08:01:17 +00:00
|
|
|
col.Length2 = col.SQLType.DefaultLength2
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
if col.Name == "" {
|
|
|
|
col.Name = engine.Mapper.Obj2Table(t.Field(i).Name)
|
|
|
|
}
|
2013-10-12 15:16:51 +00:00
|
|
|
if indexType == IndexType {
|
|
|
|
if indexName == "" {
|
|
|
|
indexName = col.Name
|
|
|
|
}
|
|
|
|
if index, ok := table.Indexes[indexName]; ok {
|
|
|
|
index.AddColumn(col.Name)
|
|
|
|
col.Indexes[index.Name] = true
|
|
|
|
} else {
|
|
|
|
index := NewIndex(indexName, IndexType)
|
|
|
|
index.AddColumn(col.Name)
|
|
|
|
table.AddIndex(index)
|
|
|
|
col.Indexes[index.Name] = true
|
|
|
|
}
|
|
|
|
} else if indexType == UniqueType {
|
|
|
|
if indexName == "" {
|
|
|
|
indexName = col.Name
|
|
|
|
}
|
|
|
|
if index, ok := table.Indexes[indexName]; ok {
|
|
|
|
index.AddColumn(col.Name)
|
|
|
|
col.Indexes[index.Name] = true
|
|
|
|
} else {
|
|
|
|
index := NewIndex(indexName, UniqueType)
|
|
|
|
index.AddColumn(col.Name)
|
|
|
|
table.AddIndex(index)
|
|
|
|
col.Indexes[index.Name] = true
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-06-04 08:56:59 +00:00
|
|
|
} else {
|
2013-05-03 07:26:51 +00:00
|
|
|
sqlType := Type2SQLType(fieldType)
|
2013-08-08 16:03:33 +00:00
|
|
|
col = &Column{engine.Mapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
|
2013-10-12 15:16:51 +00:00
|
|
|
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", make(map[string]bool), false, false,
|
2013-11-06 07:36:38 +00:00
|
|
|
TWOSIDES, false, false, false, false}
|
2013-08-08 16:03:33 +00:00
|
|
|
}
|
|
|
|
if col.IsAutoIncrement {
|
|
|
|
col.Nullable = false
|
|
|
|
}
|
2013-10-04 16:44:43 +00:00
|
|
|
|
2013-09-01 03:01:10 +00:00
|
|
|
table.AddColumn(col)
|
|
|
|
|
2013-08-08 16:03:33 +00:00
|
|
|
if col.FieldName == "Id" || strings.HasSuffix(col.FieldName, ".Id") {
|
|
|
|
idFieldColName = col.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idFieldColName != "" && table.PrimaryKey == "" {
|
|
|
|
col := table.Columns[idFieldColName]
|
|
|
|
col.IsPrimaryKey = true
|
|
|
|
col.IsAutoIncrement = true
|
|
|
|
col.Nullable = false
|
|
|
|
table.PrimaryKey = col.Name
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2013-09-30 07:08:34 +00:00
|
|
|
// Map a struct to a table
|
2013-05-03 07:26:51 +00:00
|
|
|
func (engine *Engine) Map(beans ...interface{}) (e error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
engine.mutex.Lock()
|
|
|
|
defer engine.mutex.Unlock()
|
2013-05-03 07:26:51 +00:00
|
|
|
for _, bean := range beans {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-07-03 03:49:29 +00:00
|
|
|
engine.Tables[t] = engine.MapType(t)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// Is a table has any reocrd
|
2013-09-30 07:08:34 +00:00
|
|
|
func (engine *Engine) IsTableEmpty(bean interface{}) (bool, error) {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-09-28 15:14:42 +00:00
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
return false, errors.New("bean should be a struct or struct's point")
|
|
|
|
}
|
|
|
|
engine.AutoMapType(t)
|
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
2013-10-04 16:44:43 +00:00
|
|
|
rows, err := session.Count(bean)
|
|
|
|
return rows > 0, err
|
2013-09-28 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// Is a table is exist
|
2013-09-29 08:43:10 +00:00
|
|
|
func (engine *Engine) IsTableExist(bean interface{}) (bool, error) {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-09-28 15:14:42 +00:00
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
return false, errors.New("bean should be a struct or struct's point")
|
|
|
|
}
|
|
|
|
table := engine.AutoMapType(t)
|
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
has, err := session.isTableExist(table.Name)
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
2013-10-12 15:16:51 +00:00
|
|
|
// create indexes
|
|
|
|
func (engine *Engine) CreateIndexes(bean interface{}) error {
|
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
return session.CreateIndexes(bean)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create uniques
|
|
|
|
func (engine *Engine) CreateUniques(bean interface{}) error {
|
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
return session.CreateUniques(bean)
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// If enabled cache, clear the cache bean
|
2013-09-29 08:43:10 +00:00
|
|
|
func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-09-29 08:43:10 +00:00
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
return errors.New("error params")
|
|
|
|
}
|
|
|
|
table := engine.AutoMap(bean)
|
|
|
|
if table.Cacher != nil {
|
|
|
|
table.Cacher.ClearIds(table.Name)
|
|
|
|
table.Cacher.DelBean(table.Name, id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// If enabled cache, clear some tables' cache
|
2013-09-29 08:43:10 +00:00
|
|
|
func (engine *Engine) ClearCache(beans ...interface{}) error {
|
2013-09-28 15:14:42 +00:00
|
|
|
for _, bean := range beans {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-09-29 08:43:10 +00:00
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
return errors.New("error params")
|
|
|
|
}
|
2013-09-28 15:14:42 +00:00
|
|
|
table := engine.AutoMap(bean)
|
2013-09-29 08:43:10 +00:00
|
|
|
if table.Cacher != nil {
|
|
|
|
table.Cacher.ClearIds(table.Name)
|
|
|
|
table.Cacher.ClearBeans(table.Name)
|
|
|
|
}
|
2013-09-28 15:14:42 +00:00
|
|
|
}
|
2013-09-29 08:43:10 +00:00
|
|
|
return nil
|
2013-09-28 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// Sync the new struct change 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.
|
2013-09-28 15:14:42 +00:00
|
|
|
func (engine *Engine) Sync(beans ...interface{}) error {
|
|
|
|
for _, bean := range beans {
|
|
|
|
table := engine.AutoMap(bean)
|
|
|
|
|
|
|
|
s := engine.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
isExist, err := s.Table(bean).isTableExist(table.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !isExist {
|
|
|
|
err = engine.CreateTables(bean)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-29 01:02:01 +00:00
|
|
|
}
|
|
|
|
/*isEmpty, err := engine.IsEmptyTable(bean)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}*/
|
|
|
|
var isEmpty bool = false
|
|
|
|
if isEmpty {
|
|
|
|
err = engine.DropTables(bean)
|
2013-09-28 15:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-09-29 01:02:01 +00:00
|
|
|
}
|
|
|
|
err = engine.CreateTables(bean)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, col := range table.Columns {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Statement.RefTable = table
|
|
|
|
defer session.Close()
|
|
|
|
isExist, err := session.isColumnExist(table.Name, col.Name)
|
2013-09-28 15:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-29 01:02:01 +00:00
|
|
|
if !isExist {
|
2013-09-28 15:14:42 +00:00
|
|
|
session := engine.NewSession()
|
|
|
|
session.Statement.RefTable = table
|
|
|
|
defer session.Close()
|
2013-09-29 01:02:01 +00:00
|
|
|
err = session.addColumn(col.Name)
|
2013-09-28 15:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 01:02:01 +00:00
|
|
|
}
|
2013-09-28 15:14:42 +00:00
|
|
|
|
2013-10-04 16:44:43 +00:00
|
|
|
for name, index := range table.Indexes {
|
2013-09-29 01:02:01 +00:00
|
|
|
session := engine.NewSession()
|
|
|
|
session.Statement.RefTable = table
|
|
|
|
defer session.Close()
|
2013-10-12 15:16:51 +00:00
|
|
|
if index.Type == UniqueType {
|
2013-10-28 03:16:22 +00:00
|
|
|
//isExist, err := session.isIndexExist(table.Name, name, true)
|
|
|
|
isExist, err := session.isIndexExist2(table.Name, index.Cols, true)
|
2013-09-28 15:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-04 16:44:43 +00:00
|
|
|
if !isExist {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Statement.RefTable = table
|
|
|
|
defer session.Close()
|
|
|
|
err = session.addUnique(table.Name, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-10-12 15:16:51 +00:00
|
|
|
} else if index.Type == IndexType {
|
2013-10-28 03:16:22 +00:00
|
|
|
isExist, err := session.isIndexExist2(table.Name, index.Cols, false)
|
2013-09-28 15:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-04 16:44:43 +00:00
|
|
|
if !isExist {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Statement.RefTable = table
|
|
|
|
defer session.Close()
|
|
|
|
err = session.addIndex(table.Name, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-10-12 15:16:51 +00:00
|
|
|
} else {
|
|
|
|
return errors.New("unknow index type")
|
2013-09-28 15:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
func (engine *Engine) UnMap(beans ...interface{}) (e error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
engine.mutex.Lock()
|
|
|
|
defer engine.mutex.Unlock()
|
2013-05-03 07:26:51 +00:00
|
|
|
for _, bean := range beans {
|
2013-09-30 01:17:35 +00:00
|
|
|
t := rType(bean)
|
2013-05-08 14:50:19 +00:00
|
|
|
if _, ok := engine.Tables[t]; ok {
|
|
|
|
delete(engine.Tables, t)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// Drop all mapped table
|
2013-09-30 06:48:17 +00:00
|
|
|
func (engine *Engine) DropAll() error {
|
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
2013-06-16 03:05:16 +00:00
|
|
|
|
|
|
|
err := session.Begin()
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-16 03:05:16 +00:00
|
|
|
err = session.DropAll()
|
|
|
|
if err != nil {
|
2013-08-08 05:24:38 +00:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-05-08 14:50:19 +00:00
|
|
|
return session.Commit()
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
// CreateTables create tabls according bean
|
2013-09-30 06:48:17 +00:00
|
|
|
func (engine *Engine) CreateTables(beans ...interface{}) error {
|
|
|
|
session := engine.NewSession()
|
2013-06-16 03:05:16 +00:00
|
|
|
err := session.Begin()
|
2013-06-21 04:33:54 +00:00
|
|
|
defer session.Close()
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-03 03:49:29 +00:00
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
for _, bean := range beans {
|
2013-05-19 05:25:52 +00:00
|
|
|
err = session.CreateTable(bean)
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
session.Rollback()
|
2013-05-08 14:50:19 +00:00
|
|
|
return err
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-08 14:50:19 +00:00
|
|
|
return session.Commit()
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:48:17 +00:00
|
|
|
func (engine *Engine) DropTables(beans ...interface{}) error {
|
|
|
|
session := engine.NewSession()
|
2013-07-27 13:47:22 +00:00
|
|
|
err := session.Begin()
|
|
|
|
defer session.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bean := range beans {
|
|
|
|
err = session.DropTable(bean)
|
|
|
|
if err != nil {
|
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return session.Commit()
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:48:17 +00:00
|
|
|
func (engine *Engine) CreateAll() error {
|
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
2013-08-08 05:24:38 +00:00
|
|
|
return session.CreateAll()
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
func (engine *Engine) Exec(sql string, args ...interface{}) (sql.Result, error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-08 13:42:22 +00:00
|
|
|
defer session.Close()
|
|
|
|
return session.Exec(sql, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (engine *Engine) Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-08 13:42:22 +00:00
|
|
|
defer session.Close()
|
|
|
|
return session.Query(sql, paramStr...)
|
|
|
|
}
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
|
|
|
return session.Insert(beans...)
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +00:00
|
|
|
func (engine *Engine) InsertOne(bean interface{}) (int64, error) {
|
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
return session.InsertOne(bean)
|
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
func (engine *Engine) Update(bean interface{}, condiBeans ...interface{}) (int64, error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
2013-05-08 13:42:22 +00:00
|
|
|
return session.Update(bean, condiBeans...)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (engine *Engine) Delete(bean interface{}) (int64, error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
|
|
|
return session.Delete(bean)
|
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Get(bean interface{}) (bool, error) {
|
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
|
|
|
return session.Get(bean)
|
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
func (engine *Engine) Find(beans interface{}, condiBeans ...interface{}) error {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
2013-05-08 13:42:22 +00:00
|
|
|
return session.Find(beans, condiBeans...)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 04:50:46 +00:00
|
|
|
func (engine *Engine) Iterate(bean interface{}, fun IterFunc) error {
|
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
return session.Iterate(bean, fun)
|
|
|
|
}
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
func (engine *Engine) Count(bean interface{}) (int64, error) {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
|
|
|
return session.Count(bean)
|
|
|
|
}
|