2013-05-03 07:26:51 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2013-06-24 04:08:58 +00:00
|
|
|
"fmt"
|
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 (
|
|
|
|
PQSQL = "pqsql"
|
|
|
|
MSSQL = "mssql"
|
2013-05-09 01:56:58 +00:00
|
|
|
SQLITE = "sqlite3"
|
2013-05-03 07:26:51 +00:00
|
|
|
MYSQL = "mysql"
|
|
|
|
MYMYSQL = "mymysql"
|
|
|
|
)
|
|
|
|
|
2013-06-04 08:56:59 +00:00
|
|
|
type dialect interface {
|
|
|
|
SqlType(t *Column) string
|
|
|
|
}
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
type Engine struct {
|
|
|
|
Mapper IMapper
|
2013-05-19 05:25:52 +00:00
|
|
|
TagIdentifier string
|
2013-05-09 01:56:58 +00:00
|
|
|
DriverName string
|
|
|
|
DataSourceName string
|
2013-06-04 08:56:59 +00:00
|
|
|
Dialect dialect
|
2013-06-16 03:05:16 +00:00
|
|
|
Tables map[reflect.Type]*Table
|
|
|
|
mutex *sync.Mutex
|
2013-05-03 07:26:51 +00:00
|
|
|
AutoIncrement string
|
|
|
|
ShowSQL bool
|
2013-05-12 13:37:10 +00:00
|
|
|
InsertMany bool
|
2013-05-03 07:26:51 +00:00
|
|
|
QuoteIdentifier string
|
2013-06-16 03:05:16 +00:00
|
|
|
Pool IConnectionPool
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Type(bean interface{}) reflect.Type {
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
return reflect.TypeOf(sliceValue.Interface())
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 14:50:19 +00:00
|
|
|
func StructName(v reflect.Type) string {
|
|
|
|
for v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
return v.Name()
|
|
|
|
}
|
|
|
|
|
2013-05-09 01:56:58 +00:00
|
|
|
func (e *Engine) OpenDB() (*sql.DB, error) {
|
|
|
|
return sql.Open(e.DriverName, e.DataSourceName)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
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-06-12 13:51:39 +00:00
|
|
|
func (engine *Engine) Test() error {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := engine.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
return session.Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Sql(querystring, args...)
|
|
|
|
return session
|
2013-06-12 13:51:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Where(querystring string, args ...interface{}) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Where(querystring, args...)
|
|
|
|
return session
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Id(id int64) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Id(id)
|
|
|
|
return session
|
2013-05-09 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) In(column string, args ...interface{}) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.In(column, args...)
|
|
|
|
return session
|
2013-05-11 08:27:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) Table(tableName string) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.Table(tableName)
|
|
|
|
return session
|
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()
|
|
|
|
session.Limit(limit, start...)
|
|
|
|
return session
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) OrderBy(order string) *Session {
|
|
|
|
session := engine.NewSession()
|
|
|
|
session.OrderBy(order)
|
|
|
|
return session
|
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()
|
|
|
|
session.Join(join_operator, tablename, condition)
|
|
|
|
return session
|
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()
|
|
|
|
session.GroupBy(keys)
|
|
|
|
return session
|
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()
|
|
|
|
session.Having(conditions)
|
|
|
|
return session
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
// some lock needed
|
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 {
|
|
|
|
t := Type(bean)
|
|
|
|
return engine.AutoMapType(t)
|
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
func (engine *Engine) MapType(t reflect.Type) *Table {
|
|
|
|
table := &Table{Name: engine.Mapper.Obj2Table(t.Name()), Type: t}
|
2013-05-03 07:26:51 +00:00
|
|
|
table.Columns = make(map[string]Column)
|
|
|
|
|
|
|
|
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-05-03 07:26:51 +00:00
|
|
|
var col Column
|
|
|
|
fieldType := t.Field(i).Type
|
|
|
|
|
|
|
|
if ormTagStr != "" {
|
2013-06-04 08:56:59 +00:00
|
|
|
col = Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
|
2013-06-24 04:08:58 +00:00
|
|
|
IsAutoIncrement: false, MapType: TWOSIDES}
|
2013-05-03 07:26:51 +00:00
|
|
|
ormTagStr = strings.ToLower(ormTagStr)
|
|
|
|
tags := strings.Split(ormTagStr, " ")
|
2013-06-04 08:56:59 +00:00
|
|
|
// TODO:
|
2013-05-03 07:26:51 +00:00
|
|
|
if len(tags) > 0 {
|
|
|
|
if tags[0] == "-" {
|
|
|
|
continue
|
|
|
|
}
|
2013-06-24 04:08:58 +00:00
|
|
|
if (tags[0] == "extends") &&
|
|
|
|
(fieldType.Kind() == reflect.Struct) &&
|
|
|
|
t.Field(i).Anonymous {
|
|
|
|
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-05-03 07:26:51 +00:00
|
|
|
for j, key := range tags {
|
2013-05-13 05:24:45 +00:00
|
|
|
k := strings.ToLower(key)
|
|
|
|
switch {
|
2013-06-24 04:08:58 +00:00
|
|
|
case k == "<-":
|
|
|
|
col.MapType = ONLYFROMDB
|
|
|
|
case k == "->":
|
|
|
|
col.MapType = ONLYTODB
|
2013-05-13 05:24:45 +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-05-13 05:24:45 +00:00
|
|
|
case k == "null":
|
2013-05-03 07:26:51 +00:00
|
|
|
col.Nullable = (tags[j-1] != "not")
|
2013-05-13 05:24:45 +00:00
|
|
|
case k == "autoincr":
|
2013-05-06 08:01:17 +00:00
|
|
|
col.IsAutoIncrement = true
|
2013-05-13 05:24:45 +00:00
|
|
|
case k == "default":
|
2013-05-03 07:26:51 +00:00
|
|
|
col.Default = tags[j+1]
|
2013-05-13 05:24:45 +00:00
|
|
|
case k == "text":
|
|
|
|
col.SQLType = Text
|
|
|
|
case strings.HasPrefix(k, "int"):
|
2013-06-19 02:07:28 +00:00
|
|
|
if k == "int" {
|
|
|
|
col.SQLType = Int
|
|
|
|
col.Length = Int.DefaultLength
|
|
|
|
col.Length2 = Int.DefaultLength2
|
|
|
|
} else {
|
|
|
|
col.SQLType = Int
|
|
|
|
lens := k[len("int")+1 : len(k)-1]
|
|
|
|
col.Length, _ = strconv.Atoi(lens)
|
|
|
|
}
|
2013-05-13 05:24:45 +00:00
|
|
|
case strings.HasPrefix(k, "varchar"):
|
|
|
|
col.SQLType = Varchar
|
2013-06-04 08:56:59 +00:00
|
|
|
lens := k[len("varchar")+1 : len(k)-1]
|
2013-05-13 05:24:45 +00:00
|
|
|
col.Length, _ = strconv.Atoi(lens)
|
|
|
|
case strings.HasPrefix(k, "decimal"):
|
|
|
|
col.SQLType = Decimal
|
|
|
|
lens := k[len("decimal")+1 : len(k)-1]
|
|
|
|
twolen := strings.Split(lens, ",")
|
|
|
|
col.Length, _ = strconv.Atoi(twolen[0])
|
|
|
|
col.Length2, _ = strconv.Atoi(twolen[1])
|
|
|
|
case k == "date":
|
|
|
|
col.SQLType = Date
|
2013-06-04 08:56:59 +00:00
|
|
|
case k == "datetime":
|
|
|
|
col.SQLType = DateTime
|
|
|
|
case k == "timestamp":
|
|
|
|
col.SQLType = TimeStamp
|
2013-05-13 05:24:45 +00:00
|
|
|
case k == "not":
|
2013-05-03 07:26:51 +00:00
|
|
|
default:
|
2013-05-13 05:24:45 +00:00
|
|
|
if k != col.Default {
|
|
|
|
col.Name = k
|
|
|
|
}
|
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-06-04 08:56:59 +00:00
|
|
|
if col.IsPrimaryKey {
|
|
|
|
table.PrimaryKey = col.Name
|
|
|
|
}
|
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)
|
|
|
|
col = Column{engine.Mapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
|
2013-06-24 04:08:58 +00:00
|
|
|
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", false, false, false, TWOSIDES}
|
2013-05-03 07:26:51 +00:00
|
|
|
|
2013-06-04 08:56:59 +00:00
|
|
|
if col.Name == "id" {
|
|
|
|
col.IsPrimaryKey = true
|
|
|
|
col.IsAutoIncrement = true
|
|
|
|
col.Nullable = false
|
|
|
|
table.PrimaryKey = col.Name
|
|
|
|
}
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-06-04 08:56:59 +00:00
|
|
|
table.Columns[col.Name] = col
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
// Map should use after all operation because it's not thread safe
|
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-05-08 14:50:19 +00:00
|
|
|
t := Type(bean)
|
|
|
|
if _, ok := engine.Tables[t]; !ok {
|
2013-05-19 05:25:52 +00:00
|
|
|
engine.Tables[t] = engine.MapType(t)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-08 14:50:19 +00:00
|
|
|
t := Type(bean)
|
|
|
|
if _, ok := engine.Tables[t]; ok {
|
|
|
|
delete(engine.Tables, t)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) DropAll() error {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := e.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 {
|
|
|
|
return session.Rollback()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) CreateTables(beans ...interface{}) error {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := e.NewSession()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) CreateAll() error {
|
2013-06-16 03:05:16 +00:00
|
|
|
session := e.NewSession()
|
|
|
|
err := session.Begin()
|
2013-05-03 07:26:51 +00:00
|
|
|
defer session.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-16 03:05:16 +00:00
|
|
|
err = session.CreateAll()
|
|
|
|
if err != nil {
|
|
|
|
return session.Rollback()
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-06-16 03:05:16 +00:00
|
|
|
return session.Commit()
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|