Skip to content

Commit

Permalink
generator/*: allow passing DB directly
Browse files Browse the repository at this point in the history
Closes go-jet#441
  • Loading branch information
TotallyGamerJet committed Jan 17, 2025
1 parent 269bb4a commit e4ceb42
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
7 changes: 4 additions & 3 deletions generator/mysql/mysql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Generate(destDir string, dbConn DBConnection, generatorTemplate ...template
}
defer db.Close()

err = generate(db, dbConn.DBName, destDir, generatorTemplate...)
err = GenerateDB(db, dbConn.DBName, destDir, generatorTemplate...)
if err != nil {
return err
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func GenerateDSN(dsn, destDir string, templates ...template.Template) error {
}
defer db.Close()

err = generate(db, cfg.DBName, destDir, templates...)
err = GenerateDB(db, cfg.DBName, destDir, templates...)
if err != nil {
return fmt.Errorf("failed to generate: %w", err)
}
Expand All @@ -96,7 +96,8 @@ func openConnection(connectionString string) (*sql.DB, error) {
return db, nil
}

func generate(db *sql.DB, dbName, destDir string, templates ...template.Template) error {
// GenerateDB generates jet files using the provided *sql.DB
func GenerateDB(db *sql.DB, dbName, destDir string, templates ...template.Template) error {
fmt.Println("Retrieving database information...")
// No schemas in MySQL
schemaMetaData, err := metadata.GetSchema(db, &mySqlQuerySet{}, dbName)
Expand Down
10 changes: 9 additions & 1 deletion generator/postgres/postgres_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func GenerateDSN(dsn, schema, destDir string, templates ...template.Template) er
defer db.Close()

fmt.Println("Retrieving schema information...")
return GenerateDB(db, schema, cfg.Database, destDir, templates...)
}

// GenerateDB generates jet files using the provided *sql.DB
func GenerateDB(db *sql.DB, dbName, schema, destDir string, templates ...template.Template) error {
if dbName == "" {
return fmt.Errorf("database name is required")
}
generatorTemplate := template.Default(postgres.Dialect)
if len(templates) > 0 {
generatorTemplate = templates[0]
Expand All @@ -66,7 +74,7 @@ func GenerateDSN(dsn, schema, destDir string, templates ...template.Template) er
return fmt.Errorf("failed to get '%s' schema metadata: %w", schema, err)
}

dirPath := filepath.Join(destDir, cfg.Database)
dirPath := filepath.Join(destDir, dbName)

err = template.ProcessSchema(dirPath, schemaMetadata, generatorTemplate)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions generator/sqlite/sqlite_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlite
import (
"database/sql"
"fmt"

"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/template"
"github.com/go-jet/jet/v2/sqlite"
Expand All @@ -17,7 +18,11 @@ func GenerateDSN(dsn, destDir string, templates ...template.Template) error {
defer db.Close()

fmt.Println("Retrieving schema information...")
return GenerateDB(db, destDir, templates...)
}

// GenerateDB generates jet files using the provided *sql.DB
func GenerateDB(db *sql.DB, destDir string, templates ...template.Template) error {
generatorTemplate := template.Default(sqlite.Dialect)
if len(templates) > 0 {
generatorTemplate = templates[0]
Expand Down

0 comments on commit e4ceb42

Please sign in to comment.