This commit is contained in:
go-jet 2022-08-23 12:38:16 +02:00
parent 4e1ff65023
commit a2ea1892e5
17 changed files with 44 additions and 43 deletions

22
doc.go
View file

@ -3,44 +3,45 @@ Package jet is a complete solution for efficient and high performance database a
with code generation and automatic query result data mapping. 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. Jet currently supports PostgreSQL, MySQL, MariaDB and SQLite. Future releases will add support for additional databases.
# Installation
Installation
Use the command bellow to add jet as a dependency into go.mod project: Use the command bellow to add jet as a dependency into go.mod project:
$ go get -u github.com/go-jet/jet/v2 $ go get -u github.com/go-jet/jet/v2
Jet generator can be installed in one of the following ways: Jet generator can be installed in one of the following ways:
1) (Go1.16+) Install jet generator using go install: 1. (Go1.16+) Install jet generator using go install:
go install github.com/go-jet/jet/v2/cmd/jet@latest go install github.com/go-jet/jet/v2/cmd/jet@latest
2) Install jet generator to GOPATH/bin folder: 2. Install jet generator to GOPATH/bin folder:
cd $GOPATH/src/ && GO111MODULE=off go get -u github.com/go-jet/jet/cmd/jet cd $GOPATH/src/ && GO111MODULE=off go get -u github.com/go-jet/jet/cmd/jet
3) Install jet generator into specific folder: 3. Install jet generator into specific folder:
git clone https://github.com/go-jet/jet.git git clone https://github.com/go-jet/jet.git
cd jet && go build -o dir_path ./cmd/jet cd jet && go build -o dir_path ./cmd/jet
Make sure that the destination folder is added to the PATH environment variable. Make sure that the destination folder is added to the PATH environment variable.
# Usage
Usage
Jet requires already defined database schema(with tables, enums etc), so that jet generator can generate SQL Builder 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. and Model files. File generation is very fast, and can be added as every pre-build step.
Sample command: Sample command:
jet -dsn=postgresql://user:pass@localhost:5432/jetdb -schema=dvds -path=./.gen jet -dsn=postgresql://user:pass@localhost:5432/jetdb -schema=dvds -path=./.gen
Before we can write SQL queries in Go, we need to import generated SQL builder and model types: Before we can write SQL queries in Go, we need to import generated SQL builder and model types:
import . "some_path/.gen/jetdb/dvds/table" import . "some_path/.gen/jetdb/dvds/table"
import "some_path/.gen/jetdb/dvds/model" import "some_path/.gen/jetdb/dvds/model"
To write postgres SQL queries we import: To write postgres SQL queries we import:
. "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. . "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.
Then we can write the SQL query: Then we can write the SQL query:
// sub-query // sub-query
rRatingFilms := rRatingFilms :=
SELECT( SELECT(
@ -72,6 +73,7 @@ Then we can write the SQL query:
) )
Now we can run the statement and store the result into desired destination: Now we can run the statement and store the result into desired destination:
var dest []struct { var dest []struct {
model.Film model.Film
@ -81,9 +83,11 @@ Now we can run the statement and store the result into desired destination:
err := stmt.Query(db, &dest) err := stmt.Query(db, &dest)
We can print a statement to see SQL query and arguments sent to postgres server: We can print a statement to see SQL query and arguments sent to postgres server:
fmt.Println(stmt.Sql()) fmt.Println(stmt.Sql())
Output: Output:
SELECT "rFilms"."film.film_id" AS "film.film_id", SELECT "rFilms"."film.film_id" AS "film.film_id",
"rFilms"."film.title" AS "film.title", "rFilms"."film.title" AS "film.title",
"rFilms"."film.rating" AS "film.rating", "rFilms"."film.rating" AS "film.rating",

View file

@ -84,22 +84,18 @@ func (b *boolInterfaceImpl) IS_NOT_UNKNOWN() BoolExpression {
return newPostfixBoolOperatorExpression(b.parent, "IS NOT UNKNOWN") return newPostfixBoolOperatorExpression(b.parent, "IS NOT UNKNOWN")
} }
//---------------------------------------------------//
func newBinaryBoolOperatorExpression(lhs, rhs Expression, operator string, additionalParams ...Expression) BoolExpression { func newBinaryBoolOperatorExpression(lhs, rhs Expression, operator string, additionalParams ...Expression) BoolExpression {
return BoolExp(NewBinaryOperatorExpression(lhs, rhs, operator, additionalParams...)) return BoolExp(NewBinaryOperatorExpression(lhs, rhs, operator, additionalParams...))
} }
//---------------------------------------------------//
func newPrefixBoolOperatorExpression(expression Expression, operator string) BoolExpression { func newPrefixBoolOperatorExpression(expression Expression, operator string) BoolExpression {
return BoolExp(newPrefixOperatorExpression(expression, operator)) return BoolExp(newPrefixOperatorExpression(expression, operator))
} }
//---------------------------------------------------//
func newPostfixBoolOperatorExpression(expression Expression, operator string) BoolExpression { func newPostfixBoolOperatorExpression(expression Expression, operator string) BoolExpression {
return BoolExp(newPostfixOperatorExpression(expression, operator)) return BoolExp(newPostfixOperatorExpression(expression, operator))
} }
//---------------------------------------------------//
type boolExpressionWrapper struct { type boolExpressionWrapper struct {
boolInterfaceImpl boolInterfaceImpl
Expression Expression

View file

@ -4,10 +4,10 @@ package jet
type ColumnList []ColumnExpression type ColumnList []ColumnExpression
// SET creates column assigment for each column in column list. expression should be created by ROW function // SET creates column assigment for each column in column list. expression should be created by ROW function
//
// Link.UPDATE(). // Link.UPDATE().
// SET(Link.MutableColumns.SET(ROW(String("github.com"), Bool(false))). // SET(Link.MutableColumns.SET(ROW(String("github.com"), Bool(false))).
// WHERE(Link.ID.EQ(Int(0))) // WHERE(Link.ID.EQ(Int(0)))
//
func (cl ColumnList) SET(expression Expression) ColumnAssigment { func (cl ColumnList) SET(expression Expression) ColumnAssigment {
return columnAssigmentImpl{ return columnAssigmentImpl{
column: cl, column: cl,
@ -16,8 +16,8 @@ func (cl ColumnList) SET(expression Expression) ColumnAssigment {
} }
// Except will create new column list in which columns contained in list of excluded column names are removed // Except will create new column list in which columns contained in list of excluded column names are removed
// Address.AllColumns.Except(Address.PostalCode, Address.Phone)
// //
// Address.AllColumns.Except(Address.PostalCode, Address.Phone)
func (cl ColumnList) Except(excludedColumns ...Column) ColumnList { func (cl ColumnList) Except(excludedColumns ...Column) ColumnList {
excludedColumnList := UnwidColumnList(excludedColumns) excludedColumnList := UnwidColumnList(excludedColumns)
excludedColumnNames := map[string]bool{} excludedColumnNames := map[string]bool{}

View file

@ -120,17 +120,14 @@ func (i *integerInterfaceImpl) BIT_SHIFT_RIGHT(intExpression IntegerExpression)
return newBinaryIntegerOperatorExpression(i.parent, intExpression, ">>") return newBinaryIntegerOperatorExpression(i.parent, intExpression, ">>")
} }
//---------------------------------------------------//
func newBinaryIntegerOperatorExpression(lhs, rhs IntegerExpression, operator string) IntegerExpression { func newBinaryIntegerOperatorExpression(lhs, rhs IntegerExpression, operator string) IntegerExpression {
return IntExp(NewBinaryOperatorExpression(lhs, rhs, operator)) return IntExp(NewBinaryOperatorExpression(lhs, rhs, operator))
} }
//---------------------------------------------------//
func newPrefixIntegerOperatorExpression(expression IntegerExpression, operator string) IntegerExpression { func newPrefixIntegerOperatorExpression(expression IntegerExpression, operator string) IntegerExpression {
return IntExp(newPrefixOperatorExpression(expression, operator)) return IntExp(newPrefixOperatorExpression(expression, operator))
} }
//---------------------------------------------------//
type integerExpressionWrapper struct { type integerExpressionWrapper struct {
integerInterfaceImpl integerInterfaceImpl

View file

@ -225,6 +225,7 @@ var REGEXP_LIKE = jet.REGEXP_LIKE
//----------------- Date/Time Functions and Operators ------------// //----------------- Date/Time Functions and Operators ------------//
// EXTRACT function retrieves subfields such as year or hour from date/time values // EXTRACT function retrieves subfields such as year or hour from date/time values
//
// EXTRACT(DAY, User.CreatedAt) // EXTRACT(DAY, User.CreatedAt)
func EXTRACT(field unitType, from Expression) IntegerExpression { func EXTRACT(field unitType, from Expression) IntegerExpression {
return IntExp(jet.EXTRACT(string(field), from)) return IntExp(jet.EXTRACT(string(field), from))

View file

@ -39,6 +39,7 @@ const (
type Interval = jet.Interval type Interval = jet.Interval
// INTERVAL creates new temporal interval. // INTERVAL creates new temporal interval.
//
// In a case of MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR unit type // In a case of MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR unit type
// value parameter has to be a number. // value parameter has to be a number.
// INTERVAL(1, DAY) // INTERVAL(1, DAY)

View file

@ -296,6 +296,7 @@ const (
) )
// EXTRACT function retrieves subfields such as year or hour from date/time values // EXTRACT function retrieves subfields such as year or hour from date/time values
//
// EXTRACT(DAY, User.CreatedAt) // EXTRACT(DAY, User.CreatedAt)
func EXTRACT(field unit, from Expression) FloatExpression { func EXTRACT(field unit, from Expression) FloatExpression {
return FloatExp(jet.EXTRACT(unitToString(field), from)) return FloatExp(jet.EXTRACT(unitToString(field), from))

View file

@ -120,6 +120,7 @@ type intervalExpression struct {
} }
// INTERVAL creates new interval expression from the list of quantity-unit pairs. // INTERVAL creates new interval expression from the list of quantity-unit pairs.
//
// INTERVAL(1, DAY, 3, MINUTE) // INTERVAL(1, DAY, 3, MINUTE)
func INTERVAL(quantityAndUnit ...quantityAndUnit) IntervalExpression { func INTERVAL(quantityAndUnit ...quantityAndUnit) IntervalExpression {
quantityAndUnitLen := len(quantityAndUnit) quantityAndUnitLen := len(quantityAndUnit)