2013-10-26 05:49:40 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
2013-10-26 11:02:14 +04:00
|
|
|
"errors"
|
2013-10-26 08:33:05 +04:00
|
|
|
"fmt"
|
2013-10-26 07:59:58 +04:00
|
|
|
"reflect"
|
2013-10-26 10:10:47 +04:00
|
|
|
"strconv"
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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)
|
2013-10-26 11:47:30 +04:00
|
|
|
case "CreateTable":
|
|
|
|
s.createTableSql(value)
|
2013-10-26 07:59:58 +04:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) querySql(out interface{}) {
|
2013-10-26 10:10:47 +04:00
|
|
|
s.Sql = fmt.Sprintf("SELECT * FROM %v %v", s.TableName, s.whereSql())
|
2013-10-26 07:59:58 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) query(out interface{}) {
|
2013-10-26 08:33:05 +04:00
|
|
|
var (
|
|
|
|
is_slice bool
|
|
|
|
dest_type reflect.Type
|
|
|
|
)
|
|
|
|
dest_out := reflect.Indirect(reflect.ValueOf(out))
|
|
|
|
|
|
|
|
if x := dest_out.Kind(); x == reflect.Slice {
|
|
|
|
is_slice = true
|
|
|
|
dest_type = dest_out.Type().Elem()
|
|
|
|
}
|
|
|
|
|
2013-10-26 10:10:47 +04:00
|
|
|
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
2013-10-26 11:02:14 +04:00
|
|
|
defer rows.Close()
|
|
|
|
|
2013-10-26 07:59:58 +04:00
|
|
|
s.Error = err
|
2013-10-26 11:02:14 +04:00
|
|
|
if rows.Err() != nil {
|
|
|
|
s.Error = rows.Err()
|
|
|
|
}
|
2013-10-26 08:33:05 +04:00
|
|
|
|
2013-10-26 11:02:14 +04:00
|
|
|
counts := 0
|
2013-10-26 07:59:58 +04:00
|
|
|
for rows.Next() {
|
2013-10-26 11:02:14 +04:00
|
|
|
counts += 1
|
2013-10-26 08:33:05 +04:00
|
|
|
var dest reflect.Value
|
|
|
|
if is_slice {
|
|
|
|
dest = reflect.New(dest_type).Elem()
|
|
|
|
} else {
|
|
|
|
dest = reflect.ValueOf(out).Elem()
|
|
|
|
}
|
|
|
|
|
2013-10-26 07:59:58 +04:00
|
|
|
columns, _ := rows.Columns()
|
|
|
|
var values []interface{}
|
|
|
|
for _, value := range columns {
|
2013-10-26 10:23:02 +04:00
|
|
|
values = append(values, dest.FieldByName(snakeToUpperCamel(value)).Addr().Interface())
|
2013-10-26 07:59:58 +04:00
|
|
|
}
|
|
|
|
s.Error = rows.Scan(values...)
|
2013-10-26 05:49:40 +04:00
|
|
|
}
|
2013-10-26 11:02:14 +04:00
|
|
|
if (counts == 0) && !is_slice {
|
|
|
|
s.Error = errors.New("Record not found!")
|
|
|
|
}
|
2013-10-26 05:49:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) saveSql(value interface{}) {
|
2013-10-26 12:29:39 +04:00
|
|
|
columns, values := s.Model.ColumnsAndValues()
|
2013-10-26 05:49:40 +04:00
|
|
|
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) {
|
2013-10-26 10:10:47 +04:00
|
|
|
if len(s.whereClause) == 0 {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
sql = "WHERE "
|
|
|
|
for _, clause := range s.whereClause {
|
|
|
|
sql += clause["query"].(string)
|
|
|
|
args := clause["args"].([]interface{})
|
|
|
|
for _, arg := range args {
|
|
|
|
s.SqlVars = append(s.SqlVars, arg.([]interface{})...)
|
|
|
|
sql = strings.Replace(sql, "?", "$"+strconv.Itoa(len(s.SqlVars)), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-26 05:49:40 +04:00
|
|
|
return
|
|
|
|
}
|
2013-10-26 11:47:30 +04:00
|
|
|
|
|
|
|
func (s *Orm) createTableSql(value interface{}) {
|
|
|
|
s.Sql = fmt.Sprintf(
|
|
|
|
"CREATE TABLE \"%v\" (%v)",
|
|
|
|
s.TableName,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|