Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kesonan/sqlgen
A tool to generate bun, gorm, sql, sqlx and xorm sql code.
https://github.com/kesonan/sqlgen
codegen database generator golang mysql sqlgen sqlgenerate sqlgenerator
Last synced: 3 days ago
JSON representation
A tool to generate bun, gorm, sql, sqlx and xorm sql code.
- Host: GitHub
- URL: https://github.com/kesonan/sqlgen
- Owner: kesonan
- License: mit
- Created: 2022-07-26T02:13:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-29T14:29:20.000Z (over 1 year ago)
- Last Synced: 2024-04-14T20:33:05.265Z (7 months ago)
- Topics: codegen, database, generator, golang, mysql, sqlgen, sqlgenerate, sqlgenerator
- Language: Go
- Homepage:
- Size: 164 KB
- Stars: 76
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlgen
English | [中文](README_cn.md)
[![Go](https://github.com/anqiansong/sqlgen/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/anqiansong/sqlgen/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/anqiansong/sqlgen/branch/main/graph/badge.svg?token=8mLCFUqD2l)](https://codecov.io/gh/anqiansong/sqlgen)
[![Go Reference](https://pkg.go.dev/badge/github.com/anqiansong/sqlgen.svg)](https://pkg.go.dev/github.com/anqiansong/sqlgen)
[![Go Report Card](https://goreportcard.com/badge/github.com/anqiansong/sqlgen)](https://goreportcard.com/report/github.com/anqiansong/sqlgen)
[![Release](https://img.shields.io/github/v/release/anqiansong/sqlgen.svg?style=flat-square)](https://github.com/anqiansong/sqlgen)
[![GitHub license](https://img.shields.io/github/license/anqiansong/sqlgen?style=flat-square)](https://github.com/anqiansong/sqlgen/blob/main/LICENSE)sqlgen is a tool to generate **bun**, **gorm**, **sql**, **sqlx** and **xorm** sql code from SQL
file which is inspired by- [go-zero](https://github.com/zeromicro/go-zero)
- [goctl](https://github.com/zeromicro/go-zero/tree/master/tools/goctl)
- [sqlc](https://github.com/kyleconroy/sqlc).# Installation
```bash
go install github.com/anqiansong/sqlgen@latest
```# Example
See [example](https://github.com/anqiansong/sqlgen/tree/main/example)
# Queries rule
## 1. Function Name
You can define a function via `fn` keyword in line comment, for example:
```sql
-- fn: my_func
SELECT *
FROM user;
```it will be generated as:
```go
func (m *UserModel) my_func (...) {
...
}
```## 2. Get One Record
The expression `limit 1` must be explicitly defined if you want to get only one record, for example:
```sql
-- fn: FindOne
select *
from user
where id = ? limit 1;
```## 3. Marker or Values?
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
```sql
-- fn: FindLimit
select *
from user
where id = ?;-- fn: FindLimit
select *
from user
where id = 1;```
## 4. SQL Function
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:```sql
-- fn: CountAll
select count(*) as count
from user;
```For most query cases, you can
see [example.sql](https://github.com/anqiansong/sqlgen/blob/main/example/example.sql) for details.# How it works
1. Create a SQL file
2. Write your SQL code in the SQL file
3. Run `sqlgen` to generate code# Notes
1. Only support MYSQL code generation.
3. Do not support multiple tables in one SQL file.
4. Do not support join query.