2013-10-26 05:49:40 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
2013-10-26 07:59:58 +04:00
|
|
|
"reflect"
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2013-10-26 07:59:58 +04:00
|
|
|
func (s *Orm) explain(value interface{}, operation string) *Orm {
|
2013-10-26 05:49:40 +04:00
|
|
|
s.setModel(value)
|
|
|
|
switch operation {
|
|
|
|
case "Save":
|
|
|
|
s.saveSql(value)
|
|
|
|
case "Delete":
|
|
|
|
s.deleteSql(value)
|
2013-10-26 07:59:58 +04:00
|
|
|
case "Query":
|
|
|
|
s.querySql(value)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) querySql(out interface{}) {
|
|
|
|
s.Sql = "SELECT * from users limit 1"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) query(out interface{}) {
|
|
|
|
rows, err := s.db.Query(s.Sql)
|
|
|
|
s.Error = err
|
|
|
|
for rows.Next() {
|
|
|
|
dest := reflect.ValueOf(out).Elem()
|
|
|
|
fmt.Printf("%+v", dest)
|
|
|
|
columns, _ := rows.Columns()
|
|
|
|
var values []interface{}
|
|
|
|
for _, value := range columns {
|
|
|
|
values = append(values, dest.FieldByName(value).Addr().Interface())
|
|
|
|
}
|
|
|
|
s.Error = rows.Scan(values...)
|
2013-10-26 05:49:40 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) saveSql(value interface{}) {
|
|
|
|
columns, values := modelValues(value)
|
|
|
|
s.Sql = fmt.Sprintf(
|
2013-10-26 06:06:57 +04:00
|
|
|
"INSERT INTO \"%v\" (%v) VALUES (%v)",
|
2013-10-26 05:49:40 +04:00
|
|
|
s.TableName,
|
2013-10-26 06:06:57 +04:00
|
|
|
strings.Join(quoteMap(columns), ","),
|
2013-10-26 05:49:40 +04:00
|
|
|
valuesToBinVar(values),
|
|
|
|
)
|
|
|
|
s.SqlVars = values
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) deleteSql(value interface{}) {
|
|
|
|
s.Sql = fmt.Sprintf("DELETE FROM %v WHERE %v", s.TableName, s.whereSql)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) whereSql() (sql string) {
|
|
|
|
sql = "1=1"
|
|
|
|
return
|
|
|
|
}
|