fix: model gen issues
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
pgDatabase "git.ipao.vip/rogeecn/atomctl/pkg/postgres"
|
pgDatabase "git.ipao.vip/rogeecn/atomctl/pkg/postgres"
|
||||||
|
"git.ipao.vip/rogeecn/atomctl/pkg/utils/gomod"
|
||||||
"github.com/go-jet/jet/v2/generator/metadata"
|
"github.com/go-jet/jet/v2/generator/metadata"
|
||||||
"github.com/go-jet/jet/v2/generator/postgres"
|
"github.com/go-jet/jet/v2/generator/postgres"
|
||||||
"github.com/go-jet/jet/v2/generator/template"
|
"github.com/go-jet/jet/v2/generator/template"
|
||||||
@@ -29,6 +30,10 @@ func CommandGenModel(root *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func commandGenModelE(cmd *cobra.Command, args []string) error {
|
func commandGenModelE(cmd *cobra.Command, args []string) error {
|
||||||
|
if err := gomod.Parse("go.mod"); err != nil {
|
||||||
|
return errors.Wrap(err, "parse go.mod")
|
||||||
|
}
|
||||||
|
|
||||||
_, dbConf, err := pgDatabase.GetDB(cmd.Flag("config").Value.String())
|
_, dbConf, err := pgDatabase.GetDB(cmd.Flag("config").Value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "get db")
|
return errors.Wrap(err, "get db")
|
||||||
@@ -91,13 +96,18 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
splits := strings.Split(toType, ".")
|
splits := strings.Split(toType, ".")
|
||||||
typeName := splits[len(splits)-1]
|
typeName := splits[len(splits)-1]
|
||||||
|
|
||||||
|
pkg := splits[0]
|
||||||
|
if strings.HasPrefix(pkg, "/") {
|
||||||
|
pkg = gomod.GetModuleName() + pkg
|
||||||
|
}
|
||||||
|
|
||||||
pkgSplits := strings.Split(splits[0], "/")
|
pkgSplits := strings.Split(splits[0], "/")
|
||||||
typePkg := pkgSplits[len(pkgSplits)-1]
|
typePkg := pkgSplits[len(pkgSplits)-1]
|
||||||
|
|
||||||
defaultTableModelField = defaultTableModelField.
|
defaultTableModelField = defaultTableModelField.
|
||||||
UseType(template.Type{
|
UseType(template.Type{
|
||||||
Name: fmt.Sprintf("%s.%s", typePkg, typeName),
|
Name: fmt.Sprintf("%s.%s", typePkg, typeName),
|
||||||
ImportPath: splits[0],
|
ImportPath: pkg,
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Infof("Convert table %s field %s type to : %s", table.Name, column.Name, toType)
|
log.Infof("Convert table %s field %s type to : %s", table.Name, column.Name, toType)
|
||||||
|
|||||||
@@ -1,8 +1,44 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
"embed"
|
"embed"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-jet/jet/v2/qrm"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed migrations/*
|
//go:embed migrations/*
|
||||||
var MigrationFS embed.FS
|
var MigrationFS embed.FS
|
||||||
|
|
||||||
|
type CtxDB struct{}
|
||||||
|
|
||||||
|
func FromContext(ctx context.Context, db *sql.DB) qrm.DB {
|
||||||
|
if tx, ok := ctx.Value(CtxDB{}).(*sql.Tx); ok {
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
||||||
|
func TruncateAllTables(ctx context.Context, db *sql.DB, tableName ...string) error {
|
||||||
|
for _, name := range tableName {
|
||||||
|
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
|
||||||
|
if _, err := db.ExecContext(ctx, sql); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func WrapLike(v string) string {
|
||||||
|
return "%" + v + "%"
|
||||||
|
}
|
||||||
|
|
||||||
|
func WrapLikeLeft(v string) string {
|
||||||
|
return "%" + v
|
||||||
|
}
|
||||||
|
|
||||||
|
func WrapLikeRight(v string) string {
|
||||||
|
return "%" + v
|
||||||
|
}
|
||||||
|
|||||||
32
templates/project/database/fields/common.go.tpl
Normal file
32
templates/project/database/fields/common.go.tpl
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package fields
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// implement sql.Scanner interface
|
||||||
|
type field struct{}
|
||||||
|
|
||||||
|
func (x *field) Scan(value interface{}) (err error) {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case string:
|
||||||
|
return json.Unmarshal([]byte(v), &x)
|
||||||
|
case []byte:
|
||||||
|
return json.Unmarshal(v, &x)
|
||||||
|
case *string:
|
||||||
|
return json.Unmarshal([]byte(*v), &x)
|
||||||
|
}
|
||||||
|
return errors.New("Unknown type for ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x field) Value() (driver.Value, error) {
|
||||||
|
return json.Marshal(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x field) MustValue() driver.Value {
|
||||||
|
return lo.Must(json.Marshal(x))
|
||||||
|
}
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/go-jet/jet/v2/qrm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const CtxDB = "__db__tx:"
|
|
||||||
|
|
||||||
func FromContext(ctx context.Context, db *sql.DB) qrm.DB {
|
|
||||||
if tx, ok := ctx.Value(CtxDB).(*sql.Tx); ok {
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
return db
|
|
||||||
}
|
|
||||||
|
|
||||||
func TruncateAllTables(ctx context.Context, db *sql.DB, tableName ...string) error {
|
|
||||||
for _, name := range tableName {
|
|
||||||
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
|
|
||||||
if _, err := db.ExecContext(ctx, sql); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func WrapLike(v string) string {
|
|
||||||
return "%" + v + "%"
|
|
||||||
}
|
|
||||||
|
|
||||||
func WrapLikeLeft(v string) string {
|
|
||||||
return "%" + v
|
|
||||||
}
|
|
||||||
|
|
||||||
func WrapLikeRight(v string) string {
|
|
||||||
return "%" + v
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user