Add jet generator support for SQLite
This commit is contained in:
parent
3f7efb33eb
commit
51cad22809
8 changed files with 154 additions and 31 deletions
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
sqlitegen "github.com/go-jet/jet/v2/generator/sqlite"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ import (
|
|||
"github.com/go-jet/jet/v2/postgres"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -31,7 +33,7 @@ var (
|
|||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&source, "source", "", "Database system name (PostgreSQL, MySQL or MariaDB)")
|
||||
flag.StringVar(&source, "source", "", "Database system name (PostgreSQL, MySQL, MariaDB or SQLite)")
|
||||
|
||||
flag.StringVar(&dsn, "dsn", "", "Data source name connection string (Example: postgresql://user@localhost:5432/otherdb?sslmode=trust)")
|
||||
flag.StringVar(&host, "host", "", "Database host path (Example: localhost)")
|
||||
|
|
@ -50,7 +52,7 @@ func main() {
|
|||
|
||||
flag.Usage = func() {
|
||||
_, _ = fmt.Fprint(os.Stdout, `
|
||||
Jet generator 2.5.0
|
||||
Jet generator 2.6.0
|
||||
|
||||
Usage:
|
||||
-dsn string
|
||||
|
|
@ -61,8 +63,11 @@ Usage:
|
|||
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
|
||||
SQLite: https://www.sqlite.org/c3ref/open.html#urifilenameexamples
|
||||
Example:
|
||||
file://path/to/database/file
|
||||
-source string
|
||||
Database system name (PostgreSQL, MySQL or MariaDB)
|
||||
Database system name (PostgreSQL, MySQL, MariaDB or SQLite)
|
||||
-host string
|
||||
Database host path (Example: localhost)
|
||||
-port int
|
||||
|
|
@ -76,17 +81,18 @@ Usage:
|
|||
-params string
|
||||
Additional connection string parameters(optional)
|
||||
-schema string
|
||||
Database schema name. (default "public") (ignored for MySQL and MariaDB)
|
||||
Database schema name. (default "public") (ignored for MySQL, MariaDB and SQLite)
|
||||
-sslmode string
|
||||
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, MariaDB and SQLite)
|
||||
-path string
|
||||
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
|
||||
$ 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
|
||||
$ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -schema=./dvds
|
||||
`)
|
||||
}
|
||||
|
||||
|
|
@ -95,7 +101,7 @@ Example commands:
|
|||
if dsn == "" {
|
||||
// validations for separated connection flags.
|
||||
if source == "" || host == "" || port == 0 || user == "" || dbName == "" {
|
||||
printErrorAndExit("\nERROR: required flag(s) missing")
|
||||
printErrorAndExit("ERROR: required flag(s) missing")
|
||||
}
|
||||
} else {
|
||||
if source == "" {
|
||||
|
|
@ -105,15 +111,14 @@ Example commands:
|
|||
|
||||
// validations when dsn != ""
|
||||
if source == "" {
|
||||
printErrorAndExit("\nERROR: required -source flag missing.")
|
||||
printErrorAndExit("ERROR: required -source flag missing.")
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
switch strings.ToLower(strings.TrimSpace(source)) {
|
||||
case strings.ToLower(postgres.Dialect.Name()),
|
||||
strings.ToLower(postgres.Dialect.PackageName()):
|
||||
case "postgresql", "postgres":
|
||||
if dsn != "" {
|
||||
err = postgresgen.GenerateDSN(dsn, schemaName, destDir)
|
||||
break
|
||||
|
|
@ -132,7 +137,7 @@ Example commands:
|
|||
|
||||
err = postgresgen.Generate(destDir, genData)
|
||||
|
||||
case strings.ToLower(mysql.Dialect.Name()), "mysqlx", "mariadb":
|
||||
case "mysql", "mysqlx", "mariadb":
|
||||
if dsn != "" {
|
||||
err = mysqlgen.GenerateDSN(dsn, destDir)
|
||||
break
|
||||
|
|
@ -147,9 +152,13 @@ Example commands:
|
|||
}
|
||||
|
||||
err = mysqlgen.Generate(destDir, dbConn)
|
||||
case "sqlite":
|
||||
if dsn == "" {
|
||||
printErrorAndExit("ERROR: required -dsn flag missing.")
|
||||
}
|
||||
err = sqlitegen.GenerateDSN(dsn, destDir)
|
||||
default:
|
||||
fmt.Println("ERROR: unsupported source " + source + ". " + postgres.Dialect.Name() + " and " + mysql.Dialect.Name() + " are currently supported.")
|
||||
os.Exit(-4)
|
||||
printErrorAndExit("ERROR: unsupported source " + source + ". " + postgres.Dialect.Name() + " and " + mysql.Dialect.Name() + " are currently supported.")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -159,12 +168,12 @@ Example commands:
|
|||
}
|
||||
|
||||
func printErrorAndExit(error string) {
|
||||
fmt.Println(error)
|
||||
fmt.Println("\n", error)
|
||||
flag.Usage()
|
||||
os.Exit(-2)
|
||||
}
|
||||
|
||||
func detectSchema(dsn string) (source string) {
|
||||
func detectSchema(dsn string) string {
|
||||
match := strings.SplitN(dsn, "://", 2)
|
||||
if len(match) < 2 { // not found
|
||||
return ""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue