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,9 +3,10 @@ package postgres
|
|||
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/postgres"
|
||||
"path"
|
||||
"strconv"
|
||||
|
|
@ -25,38 +26,39 @@ 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, genTemplate ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
db, err := openConnection(dbConn)
|
||||
utils.PanicOnError(err)
|
||||
db := openConnection(dbConn)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
fmt.Println("Retrieving schema information...")
|
||||
schemaInfo := metadata.GetSchemaMetaData(db, dbConn.SchemaName, &postgresQuerySet{})
|
||||
|
||||
genPath := path.Join(destDir, dbConn.DBName, dbConn.SchemaName)
|
||||
template.GenerateFiles(genPath, schemaInfo, postgres.Dialect)
|
||||
generatorTemplate := template.Default(postgres.Dialect)
|
||||
if len(genTemplate) > 0 {
|
||||
generatorTemplate = genTemplate[0]
|
||||
}
|
||||
|
||||
schemaMetadata := metadata.GetSchema(db, &postgresQuerySet{}, dbConn.SchemaName)
|
||||
|
||||
dirPath := path.Join(destDir, dbConn.DBName)
|
||||
|
||||
template.ProcessSchema(dirPath, schemaMetadata, generatorTemplate)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func openConnection(dbConn DBConnection) (*sql.DB, error) {
|
||||
func openConnection(dbConn DBConnection) *sql.DB {
|
||||
connectionString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s %s",
|
||||
dbConn.Host, strconv.Itoa(dbConn.Port), dbConn.User, dbConn.Password, dbConn.DBName, dbConn.SslMode, dbConn.Params)
|
||||
|
||||
fmt.Println("Connecting to postgres database: " + connectionString)
|
||||
|
||||
db, err := sql.Open("postgres", connectionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
throw.OnError(err)
|
||||
|
||||
err = db.Ping()
|
||||
throw.OnError(err)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
return db
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue