Jet usage improvements.

This commit is contained in:
go-jet 2019-07-11 17:45:23 +02:00
parent 63f2d04651
commit 5bff434eb0
2 changed files with 42 additions and 16 deletions

View file

@ -63,8 +63,8 @@ Generating enum sql builder files...
Generating enum model files... Generating enum model files...
Done Done
``` ```
As jet command output suggest, jet will: As command output suggest, Jet will:
- connect to postgres database and retrieve information about the tables and enums of `dvds` schema - connect to postgres database and retrieve information about the _tables_ and _enums_ of `dvds` schema
- delete everything in destination folder `./gen`, - delete everything in destination folder `./gen`,
- and finally generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`. - and finally generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`.
@ -88,7 +88,7 @@ Generated files folder structure will look like this:
... ...
``` ```
You will be using types from `table` and `enum` to write type safe SQL in Go, and `model` types would be used to store results of the queries. Types from `table` and `enum` are used to write type safe SQL in Go, and `model` types are used to store results of the queries.
#### Lets write some SQL queries in Go #### Lets write some SQL queries in Go
First lets import jet and generated files from previous step First lets import jet and generated files from previous step
@ -100,8 +100,8 @@ import (
"github.com/go-jet/jet/examples/quick-start/gen/jetdb/dvds/model" "github.com/go-jet/jet/examples/quick-start/gen/jetdb/dvds/model"
) )
``` ```
Lets say we want to retrieve the list of all actors that acted in films longer than 180 minutes, film language is 'English' Lets say we want to retrieve the list of all _actors_ that acted in _films_ longer than 180 minutes, _film language_ is 'English'
and film category is not 'Action'. and _film category_ is not 'Action'.
```go ```go
stmt := SELECT( stmt := SELECT(
Actor.ActorID, Actor.FirstName, Actor.LastName, Actor.LastUpdate, // list of all actor columns (equivalent to Actor.AllColumns) Actor.ActorID, Actor.FirstName, Actor.LastName, Actor.LastUpdate, // list of all actor columns (equivalent to Actor.AllColumns)
@ -217,7 +217,9 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
``` ```
</details> </details>
Well formed sql is just a first half the job. Now lets execute sql statement and store result in desired structure. Well formed sql is just a first half the job.
##### Execute query and store result
Let's say this is our desired structure: Let's say this is our desired structure:
```go ```go
var dest []struct { var dest []struct {
@ -445,7 +447,7 @@ Writing code is much faster and code is more robust. Automatic scan to arbitrary
boilerplate code needed to structure database query result. boilerplate code needed to structure database query result.
With Jet programmer have the power of SQL but also ease of use of NoSQL. With Jet programmer have the power of SQL but also ease of use of NoSQL.
##### Speed of execution ##### Speed of execution
Common web and database server usually is not on the same physical machine, and there is some latency between them. Common web and database server usually are not on the same physical machine, and there is some latency between them.
Latency can vary from 5ms to 50+ms. In majority of cases query executed on database is simple query lasting no more than 1ms. Latency can vary from 5ms to 50+ms. In majority of cases query executed on database is simple query lasting no more than 1ms.
In those cases web server handler execution time is directly proportional to latency between server and database. In those cases web server handler execution time is directly proportional to latency between server and database.
This is not such a big problem if handler calls database couple of times, but what if web server is using ORM to retrieve all data from database. This is not such a big problem if handler calls database couple of times, but what if web server is using ORM to retrieve all data from database.

View file

@ -5,11 +5,12 @@ import (
"fmt" "fmt"
"github.com/go-jet/jet/generator/postgres" "github.com/go-jet/jet/generator/postgres"
"os" "os"
"strconv"
) )
var ( var (
host string host string
port string port int
user string user string
password string password string
sslmode string sslmode string
@ -22,31 +23,54 @@ var (
func init() { func init() {
flag.StringVar(&host, "host", "", "Database host path (Example: localhost)") flag.StringVar(&host, "host", "", "Database host path (Example: localhost)")
flag.StringVar(&port, "port", "", "Database port") flag.IntVar(&port, "port", 0, "Database port")
flag.StringVar(&user, "user", "", "Database user") flag.StringVar(&user, "user", "", "Database user")
flag.StringVar(&password, "password", "", "The users password") flag.StringVar(&password, "password", "", "The users password")
flag.StringVar(&sslmode, "sslmode", "disable", "Whether or not to use SSL(optional)") flag.StringVar(&sslmode, "sslmode", "disable", "Whether or not to use SSL(optional)")
flag.StringVar(&params, "params", "", "Additional connection string parameters(optional)") flag.StringVar(&params, "params", "", "Additional connection string parameters(optional)")
flag.StringVar(&dbName, "dbname", "", "name of the database") flag.StringVar(&dbName, "dbname", "", "name of the database")
flag.StringVar(&schemaName, "schema", "public", "Database schema name.") flag.StringVar(&schemaName, "schema", "public", "Database schema name.")
flag.StringVar(&destDir, "path", "", "Destination dir for generated files.") flag.StringVar(&destDir, "path", "", "Destination dir for files generated.")
flag.Parse()
} }
func main() { func main() {
if host == "" || port == "" || user == "" || dbName == "" || schemaName == "" { flag.Usage = func() {
fmt.Println("jet: required flag missing") _, _ = fmt.Fprint(os.Stdout, `
Usage of jet:
-host string
Database host path (Example: localhost)
-port int
Database port
-user string
Database user
-password string
The users password
-dbname string
name of the database
-params string
Additional connection string parameters(optional)
-schema string
Database schema name. (default "public")
-sslmode string
Whether or not to use SSL(optional) (default "disable")
-path string
Destination dir for files generated.
`)
}
flag.Parse()
if host == "" || port == 0 || user == "" || dbName == "" || schemaName == "" {
fmt.Println("\njet: required flag missing")
flag.Usage() flag.Usage()
os.Exit(-2) os.Exit(-2)
} }
genData := postgres.DBConnection{ genData := postgres.DBConnection{
Host: host, Host: host,
Port: port, Port: strconv.Itoa(port),
User: user, User: user,
Password: password, Password: password,
SslMode: sslmode, SslMode: sslmode,