mirror of https://github.com/go-gorm/gorm.git
quote map for utils
This commit is contained in:
parent
11758c647f
commit
c26e10813d
5
orm.go
5
orm.go
|
@ -11,6 +11,7 @@ import (
|
||||||
type Orm struct {
|
type Orm struct {
|
||||||
TableName string
|
TableName string
|
||||||
PrimaryKey string
|
PrimaryKey string
|
||||||
|
SqlResult sql.Result
|
||||||
Error error
|
Error error
|
||||||
Sql string
|
Sql string
|
||||||
SqlVars []interface{}
|
SqlVars []interface{}
|
||||||
|
@ -105,9 +106,9 @@ func (s *Orm) Updates(values map[string]string) *Orm {
|
||||||
|
|
||||||
func (s *Orm) Exec(sql ...string) *Orm {
|
func (s *Orm) Exec(sql ...string) *Orm {
|
||||||
if len(sql) == 0 {
|
if len(sql) == 0 {
|
||||||
s.db.Exec(s.Sql, s.SqlVars...)
|
s.SqlResult, s.Error = s.db.Exec(s.Sql, s.SqlVars...)
|
||||||
} else {
|
} else {
|
||||||
s.db.Exec(sql[0])
|
s.SqlResult, s.Error = s.db.Exec(sql[0])
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,16 @@ type User struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDB() DB {
|
func getDB() DB {
|
||||||
db, _ := Open("postgres", "user=gorm dbname=gorm")
|
db, _ := Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveAndFirst(t *testing.T) {
|
func TestSaveAndFirst(t *testing.T) {
|
||||||
db := getDB()
|
db := getDB()
|
||||||
u := &User{Name: "jinzhu"}
|
u := &User{Name: "jinzhu"}
|
||||||
|
fmt.Println("*******")
|
||||||
fmt.Println(db.Save(u).Sql)
|
fmt.Println(db.Save(u).Sql)
|
||||||
|
fmt.Println(db.Save(u).Error)
|
||||||
fmt.Println(time.Now().String())
|
fmt.Println(time.Now().String())
|
||||||
|
|
||||||
user := &User{}
|
user := &User{}
|
||||||
|
|
4
sql.go
4
sql.go
|
@ -20,9 +20,9 @@ func (s *Orm) explain(value interface{}, operation string) {
|
||||||
func (s *Orm) saveSql(value interface{}) {
|
func (s *Orm) saveSql(value interface{}) {
|
||||||
columns, values := modelValues(value)
|
columns, values := modelValues(value)
|
||||||
s.Sql = fmt.Sprintf(
|
s.Sql = fmt.Sprintf(
|
||||||
"INSERT INTO %v (%v) VALUES (%v)",
|
"INSERT INTO \"%v\" (%v) VALUES (%v)",
|
||||||
s.TableName,
|
s.TableName,
|
||||||
strings.Join(columns, ","),
|
strings.Join(quoteMap(columns), ","),
|
||||||
valuesToBinVar(values),
|
valuesToBinVar(values),
|
||||||
)
|
)
|
||||||
s.SqlVars = values
|
s.SqlVars = values
|
||||||
|
|
Loading…
Reference in New Issue