2013-10-26 05:49:40 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
2013-10-27 15:41:58 +04:00
|
|
|
"database/sql"
|
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-27 15:41:58 +04:00
|
|
|
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
type Do struct {
|
|
|
|
chain *Chain
|
|
|
|
db *sql.DB
|
|
|
|
driver string
|
|
|
|
TableName string
|
|
|
|
Errors []error
|
|
|
|
|
|
|
|
model *Model
|
|
|
|
value interface{}
|
|
|
|
SqlResult sql.Result
|
|
|
|
|
|
|
|
Sql string
|
|
|
|
SqlVars []interface{}
|
|
|
|
|
|
|
|
whereClause []map[string]interface{}
|
|
|
|
orClause []map[string]interface{}
|
|
|
|
selectStr string
|
|
|
|
orderStrs []string
|
|
|
|
offsetStr string
|
|
|
|
limitStr string
|
|
|
|
operation string
|
2013-10-27 06:17:11 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) err(err error) {
|
|
|
|
if err != nil {
|
|
|
|
s.Errors = append(s.Errors, err)
|
|
|
|
s.chain.err(err)
|
2013-10-26 07:59:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) setModel(value interface{}) {
|
|
|
|
s.value = value
|
|
|
|
s.model = &Model{Data: value, driver: s.driver}
|
|
|
|
s.TableName = s.model.TableName()
|
2013-10-26 07:59:58 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) addToVars(value interface{}) string {
|
|
|
|
s.SqlVars = append(s.SqlVars, value)
|
|
|
|
return fmt.Sprintf("$%d", len(s.SqlVars))
|
|
|
|
}
|
2013-10-26 08:33:05 +04:00
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) Exec(sql ...string) {
|
|
|
|
var err error
|
|
|
|
if len(sql) == 0 {
|
|
|
|
s.SqlResult, err = s.db.Exec(s.Sql, s.SqlVars...)
|
|
|
|
} else {
|
|
|
|
s.SqlResult, err = s.db.Exec(sql[0])
|
2013-10-26 08:33:05 +04:00
|
|
|
}
|
2013-10-27 09:35:22 +04:00
|
|
|
s.err(err)
|
2013-10-26 05:49:40 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) save() *Do {
|
|
|
|
if s.model.PrimaryKeyZero() {
|
|
|
|
s.create()
|
|
|
|
} else {
|
|
|
|
s.update()
|
2013-10-27 05:32:49 +04:00
|
|
|
}
|
2013-10-27 15:41:58 +04:00
|
|
|
return s
|
2013-10-27 05:32:49 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) prepareCreateSql() *Do {
|
2013-10-27 10:34:34 +04:00
|
|
|
columns, values := s.model.ColumnsAndValues("create")
|
2013-10-26 19:30:17 +04:00
|
|
|
|
|
|
|
var sqls []string
|
|
|
|
for _, value := range values {
|
|
|
|
sqls = append(sqls, s.addToVars(value))
|
|
|
|
}
|
|
|
|
|
2013-10-26 05:49:40 +04:00
|
|
|
s.Sql = fmt.Sprintf(
|
2013-10-26 16:20:49 +04:00
|
|
|
"INSERT INTO \"%v\" (%v) VALUES (%v) %v",
|
2013-10-26 05:49:40 +04:00
|
|
|
s.TableName,
|
2013-10-26 17:37:42 +04:00
|
|
|
strings.Join(s.quoteMap(columns), ","),
|
2013-10-26 19:30:17 +04:00
|
|
|
strings.Join(sqls, ","),
|
2013-10-27 03:52:04 +04:00
|
|
|
s.model.ReturningStr(),
|
2013-10-26 05:49:40 +04:00
|
|
|
)
|
2013-10-27 15:41:58 +04:00
|
|
|
return s
|
2013-10-26 05:49:40 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) create() *Do {
|
2013-10-27 10:51:23 +04:00
|
|
|
s.err(s.model.callMethod("BeforeCreate"))
|
|
|
|
s.err(s.model.callMethod("BeforeSave"))
|
2013-10-27 15:41:58 +04:00
|
|
|
|
|
|
|
s.prepareCreateSql()
|
2013-10-26 16:20:49 +04:00
|
|
|
|
2013-10-27 12:06:45 +04:00
|
|
|
if len(s.Errors) == 0 {
|
2013-10-27 15:41:58 +04:00
|
|
|
var id int64
|
2013-10-27 12:06:45 +04:00
|
|
|
if s.driver == "postgres" {
|
|
|
|
s.err(s.db.QueryRow(s.Sql, s.SqlVars...).Scan(&id))
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
s.SqlResult, err = s.db.Exec(s.Sql, s.SqlVars...)
|
|
|
|
s.err(err)
|
|
|
|
id, err = s.SqlResult.LastInsertId()
|
|
|
|
s.err(err)
|
|
|
|
}
|
|
|
|
result := reflect.ValueOf(s.model.Data).Elem()
|
|
|
|
result.FieldByName(s.model.PrimaryKey()).SetInt(id)
|
|
|
|
|
|
|
|
s.err(s.model.callMethod("AfterCreate"))
|
|
|
|
s.err(s.model.callMethod("AfterSave"))
|
|
|
|
}
|
2013-10-27 15:41:58 +04:00
|
|
|
|
|
|
|
return s
|
2013-10-26 16:20:49 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) prepareUpdateSql() *Do {
|
2013-10-27 10:34:34 +04:00
|
|
|
columns, values := s.model.ColumnsAndValues("update")
|
2013-10-26 17:37:42 +04:00
|
|
|
var sets []string
|
|
|
|
for index, column := range columns {
|
2013-10-26 19:30:17 +04:00
|
|
|
sets = append(sets, fmt.Sprintf("%v = %v", s.quote(column), s.addToVars(values[index])))
|
2013-10-26 17:37:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Sql = fmt.Sprintf(
|
2013-10-26 19:30:17 +04:00
|
|
|
"UPDATE %v SET %v %v",
|
2013-10-26 17:37:42 +04:00
|
|
|
s.TableName,
|
|
|
|
strings.Join(sets, ", "),
|
2013-10-27 07:21:33 +04:00
|
|
|
s.combinedSql(),
|
2013-10-26 17:37:42 +04:00
|
|
|
)
|
2013-10-27 15:41:58 +04:00
|
|
|
return s
|
2013-10-26 16:20:49 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) update() *Do {
|
2013-10-27 10:51:23 +04:00
|
|
|
s.err(s.model.callMethod("BeforeUpdate"))
|
|
|
|
s.err(s.model.callMethod("BeforeSave"))
|
2013-10-27 12:06:45 +04:00
|
|
|
if len(s.Errors) == 0 {
|
2013-10-27 15:41:58 +04:00
|
|
|
s.prepareUpdateSql().Exec()
|
2013-10-27 12:06:45 +04:00
|
|
|
}
|
2013-10-27 10:51:23 +04:00
|
|
|
s.err(s.model.callMethod("AfterUpdate"))
|
|
|
|
s.err(s.model.callMethod("AfterSave"))
|
2013-10-27 15:41:58 +04:00
|
|
|
return s
|
2013-10-26 16:20:49 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) prepareDeleteSql() *Do {
|
2013-10-27 07:21:33 +04:00
|
|
|
s.Sql = fmt.Sprintf("DELETE FROM %v %v", s.TableName, s.combinedSql())
|
2013-10-27 15:41:58 +04:00
|
|
|
return s
|
2013-10-26 05:49:40 +04:00
|
|
|
}
|
2013-10-27 10:51:23 +04:00
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) delete() *Do {
|
2013-10-27 10:51:23 +04:00
|
|
|
s.err(s.model.callMethod("BeforeDelete"))
|
2013-10-27 12:06:45 +04:00
|
|
|
if len(s.Errors) == 0 {
|
2013-10-27 15:41:58 +04:00
|
|
|
s.prepareDeleteSql().Exec()
|
2013-10-27 12:06:45 +04:00
|
|
|
}
|
2013-10-27 10:51:23 +04:00
|
|
|
s.err(s.model.callMethod("AfterDelete"))
|
2013-10-27 15:41:58 +04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Do) prepareQuerySql() *Do {
|
|
|
|
s.Sql = fmt.Sprintf("SELECT %v FROM %v %v", s.selectSql(), s.TableName, s.combinedSql())
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Do) query() {
|
|
|
|
var (
|
|
|
|
is_slice bool
|
|
|
|
dest_type reflect.Type
|
|
|
|
)
|
|
|
|
dest_out := reflect.Indirect(reflect.ValueOf(s.value))
|
|
|
|
|
|
|
|
if x := dest_out.Kind(); x == reflect.Slice {
|
|
|
|
is_slice = true
|
|
|
|
dest_type = dest_out.Type().Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.prepareQuerySql()
|
|
|
|
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
|
|
|
defer rows.Close()
|
|
|
|
s.err(err)
|
|
|
|
|
|
|
|
if rows.Err() != nil {
|
|
|
|
s.err(rows.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
counts := 0
|
|
|
|
for rows.Next() {
|
|
|
|
counts += 1
|
|
|
|
var dest reflect.Value
|
|
|
|
if is_slice {
|
|
|
|
dest = reflect.New(dest_type).Elem()
|
|
|
|
} else {
|
|
|
|
dest = reflect.ValueOf(s.value).Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
columns, _ := rows.Columns()
|
|
|
|
var values []interface{}
|
|
|
|
for _, value := range columns {
|
|
|
|
values = append(values, dest.FieldByName(snakeToUpperCamel(value)).Addr().Interface())
|
|
|
|
}
|
|
|
|
s.err(rows.Scan(values...))
|
|
|
|
|
|
|
|
if is_slice {
|
|
|
|
dest_out.Set(reflect.Append(dest_out, dest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counts == 0) && !is_slice {
|
|
|
|
s.err(errors.New("Record not found!"))
|
|
|
|
}
|
2013-10-27 10:51:23 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 16:07:13 +04:00
|
|
|
func (s *Do) count(value interface{}) {
|
|
|
|
dest_out := reflect.Indirect(reflect.ValueOf(value))
|
|
|
|
|
|
|
|
s.prepareQuerySql()
|
|
|
|
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
|
|
|
s.err(err)
|
|
|
|
for rows.Next() {
|
|
|
|
var dest int64
|
|
|
|
s.err(rows.Scan(&dest))
|
|
|
|
dest_out.Set(reflect.ValueOf(dest))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) pluck(value interface{}) *Do {
|
|
|
|
dest_out := reflect.Indirect(reflect.ValueOf(value))
|
|
|
|
dest_type := dest_out.Type().Elem()
|
|
|
|
s.prepareQuerySql()
|
|
|
|
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
|
|
|
s.err(err)
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
dest := reflect.New(dest_type).Elem().Interface()
|
|
|
|
s.err(rows.Scan(&dest))
|
|
|
|
switch dest.(type) {
|
|
|
|
case []uint8:
|
|
|
|
if dest_type.String() == "string" {
|
|
|
|
dest = string(dest.([]uint8))
|
|
|
|
}
|
|
|
|
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
|
|
|
default:
|
|
|
|
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Do) buildWhereCondition(clause map[string]interface{}) string {
|
2013-10-27 08:00:39 +04:00
|
|
|
str := "( " + clause["query"].(string) + " )"
|
|
|
|
|
|
|
|
args := clause["args"].([]interface{})
|
|
|
|
for _, arg := range args {
|
|
|
|
switch reflect.TypeOf(arg).Kind() {
|
|
|
|
case reflect.Slice: // For where("id in (?)", []int64{1,2})
|
|
|
|
v := reflect.ValueOf(arg)
|
|
|
|
|
|
|
|
var temp_marks []string
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
temp_marks = append(temp_marks, "?")
|
|
|
|
}
|
|
|
|
|
|
|
|
str = strings.Replace(str, "?", strings.Join(temp_marks, ","), 1)
|
|
|
|
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
str = strings.Replace(str, "?", s.addToVars(v.Index(i).Addr().Interface()), 1)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
str = strings.Replace(str, "?", s.addToVars(arg), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2013-10-26 05:49:40 +04:00
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) whereSql() (sql string) {
|
2013-10-27 08:00:39 +04:00
|
|
|
var primary_condiation string
|
2013-10-27 08:31:51 +04:00
|
|
|
var and_conditions, or_conditions []string
|
2013-10-27 08:00:39 +04:00
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
if !s.model.PrimaryKeyZero() {
|
2013-10-27 08:00:39 +04:00
|
|
|
primary_condiation = fmt.Sprintf("(%v = %v)", s.quote(s.model.PrimaryKeyDb()), s.addToVars(s.model.PrimaryKeyValue()))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, clause := range s.whereClause {
|
|
|
|
and_conditions = append(and_conditions, s.buildWhereCondition(clause))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, clause := range s.orClause {
|
|
|
|
or_conditions = append(or_conditions, s.buildWhereCondition(clause))
|
|
|
|
}
|
|
|
|
|
|
|
|
and_sql := strings.Join(and_conditions, " AND ")
|
2013-10-27 08:31:51 +04:00
|
|
|
or_sql := strings.Join(or_conditions, " OR ")
|
2013-10-27 08:00:39 +04:00
|
|
|
combined_conditions := and_sql
|
|
|
|
if len(combined_conditions) > 0 {
|
|
|
|
if len(or_sql) > 0 {
|
|
|
|
combined_conditions = combined_conditions + " OR " + or_sql
|
2013-10-26 10:10:47 +04:00
|
|
|
}
|
2013-10-27 08:00:39 +04:00
|
|
|
} else {
|
|
|
|
combined_conditions = or_sql
|
2013-10-26 10:10:47 +04:00
|
|
|
}
|
2013-10-26 19:30:17 +04:00
|
|
|
|
2013-10-27 08:00:39 +04:00
|
|
|
if len(primary_condiation) > 0 {
|
|
|
|
sql = "WHERE " + primary_condiation
|
|
|
|
if len(combined_conditions) > 0 {
|
|
|
|
sql = sql + " AND ( " + combined_conditions + " )"
|
|
|
|
}
|
|
|
|
} else if len(combined_conditions) > 0 {
|
|
|
|
sql = "WHERE " + combined_conditions
|
2013-10-26 19:30:17 +04:00
|
|
|
}
|
2013-10-26 05:49:40 +04:00
|
|
|
return
|
|
|
|
}
|
2013-10-26 19:30:17 +04:00
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) selectSql() string {
|
2013-10-27 05:50:11 +04:00
|
|
|
if len(s.selectStr) == 0 {
|
|
|
|
return " * "
|
|
|
|
} else {
|
|
|
|
return s.selectStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) orderSql() string {
|
2013-10-27 07:21:33 +04:00
|
|
|
if len(s.orderStrs) == 0 {
|
2013-10-27 07:38:05 +04:00
|
|
|
return ""
|
2013-10-27 07:21:33 +04:00
|
|
|
} else {
|
|
|
|
return " ORDER BY " + strings.Join(s.orderStrs, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) limitSql() string {
|
2013-10-27 07:38:05 +04:00
|
|
|
if len(s.limitStr) == 0 {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return " LIMIT " + s.limitStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) offsetSql() string {
|
2013-10-27 07:44:47 +04:00
|
|
|
if len(s.offsetStr) == 0 {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return " OFFSET " + s.offsetStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) combinedSql() string {
|
2013-10-27 07:44:47 +04:00
|
|
|
return s.whereSql() + s.orderSql() + s.limitSql() + s.offsetSql()
|
2013-10-27 07:21:33 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *Do) createTable() *Do {
|
|
|
|
s.Sql = s.model.CreateTable()
|
|
|
|
return s
|
2013-10-26 19:30:17 +04:00
|
|
|
}
|