gorm/orm.go

168 lines
3.3 KiB
Go
Raw Normal View History

2013-10-25 14:04:48 +04:00
package gorm
2013-10-26 03:14:57 +04:00
import (
"database/sql"
2013-10-26 03:14:57 +04:00
"errors"
2013-10-26 16:53:21 +04:00
2013-10-26 03:14:57 +04:00
"strconv"
)
2013-10-25 14:04:48 +04:00
type Orm struct {
TableName string
PrimaryKey string
2013-10-26 06:06:57 +04:00
SqlResult sql.Result
2013-10-26 05:49:40 +04:00
Sql string
SqlVars []interface{}
2013-10-27 03:52:04 +04:00
model *Model
2013-10-25 18:31:56 +04:00
Errors []error
Error error
2013-10-25 18:31:56 +04:00
db *sql.DB
2013-10-26 13:28:52 +04:00
driver string
2013-10-26 10:10:47 +04:00
whereClause []map[string]interface{}
2013-10-27 08:00:39 +04:00
orClause []map[string]interface{}
2013-10-26 03:14:57 +04:00
selectStr string
2013-10-27 07:21:33 +04:00
orderStrs []string
2013-10-27 07:44:47 +04:00
offsetStr string
2013-10-27 07:38:05 +04:00
limitStr string
2013-10-26 05:49:40 +04:00
operation string
}
func (s *Orm) err(err error) {
if err != nil {
s.Errors = append(s.Errors, err)
s.Error = err
}
}
2013-10-27 04:06:01 +04:00
func (s *Orm) Model(model interface{}) *Orm {
2013-10-27 03:52:04 +04:00
s.model = s.toModel(model)
s.TableName = s.model.TableName()
s.PrimaryKey = s.model.PrimaryKeyDb()
2013-10-27 04:06:01 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 14:31:10 +04:00
func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm {
2013-10-25 18:31:56 +04:00
s.whereClause = append(s.whereClause, map[string]interface{}{"query": querystring, "args": args})
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 14:31:10 +04:00
func (s *Orm) Limit(value interface{}) *Orm {
2013-10-26 03:14:57 +04:00
switch value := value.(type) {
case string:
2013-10-27 07:38:05 +04:00
s.limitStr = value
2013-10-26 03:14:57 +04:00
case int:
2013-10-27 07:38:05 +04:00
if value < 0 {
s.limitStr = ""
} else {
s.limitStr = strconv.Itoa(value)
}
2013-10-26 03:14:57 +04:00
default:
s.err(errors.New("Can' understand the value of Limit, Should be int"))
2013-10-26 03:14:57 +04:00
}
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 14:31:10 +04:00
func (s *Orm) Offset(value interface{}) *Orm {
2013-10-26 03:14:57 +04:00
switch value := value.(type) {
case string:
2013-10-27 07:44:47 +04:00
s.offsetStr = value
2013-10-26 03:14:57 +04:00
case int:
2013-10-27 07:44:47 +04:00
if value < 0 {
s.offsetStr = ""
} else {
s.offsetStr = strconv.Itoa(value)
}
2013-10-26 03:14:57 +04:00
default:
s.err(errors.New("Can' understand the value of Offset, Should be int"))
2013-10-26 03:14:57 +04:00
}
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-27 03:38:08 +04:00
func (s *Orm) Order(value string, reorder ...bool) *Orm {
2013-10-27 07:21:33 +04:00
defer s.validSql(value)
2013-10-27 03:38:08 +04:00
if len(reorder) > 0 && reorder[0] {
2013-10-27 07:21:33 +04:00
s.orderStrs = append([]string{}, value)
2013-10-27 03:38:08 +04:00
} else {
2013-10-27 07:21:33 +04:00
s.orderStrs = append(s.orderStrs, value)
2013-10-26 03:14:57 +04:00
}
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 14:31:10 +04:00
func (s *Orm) Count() int64 {
return 0
2013-10-25 14:04:48 +04:00
}
2013-10-26 03:14:57 +04:00
func (s *Orm) Select(value interface{}) *Orm {
defer func() { s.validSql(s.selectStr) }()
2013-10-26 03:14:57 +04:00
switch value := value.(type) {
case string:
s.selectStr = value
default:
s.err(errors.New("Can' understand the value of Select, Should be string"))
2013-10-26 03:14:57 +04:00
}
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 14:31:10 +04:00
func (s *Orm) Save(value interface{}) *Orm {
2013-10-27 03:52:04 +04:00
s.Model(value)
if s.model.PrimaryKeyIsEmpty() {
2013-10-26 16:53:21 +04:00
s.explain(value, "Create").create(value)
} else {
s.explain(value, "Update").update(value)
}
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 14:31:10 +04:00
func (s *Orm) Delete(value interface{}) *Orm {
2013-10-27 10:51:23 +04:00
s.explain(value, "Delete").delete(value)
2013-10-25 14:31:10 +04:00
return s
2013-10-25 14:04:48 +04:00
}
2013-10-25 17:11:29 +04:00
func (s *Orm) Update(column string, value string) *Orm {
return s
}
func (s *Orm) Updates(values map[string]string) *Orm {
return s
}
2013-10-26 05:49:40 +04:00
func (s *Orm) Exec(sql ...string) *Orm {
var err error
2013-10-26 05:49:40 +04:00
if len(sql) == 0 {
s.SqlResult, err = s.db.Exec(s.Sql, s.SqlVars...)
2013-10-26 05:49:40 +04:00
} else {
s.SqlResult, err = s.db.Exec(sql[0])
2013-10-26 05:49:40 +04:00
}
s.err(err)
2013-10-25 14:31:10 +04:00
return s
}
2013-10-26 03:14:57 +04:00
func (s *Orm) First(out interface{}) *Orm {
2013-10-26 07:59:58 +04:00
s.explain(out, "Query").query(out)
2013-10-26 03:14:57 +04:00
return s
}
func (s *Orm) Find(out interface{}) *Orm {
2013-10-26 07:59:58 +04:00
s.explain(out, "Query").query(out)
2013-10-26 03:14:57 +04:00
return s
}
2013-10-27 04:06:01 +04:00
func (s *Orm) Pluck(column string, value interface{}) (orm *Orm) {
2013-10-27 05:32:49 +04:00
s.Select(column).explain(s.model.Data, "Query").pluck(value)
return s
2013-10-27 04:06:01 +04:00
}
2013-10-26 03:14:57 +04:00
func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm {
2013-10-27 08:00:39 +04:00
s.orClause = append(s.orClause, map[string]interface{}{"query": querystring, "args": args})
2013-10-26 03:14:57 +04:00
return s
}
2013-10-26 11:47:30 +04:00
func (s *Orm) CreateTable(value interface{}) *Orm {
2013-10-26 13:28:52 +04:00
s.explain(value, "CreateTable").Exec()
2013-10-26 11:47:30 +04:00
return s
}