gorm/orm.go

131 lines
2.5 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 (
"errors"
2013-10-26 05:49:40 +04:00
2013-10-26 03:14:57 +04:00
"strconv"
"database/sql"
)
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 03:14:57 +04:00
Error error
2013-10-26 05:49:40 +04:00
Sql string
SqlVars []interface{}
2013-10-25 18:31:56 +04:00
db *sql.DB
whereClause []interface{}
2013-10-26 03:14:57 +04:00
selectStr string
2013-10-25 18:31:56 +04:00
orderStr string
2013-10-26 03:14:57 +04:00
offsetInt int
limitInt int
2013-10-26 05:49:40 +04:00
operation string
}
func (s *Orm) setModel(model interface{}) (err error) {
2013-10-26 06:15:09 +04:00
s.TableName = "users"
2013-10-26 05:49:40 +04:00
s.PrimaryKey = "id"
return
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:
s.limitInt, _ = strconv.Atoi(value)
case int:
s.limitInt = value
default:
s.Error = errors.New("Can' understand the value of Limit, Should be int")
}
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:
s.offsetInt, _ = strconv.Atoi(value)
case int:
s.offsetInt = value
default:
s.Error = errors.New("Can' understand the value of Offset, Should be int")
}
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) Order(value interface{}) *Orm {
2013-10-26 03:14:57 +04:00
switch value := value.(type) {
case string:
s.orderStr = value
default:
s.Error = errors.New("Can' understand the value of Order, Should be string")
}
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 {
switch value := value.(type) {
case string:
s.selectStr = value
default:
s.Error = errors.New("Can' understand the value of Select, Should be string")
}
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-26 08:33:05 +04:00
s.explain(value, "Save").Exec()
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-26 08:33:05 +04:00
s.explain(value, "Delete").Exec()
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 {
if len(sql) == 0 {
2013-10-26 06:06:57 +04:00
s.SqlResult, s.Error = s.db.Exec(s.Sql, s.SqlVars...)
2013-10-26 05:49:40 +04:00
} else {
2013-10-26 06:06:57 +04:00
s.SqlResult, s.Error = s.db.Exec(sql[0])
2013-10-26 05:49:40 +04:00
}
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
}
func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm {
return s
}
func (s *Orm) Not(querystring interface{}, args ...interface{}) *Orm {
return s
2013-10-25 14:04:48 +04:00
}