8000 Feat/sql by kesonan · Pull Request #3 · kesonan/sqlgen · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Feat/sql #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its mainta 8000 iners and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ var rootCmd = &cobra.Command{
Use: "sqlgen",
Short: "A cli for mysql generator",
}

var sqlCmd = &cobra.Command{
Use: "sql",
Short: "Generate SQL model",
Short: "Generate sql model",
Run: func(cmd *cobra.Command, args []string) {
arg.Mode = flags.SQL
flags.Run(arg)
Expand Down Expand Up @@ -68,10 +69,11 @@ func init() {
persistentFlags.StringVarP(&arg.Output, "output", "o", ".", "The output directory")

// sub commands init
rootCmd.AddCommand(sqlCmd)
rootCmd.AddCommand(bunCmd)
rootCmd.AddCommand(gormCmd)
rootCmd.AddCommand(xormCmd)
rootCmd.AddCommand(sqlCmd)
rootCmd.AddCommand(sqlxCmd)
rootCmd.AddCommand(xormCmd)
}

func Execute() {
Expand Down
16 changes: 16 additions & 0 deletions example/bun/create/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- 用户表 --
CREATE TABLE `user`
(
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`name` varchar(255) COLLATE utf8mb4_general_ci NULL COMMENT '用户\t名称',
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户\n密码',
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公\r开',
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
`type` tinyint(1) COLLATE utf8mb4_general_ci DEFAULT 0 COMMENT '用户类型',
`create_time` timestamp NULL,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `name_index` (`name`),
UNIQUE KEY `type_index` (`type`),
UNIQUE KEY `mobile_index` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '用户表' COLLATE=utf8mb4_general_ci;
36 changes: 36 additions & 0 deletions example/bun/delete/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- 用户表 --
CREATE TABLE `user`
(
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`name` varchar(255) COLLATE utf8mb4_general_ci NULL COMMENT '用户\t名称',
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户\n密码',
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公\r开',
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
`type` tinyint(1) COLLATE utf8mb4_general_ci DEFAULT 0 COMMENT '用户类型',
`create_time` timestamp NULL,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `name_index` (`name`),
UNIQUE KEY `type_index` (`type`),
UNIQUE KEY `mobile_index` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '用户表' COLLATE=utf8mb4_general_ci;

-- example1: delete by primary key
-- fn: Delete
delete from user where id = ?;

-- example2: delete by unique key
-- fn: DeleteByName
delete from user where name = ?;

-- example3: delete by unique keys
-- fn: DeleteByNameAndMobile
delete from user where name = ? and mobile = ?;

-- example4: delete by id order by id
-- fn: DeleteOrderByID
delete from user where id = ? order by id desc;

-- example5 delete by id order by id limit 10
-- fn: DeleteOrderByIDLimit
delete from user where id = ? order by id desc limit 10;
199 changes: 199 additions & 0 deletions example/bun/read/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
-- 用户表 --
CREATE TABLE `user`
(
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`name` varchar(255) COLLATE utf8mb4_general_ci NULL COMMENT '用户\t名称',
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户\n密码',
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公\r开',
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
`type` tinyint(1) COLLATE utf8mb4_general_ci DEFAULT 0 COMMENT '用户类型',
`create_time` timestamp NULL,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `name_index` (`name`),
UNIQUE KEY `type_index` (`type`),
UNIQUE KEY `mobile_index` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '用户表' COLLATE=utf8mb4_general_ci;

-- example1: find one by primary key
-- if you want to find one result, you have to explicitly declare limit 1 statement.
-- fn: FindOne
select *
from user
where id = ? limit 1;

-- example2: find one by unique key
-- fn: FindByName
select *
from user
where name = ? limit 1;

-- example3: find part of fields by primary key
-- fn: FindOnePart
select id, name, nickname
from user
where id = ? limit 1;

-- example4: find part of fields by unique key
-- fn: FindByNamePart
select id, name, nickname
from user
where name = ? limit 1;

-- example5: find all
-- fn: FindAll
select *
from user;

-- example6: find all count, if call function, you must use AS keyword to alias result.
-- fn: FindAllCount
select count(*) AS count
from user;

-- example7: find all part of fields
-- fn: FindAllPart
select id, name, nickname
from user;

-- example8: find all part of fields count, if call function, you must use AS keyword to alias result.
-- fn: FindAllPartCount
select count(id) AS count
from user;

-- example9: find one by name and password
-- fn: FindOneByNameAndPassword
select *
from user
where name = ?
and password = ? limit 1;

-- example10: list user by primary key, group by name
-- fn: ListUserByNameAsc
select *
from user
where id > ?
group by name;

-- example11: list user by primary key, group by name asc, having count(type) > ?
-- having clause must be a alias, do not use function expression, for example:
-- select * from user where id > ? group by name asc having count(type) > ?; in this
-- statement, count(type) is a function expression, it will not work, you can use
-- select *,count(type) AS typeCount from user where id > ? group by name asc having typeCount > ?; instead.
-- fn: ListUserByNameAscHavingCountTypeGt
select *, count(type) AS typeCount
from user
where id > ?
group by name
having typeCount > ?;

-- example13: list user by primary key, group by name desc, having count(type) > ?, order by id desc
-- fn: ListUserByNameDescHavingCountTypeGtOrderByIdDesc
select *, count(type) AS typeCount
from user
where id > ?
group by name
having typeCount > ?
order by id desc;

-- example14: list user by primary key, group by name desc, having count(type) > ?, order by id desc, limit 10
-- fn: ListUserByNameDescHavingCountTypeGtOrderByIdDescLimit10
select *, count(type) AS typeCount
from user
where id > ?
group by name
having typeCount > ?
order by id desc limit 10;

-- example15: list user by primary key, group by name desc, having count(type) > ?, order by id desc, limit 10, 10
-- fn: ListUserByNameDescHavingCountTypeGtOrderByIdDescLimit10Offset10
select *, count(type) AS typeCount
from user
where id > ?
group by name
having typeCount > ?
order by id desc limit 10, 10;

-- example16: find one by name like
-- fn: FindOneByNameLike
select *
from user
where name like ? limit 1;

-- example17: find all by name not like
-- fn: FindAllByNameNotLike
select *
from user
where name not like ?;

-- example18: find all by id in
-- fn: FindAllByIdIn
select *
from user
where id in (?);

-- example19: find all by id not in
-- fn: FindAllByIdNotIn
select *
from user
where id not in (?);

-- example20: find all by id between
-- fn: FindAllByIdBetween
select *
from user
where id between ? and ?;

-- example21: find all by id not between
-- fn: FindAllByIdNotBetween
select *
from user
where id not between ? and ?;

-- example22: find all by id greater than or equal to
-- fn: FindAllByIdGte
select *
from user
where id >= ?;

-- example23: find all by id less than or equal to
-- fn: FindAllByIdLte
select *
from user
where id <= ?;

-- example24: find all by id not equal to
-- fn: FindAllByIdNeq
select *
from user
where id != ?;

-- example25: find all by id in, or, not in
-- fn: FindAllByIdInOrNotIn
select *
from user
where id in (?)
or id not in (?);

-- example26: complex query
-- fn: ComplexQuery
select *
from user
where id > ?
and id < ?
and id != ?
and id in (?)
and id not in (?)
and id between ? and ?
and id not between ? and ?
and id >= ?
and id <= ?
and id != ?
and name like ?
and name not like ?
and name in (?)
and name not in (?)
and name between ?
and ? and name not between ? and ?
and name >= ?
and name <= ?
and name != ?;
41 changes: 41 additions & 0 deletions example/bun/update/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- 用户表 --
CREATE TABLE `user`
(
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT primary key,
`name` varchar(255) COLLATE utf8mb4_general_ci NULL COMMENT '用户\t名称',
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户\n密码',
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公\r开',
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
`type` tinyint(1) COLLATE utf8mb4_general_ci DEFAULT 0 COMMENT '用户类型',
`create_time` timestamp NULL,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `name_index` (`name`),
UNIQUE KEY `type_index` (`type`),
UNIQUE KEY `mobile_index` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '用户表' COLLATE=utf8mb4_general_ci;

-- example1: update by primary key
-- fn: Update
update user set name = ?, password = ?, mobile = ?, gender = ?, nickname = ?, type = ?, create_time = ?, update_time = ? where id = ?;

-- example2: update by unique key
-- fn: UpdateByName
update user set password = ?, mobile = ?, gender = ?, nickname = ?, type = ?, create_time = ?, update_time = ? where name = ?;

-- example3: update part columns by primary key
-- fn: UpdatePart
update user set name = ?, nickname = ? where id = ?;

-- example4: update part columns by unique key
-- fn: UpdatePartByName
update user set name = ?, nickname = ? where name = ?;

-- example5: update name limit ?
-- fn: UpdateNameLimit
update user set name = ? where id > ? limit ?;

-- example6: update name limit ? order by id desc
-- fn: UpdateNameLimitOrder
update user set name = ? where id > ? order by id desc limit ?;

2 changes: 1 addition & 1 deletion example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
gorm.io/gorm v1.23.8
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978
xorm.io/xorm v1.3.1
)

Expand All @@ -17,5 +18,4 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
)
5 changes: 5 additions & 0 deletions example/gorm/create/user_model.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion example/gorm/delete/example.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ delete from user where name = ?;

-- example3: delete by unique keys
-- fn: DeleteByNameAndMobile
delete from user where name = ? and mobile = ?;
delete from user where name = ? and mobile = ?;

-- example4: delete by id order by id
-- fn: DeleteOrderByID
delete from user where id = ? order by id desc;

-- example5 delete by id order by id limit 10
-- fn: DeleteOrderByIDLimit
delete from user where id = ? order by id desc limit 10;
Loading
0