Fill up id after create record

This commit is contained in:
Jinzhu 2013-10-26 20:20:49 +08:00
parent a7f62c24b0
commit cef81cba11
5 changed files with 54 additions and 17 deletions

View File

@ -3,8 +3,6 @@
Yet Another ORM library for Go, aims for developer friendly Yet Another ORM library for Go, aims for developer friendly
## TODO ## TODO
* Perimary key
* Save and fill the record
* Update * Update
* Delete * Delete
* Complex where query (= / > / < / <> / in) * Complex where query (= / > / < / <> / in)
@ -16,9 +14,10 @@ Yet Another ORM library for Go, aims for developer friendly
* Not query * Not query
* Even more complex where query (with map or struct) * Even more complex where query (with map or struct)
* ORM.Errors * ORM.Errors
* Better First method (First(&user, primary_key)) * Better First method (First(&user, primary_key, where conditions))
* Soft Delete * Soft Delete
* After/Before Save/Update/Create/Delete * After/Before Save/Update/Create/Delete
* CreatedAt, UpdatedAt
* FindOrInitialize / FindOrCreate * FindOrInitialize / FindOrCreate
* SQL Log * SQL Log
* Auto Migration * Auto Migration

View File

@ -27,6 +27,10 @@ func (m *Model) PrimaryKey() string {
return "Id" return "Id"
} }
func (m *Model) PrimaryKeyDb() string {
return toSnake(m.PrimaryKey())
}
func (m *Model) Fields() (fields []Field) { func (m *Model) Fields() (fields []Field) {
typ := reflect.TypeOf(m.Data).Elem() typ := reflect.TypeOf(m.Data).Elem()
@ -37,7 +41,7 @@ func (m *Model) Fields() (fields []Field) {
field.Name = p.Name field.Name = p.Name
field.DbName = toSnake(p.Name) field.DbName = toSnake(p.Name)
field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface() field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface()
if m.PrimaryKey() == p.Name { if m.PrimaryKeyDb() == field.DbName {
field.SqlType = getPrimaryKeySqlType(m.driver, field.Value, 0) field.SqlType = getPrimaryKeySqlType(m.driver, field.Value, 0)
} else { } else {
field.SqlType = getSqlType(m.driver, field.Value, 0) field.SqlType = getSqlType(m.driver, field.Value, 0)
@ -54,9 +58,12 @@ func (m *Model) ColumnsAndValues() (columns []string, values []interface{}) {
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i) p := typ.Field(i)
if !p.Anonymous { if !p.Anonymous {
columns = append(columns, toSnake(p.Name)) db_name := toSnake(p.Name)
value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name) if m.PrimaryKeyDb() != db_name {
values = append(values, value.Interface()) columns = append(columns, db_name)
value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name)
values = append(values, value.Interface())
}
} }
} }
return return
@ -83,10 +90,6 @@ func (model *Model) MissingColumns() (results []string) {
return return
} }
func (model *Model) ColumnType(column string) (result string) {
return
}
func (model *Model) CreateTable() (sql string) { func (model *Model) CreateTable() (sql string) {
var sqls []string var sqls []string
for _, field := range model.Fields() { for _, field := range model.Fields() {
@ -100,3 +103,10 @@ func (model *Model) CreateTable() (sql string) {
) )
return return
} }
func (model *Model) ReturningStr() (str string) {
if model.driver == "postgres" {
str = fmt.Sprintf("RETURNING \"%v\"", model.PrimaryKeyDb())
}
return
}

5
orm.go
View File

@ -28,7 +28,7 @@ type Orm struct {
func (s *Orm) setModel(model interface{}) (err error) { func (s *Orm) setModel(model interface{}) (err error) {
s.Model = s.toModel(model) s.Model = s.toModel(model)
s.TableName = s.Model.TableName() s.TableName = s.Model.TableName()
s.PrimaryKey = s.Model.PrimaryKey() s.PrimaryKey = s.Model.PrimaryKeyDb()
return return
} }
@ -86,7 +86,8 @@ func (s *Orm) Select(value interface{}) *Orm {
} }
func (s *Orm) Save(value interface{}) *Orm { func (s *Orm) Save(value interface{}) *Orm {
s.explain(value, "Save").Exec() s.explain(value, "Create").create(value)
// s.explain(value, "Update").update(value)
return s return s
} }

View File

@ -17,6 +17,9 @@ func TestSaveAndFirst(t *testing.T) {
db := getDB() db := getDB()
u := &User{Name: "jinzhu"} u := &User{Name: "jinzhu"}
db.Save(u) db.Save(u)
if u.Id == 0 {
t.Errorf("Should have ID after create record")
}
user := &User{} user := &User{}
db.First(user) db.First(user)

32
sql.go
View File

@ -11,8 +11,10 @@ import (
func (s *Orm) explain(value interface{}, operation string) *Orm { func (s *Orm) explain(value interface{}, operation string) *Orm {
s.setModel(value) s.setModel(value)
switch operation { switch operation {
case "Save": case "Create":
s.saveSql(value) s.createSql(value)
case "Update":
s.updateSql(value)
case "Delete": case "Delete":
s.deleteSql(value) s.deleteSql(value)
case "Query": case "Query":
@ -70,18 +72,40 @@ func (s *Orm) query(out interface{}) {
} }
} }
func (s *Orm) saveSql(value interface{}) { func (s *Orm) createSql(value interface{}) {
columns, values := s.Model.ColumnsAndValues() columns, values := s.Model.ColumnsAndValues()
s.Sql = fmt.Sprintf( s.Sql = fmt.Sprintf(
"INSERT INTO \"%v\" (%v) VALUES (%v)", "INSERT INTO \"%v\" (%v) VALUES (%v) %v",
s.TableName, s.TableName,
strings.Join(quoteMap(columns), ","), strings.Join(quoteMap(columns), ","),
valuesToBinVar(values), valuesToBinVar(values),
s.Model.ReturningStr(),
) )
s.SqlVars = values s.SqlVars = values
return return
} }
func (s *Orm) create(value interface{}) {
var id int64
if s.driver == "postgres" {
s.Error = s.db.QueryRow(s.Sql, s.SqlVars...).Scan(&id)
} else {
s.SqlResult, s.Error = s.db.Exec(s.Sql, s.SqlVars...)
id, s.Error = s.SqlResult.LastInsertId()
}
result := reflect.ValueOf(s.Model.Data).Elem()
result.FieldByName(s.Model.PrimaryKey()).SetInt(id)
}
func (s *Orm) updateSql(value interface{}) {
return
}
func (s *Orm) update(value interface{}) {
return
}
func (s *Orm) deleteSql(value interface{}) { func (s *Orm) deleteSql(value interface{}) {
s.Sql = fmt.Sprintf("DELETE FROM %v WHERE %v", s.TableName, s.whereSql) s.Sql = fmt.Sprintf("DELETE FROM %v WHERE %v", s.TableName, s.whereSql)
return return