Skip to content
/ sql Public

基于 sqlx 实现更易用的sql, 接口优雅的同时媲美 sqlx 的性能

License

Notifications You must be signed in to change notification settings

goclub/sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f7eb6e5 · Jun 7, 2024
Oct 12, 2023
Apr 29, 2023
Oct 7, 2021
Apr 29, 2023
Jan 23, 2022
Jun 27, 2022
Jan 30, 2022
Oct 12, 2023
Jul 24, 2023
Jul 24, 2023
Oct 12, 2023
Oct 12, 2023
Dec 12, 2020
Jan 24, 2023
Jan 24, 2023
Oct 7, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
May 7, 2022
Jul 24, 2023
Mar 17, 2021
Jun 7, 2024
Jul 24, 2023
Oct 7, 2023
Oct 7, 2023
Oct 12, 2023
Aug 21, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023
Jul 24, 2023

Repository files navigation

permalink sidebarBasedOnContent
/
true

goclub/sql

Go Reference

goclub/sql 让你了解每一个函数执行的sql是什么,保证SQL的性能最大化的同时超越ORM的便捷。

指南

在 Go 中与 sql 交互一般会使用 sqlxgorm xorm

sqlx 偏底层,是对 database/sql 的封装,主要提供了基于结构体标签 db:"name""将查询结果解析为结构体的功能。而GORM XORM 功能则更丰富。

直接使用 sqlx 频繁的手写 sql 非常繁琐且容易出错。(database/sql 的接口设计的不是很友好)。

GORM XORM 存在 ORM 都有的特点,使用者容易使用 ORM 运行一些性能不高的 SQL。虽然合理使用也可以写出高效SQL,但使用者在使用 ORM 的时候容易忽略最终运行的SQL是什么。

goclub/sql 提供介于手写 sql 和 ORM 之间的使用体验。

Open

连接数据库

goclub/sql 与 database/sql 连接方式相同,只是多返回了 dbClose 函数。 dbClose 等同于 db.Close

Open

ExecMigrate

通过迁移代码创建表结构

创建用户迁移文件

ExecMigrate

定义Model

通过表单创建 Model: goclub.run

Insert

使用 Insert 插入数据

Insert

InsertModel

基于 Model 插入数据

大部分场景下使用 db.Insert 插入数据有点繁琐。基于 sq.Model 使用 db.InsertModel操作数据会方便很多。

InsertModel

Update

使用 Update 更新数据

Update

goclub/sql 故意没有提供 UpdateModel 方法, 因为使用 UpdateModel 性能并不好,会修改一下原本不需要修改的数据. 使用 db.Update(ctx, sq.QB{...}) 可以"精准"的更新数据

Query

使用 Query 查询单条数据 使用 QuerySlice 查询多条数据

Query

goclub/sql 特意没有提供 QueryModel 方法, 使用 db.Query(ctx, &user, sq.QB{ Where: sq.And(col.ID, sq.Equal(userID)) }) 可以查询 Model

SoftDelete HardDelete

使用SoftDelete 或者 HardDelete 删除数据

delete

Relation

relation

Debug

sq.QB{
	Debug: true
}

打开Debug可以查看

  1. 运行的SQL
  2. explain
  3. 执行时间
  4. last_query_cost

你也可以单独打开某一项或几项

sq.QB{
    PrintSQL: true,
}

sq.QB{
    Explain: true,
}

sq.QB{
    RunTime: true,
}

sq.QB{
    LastQueryCost: true,
}

Review

Review 的作用是用于审查 sql 或增加代码可读性

{#IN#}

语法: {#IN#}

默认会直接与执行SQL进行比对, 执行SQL与Review不一致则会在运行时 print 错误.

有时候执行的SQL不是固定的字符串例如

where in 时会根据查询条件不同导致有多种情况

select * from user where id in (?)
select * from user where id in (?,?)
select * from user where id in (?,?,?)
...

虽然可以使用 Reviews 配置多个review

sq.QB{
    Review: []string{
    	"select * from user where id in (?)",
    	"select * from user where id in (?,?)",
		"select * from user where id in (?,?,?),
    },
}

但这样无法覆盖全部的情况.

可以使用 {#IN#} 模糊匹配

sq.QB{
    Review: "select * from user where id in {#IN#}"
}

零次一次

语法

{{#任意字符#}}

如果你使用了 sq.IF 你可能需要用到 Reviews

sq.QB{
    From: &User{},
    Select: []sq.Column{"id"},
    Where: sq.And("name", sq.IF(searchName != "", sq.Equal(searchName))),
    Reviews: []string{
        "SELECT `id` FROM `user` WHERE `name` = ? AND `deleted_at` IS NULL",
        "SELECT `id` FROM `user` WHERE `deleted_at` IS NULL",
    },
}

你可以使用 {{# and name = ?#}} 代替多个 review 建议将空格前置:使用 {{# and name = ?#}}, 而不是 {{#and name = ? #}}`


sq.QB{
    From: &User{},
    Select: []sq.Column{"id"},
    Where: sq.And("name", sq.IF(searchName != "", sq.Equal(searchName))),
    Review: "SELECT `id` FROM `user` WHERE{{# and name = ?#}} AND `deleted_at` IS NULL",
    },
}

{#VALUES#}

语法: {#VALUES#}

一些 Insert 语句会出现 (?,?) (?,?),(?,?) 的情况

INSERT INTO `user` (`name`,`age`) VALUES (?,?),(?,?)
INSERT INTO `user` (`name`,`age`) VALUES (?,?)

可以使用 {#VALUES#} 模糊匹配

sq.QB{
    Review: "INSERT INTO `user` (`name`,`age`) VALUES {#VALUES#}"
}

致谢

感谢 jetbrains 提供 Goland 开源授权

About

基于 sqlx 实现更易用的sql, 接口优雅的同时媲美 sqlx 的性能

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages