Connection via DSN
This commit is contained in:
parent
b0838999d6
commit
5b7c34e333
8 changed files with 220 additions and 54 deletions
|
|
@ -3,11 +3,13 @@ package mysql
|
|||
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/internal/utils"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
mysqldr "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// DBConnection contains MySQL connection details
|
||||
|
|
@ -25,28 +27,38 @@ type DBConnection struct {
|
|||
func Generate(destDir string, dbConn DBConnection, generatorTemplate ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
db := openConnection(dbConn)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
fmt.Println("Retrieving database information...")
|
||||
// No schemas in MySQL
|
||||
schemaMetaData := metadata.GetSchema(db, &mySqlQuerySet{}, dbConn.DBName)
|
||||
|
||||
genTemplate := template.Default(mysql.Dialect)
|
||||
if len(generatorTemplate) > 0 {
|
||||
genTemplate = generatorTemplate[0]
|
||||
connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConn.User, dbConn.Password, dbConn.Host, dbConn.Port, dbConn.DBName)
|
||||
if dbConn.Params != "" {
|
||||
connectionString += "?" + dbConn.Params
|
||||
}
|
||||
|
||||
template.ProcessSchema(destDir, schemaMetaData, genTemplate)
|
||||
db := openConnection(connectionString)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
generate(db, dbConn.DBName, destDir, generatorTemplate...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func openConnection(dbConn DBConnection) *sql.DB {
|
||||
var connectionString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConn.User, dbConn.Password, dbConn.Host, dbConn.Port, dbConn.DBName)
|
||||
if dbConn.Params != "" {
|
||||
connectionString += "?" + dbConn.Params
|
||||
// GenerateDSN opens connection via DSN string and does everything what Generate does.
|
||||
func GenerateDSN(dsn, destDir string, templates ...template.Template) (err error) {
|
||||
defer utils.ErrorCatch(&err)
|
||||
|
||||
cfg, err := mysqldr.ParseDSN(dsn)
|
||||
throw.OnError(err)
|
||||
if cfg.DBName == "" {
|
||||
panic("database name is required")
|
||||
}
|
||||
|
||||
db := openConnection(dsn)
|
||||
defer utils.DBClose(db)
|
||||
|
||||
generate(db, cfg.DBName, destDir, templates...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func openConnection(connectionString string) *sql.DB {
|
||||
fmt.Println("Connecting to MySQL database: " + connectionString)
|
||||
db, err := sql.Open("mysql", connectionString)
|
||||
throw.OnError(err)
|
||||
|
|
@ -56,3 +68,16 @@ func openConnection(dbConn DBConnection) *sql.DB {
|
|||
|
||||
return db
|
||||
}
|
||||
|
||||
func generate(db *sql.DB, dbName, destDir string, templates ...template.Template) {
|
||||
fmt.Println("Retrieving database information...")
|
||||
// No schemas in MySQL
|
||||
schemaMetaData := metadata.GetSchema(db, &mySqlQuerySet{}, dbName)
|
||||
|
||||
genTemplate := template.Default(mysql.Dialect)
|
||||
if len(templates) > 0 {
|
||||
genTemplate = templates[0]
|
||||
}
|
||||
|
||||
template.ProcessSchema(destDir, schemaMetaData, genTemplate)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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