Go fmt.
This commit is contained in:
parent
4e1ff65023
commit
a2ea1892e5
17 changed files with 44 additions and 43 deletions
22
doc.go
22
doc.go
|
|
@ -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",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package jet
|
package jet
|
||||||
|
|
||||||
//BoolExpression interface
|
// BoolExpression interface
|
||||||
type BoolExpression interface {
|
type BoolExpression interface {
|
||||||
Expression
|
Expression
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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{}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package jet
|
package jet
|
||||||
|
|
||||||
//FloatExpression is interface for SQL float columns
|
// FloatExpression is interface for SQL float columns
|
||||||
type FloatExpression interface {
|
type FloatExpression interface {
|
||||||
Expression
|
Expression
|
||||||
numericExpression
|
numericExpression
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ func Uint64(value uint64) IntegerExpression {
|
||||||
return intLiteral(value)
|
return intLiteral(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
// ---------------------------------------------------//
|
||||||
type boolLiteralExpression struct {
|
type boolLiteralExpression struct {
|
||||||
boolInterfaceImpl
|
boolInterfaceImpl
|
||||||
literalExpressionImpl
|
literalExpressionImpl
|
||||||
|
|
@ -134,7 +134,7 @@ func Bool(value bool) BoolExpression {
|
||||||
return &boolLiteralExpression
|
return &boolLiteralExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
// ---------------------------------------------------//
|
||||||
type floatLiteral struct {
|
type floatLiteral struct {
|
||||||
floatInterfaceImpl
|
floatInterfaceImpl
|
||||||
literalExpressionImpl
|
literalExpressionImpl
|
||||||
|
|
@ -160,7 +160,7 @@ func Decimal(value string) FloatExpression {
|
||||||
return &floatLiteral
|
return &floatLiteral
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
// ---------------------------------------------------//
|
||||||
type stringLiteral struct {
|
type stringLiteral struct {
|
||||||
stringInterfaceImpl
|
stringInterfaceImpl
|
||||||
literalExpressionImpl
|
literalExpressionImpl
|
||||||
|
|
@ -351,7 +351,7 @@ func (n *nullLiteral) serialize(statement StatementType, out *SQLBuilder, option
|
||||||
out.WriteString("NULL")
|
out.WriteString("NULL")
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------//
|
// --------------------------------------------------//
|
||||||
type starLiteral struct {
|
type starLiteral struct {
|
||||||
ExpressionInterfaceImpl
|
ExpressionInterfaceImpl
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK)
|
// Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK)
|
||||||
type Statement interface {
|
type Statement interface {
|
||||||
// Sql returns parametrized sql query with list of arguments.
|
// Sql returns parametrized sql query with list of arguments.
|
||||||
Sql() (query string, args []interface{})
|
Sql() (query string, args []interface{})
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ func (s *stringInterfaceImpl) NOT_REGEXP_LIKE(pattern StringExpression, caseSens
|
||||||
return newBinaryBoolOperatorExpression(s.parent, pattern, StringNotRegexpLikeOperator, Bool(len(caseSensitive) > 0 && caseSensitive[0]))
|
return newBinaryBoolOperatorExpression(s.parent, pattern, StringNotRegexpLikeOperator, Bool(len(caseSensitive) > 0 && caseSensitive[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
// ---------------------------------------------------//
|
||||||
func newBinaryStringOperatorExpression(lhs, rhs Expression, operator string) StringExpression {
|
func newBinaryStringOperatorExpression(lhs, rhs Expression, operator string) StringExpression {
|
||||||
return StringExp(NewBinaryOperatorExpression(lhs, rhs, operator))
|
return StringExp(NewBinaryOperatorExpression(lhs, rhs, operator))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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))
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ type NumericExpression = jet.NumericExpression
|
||||||
// IntegerExpression interface
|
// IntegerExpression interface
|
||||||
type IntegerExpression = jet.IntegerExpression
|
type IntegerExpression = jet.IntegerExpression
|
||||||
|
|
||||||
//FloatExpression is interface
|
// FloatExpression is interface
|
||||||
type FloatExpression = jet.FloatExpression
|
type FloatExpression = jet.FloatExpression
|
||||||
|
|
||||||
// TimeExpression interface
|
// TimeExpression interface
|
||||||
|
|
|
||||||
|
|
@ -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))
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ type SelectStatement interface {
|
||||||
AsTable(alias string) SelectTable
|
AsTable(alias string) SelectTable
|
||||||
}
|
}
|
||||||
|
|
||||||
//SELECT creates new SelectStatement with list of projections
|
// SELECT creates new SelectStatement with list of projections
|
||||||
func SELECT(projection Projection, projections ...Projection) SelectStatement {
|
func SELECT(projection Projection, projections ...Projection) SelectStatement {
|
||||||
return newSelectStatement(nil, append([]Projection{projection}, projections...))
|
return newSelectStatement(nil, append([]Projection{projection}, projections...))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ type ColumnDateTime = jet.ColumnTimestamp
|
||||||
// DateTimeColumn creates named timestamp column
|
// DateTimeColumn creates named timestamp column
|
||||||
var DateTimeColumn = jet.TimestampColumn
|
var DateTimeColumn = jet.TimestampColumn
|
||||||
|
|
||||||
//ColumnTimestamp is interface of SQL timestamp columns.
|
// ColumnTimestamp is interface of SQL timestamp columns.
|
||||||
type ColumnTimestamp = jet.ColumnTimestamp
|
type ColumnTimestamp = jet.ColumnTimestamp
|
||||||
|
|
||||||
// TimestampColumn creates named timestamp column
|
// TimestampColumn creates named timestamp column
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ type SelectStatement interface {
|
||||||
AsTable(alias string) SelectTable
|
AsTable(alias string) SelectTable
|
||||||
}
|
}
|
||||||
|
|
||||||
//SELECT creates new SelectStatement with list of projections
|
// SELECT creates new SelectStatement with list of projections
|
||||||
func SELECT(projection Projection, projections ...Projection) SelectStatement {
|
func SELECT(projection Projection, projections ...Projection) SelectStatement {
|
||||||
return newSelectStatement(nil, append([]Projection{projection}, projections...))
|
return newSelectStatement(nil, append([]Projection{projection}, projections...))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue