yay, update works

This commit is contained in:
Jinzhu 2013-10-26 21:37:42 +08:00
parent 91ad8a0459
commit f4f7e44199
5 changed files with 49 additions and 5 deletions

View File

@ -3,7 +3,6 @@
Yet Another ORM library for Go, aims for developer friendly Yet Another ORM library for Go, aims for developer friendly
## TODO ## TODO
* Update
* Delete * Delete
* Complex where query (= / > / < / <> / in) * Complex where query (= / > / < / <> / in)
* Order * Order

View File

@ -26,7 +26,7 @@ func (s *Orm) toModel(value interface{}) *Model {
func (m *Model) PrimaryKeyIsEmpty() bool { func (m *Model) PrimaryKeyIsEmpty() bool {
result := reflect.ValueOf(m.Data).Elem() result := reflect.ValueOf(m.Data).Elem()
return result.FieldByName(m.PrimaryKey()).Interface().(int64) == int64(0) return result.FieldByName(m.PrimaryKey()).Interface().(int64) == 0
} }
func (m *Model) PrimaryKey() string { func (m *Model) PrimaryKey() string {

View File

@ -39,6 +39,34 @@ func TestSaveAndFind(t *testing.T) {
db.Find(&users) db.Find(&users)
} }
func TestUpdate(t *testing.T) {
name := "update"
user := User{Name: name}
db.Save(&user)
user_id := user.Id
if user_id == 0 {
t.Errorf("User Id should exist after create")
}
orm := db.Where("name = ?", "update").First(&User{})
if orm.Error != nil {
t.Errorf("No error should raise when looking for a exiting user")
}
user.Name = "update2"
db.Save(&user)
orm = db.Where("name = ?", "update").First(&User{})
if orm.Error == nil {
t.Errorf("Should raise error when looking for a existing user with an outdated name")
}
orm = db.Where("name = ?", "update2").First(&User{})
if orm.Error != nil {
t.Errorf("Shouldn't raise error when looking for a existing user with the new name")
}
}
func TestWhere(t *testing.T) { func TestWhere(t *testing.T) {
name := "where" name := "where"
db.Save(&User{Name: name}) db.Save(&User{Name: name})

15
sql.go
View File

@ -77,7 +77,7 @@ func (s *Orm) createSql(value interface{}) {
s.Sql = fmt.Sprintf( s.Sql = fmt.Sprintf(
"INSERT INTO \"%v\" (%v) VALUES (%v) %v", "INSERT INTO \"%v\" (%v) VALUES (%v) %v",
s.TableName, s.TableName,
strings.Join(quoteMap(columns), ","), strings.Join(s.quoteMap(columns), ","),
valuesToBinVar(values), valuesToBinVar(values),
s.Model.ReturningStr(), s.Model.ReturningStr(),
) )
@ -99,10 +99,23 @@ func (s *Orm) create(value interface{}) {
} }
func (s *Orm) updateSql(value interface{}) { func (s *Orm) updateSql(value interface{}) {
columns, values := s.Model.ColumnsAndValues()
var sets []string
for index, column := range columns {
s.SqlVars = append(s.SqlVars, values[index])
sets = append(sets, fmt.Sprintf("%v = $%d", s.quote(column), len(s.SqlVars)))
}
s.Sql = fmt.Sprintf(
"UPDATE %v SET %v",
s.TableName,
strings.Join(sets, ", "),
)
return return
} }
func (s *Orm) update(value interface{}) { func (s *Orm) update(value interface{}) {
s.Exec()
return return
} }

View File

@ -16,9 +16,13 @@ func valuesToBinVar(values []interface{}) string {
return strings.Join(sqls, ",") return strings.Join(sqls, ",")
} }
func quoteMap(values []string) (results []string) { func (s *Orm) quote(value string) string {
return "\"" + value + "\""
}
func (s *Orm) quoteMap(values []string) (results []string) {
for _, value := range values { for _, value := range values {
results = append(results, "\""+value+"\"") results = append(results, s.quote(value))
} }
return return
} }