jet/doc.go

161 lines
4.2 KiB
Go
Raw Normal View History

2019-07-16 12:17:27 +02:00
/*
2021-12-24 17:13:13 +01:00
Package jet is a complete solution for efficient and high performance database access, consisting of type-safe SQL builder
with code generation and automatic query result data mapping.
Jet currently supports PostgreSQL, MySQL, MariaDB and SQLite. Future releases will add support for additional databases.
2019-08-20 09:45:56 +02:00
2022-08-23 12:38:16 +02:00
# Installation
2019-08-20 09:45:56 +02:00
2021-12-24 17:13:13 +01:00
Use the command bellow to add jet as a dependency into go.mod project:
2022-08-23 12:38:16 +02:00
2021-12-24 17:13:13 +01:00
$ go get -u github.com/go-jet/jet/v2
2020-06-27 18:48:19 +02:00
2021-12-24 17:13:13 +01:00
Jet generator can be installed in one of the following ways:
2019-08-20 09:45:56 +02:00
2022-08-23 12:38:16 +02:00
1. (Go1.16+) Install jet generator using go install:
go install github.com/go-jet/jet/v2/cmd/jet@latest
2021-12-24 17:13:13 +01:00
2022-08-23 12:38:16 +02:00
2. Install jet generator to GOPATH/bin folder:
cd $GOPATH/src/ && GO111MODULE=off go get -u github.com/go-jet/jet/cmd/jet
2021-12-24 17:13:13 +01:00
2022-08-23 12:38:16 +02:00
3. Install jet generator into specific folder:
git clone https://github.com/go-jet/jet.git
cd jet && go build -o dir_path ./cmd/jet
2021-12-24 17:13:13 +01:00
Make sure that the destination folder is added to the PATH environment variable.
2019-08-20 09:45:56 +02:00
2022-08-23 12:38:16 +02:00
# Usage
2021-12-24 17:13:13 +01:00
2019-08-20 09:45:56 +02:00
Jet requires already defined database schema(with tables, enums etc), so that jet generator can generate SQL Builder
and Model files. File generation is very fast, and can be added as every pre-build step.
Sample command:
2022-08-23 12:38:16 +02:00
2021-12-24 17:13:13 +01:00
jet -dsn=postgresql://user:pass@localhost:5432/jetdb -schema=dvds -path=./.gen
2019-08-20 09:45:56 +02:00
2021-12-24 17:13:13 +01:00
Before we can write SQL queries in Go, we need to import generated SQL builder and model types:
2022-08-23 12:38:16 +02:00
2019-08-20 09:45:56 +02:00
import . "some_path/.gen/jetdb/dvds/table"
import "some_path/.gen/jetdb/dvds/model"
2021-12-24 17:13:13 +01:00
To write postgres SQL queries we import:
2022-08-23 12:38:16 +02:00
2021-12-24 17:13:13 +01:00
. "github.com/go-jet/jet/v2/postgres" // Dot import is used so that Go code resemble as much as native SQL. It is not mandatory.
2019-08-20 09:45:56 +02:00
2021-12-24 17:13:13 +01:00
Then we can write the SQL query:
2022-08-23 12:38:16 +02:00
2019-08-20 09:45:56 +02:00
// sub-query
2021-12-24 17:13:13 +01:00
rRatingFilms :=
SELECT(
Film.FilmID,
Film.Title,
Film.Rating,
).FROM(
Film,
).WHERE(
Film.Rating.EQ(enum.FilmRating.R),
).AsTable("rFilms")
2019-08-20 09:45:56 +02:00
// export column from sub-query
rFilmID := Film.FilmID.From(rRatingFilms)
// main-query
2021-12-24 17:13:13 +01:00
stmt :=
SELECT(
2019-08-20 09:45:56 +02:00
Actor.AllColumns,
FilmActor.AllColumns,
rRatingFilms.AllColumns(),
2021-12-24 17:13:13 +01:00
).FROM(
2019-08-20 09:45:56 +02:00
rRatingFilms.
2021-12-24 17:13:13 +01:00
INNER_JOIN(FilmActor, FilmActor.FilmID.EQ(rFilmID)).
INNER_JOIN(Actor, Actor.ActorID.EQ(FilmActor.ActorID)
).ORDER_BY(
rFilmID,
Actor.ActorID,
)
Now we can run the statement and store the result into desired destination:
2022-08-23 12:38:16 +02:00
2019-08-20 09:45:56 +02:00
var dest []struct {
model.Film
Actors []model.Actor
}
2021-12-24 17:13:13 +01:00
err := stmt.Query(db, &dest)
We can print a statement to see SQL query and arguments sent to postgres server:
2022-08-23 12:38:16 +02:00
2021-12-24 17:13:13 +01:00
fmt.Println(stmt.Sql())
Output:
2022-08-23 12:38:16 +02:00
2021-12-24 17:13:13 +01:00
SELECT "rFilms"."film.film_id" AS "film.film_id",
"rFilms"."film.title" AS "film.title",
"rFilms"."film.rating" AS "film.rating",
actor.actor_id AS "actor.actor_id",
actor.first_name AS "actor.first_name",
actor.last_name AS "actor.last_name",
actor.last_update AS "actor.last_update",
film_actor.actor_id AS "film_actor.actor_id",
film_actor.film_id AS "film_actor.film_id",
film_actor.last_update AS "film_actor.last_update"
FROM (
SELECT film.film_id AS "film.film_id",
film.title AS "film.title",
film.rating AS "film.rating"
FROM dvds.film
WHERE film.rating = 'R'
) AS "rFilms"
INNER JOIN dvds.film_actor ON (film_actor.film_id = "rFilms"."film.film_id")
INNER JOIN dvds.actor ON (film_actor.actor_id = actor.actor_id)
WHERE "rFilms"."film.film_id" < $1
ORDER BY "rFilms"."film.film_id" ASC, actor.actor_id ASC;
[50]
If we print destination as json, we'll get:
[
{
"FilmID": 8,
"Title": "Airport Pollock",
"Rating": "R",
"Actors": [
{
"ActorID": 55,
"FirstName": "Fay",
"LastName": "Kilmer",
"LastUpdate": "2013-05-26T14:47:57.62Z"
},
{
"ActorID": 96,
"FirstName": "Gene",
"LastName": "Willis",
"LastUpdate": "2013-05-26T14:47:57.62Z"
},
...
]
},
{
"FilmID": 17,
"Title": "Alone Trip",
"Actors": [
{
"ActorID": 3,
"FirstName": "Ed",
"LastName": "Chase",
"LastUpdate": "2013-05-26T14:47:57.62Z"
},
{
"ActorID": 12,
"FirstName": "Karl",
"LastName": "Berry",
"LastUpdate": "2013-05-26T14:47:57.62Z"
},
...
...
]
Detail info about all statements, features and use cases can be
2019-08-20 09:45:56 +02:00
found at project wiki page - https://github.com/go-jet/jet/wiki.
2019-07-16 12:17:27 +02:00
*/
2019-06-21 13:56:57 +02:00
package jet