Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liudanking/gorm2sql
auto generate sql from gorm model struct
https://github.com/liudanking/gorm2sql
ast codegenerator gorm sql
Last synced: 30 days ago
JSON representation
auto generate sql from gorm model struct
- Host: GitHub
- URL: https://github.com/liudanking/gorm2sql
- Owner: liudanking
- Created: 2018-04-30T11:12:16.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-07-13T03:38:21.000Z (over 2 years ago)
- Last Synced: 2024-08-03T17:17:32.477Z (4 months ago)
- Topics: ast, codegenerator, gorm, sql
- Language: Go
- Size: 11.7 KB
- Stars: 122
- Watchers: 3
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- go-awesome - gorm2sql - generate table creation statement according to model struct (Open source library / Database)
README
# gorm2sql: auto generate sql from gorm model struct
A Swiss Army Knife helps you generate sql from [gorm](https://github.com/jinzhu/gorm) model struct.
## Installation
```
go get github.com/liudanking/gorm2sql
```## Usage
`user_email.go`:
```go
type UserBase struct {
UserId string `sql:"index:idx_ub"`
Ip string `sql:"unique_index:uniq_ip"`
}type UserEmail struct {
Id int64 `gorm:"primary_key"`
UserBase
Email string
Sex bool
Age int
Score float64
UpdateTime time.Time `sql:"default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`
CreateTime time.Time `sql:"default:CURRENT_TIMESTAMP"`
}
``````
gorm2sql sql -f user_email.go -s UserEmail -o db.sql
```Result:
```sql
CREATE TABLE `user_email`
(
`id` bigint AUTO_INCREMENT NOT NULL ,
`user_id` varchar(128) NOT NULL ,
`ip` varchar(128) NOT NULL ,
`email` varchar(128) NOT NULL ,
`sex` boolean NOT NULL ,
`age` int NOT NULL ,
`score` double NOT NULL ,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_ub (`user_id`),
UNIQUE INDEX uniq_ip (`ip`),
PRIMARY KEY (`id`)
) engine=innodb DEFAULT charset=utf8mb4;
```## How it works
`gorm2sql` loads go source file to golang AST, then generate sql according to `tag` of gorm struct field.