model support globbing patterns (#153)
* model support globbing patterns * optimize model * optimize model * format codemaster
parent
1fd2ef9347
commit
c9494c8bc7
@ -1,7 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# generate model with cache from ddl
|
# generate model with cache from ddl
|
||||||
goctl model mysql ddl -src="./sql/user.sql" -dir="./sql/model" -c
|
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/user" -c
|
||||||
|
|
||||||
# generate model with cache from data source
|
# generate model with cache from data source
|
||||||
#goctl model mysql datasource -url="user:password@tcp(127.0.0.1:3306)/database" -table="table1,table2" -dir="./model"
|
#user=root
|
||||||
|
#password=password
|
||||||
|
#datasource=127.0.0.1:3306
|
||||||
|
#database=test
|
||||||
|
#goctl model mysql datasource -url="${user}:${password}@tcp(${datasource})/${database}" -table="*" -dir ./model
|
@ -0,0 +1,15 @@
|
|||||||
|
-- 用户表 --
|
||||||
|
CREATE TABLE `user1` (
|
||||||
|
`id` bigint(10) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',
|
||||||
|
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',
|
||||||
|
`mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
|
||||||
|
`gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',
|
||||||
|
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',
|
||||||
|
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `name_index` (`name`),
|
||||||
|
UNIQUE KEY `mobile_index` (`mobile`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
@ -0,0 +1,18 @@
|
|||||||
|
package gen
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
source = "-- 用户表 --\nCREATE TABLE `user` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n UNIQUE KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewDefaultGenerator(t *testing.T) {
|
||||||
|
_ = Clean()
|
||||||
|
g := NewDefaultGenerator(source, "./model/user")
|
||||||
|
err := g.Start(true)
|
||||||
|
logx.Must(err)
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
InformationSchemaModel struct {
|
||||||
|
conn sqlx.SqlConn
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewInformationSchemaModel(conn sqlx.SqlConn) *InformationSchemaModel {
|
||||||
|
return &InformationSchemaModel{conn: conn}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *InformationSchemaModel) GetAllTables(database string) ([]string, error) {
|
||||||
|
query := `select TABLE_NAME from TABLES where TABLE_SCHEMA = ?`
|
||||||
|
var tables []string
|
||||||
|
err := m.conn.QueryRows(&tables, query, database)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tables, nil
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
package template
|
package template
|
||||||
|
|
||||||
var New = `
|
var New = `
|
||||||
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn,{{if .withCache}} c cache.CacheConf,{{end}} table string) *{{.upperStartCamelObject}}Model {
|
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf{{end}}) *{{.upperStartCamelObject}}Model {
|
||||||
return &{{.upperStartCamelObject}}Model{
|
return &{{.upperStartCamelObject}}Model{
|
||||||
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||||
table: table,
|
table: "{{.table}}",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMatchFiles(t *testing.T) {
|
||||||
|
dir, err := filepath.Abs("./")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
files, err := MatchFiles("./*.sql")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, []string{filepath.Join(dir, "studeat.sql"), filepath.Join(dir, "student.sql"), filepath.Join(dir, "xx.sql")}, files)
|
||||||
|
|
||||||
|
files, err = MatchFiles("./??.sql")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, []string{filepath.Join(dir, "xx.sql")}, files)
|
||||||
|
|
||||||
|
files, err = MatchFiles("./*.sq*")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, []string{filepath.Join(dir, "studeat.sql"), filepath.Join(dir, "student.sql"), filepath.Join(dir, "xx.sql"), filepath.Join(dir, "xx.sql1")}, files)
|
||||||
|
|
||||||
|
files, err = MatchFiles("./student.sql")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, []string{filepath.Join(dir, "student.sql")}, files)
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// expression: globbing patterns
|
||||||
|
func MatchFiles(in string) ([]string, error) {
|
||||||
|
dir, pattern := filepath.Split(in)
|
||||||
|
abs, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := ioutil.ReadDir(abs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var res []string
|
||||||
|
for _, file := range files {
|
||||||
|
if file.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := file.Name()
|
||||||
|
match, err := filepath.Match(pattern, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !match {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
res = append(res, filepath.Join(abs, name))
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
Loading…
Reference in New Issue