Last active
August 29, 2015 14:23
-
-
Save lysu/61c0e7fdf60cba600f53 to your computer and use it in GitHub Desktop.
generate-pojo.go
This file contains 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
package main | |
import ( | |
"bytes" | |
"database/sql" | |
"fmt" | |
"log" | |
"regexp" | |
"strings" | |
"text/template" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
type tableInfo struct { | |
Name string | |
Comment string | |
Col []fieldInfo | |
} | |
type fieldInfo struct { | |
Field string | |
Ctype string | |
Comment string | |
} | |
var camelingRegex = regexp.MustCompile("[0-9A-Za-z]+") | |
func fCamelCase(src string) string { | |
byteSrc := []byte(src) | |
chunks := camelingRegex.FindAll(byteSrc, -1) | |
for idx, val := range chunks { | |
chunks[idx] = bytes.Title(val) | |
} | |
return string(bytes.Join(chunks, nil)) | |
} | |
func camelCase(src string) string { | |
byteSrc := []byte(src) | |
chunks := camelingRegex.FindAll(byteSrc, -1) | |
for idx, val := range chunks { | |
if idx > 0 { | |
chunks[idx] = bytes.Title(val) | |
} | |
} | |
return string(bytes.Join(chunks, nil)) | |
} | |
func showFields(cols []fieldInfo) string { | |
var funcMap = template.FuncMap{ | |
"camelCase": camelCase, | |
"toJavaType": toJavaType, | |
} | |
t := template.New("fields").Funcs(funcMap) | |
t, _ = t.Parse(` | |
/** | |
* {{.Comment}} | |
**/ | |
private {{toJavaType .Ctype}} {{camelCase .Field}}; | |
`) | |
var doc bytes.Buffer | |
for _, item := range cols { | |
t.Execute(&doc, item) | |
} | |
return doc.String() | |
} | |
func showProps(cols []fieldInfo) string { | |
var funcMap = template.FuncMap{ | |
"fCamelCase": fCamelCase, | |
"camelCase": camelCase, | |
"toJavaType": toJavaType, | |
} | |
t := template.New("fields").Funcs(funcMap) | |
t, _ = t.Parse(` | |
public {{toJavaType .Ctype}} get{{fCamelCase .Field}}() { | |
return this.{{camelCase .Field}}; | |
} | |
pubic void set{{fCamelCase .Field}}({{toJavaType .Ctype}} {{camelCase .Field}}) { | |
this.{{camelCase .Field}} = {{camelCase .Field}}; | |
} | |
`) | |
var doc bytes.Buffer | |
for _, item := range cols { | |
t.Execute(&doc, item) | |
} | |
return doc.String() | |
} | |
func toJavaType(s string) string { | |
idx := strings.Index(s, "(") | |
if idx > 0 { | |
s = s[:idx] | |
} | |
switch s { | |
case "int", "short", "enum": | |
return "Integer" | |
case "bigint": | |
return "Long" | |
} | |
return "String" | |
} | |
func main() { | |
db, err := sql.Open("mysql", "root@tcp/xx") | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer db.Close() | |
rows, err := db.Query("show table status") | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
cols, err := rows.Columns() | |
if err != nil { | |
fmt.Println("Failed to get columns", err) | |
return | |
} | |
rawResult := make([][]byte, len(cols)) | |
dest := make([]interface{}, len(cols)) | |
for i := range rawResult { | |
dest[i] = &rawResult[i] | |
} | |
tables := []tableInfo{} | |
for rows.Next() { | |
if err := rows.Scan(dest...); err != nil { | |
log.Fatal(err) | |
} | |
var name, comment string | |
name = string(rawResult[0]) | |
comment = string(rawResult[len(rawResult)-1]) | |
tables = append(tables, tableInfo{name, comment, []fieldInfo{}}) | |
} | |
for i := range tables { | |
table := &tables[i] | |
rows, err := db.Query("show full fields from " + table.Name) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
cols, err := rows.Columns() | |
if err != nil { | |
fmt.Println("Failed to get columns", err) | |
return | |
} | |
rawResult := make([][]byte, len(cols)) | |
dest := make([]interface{}, len(cols)) | |
for i := range rawResult { | |
dest[i] = &rawResult[i] | |
} | |
for rows.Next() { | |
if err := rows.Scan(dest...); err != nil { | |
log.Fatal(err) | |
} | |
var name, ctype, comment string | |
name = string(rawResult[0]) | |
ctype = string(rawResult[1]) | |
comment = string(rawResult[len(rawResult)-1]) | |
table.Col = append(table.Col, fieldInfo{name, ctype, comment}) | |
} | |
} | |
var funcMap = template.FuncMap{ | |
"fCamelCase": fCamelCase, | |
"fields": showFields, | |
"props": showProps, | |
} | |
t := template.New("javaBean").Funcs(funcMap) | |
t, err = t.Parse(` | |
/** | |
* {{.Comment}} | |
**/ | |
class {{fCamelCase .Name}} { | |
{{fields .Col}} | |
{{props .Col}} | |
} | |
`) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var doc bytes.Buffer | |
t.Execute(&doc, tables[2]) | |
fmt.Printf("%s", doc.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment