Skip to content

Commit 5f6f345

Browse files
author
billcheng
committed
update README.md
1 parent 0735b72 commit 5f6f345

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

README.md

+111-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,121 @@ sqlparser
77
## Usage
88

99
import (
10-
"github.com/Bill-cc/go-sql2struct"
10+
sqlparser "github.com/Bill-cc/go-sql2struct"
1111
)
1212

1313
## example
1414

1515
[parse_test.go](parse_test.go)
1616

1717
[parse_next_test.go](parse_next_test.go)
18-
...
18+
...
19+
20+
## SQL convert
21+
```
22+
// Mysql type to golang type
23+
var typeForMysqlToGo = map[string]string{
24+
"int": "int64",
25+
"integer": "int64",
26+
"tinyint": "int64",
27+
"smallint": "int64",
28+
"mediumint": "int64",
29+
"bigint": "int64",
30+
"int unsigned": "int64",
31+
"integer unsigned": "int64",
32+
"tinyint unsigned": "int64",
33+
"smallint unsigned": "int64",
34+
"mediumint unsigned": "int64",
35+
"bigint unsigned": "int64",
36+
"bit": "int64",
37+
"bool": "bool",
38+
"enum": "string",
39+
"set": "string",
40+
"varchar": "string",
41+
"char": "string",
42+
"tinytext": "string",
43+
"mediumtext": "string",
44+
"text": "string",
45+
"longtext": "string",
46+
"blob": "string",
47+
"tinyblob": "string",
48+
"mediumblob": "string",
49+
"longblob": "string",
50+
"date": "string",
51+
"datetime": "string",
52+
"timestamp": "string",
53+
"time": "string",
54+
"float": "float64",
55+
"double": "float64",
56+
"decimal": "float64",
57+
"binary": "string",
58+
"varbinary": "string",
59+
}
60+
61+
// Obtain SQL info
62+
func parseSQLToStruct(sqlPath string) error {
63+
// convert abs path
64+
sqlPathAbs, err := filepath.Abs(sqlPath)
65+
if err != nil {
66+
return fmt.Errorf("Get sql file filepath.Abs failed, %s", err)
67+
}
68+
fmt.Printf("SQL file path : %s\n", sqlPathAbs)
69+
70+
// sql script
71+
sqlContent, err := readFileContent(sqlPathAbs)
72+
if err != nil {
73+
return err
74+
}
75+
76+
// piece of sql script
77+
pieces, err := sqlparser.SplitStatementToPieces(sqlContent)
78+
if err != nil {
79+
return fmt.Errorf("sqlparser.SplitStatementToPieces fail : %v", err)
80+
}
81+
for _, sql := range pieces {
82+
stat, err := sqlparser.ParseStrictDDL(sql)
83+
if err != nil {
84+
fmt.Printf("sqlparser.Parse : %s\nerror : %v \n", sql, err)
85+
}
86+
87+
// convert ot DDL
88+
ddl, ok := stat.(*sqlparser.DDL)
89+
// process DDL as CREATE TABLE
90+
if ok && ddl.Action.ToString() == sqlparser.CreateStr {
91+
// table info
92+
sqlInfo := SQLInfo{}
93+
sqlInfo.SQLContent = sql
94+
sqlInfo.TableName = ddl.Table.Name.String()
95+
sqlInfo.TableNameStd = Marshal(sqlInfo.TableName)
96+
// table column
97+
for _, col := range ddl.TableSpec.Columns {
98+
colInfo := ColInfo{}
99+
// Autoincrement
100+
colInfo.Autoincrement = col.Type.Autoincrement
101+
// field define
102+
buf := sqlparser.NewTrackedBuffer(nil)
103+
col.Type.Format(buf)
104+
colInfo.ColContent = buf.String()
105+
// field name
106+
colInfo.Name = col.Name.String()
107+
// field name hump
108+
colInfo.NameStd = Marshal(colInfo.Name)
109+
if colInfo.Autoincrement {
110+
sqlInfo.AutoCol = colInfo.Name
111+
}
112+
// comment
113+
if col.Type.Comment != nil {
114+
colInfo.Comment = sqlparser.String(col.Type.Comment)
115+
}
116+
// Mysql type
117+
colInfo.Type = col.Type.Type
118+
// Golang tyoe
119+
colInfo.GoType = typeForMysqlToGo[colInfo.Type]
120+
sqlInfo.ColInfos = append(sqlInfo.ColInfos, colInfo)
121+
}
122+
sqlInfoList = append(sqlInfoList, sqlInfo)
123+
}
124+
}
125+
return nil
126+
}
127+
```

0 commit comments

Comments
 (0)