Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

由于ORM的安全性保障,所有输入的参数在底层都将使用预处理模式执行,防止常见的SQL注入风险。在某一些场景中,我们期望在生成执行的SQL语句中嵌入自定义的SQL语句,那么我们可以使用ORMRawSQL特性,通过gdb.Raw类型来实现。我们来看几个示例。

Insert中使用RawSQL

gdb.Raw是字符串类型,该类型的参数将会直接作为SQL片段嵌入到提交到底层的SQL语句中,不会被自动转换为字符串参数类型、也不会被当做预处理参数。例如:

Code Block
languagego
// INSERT INTO `user`(`id`,`passport`,`password`,`nickname`,`create_time`) VALUES('id+2','john','123456','now()')
g.Model("user").Data(g.Map{
	"id":          "id+2",
	"passport":    "john",
	"password":    "123456",
	"nickname":    "JohnGuo",
	"create_time": "now()",
}).Insert()
// 执行报错:Error Code: 1136. Column count doesn't match value count at row 1

使用gdb.Raw改造后:

Code Block
languagego
// INSERT INTO `user`(`id`,`passport`,`password`,`nickname`,`create_time`) VALUES(id+2,'john','123456',now())
g.Model("user").Data(g.Map{
	"id":          gdb.Raw("id+2"),
	"passport":    "john",
	"password":    "123456",
	"nickname":    "JohnGuo",
	"create_time": gdb.Raw("now()"),
}).Insert()

Update中使用RawSQL

Code Block
languagego
// UPDATE `user` SET login_count='login_count+1',update_time='now()' WHERE id=1
g.Model("user").Data(g.Map{
    "login_count": "login_count+1",
    "update_time": "now()",
}).Where("id", 1).Update()
// 执行报错:Error Code: 1136. Column count doesn't match value count at row 1

使用gdb.Raw改造后:

Code Block
languagego
// UPDATE `user` SET login_count=login_count+1,update_time=now() WHERE id=1
g.Model("user").Data(g.Map{
    "login_count": gdb.Raw("login_count+1"),
    "update_time": gdb.Raw("now()"),
}).Where("id", 1).Update()

Select中使用RawSQL

Code Block
languagego
// SELECT * FROM `user` WHERE `created_at`<'now()'
g.Model("user").WhereLT("created_at", "now()").All()

使用gdb.Raw改造后:

Code Block
languagego
// SELECT * FROM `user` WHERE `created_at`<now()
g.Model("user").WhereLT("created_at", gdb.Raw("now()")).All()











Panel
titleContent Menu

Table of Contents