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

30
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.
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:
$ go get -u github.com/go-jet/jet/v2
Jet generator can be installed in one of the following ways:
1) (Go1.16+) Install jet generator using go install:
go install github.com/go-jet/jet/v2/cmd/jet@latest
1. (Go1.16+) Install jet generator using go install:
go install github.com/go-jet/jet/v2/cmd/jet@latest
2) Install jet generator to GOPATH/bin folder:
cd $GOPATH/src/ && GO111MODULE=off go get -u github.com/go-jet/jet/cmd/jet
2. Install jet generator to GOPATH/bin folder:
cd $GOPATH/src/ && GO111MODULE=off go get -u github.com/go-jet/jet/cmd/jet
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
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
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
and Model files. File generation is very fast, and can be added as every pre-build step.
Sample command:
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:
import . "some_path/.gen/jetdb/dvds/table"
import "some_path/.gen/jetdb/dvds/model"
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.
Then we can write the SQL query:
// sub-query
rRatingFilms :=
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:
var dest []struct {
model.Film
@ -81,9 +83,11 @@ Now we can run the statement and store the result into desired destination:
err := stmt.Query(db, &dest)
We can print a statement to see SQL query and arguments sent to postgres server:
fmt.Println(stmt.Sql())
Output:
SELECT "rFilms"."film.film_id" AS "film.film_id",
"rFilms"."film.title" AS "film.title",
"rFilms"."film.rating" AS "film.rating",

View file

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

View file

@ -4,10 +4,10 @@ package jet
type ColumnList []ColumnExpression
// SET creates column assigment for each column in column list. expression should be created by ROW function
// Link.UPDATE().
// SET(Link.MutableColumns.SET(ROW(String("github.com"), Bool(false))).
// WHERE(Link.ID.EQ(Int(0)))
//
// Link.UPDATE().
// SET(Link.MutableColumns.SET(ROW(String("github.com"), Bool(false))).
// WHERE(Link.ID.EQ(Int(0)))
func (cl ColumnList) SET(expression Expression) ColumnAssigment {
return columnAssigmentImpl{
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
// Address.AllColumns.Except(Address.PostalCode, Address.Phone)
//
// Address.AllColumns.Except(Address.PostalCode, Address.Phone)
func (cl ColumnList) Except(excludedColumns ...Column) ColumnList {
excludedColumnList := UnwidColumnList(excludedColumns)
excludedColumnNames := map[string]bool{}

View file

@ -1,6 +1,6 @@
package jet
//FloatExpression is interface for SQL float columns
// FloatExpression is interface for SQL float columns
type FloatExpression interface {
Expression
numericExpression

View file

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

View file

@ -118,7 +118,7 @@ func Uint64(value uint64) IntegerExpression {
return intLiteral(value)
}
//---------------------------------------------------//
// ---------------------------------------------------//
type boolLiteralExpression struct {
boolInterfaceImpl
literalExpressionImpl
@ -134,7 +134,7 @@ func Bool(value bool) BoolExpression {
return &boolLiteralExpression
}
//---------------------------------------------------//
// ---------------------------------------------------//
type floatLiteral struct {
floatInterfaceImpl
literalExpressionImpl
@ -160,7 +160,7 @@ func Decimal(value string) FloatExpression {
return &floatLiteral
}
//---------------------------------------------------//
// ---------------------------------------------------//
type stringLiteral struct {
stringInterfaceImpl
literalExpressionImpl
@ -351,7 +351,7 @@ func (n *nullLiteral) serialize(statement StatementType, out *SQLBuilder, option
out.WriteString("NULL")
}
//--------------------------------------------------//
// --------------------------------------------------//
type starLiteral struct {
ExpressionInterfaceImpl
}

View file

@ -7,7 +7,7 @@ import (
"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 {
// Sql returns parametrized sql query with list of arguments.
Sql() (query string, args []interface{})

View file

@ -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]))
}
//---------------------------------------------------//
// ---------------------------------------------------//
func newBinaryStringOperatorExpression(lhs, rhs Expression, operator string) StringExpression {
return StringExp(NewBinaryOperatorExpression(lhs, rhs, operator))
}

View file

@ -68,7 +68,7 @@ func (c *castImpl) AS_CHAR(length ...int) StringExpression {
return StringExp(c.AS("CHAR"))
}
// AS_DATE casts expression AS DATE type
// AS_DATE casts expression AS DATE type
func (c *castImpl) AS_DATE() DateExpression {
return DateExp(c.AS("DATE"))
}

View file

@ -225,7 +225,8 @@ var REGEXP_LIKE = jet.REGEXP_LIKE
//----------------- Date/Time Functions and Operators ------------//
// 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 {
return IntExp(jet.EXTRACT(string(field), from))
}

View file

@ -39,10 +39,11 @@ const (
type Interval = jet.Interval
// INTERVAL creates new temporal interval.
// In a case of MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR unit type
// value parameter has to be a number.
//
// In a case of MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR unit type
// value parameter has to be a number.
// INTERVAL(1, DAY)
// In a case of other unit types, value should be string with appropriate format.
// In a case of other unit types, value should be string with appropriate format.
// INTERVAL("10:08:50", HOUR_SECOND)
func INTERVAL(value interface{}, unitType unitType) Interval {
switch unitType {

View file

@ -18,7 +18,7 @@ type NumericExpression = jet.NumericExpression
// IntegerExpression interface
type IntegerExpression = jet.IntegerExpression
//FloatExpression is interface
// FloatExpression is interface
type FloatExpression = jet.FloatExpression
// TimeExpression interface

View file

@ -296,7 +296,8 @@ const (
)
// 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 {
return FloatExp(jet.EXTRACT(unitToString(field), from))
}

View file

@ -120,7 +120,8 @@ type intervalExpression struct {
}
// 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 {
quantityAndUnitLen := len(quantityAndUnit)
if quantityAndUnitLen == 0 || quantityAndUnitLen%2 != 0 {

View file

@ -65,7 +65,7 @@ type SelectStatement interface {
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 {
return newSelectStatement(nil, append([]Projection{projection}, projections...))
}

View file

@ -51,7 +51,7 @@ type ColumnDateTime = jet.ColumnTimestamp
// DateTimeColumn creates named timestamp column
var DateTimeColumn = jet.TimestampColumn
//ColumnTimestamp is interface of SQL timestamp columns.
// ColumnTimestamp is interface of SQL timestamp columns.
type ColumnTimestamp = jet.ColumnTimestamp
// TimestampColumn creates named timestamp column

View file

@ -58,7 +58,7 @@ type SelectStatement interface {
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 {
return newSelectStatement(nil, append([]Projection{projection}, projections...))
}