https://github.com/liangyaopei/sqltogo
Golang converts a sql create statement to a go struct, saving to time of manually writing template code
https://github.com/liangyaopei/sqltogo
golang parse sql
Last synced: 4 months ago
JSON representation
Golang converts a sql create statement to a go struct, saving to time of manually writing template code
- Host: GitHub
- URL: https://github.com/liangyaopei/sqltogo
- Owner: liangyaopei
- License: mit
- Created: 2020-07-07T13:07:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-31T14:25:45.000Z (almost 6 years ago)
- Last Synced: 2024-06-20T03:56:40.926Z (almost 2 years ago)
- Topics: golang, parse, sql
- Language: Go
- Homepage:
- Size: 118 KB
- Stars: 21
- Watchers: 1
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/liangyaopei/sqltogo)
[](http://godoc.org/github.com/liangyaopei/sqltogo)
[中文版说明](./README_zh.md)
## Description
This repository provide a way to convert SQL create statement to Golang struct (ORM struct) by parsing the SQL statement.
For example, with the input
```mysql
CREATE TABLE `USER`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`ip_address` INT NOT NULL DEFAULT 0 COMMENT 'ip_address',
`nickname` VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'user note',
`description` VARCHAR(256) NOT NULL DEFAULT '' COMMENT 'user description',
`creator_email` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'creator email',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create time',
`deleted_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'delete time',
PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='user table';
```
the function `SqlToGo` will convert it to as follow, with the `package` name is optional.
```go
package sql_to_go_test
import (
"time"
)
type USER struct {
Id uint `comment:"primary key"`
IpAddress int `comment:"ip_address"`
Nickname string `comment:"user note"`
Description string `comment:"user description"`
CreatorEmail string `comment:"creator email"`
CreatedAt time.Time `comment:"create time"`
DeletedAt time.Time `comment:"delete time"`
}
```
## Snake case To Camel case
In SQL, the naming convention of a filed is snake case, such as `ip_address`, while in Golang,
the naming convention of struct's field is Camel case. So the `SqlToGo` function will convert snake case to
Camel case.
## Test & run
The test will read the sample sql from `input.sql`, and print the output, then `gofmt` it and save
the result in `_output.go`
```sh
cd sql_to_go_test
go test -v .
```