Skip to content

Commit

Permalink
gen table name
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Oct 11, 2021
1 parent 54a4862 commit b52d802
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
42 changes: 42 additions & 0 deletions examples/model/gen.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,48 @@ func (c *b64Cipher) Decode(src string) string {
return string(decoded)
}

//DBMgr
type _DBMgr struct {
db DB
}

func DBMgr(db DB) *_DBMgr {
return &_DBMgr{db: db}
}

func (m *_DBMgr) TableExist(ctx context.Context, database string, table string) (bool, error) {
qs := []string{
"SELECT",
"count(1)",
"FROM",
"INFORMATION_SCHEMA.TABLES",
"WHERE",
"TABLE_SCHEMA = ?",
"AND",
"TABLE_NAME = ?",
}
ps := []interface{}{
database,
table,
}

rows, err := m.db.QueryContext(ctx, strings.Join(qs, " "), ps...)
if err != nil {
return false, err
}
defer rows.Close()

var count int64
for rows.Next() {
if err = rows.Scan(&count); err != nil {
return false, err
}
break
}
return count != 0, nil
}

//utils
var Password PasswordFunc
var Encoder Cipher

Expand Down
2 changes: 2 additions & 0 deletions examples/model/gen.table.User.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var UserDBColumns = []string{
"`update_at`",
}

var UserDBTable = "user"

type _UserMgr struct {
}

Expand Down
2 changes: 1 addition & 1 deletion tpl/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tpl/code/golang/table.gogo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var {{$table.Name}}DBColumns = []string {
{{- end}}
}

var {{$table.Name}}DBTable = "{{$table.DBName}}"

type _{{$table.Name}}Mgr struct {
}
var {{$table.Name}}Mgr *_{{$table.Name}}Mgr
Expand Down

0 comments on commit b52d802

Please sign in to comment.