Add the ability to fully customize jet generated files.
This commit is contained in:
parent
caa81930dc
commit
8864667f47
40 changed files with 2274 additions and 882 deletions
|
|
@ -3,11 +3,11 @@ package mysql
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/generator/internal/metadata"
|
||||
"github.com/go-jet/jet/v2/generator/internal/template"
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/generator/template"
|
||||
"github.com/go-jet/jet/v2/internal/utils"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
"path"
|
||||
)
|
||||
|
||||
// DBConnection contains MySQL connection details
|
||||
|
|
@ -22,7 +22,7 @@ type DBConnection struct {
|
|||
}
|
||||
|
||||
// Generate generates jet files at destination dir from database connection details
|
||||
func Generate(destDir string, dbConn DBConnection) (err error) {
|
||||
func Generate(destDir string, dbConn DBConnection, generatorTemplate ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
db := openConnection(dbConn)
|
||||
|
|
@ -30,11 +30,14 @@ func Generate(destDir string, dbConn DBConnection) (err error) {
|
|||
|
||||
fmt.Println("Retrieving database information...")
|
||||
// No schemas in MySQL
|
||||
dbInfo := metadata.GetSchemaMetaData(db, dbConn.DBName, &mySqlQuerySet{})
|
||||
schemaMetaData := metadata.GetSchema(db, &mySqlQuerySet{}, dbConn.DBName)
|
||||
|
||||
genPath := path.Join(destDir, dbConn.DBName)
|
||||
genTemplate := template.Default(mysql.Dialect)
|
||||
if len(generatorTemplate) > 0 {
|
||||
genTemplate = generatorTemplate[0]
|
||||
}
|
||||
|
||||
template.GenerateFiles(genPath, dbInfo, mysql.Dialect)
|
||||
template.ProcessSchema(destDir, schemaMetaData, genTemplate)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -46,10 +49,10 @@ func openConnection(dbConn DBConnection) *sql.DB {
|
|||
}
|
||||
fmt.Println("Connecting to MySQL database: " + connectionString)
|
||||
db, err := sql.Open("mysql", connectionString)
|
||||
utils.PanicOnError(err)
|
||||
throw.OnError(err)
|
||||
|
||||
err = db.Ping()
|
||||
utils.PanicOnError(err)
|
||||
throw.OnError(err)
|
||||
|
||||
return db
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue