2019-07-27 10:40:30 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
2021-09-24 02:49:41 -07:00
|
|
|
"net/url"
|
2021-08-30 15:09:09 +03:00
|
|
|
"path"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
"github.com/go-jet/jet/v2/generator/metadata"
|
|
|
|
|
"github.com/go-jet/jet/v2/generator/template"
|
2020-06-27 18:48:19 +02:00
|
|
|
"github.com/go-jet/jet/v2/internal/utils"
|
2021-07-27 17:39:21 +02:00
|
|
|
"github.com/go-jet/jet/v2/internal/utils/throw"
|
2020-06-27 18:48:19 +02:00
|
|
|
"github.com/go-jet/jet/v2/postgres"
|
2021-08-30 15:09:09 +03:00
|
|
|
"github.com/jackc/pgconn"
|
2019-07-27 10:40:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DBConnection contains postgres connection details
|
|
|
|
|
type DBConnection struct {
|
|
|
|
|
Host string
|
|
|
|
|
Port int
|
|
|
|
|
User string
|
|
|
|
|
Password string
|
|
|
|
|
SslMode string
|
|
|
|
|
Params string
|
|
|
|
|
|
|
|
|
|
DBName string
|
|
|
|
|
SchemaName string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate generates jet files at destination dir from database connection details
|
2021-07-27 17:39:21 +02:00
|
|
|
func Generate(destDir string, dbConn DBConnection, genTemplate ...template.Template) (err error) {
|
2021-10-04 10:46:46 +02:00
|
|
|
dsn := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=%s",
|
2021-10-22 18:08:05 +02:00
|
|
|
url.PathEscape(dbConn.User),
|
|
|
|
|
url.PathEscape(dbConn.Password),
|
2021-10-04 10:46:46 +02:00
|
|
|
dbConn.Host,
|
|
|
|
|
strconv.Itoa(dbConn.Port),
|
2021-10-22 18:08:05 +02:00
|
|
|
url.PathEscape(dbConn.DBName),
|
2021-10-04 10:46:46 +02:00
|
|
|
dbConn.SslMode,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return GenerateDSN(dsn, dbConn.SchemaName, destDir, genTemplate...)
|
2021-08-30 15:09:09 +03:00
|
|
|
}
|
2021-07-27 17:39:21 +02:00
|
|
|
|
2021-10-04 10:46:46 +02:00
|
|
|
// GenerateDSN generates jet files using dsn connection string
|
2021-08-30 15:09:09 +03:00
|
|
|
func GenerateDSN(dsn, schema, destDir string, templates ...template.Template) (err error) {
|
|
|
|
|
defer utils.ErrorCatch(&err)
|
2021-07-27 17:39:21 +02:00
|
|
|
|
2021-08-30 15:09:09 +03:00
|
|
|
cfg, err := pgconn.ParseConfig(dsn)
|
|
|
|
|
throw.OnError(err)
|
|
|
|
|
if cfg.Database == "" {
|
|
|
|
|
panic("database name is required")
|
|
|
|
|
}
|
|
|
|
|
db := openConnection(dsn)
|
|
|
|
|
defer utils.DBClose(db)
|
2021-07-27 17:39:21 +02:00
|
|
|
|
2021-10-04 10:46:46 +02:00
|
|
|
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, cfg.Database)
|
2019-07-27 10:40:30 +02:00
|
|
|
|
2021-10-04 10:46:46 +02:00
|
|
|
template.ProcessSchema(dirPath, schemaMetadata, generatorTemplate)
|
2019-09-20 18:20:26 +02:00
|
|
|
return
|
2019-07-27 10:40:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 15:09:09 +03:00
|
|
|
func openConnection(dsn string) *sql.DB {
|
|
|
|
|
fmt.Println("Connecting to postgres database: " + dsn)
|
2019-07-27 10:40:30 +02:00
|
|
|
|
2021-08-30 15:09:09 +03:00
|
|
|
db, err := sql.Open("postgres", dsn)
|
2021-07-27 17:39:21 +02:00
|
|
|
throw.OnError(err)
|
2019-07-27 10:40:30 +02:00
|
|
|
|
|
|
|
|
err = db.Ping()
|
2021-07-27 17:39:21 +02:00
|
|
|
throw.OnError(err)
|
2019-07-27 10:40:30 +02:00
|
|
|
|
2021-07-27 17:39:21 +02:00
|
|
|
return db
|
2019-07-27 10:40:30 +02:00
|
|
|
}
|