English | 中文
sqlgen is a tool to generate bun, gorm, sql, sqlx and xorm sql code from SQL file which is inspired by
go install github.com/anqiansong/sqlgen@latest
See example
You can define a function via fn
keyword in line comment, for example:
-- fn: my_func
SELECT *
FROM user;
it will be generated as:
func (m *UserModel) my_func (...) {
...
}
The expression limit 1
must be explicitly defined if you want to get only one record, for example:
-- fn: FindOne
select *
from user
where id = ? limit 1;
For arguments of SQL, you can use ?
or explicitly values to mark them, in sqlgen, the arguments
will be converted into variables, for example, the following query are equivalent:
NOTES: It does not apply to rule 2
-- fn: FindLimit
select *
from user
where id = ?;
-- fn: FindLimit
select *
from user
where id = 1;
sqlgen supports aggregate function queries in sql, other than that, other functions are not supported so far. All the aggregate function query expressions must contain AS expression, for example:
-- fn: CountAll
select count(*) as count
from user;
For most query cases, you can see example.sql for details.
- Create a SQL file
- Write your SQL code in the SQL file
- Run
sqlgen
to generate code
- Only support MYSQL code generation.
- Do not support multiple tables in one SQL file.
- Do not support join query.