From c26e10813d4b6a1bcdc20935250ae4f87cc34f23 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 26 Oct 2013 10:06:57 +0800 Subject: [PATCH] quote map for utils --- orm.go | 5 +++-- orm_test.go | 5 +++-- sql.go | 4 ++-- utils.go | 7 +++++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/orm.go b/orm.go index e1b926f3..43a609c1 100644 --- a/orm.go +++ b/orm.go @@ -11,6 +11,7 @@ import ( type Orm struct { TableName string PrimaryKey string + SqlResult sql.Result Error error Sql string SqlVars []interface{} @@ -105,9 +106,9 @@ func (s *Orm) Updates(values map[string]string) *Orm { func (s *Orm) Exec(sql ...string) *Orm { if len(sql) == 0 { - s.db.Exec(s.Sql, s.SqlVars...) + s.SqlResult, s.Error = s.db.Exec(s.Sql, s.SqlVars...) } else { - s.db.Exec(sql[0]) + s.SqlResult, s.Error = s.db.Exec(sql[0]) } return s } diff --git a/orm_test.go b/orm_test.go index 8dc1492c..b2311c41 100644 --- a/orm_test.go +++ b/orm_test.go @@ -12,15 +12,16 @@ type User struct { } func getDB() DB { - db, _ := Open("postgres", "user=gorm dbname=gorm") + db, _ := Open("postgres", "user=gorm dbname=gorm sslmode=disable") return db } func TestSaveAndFirst(t *testing.T) { db := getDB() u := &User{Name: "jinzhu"} + fmt.Println("*******") fmt.Println(db.Save(u).Sql) - + fmt.Println(db.Save(u).Error) fmt.Println(time.Now().String()) user := &User{} diff --git a/sql.go b/sql.go index c61b0910..e39c6242 100644 --- a/sql.go +++ b/sql.go @@ -20,9 +20,9 @@ func (s *Orm) explain(value interface{}, operation string) { func (s *Orm) saveSql(value interface{}) { columns, values := modelValues(value) s.Sql = fmt.Sprintf( - "INSERT INTO %v (%v) VALUES (%v)", + "INSERT INTO \"%v\" (%v) VALUES (%v)", s.TableName, - strings.Join(columns, ","), + strings.Join(quoteMap(columns), ","), valuesToBinVar(values), ) s.SqlVars = values diff --git a/utils.go b/utils.go index cc687a6a..9a439fbd 100644 --- a/utils.go +++ b/utils.go @@ -27,3 +27,10 @@ func valuesToBinVar(values []interface{}) string { } return strings.Join(sqls, ",") } + +func quoteMap(values []string) (results []string) { + for _, value := range values { + results = append(results, "\""+value+"\"") + } + return +}