-
Notifications
You must be signed in to change notification settings - Fork 124
add comment #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers 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
Open
hktk1001
wants to merge
16
commits into
Shelnutt2:master
Choose a base branch
from
hktk1001:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add comment #36
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d1fbf73
add note
b03eccf
add jsonconv.go
4dec1c1
add
f9ce46a
add key
876f8a7
add some
cba7f6c
add save
0f3cbef
test
85b3407
增加使用说明
hktk1001 0758fc7
modify readme
hktk1001 23974f2
modify readme
hktk1001 d7c5604
modify gitignore
hktk1001 b138d06
modify host
hktk1001 d0d5954
modify run
ac85284
增加tmpfile
06cb6cf
custom code
9881330
modify run
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
.vscode/ | ||
db2struct | ||
~/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,9 @@ | ||
# db2struct [](https://travis-ci.org/Shelnutt2/db2struct) [](https://coveralls.io/github/Shelnutt2/db2struct?branch=1-add-coveralls-support) [](https://godoc.org/github.com/Shelnutt2/db2struct) | ||
# 使用说明 | ||
# 调用命令 sh build.sh 编译 | ||
# 请设置环境变量(数据库密码) IMPORT_DB_PWD | ||
# 修改配置,请修改run.sh 中的create参数等 | ||
|
||
The db2struct package produces a usable golang struct from a given database table for use in a .go file. | ||
# create 表名 结构体名字 | ||
|
||
By reading details from the database about the column structure, db2struct generates a go compatible struct type | ||
with the required column names, data types, and annotations. | ||
|
||
Generated datatypes include support for nullable columns [sql.NullX types](https://golang.org/pkg/database/sql/#NullBool) or [guregu null.X types](https://github.com/guregu/null) | ||
and the expected basic built in go types. | ||
|
||
Db2Struct is based/inspired by the work of ChimeraCoder's gojson package | ||
[gojson](https://github.com/ChimeraCoder/gojson) | ||
|
||
|
||
|
||
## Usage | ||
|
||
```BASH | ||
go get github.com/Shelnutt2/db2struct/cmd/db2struct | ||
db2struct --host localhost -d test -t test_table --package myGoPackage --struct testTable -p --user testUser | ||
``` | ||
|
||
## Example | ||
|
||
MySQL table named users with four columns: id (int), user_name (varchar(255)), number_of_logins (int(11),nullable), and LAST_NAME (varchar(255), nullable) | ||
|
||
Example below uses guregu's null package, but without the option it procuded the sql.NullInt64 and so on. | ||
```BASH | ||
db2struct --host localhost -d example.com -t users --package example --struct user -p --user exampleUser --guregu --gorm | ||
``` | ||
|
||
Output: | ||
```GOLANG | ||
|
||
package example | ||
|
||
type User struct { | ||
ID int `gorm:"column:id"` | ||
UserName string `gorm:"column:user_name"` | ||
NumberOfLogins null.Int `gorm:"column:number_of_logins"` | ||
LastName null.String `gorm:"column:LAST_NAME"` | ||
} | ||
``` | ||
|
||
## Supported Databases | ||
|
||
Currently Supported | ||
- MariaDB | ||
- MySQL | ||
|
||
Planned Support | ||
- PostgreSQL | ||
- Oracle | ||
- Microsoft SQL Server | ||
|
||
### MariaDB/MySQL | ||
|
||
Structures are created by querying the INFORMATION_SCHEMA.Columns table and then formatting the types, column names, | ||
and metadata to create a usable go compatible struct type. | ||
|
||
#### Supported Datatypes | ||
|
||
Currently only a limited number of MariaDB/MySQL datatypes are supported. Initial support includes: | ||
- tinyint (sql.NullInt64 or null.Int) | ||
- int (sql.NullInt64 or null.Int) | ||
- smallint (sql.NullInt64 or null.Int) | ||
- mediumint (sql.NullInt64 or null.Int) | ||
- bigint (sql.NullInt64 or null.Int) | ||
- decimal (sql.NullFloat64 or null.Float) | ||
- float (sql.NullFloat64 or null.Float) | ||
- double (sql.NullFloat64 or null.Float) | ||
- datetime (null.Time) | ||
- time (null.Time) | ||
- date (null.Time) | ||
- timestamp (null.Time) | ||
- var (sql.String or null.String) | ||
- enum (sql.String or null.String) | ||
- varchar (sql.String or null.String) | ||
- longtext (sql.String or null.String) | ||
- mediumtext (sql.String or null.String) | ||
- text (sql.String or null.String) | ||
- tinytext (sql.String or null.String) | ||
- binary | ||
- blob | ||
- longblob | ||
- mediumblob | ||
- varbinary | ||
- json | ||
# 保存run.sh后执行命令: | ||
# sh run.sh 生成model文件 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
#!/bin/bash -e | ||
|
||
name="db2struct" | ||
start_seconds=$(date +%s); | ||
|
||
#export CGO_ENABLED="0" | ||
export GO111MODULE=on | ||
go build -o $name cmd/db2struct/main.go | ||
|
||
if [ $? -ne 0 ];then | ||
end_seconds=$(date +%s); | ||
echo | ||
echo "---> build time: "$((end_seconds-start_seconds))"s, build error!!!" | ||
echo | ||
exit 1 | ||
else | ||
end_seconds=$(date +%s); | ||
echo | ||
ls -lt |grep $name | ||
echo | ||
echo "Build Success,took "$((end_seconds-start_seconds))"s" | ||
echo | ||
exit 0 | ||
fi | ||
|
||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
module github.com/Shelnutt2/db2struct | ||
module hktk1001/db2struct | ||
|
||
go 1.12 | ||
go 1.15 | ||
|
||
require ( | ||
github.com/Shelnutt2/db2struct v1.0.1 | ||
github.com/droundy/goopt v0.0.0-20170604162106-0b8effe182da | ||
github.com/go-sql-driver/mysql v1.4.1 | ||
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c | ||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect | ||
github.com/gin-gonic/gin v1.6.3 | ||
github.com/go-sql-driver/mysql v1.5.0 | ||
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c | ||
github.com/jinzhu/gorm v1.9.16 | ||
github.com/prometheus/common v0.19.0 | ||
github.com/smartystreets/goconvey v1.6.4 | ||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect | ||
) | ||
|
||
replace github.com/Shelnutt2/db2struct => ../db2struct |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the mod files (
go.mod
andgo.sum
), the changes here look like they are only from your fork?