Review fixes

This commit is contained in:
vetcher 2021-09-01 14:38:45 +03:00 committed by go-jet
parent 5b7c34e333
commit b31464e491
2 changed files with 21 additions and 14 deletions

View file

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "strings"
mysqlgen "github.com/go-jet/jet/v2/generator/mysql" mysqlgen "github.com/go-jet/jet/v2/generator/mysql"
@ -57,9 +56,11 @@ Usage:
-dsn string -dsn string
Data source name. Unified format for connecting to database. Data source name. Unified format for connecting to database.
PostgreSQL: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING PostgreSQL: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
Example: postgresql://user:pass@localhost:5432/dbname Example:
postgresql://user:pass@localhost:5432/dbname
MySQL: https://dev.mysql.com/doc/refman/8.0/en/connecting-using-uri-or-key-value-pairs.html MySQL: https://dev.mysql.com/doc/refman/8.0/en/connecting-using-uri-or-key-value-pairs.html
Example: mysql://jet:jet@tcp(localhost:3306)/dvds Example:
mysql://jet:jet@tcp(localhost:3306)/dvds
-source string -source string
Database system name (PostgreSQL, MySQL or MariaDB) Database system name (PostgreSQL, MySQL or MariaDB)
-host string -host string
@ -80,6 +81,12 @@ Usage:
Whether or not to use SSL(optional) (default "disable") (ignored for MySQL and MariaDB) Whether or not to use SSL(optional) (default "disable") (ignored for MySQL and MariaDB)
-path string -path string
Destination dir for files generated. Destination dir for files generated.
Example commands:
$ jet -source=PostgreSQL -dbname=jetdb -host=localhost -port=5432 -user=jet -password=jet -schema=dvds
$ jet -dsn=postgresql://jet:jet@localhost:5432/jetdb -schema=dvds
$ jet -source=postgres -dsn="user=jet password=jet host=localhost port=5432 dbname=jetdb" -schema=dvds
`) `)
} }
@ -127,14 +134,6 @@ Usage:
case strings.ToLower(mysql.Dialect.Name()), "mysqlx", "mariadb": case strings.ToLower(mysql.Dialect.Name()), "mysqlx", "mariadb":
if dsn != "" { if dsn != "" {
// Special case for go mysql driver. It does not understand schema,
// so we need to trim it before passing to generator
// https://github.com/go-sql-driver/mysql#dsn-data-source-name
idx := strings.Index(dsn, "://")
if idx != -1 {
dsn = dsn[idx+len("://"):]
}
err = mysqlgen.GenerateDSN(dsn, destDir) err = mysqlgen.GenerateDSN(dsn, destDir)
break break
} }
@ -166,10 +165,9 @@ func printErrorAndExit(error string) {
} }
func detectSchema(dsn string) (source string) { func detectSchema(dsn string) (source string) {
schemeRe := regexp.MustCompile(`^(.+)://.*`) match := strings.SplitN(dsn, "://", 2)
match := schemeRe.FindStringSubmatch(dsn)
if len(match) < 2 { // not found if len(match) < 2 { // not found
return "" return ""
} }
return match[1] return match[0]
} }

View file

@ -3,6 +3,7 @@ package mysql
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"strings"
"github.com/go-jet/jet/v2/generator/metadata" "github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/template" "github.com/go-jet/jet/v2/generator/template"
@ -44,6 +45,14 @@ func Generate(destDir string, dbConn DBConnection, generatorTemplate ...template
func GenerateDSN(dsn, destDir string, templates ...template.Template) (err error) { func GenerateDSN(dsn, destDir string, templates ...template.Template) (err error) {
defer utils.ErrorCatch(&err) defer utils.ErrorCatch(&err)
// Special case for go mysql driver. It does not understand schema,
// so we need to trim it before passing to generator
// https://github.com/go-sql-driver/mysql#dsn-data-source-name
idx := strings.Index(dsn, "://")
if idx != -1 {
dsn = dsn[idx+len("://"):]
}
cfg, err := mysqldr.ParseDSN(dsn) cfg, err := mysqldr.ParseDSN(dsn)
throw.OnError(err) throw.OnError(err)
if cfg.DBName == "" { if cfg.DBName == "" {