Connection via DSN
This commit is contained in:
parent
b0838999d6
commit
5b7c34e333
8 changed files with 220 additions and 54 deletions
|
|
@ -3,13 +3,15 @@ package postgres
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
"github.com/jackc/pgconn"
|
||||
)
|
||||
|
||||
// DBConnection contains postgres connection details
|
||||
|
|
@ -29,32 +31,37 @@ type DBConnection struct {
|
|||
func Generate(destDir string, dbConn DBConnection, genTemplate ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
db := openConnection(dbConn)
|
||||
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)
|
||||
|
||||
db := openConnection(connectionString)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
fmt.Println("Retrieving schema information...")
|
||||
|
||||
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)
|
||||
generate(db, dbConn.DBName, dbConn.SchemaName, destDir, genTemplate...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
func GenerateDSN(dsn, schema, destDir string, templates ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
fmt.Println("Connecting to postgres database: " + connectionString)
|
||||
cfg, err := pgconn.ParseConfig(dsn)
|
||||
throw.OnError(err)
|
||||
if cfg.Database == "" {
|
||||
panic("database name is required")
|
||||
}
|
||||
db := openConnection(dsn)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
db, err := sql.Open("postgres", connectionString)
|
||||
generate(db, cfg.Database, schema, destDir, templates...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func openConnection(dsn string) *sql.DB {
|
||||
fmt.Println("Connecting to postgres database: " + dsn)
|
||||
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
throw.OnError(err)
|
||||
|
||||
err = db.Ping()
|
||||
|
|
@ -62,3 +69,17 @@ func openConnection(dbConn DBConnection) *sql.DB {
|
|||
|
||||
return db
|
||||
}
|
||||
|
||||
func generate(db *sql.DB, dbName, schema, destDir string, templates ...template.Template) {
|
||||
fmt.Println("Retrieving schema information...")
|
||||
generatorTemplate := template.Default(postgres.Dialect)
|
||||
if len(templates) > 0 {
|
||||
generatorTemplate = templates[0]
|
||||
}
|
||||
|
||||
schemaMetadata := metadata.GetSchema(db, &postgresQuerySet{}, schema)
|
||||
|
||||
dirPath := path.Join(destDir, dbName)
|
||||
|
||||
template.ProcessSchema(dirPath, schemaMetadata, generatorTemplate)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue