2013-05-03 07:26:51 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Session struct {
|
2013-05-12 13:37:10 +00:00
|
|
|
Db *sql.DB
|
|
|
|
Engine *Engine
|
|
|
|
Tx *sql.Tx
|
|
|
|
Statement Statement
|
|
|
|
IsAutoCommit bool
|
|
|
|
IsCommitedOrRollbacked bool
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) Init() {
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement = Statement{Engine: session.Engine}
|
2013-05-06 08:01:17 +00:00
|
|
|
session.IsAutoCommit = true
|
2013-05-12 13:37:10 +00:00
|
|
|
session.IsCommitedOrRollbacked = false
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) Close() {
|
|
|
|
defer session.Db.Close()
|
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
func (session *Session) Where(querystring string, args ...interface{}) *Session {
|
2013-05-08 13:42:22 +00:00
|
|
|
session.Statement.Where(querystring, args...)
|
2013-05-03 07:26:51 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
func (session *Session) Id(id int64) *Session {
|
2013-05-09 01:56:58 +00:00
|
|
|
session.Statement.Id(id)
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
func (session *Session) Table(tableName string) *Session {
|
|
|
|
session.Statement.Table(tableName)
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2013-05-11 08:27:17 +00:00
|
|
|
func (session *Session) In(column string, args ...interface{}) *Session {
|
|
|
|
session.Statement.In(column, args...)
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
func (session *Session) Limit(limit int, start ...int) *Session {
|
2013-05-08 13:42:22 +00:00
|
|
|
session.Statement.Limit(limit, start...)
|
2013-05-03 07:26:51 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) OrderBy(order string) *Session {
|
2013-05-08 13:42:22 +00:00
|
|
|
session.Statement.OrderBy(order)
|
2013-05-03 07:26:51 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
|
|
|
func (session *Session) Join(join_operator, tablename, condition string) *Session {
|
2013-05-08 13:42:22 +00:00
|
|
|
session.Statement.Join(join_operator, tablename, condition)
|
2013-05-03 07:26:51 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) GroupBy(keys string) *Session {
|
2013-05-08 13:42:22 +00:00
|
|
|
session.Statement.GroupBy(keys)
|
2013-05-03 07:26:51 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) Having(conditions string) *Session {
|
2013-05-08 13:42:22 +00:00
|
|
|
session.Statement.Having(conditions)
|
2013-05-03 07:26:51 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
func (session *Session) Begin() error {
|
2013-05-12 13:37:10 +00:00
|
|
|
if session.IsAutoCommit {
|
|
|
|
session.IsAutoCommit = false
|
|
|
|
session.IsCommitedOrRollbacked = false
|
|
|
|
tx, err := session.Db.Begin()
|
|
|
|
session.Tx = tx
|
|
|
|
if session.Engine.ShowSQL {
|
|
|
|
fmt.Println("BEGIN TRANSACTION")
|
|
|
|
}
|
|
|
|
return err
|
2013-05-08 13:42:22 +00:00
|
|
|
}
|
2013-05-12 13:37:10 +00:00
|
|
|
return nil
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
func (session *Session) Rollback() error {
|
2013-05-12 13:37:10 +00:00
|
|
|
if !session.IsAutoCommit && !session.IsCommitedOrRollbacked {
|
|
|
|
if session.Engine.ShowSQL {
|
|
|
|
fmt.Println("ROLL BACK")
|
|
|
|
}
|
|
|
|
session.IsCommitedOrRollbacked = true
|
|
|
|
return session.Tx.Rollback()
|
2013-05-08 13:42:22 +00:00
|
|
|
}
|
2013-05-12 13:37:10 +00:00
|
|
|
return nil
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
func (session *Session) Commit() error {
|
2013-05-12 13:37:10 +00:00
|
|
|
if !session.IsAutoCommit && !session.IsCommitedOrRollbacked {
|
|
|
|
if session.Engine.ShowSQL {
|
|
|
|
fmt.Println("COMMIT")
|
|
|
|
}
|
|
|
|
session.IsCommitedOrRollbacked = true
|
|
|
|
return session.Tx.Commit()
|
2013-05-08 13:42:22 +00:00
|
|
|
}
|
2013-05-12 13:37:10 +00:00
|
|
|
return nil
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]byte) error {
|
|
|
|
dataStruct := reflect.Indirect(reflect.ValueOf(obj))
|
|
|
|
if dataStruct.Kind() != reflect.Struct {
|
|
|
|
return errors.New("expected a pointer to a struct")
|
|
|
|
}
|
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
table := session.Engine.Tables[Type(obj)]
|
2013-05-03 07:26:51 +00:00
|
|
|
|
|
|
|
for key, data := range objMap {
|
|
|
|
structField := dataStruct.FieldByName(table.Columns[key].FieldName)
|
|
|
|
if !structField.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var v interface{}
|
|
|
|
|
|
|
|
switch structField.Type().Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
v = data
|
|
|
|
case reflect.String:
|
|
|
|
v = string(data)
|
|
|
|
case reflect.Bool:
|
|
|
|
v = string(data) == "1"
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
|
|
|
x, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("arg " + key + " as int: " + err.Error())
|
|
|
|
}
|
|
|
|
v = x
|
|
|
|
case reflect.Int64:
|
|
|
|
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("arg " + key + " as int: " + err.Error())
|
|
|
|
}
|
|
|
|
v = x
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
x, err := strconv.ParseFloat(string(data), 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("arg " + key + " as float64: " + err.Error())
|
|
|
|
}
|
|
|
|
v = x
|
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("arg " + key + " as int: " + err.Error())
|
|
|
|
}
|
|
|
|
v = x
|
|
|
|
//Now only support Time type
|
|
|
|
case reflect.Struct:
|
|
|
|
if structField.Type().String() != "time.Time" {
|
|
|
|
return errors.New("unsupported struct type in Scan: " + structField.Type().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
x, err := time.Parse("2006-01-02 15:04:05", string(data))
|
|
|
|
if err != nil {
|
|
|
|
x, err = time.Parse("2006-01-02 15:04:05.000 -0700", string(data))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("unsupported time format: " + string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v = x
|
|
|
|
default:
|
|
|
|
return errors.New("unsupported type in Scan: " + reflect.TypeOf(v).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
structField.Set(reflect.ValueOf(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
//Execute sql
|
2013-05-08 13:42:22 +00:00
|
|
|
func (session *Session) innerExec(sql string, args ...interface{}) (sql.Result, error) {
|
|
|
|
rs, err := session.Db.Prepare(sql)
|
2013-05-06 08:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rs.Close()
|
|
|
|
|
|
|
|
res, err := rs.Exec(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
func (session *Session) Exec(sql string, args ...interface{}) (sql.Result, error) {
|
2013-05-19 05:25:52 +00:00
|
|
|
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
|
|
|
sql = strings.Replace(sql, "(id)", session.Statement.RefTable.PrimaryKey, -1)
|
2013-05-09 01:56:58 +00:00
|
|
|
}
|
2013-05-08 13:42:22 +00:00
|
|
|
if session.Engine.ShowSQL {
|
|
|
|
fmt.Println(sql)
|
|
|
|
}
|
|
|
|
if session.IsAutoCommit {
|
|
|
|
return session.innerExec(sql, args...)
|
|
|
|
}
|
|
|
|
return session.Tx.Exec(sql, args...)
|
|
|
|
}
|
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
func (session *Session) CreateTable(bean interface{}) error {
|
|
|
|
statement := session.Statement
|
|
|
|
defer statement.Init()
|
|
|
|
statement.RefTable = session.Engine.AutoMap(bean)
|
|
|
|
sql := statement.genCreateSQL()
|
|
|
|
_, err := session.Exec(sql)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
func (session *Session) Get(bean interface{}) error {
|
2013-05-08 13:42:22 +00:00
|
|
|
statement := session.Statement
|
2013-05-19 05:25:52 +00:00
|
|
|
defer statement.Init()
|
2013-05-06 08:01:17 +00:00
|
|
|
statement.Limit(1)
|
2013-05-13 11:56:38 +00:00
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
sql, args := statement.genGetSql(bean)
|
|
|
|
resultsSlice, err := session.Query(sql, args...)
|
2013-05-08 13:42:22 +00:00
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resultsSlice) == 0 {
|
|
|
|
return nil
|
|
|
|
} else if len(resultsSlice) == 1 {
|
|
|
|
results := resultsSlice[0]
|
2013-05-06 08:01:17 +00:00
|
|
|
err := session.scanMapIntoStruct(bean, results)
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("More than one record")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) Count(bean interface{}) (int64, error) {
|
2013-05-08 13:42:22 +00:00
|
|
|
statement := session.Statement
|
|
|
|
defer session.Statement.Init()
|
2013-05-19 05:25:52 +00:00
|
|
|
sql, args := statement.genCountSql(bean)
|
2013-05-03 07:26:51 +00:00
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
resultsSlice, err := session.Query(sql, args...)
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var total int64 = 0
|
|
|
|
for _, results := range resultsSlice {
|
|
|
|
total, err = strconv.ParseInt(string(results["total"]), 10, 64)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(total), err
|
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
|
|
|
|
statement := session.Statement
|
|
|
|
defer session.Statement.Init()
|
2013-05-03 07:26:51 +00:00
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
if sliceValue.Kind() != reflect.Slice {
|
|
|
|
return errors.New("needs a pointer to a slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceElementType := sliceValue.Type().Elem()
|
2013-05-13 11:56:38 +00:00
|
|
|
table := session.Engine.AutoMapType(sliceElementType)
|
2013-05-19 05:25:52 +00:00
|
|
|
statement.RefTable = table
|
2013-05-03 07:26:51 +00:00
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
if len(condiBean) > 0 {
|
2013-05-19 05:25:52 +00:00
|
|
|
colNames, args := BuildConditions(session.Engine, table, condiBean[0])
|
2013-05-08 13:42:22 +00:00
|
|
|
statement.ColumnStr = strings.Join(colNames, " and ")
|
|
|
|
statement.BeanArgs = args
|
|
|
|
}
|
|
|
|
|
|
|
|
sql := statement.generateSql()
|
|
|
|
resultsSlice, err := session.Query(sql, append(statement.Params, statement.BeanArgs...)...)
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, results := range resultsSlice {
|
|
|
|
newValue := reflect.New(sliceElementType)
|
|
|
|
err := session.scanMapIntoStruct(newValue.Interface(), results)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(newValue.Interface()))))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-09 01:56:58 +00:00
|
|
|
func (session *Session) Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
2013-05-19 05:25:52 +00:00
|
|
|
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
|
|
|
sql = strings.Replace(sql, "(id)", session.Statement.RefTable.PrimaryKey, -1)
|
2013-05-09 01:56:58 +00:00
|
|
|
}
|
2013-05-03 07:26:51 +00:00
|
|
|
if session.Engine.ShowSQL {
|
2013-05-09 01:56:58 +00:00
|
|
|
fmt.Println(sql)
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-05-09 01:56:58 +00:00
|
|
|
s, err := session.Db.Prepare(sql)
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
res, err := s.Query(paramStr...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Close()
|
|
|
|
fields, err := res.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for res.Next() {
|
|
|
|
result := make(map[string][]byte)
|
2013-05-06 08:01:17 +00:00
|
|
|
//scanResultContainers := make([]interface{}, len(fields))
|
2013-05-03 07:26:51 +00:00
|
|
|
var scanResultContainers []interface{}
|
|
|
|
for i := 0; i < len(fields); i++ {
|
|
|
|
var scanResultContainer interface{}
|
|
|
|
scanResultContainers = append(scanResultContainers, &scanResultContainer)
|
|
|
|
}
|
|
|
|
if err := res.Scan(scanResultContainers...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for ii, key := range fields {
|
|
|
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
|
|
|
|
|
|
|
//if row is null then ignore
|
|
|
|
if rawValue.Interface() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
aa := reflect.TypeOf(rawValue.Interface())
|
|
|
|
vv := reflect.ValueOf(rawValue.Interface())
|
|
|
|
var str string
|
|
|
|
switch aa.Kind() {
|
|
|
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
str = strconv.FormatInt(vv.Int(), 10)
|
|
|
|
result[key] = []byte(str)
|
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
str = strconv.FormatUint(vv.Uint(), 10)
|
|
|
|
result[key] = []byte(str)
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
str = strconv.FormatFloat(vv.Float(), 'f', -1, 64)
|
|
|
|
result[key] = []byte(str)
|
|
|
|
case reflect.Slice:
|
|
|
|
if aa.Elem().Kind() == reflect.Uint8 {
|
|
|
|
result[key] = rawValue.Interface().([]byte)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
str = vv.String()
|
|
|
|
result[key] = []byte(str)
|
|
|
|
//时间类型
|
|
|
|
case reflect.Struct:
|
|
|
|
str = rawValue.Interface().(time.Time).Format("2006-01-02 15:04:05.000 -0700")
|
|
|
|
result[key] = []byte(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
resultsSlice = append(resultsSlice, result)
|
|
|
|
}
|
|
|
|
return resultsSlice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
|
|
|
var lastId int64 = -1
|
2013-05-12 13:37:10 +00:00
|
|
|
var err error = nil
|
|
|
|
isInTransaction := !session.IsAutoCommit
|
|
|
|
|
|
|
|
if !isInTransaction {
|
|
|
|
session.Begin()
|
|
|
|
}
|
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
for _, bean := range beans {
|
2013-05-12 13:37:10 +00:00
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
if sliceValue.Kind() == reflect.Slice {
|
|
|
|
if session.Engine.InsertMany {
|
|
|
|
lastId, err = session.InsertMulti(bean)
|
|
|
|
if err != nil {
|
|
|
|
if !isInTransaction {
|
|
|
|
session.Rollback()
|
|
|
|
}
|
|
|
|
return lastId, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size := sliceValue.Len()
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
lastId, err = session.InsertOne(sliceValue.Index(i).Interface())
|
|
|
|
if err != nil {
|
|
|
|
if !isInTransaction {
|
|
|
|
session.Rollback()
|
|
|
|
}
|
|
|
|
return lastId, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lastId, err = session.InsertOne(bean)
|
|
|
|
if err != nil {
|
|
|
|
if !isInTransaction {
|
|
|
|
session.Rollback()
|
|
|
|
}
|
|
|
|
return lastId, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isInTransaction {
|
|
|
|
err = session.Commit()
|
|
|
|
}
|
|
|
|
return lastId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
if sliceValue.Kind() != reflect.Slice {
|
|
|
|
return -1, errors.New("needs a pointer to a slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
bean := sliceValue.Index(0).Interface()
|
|
|
|
sliceElementType := Type(bean)
|
|
|
|
|
2013-05-13 11:56:38 +00:00
|
|
|
table := session.Engine.AutoMapType(sliceElementType)
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.RefTable = table
|
2013-05-12 13:37:10 +00:00
|
|
|
|
|
|
|
size := sliceValue.Len()
|
|
|
|
|
|
|
|
colNames := make([]string, 0)
|
|
|
|
colMultiPlaces := make([]string, 0)
|
|
|
|
var args = make([]interface{}, 0)
|
2013-05-13 05:24:45 +00:00
|
|
|
cols := make([]Column, 0)
|
2013-05-12 13:37:10 +00:00
|
|
|
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
elemValue := sliceValue.Index(i).Interface()
|
|
|
|
colPlaces := make([]string, 0)
|
|
|
|
|
2013-05-13 05:24:45 +00:00
|
|
|
if i == 0 {
|
|
|
|
for _, col := range table.Columns {
|
|
|
|
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
|
|
|
val := fieldValue.Interface()
|
|
|
|
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, val)
|
2013-05-12 13:37:10 +00:00
|
|
|
colNames = append(colNames, col.Name)
|
2013-05-13 05:24:45 +00:00
|
|
|
cols = append(cols, col)
|
|
|
|
colPlaces = append(colPlaces, "?")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, col := range cols {
|
|
|
|
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
|
|
|
val := fieldValue.Interface()
|
|
|
|
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, val)
|
|
|
|
colPlaces = append(colPlaces, "?")
|
2013-05-12 13:37:10 +00:00
|
|
|
}
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-05-12 13:37:10 +00:00
|
|
|
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := fmt.Sprintf("INSERT INTO %v%v%v (%v) VALUES (%v)",
|
|
|
|
session.Engine.QuoteIdentifier,
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.TableName(),
|
2013-05-12 13:37:10 +00:00
|
|
|
session.Engine.QuoteIdentifier,
|
|
|
|
strings.Join(colNames, ", "),
|
|
|
|
strings.Join(colMultiPlaces, "),("))
|
|
|
|
|
|
|
|
res, err := session.Exec(statement, args...)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2013-05-12 13:37:10 +00:00
|
|
|
|
|
|
|
id, err := res.LastInsertId()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) InsertOne(bean interface{}) (int64, error) {
|
2013-05-13 11:56:38 +00:00
|
|
|
table := session.Engine.AutoMap(bean)
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.RefTable = table
|
2013-05-03 07:26:51 +00:00
|
|
|
colNames := make([]string, 0)
|
|
|
|
colPlaces := make([]string, 0)
|
|
|
|
var args = make([]interface{}, 0)
|
|
|
|
for _, col := range table.Columns {
|
|
|
|
fieldValue := reflect.Indirect(reflect.ValueOf(bean)).FieldByName(col.FieldName)
|
|
|
|
val := fieldValue.Interface()
|
2013-05-08 13:42:22 +00:00
|
|
|
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
|
|
|
continue
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
args = append(args, val)
|
|
|
|
colNames = append(colNames, col.Name)
|
|
|
|
colPlaces = append(colPlaces, "?")
|
|
|
|
}
|
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
sql := fmt.Sprintf("INSERT INTO %v%v%v (%v) VALUES (%v)",
|
2013-05-03 07:26:51 +00:00
|
|
|
session.Engine.QuoteIdentifier,
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.TableName(),
|
2013-05-03 07:26:51 +00:00
|
|
|
session.Engine.QuoteIdentifier,
|
|
|
|
strings.Join(colNames, ", "),
|
|
|
|
strings.Join(colPlaces, ", "))
|
|
|
|
|
2013-05-19 05:25:52 +00:00
|
|
|
res, err := session.Exec(sql, args...)
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := res.LastInsertId()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2013-05-08 13:42:22 +00:00
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error) {
|
2013-05-13 11:56:38 +00:00
|
|
|
table := session.Engine.AutoMap(bean)
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.RefTable = table
|
|
|
|
colNames, args := BuildConditions(session.Engine, table, bean)
|
2013-05-08 13:42:22 +00:00
|
|
|
var condiColNames []string
|
|
|
|
var condiArgs []interface{}
|
|
|
|
|
|
|
|
if len(condiBean) > 0 {
|
2013-05-19 05:25:52 +00:00
|
|
|
condiColNames, condiArgs = BuildConditions(session.Engine, table, condiBean[0])
|
2013-05-08 13:42:22 +00:00
|
|
|
}
|
2013-05-06 08:01:17 +00:00
|
|
|
|
2013-05-03 07:26:51 +00:00
|
|
|
var condition = ""
|
2013-05-08 13:42:22 +00:00
|
|
|
st := session.Statement
|
|
|
|
defer session.Statement.Init()
|
2013-05-06 08:01:17 +00:00
|
|
|
if st.WhereStr != "" {
|
2013-05-03 07:26:51 +00:00
|
|
|
condition = fmt.Sprintf("WHERE %v", st.WhereStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if condition == "" {
|
2013-05-08 13:42:22 +00:00
|
|
|
if len(condiColNames) > 0 {
|
|
|
|
condition = fmt.Sprintf("WHERE %v ", strings.Join(condiColNames, " and "))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(condiColNames) > 0 {
|
|
|
|
condition = fmt.Sprintf("%v and %v", condition, strings.Join(condiColNames, " and "))
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := fmt.Sprintf("UPDATE %v%v%v SET %v %v",
|
|
|
|
session.Engine.QuoteIdentifier,
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.TableName(),
|
2013-05-03 07:26:51 +00:00
|
|
|
session.Engine.QuoteIdentifier,
|
|
|
|
strings.Join(colNames, ", "),
|
|
|
|
condition)
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
eargs := append(append(args, st.Params...), condiArgs...)
|
|
|
|
res, err := session.Exec(statement, eargs...)
|
2013-05-03 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := res.RowsAffected()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) Delete(bean interface{}) (int64, error) {
|
2013-05-13 11:56:38 +00:00
|
|
|
table := session.Engine.AutoMap(bean)
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.RefTable = table
|
|
|
|
colNames, args := BuildConditions(session.Engine, table, bean)
|
2013-05-03 07:26:51 +00:00
|
|
|
|
|
|
|
var condition = ""
|
2013-05-08 13:42:22 +00:00
|
|
|
st := session.Statement
|
|
|
|
defer session.Statement.Init()
|
2013-05-06 08:01:17 +00:00
|
|
|
if st.WhereStr != "" {
|
2013-05-03 07:26:51 +00:00
|
|
|
condition = fmt.Sprintf("WHERE %v", st.WhereStr)
|
|
|
|
if len(colNames) > 0 {
|
|
|
|
condition += " and "
|
|
|
|
condition += strings.Join(colNames, " and ")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
condition = "WHERE " + strings.Join(colNames, " and ")
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := fmt.Sprintf("DELETE FROM %v%v%v %v",
|
|
|
|
session.Engine.QuoteIdentifier,
|
2013-05-19 05:25:52 +00:00
|
|
|
session.Statement.TableName(),
|
2013-05-03 07:26:51 +00:00
|
|
|
session.Engine.QuoteIdentifier,
|
|
|
|
condition)
|
|
|
|
|
2013-05-08 13:42:22 +00:00
|
|
|
res, err := session.Exec(statement, append(st.Params, args...)...)
|
2013-05-03 07:26:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := res.RowsAffected()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|