Skip to content

Commit

Permalink
Merge pull request #180 from go-jet/develop
Browse files Browse the repository at this point in the history
Release v.2.9.0
  • Loading branch information
go-jet authored Sep 30, 2022
2 parents 38b6caf + 59f9df9 commit 24857bc
Show file tree
Hide file tree
Showing 41 changed files with 692 additions and 183 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

[![CircleCI](https://circleci.com/gh/go-jet/jet/tree/master.svg?style=svg&circle-token=97f255c6a4a3ab6590ea2e9195eb3ebf9f97b4a7)](https://circleci.com/gh/go-jet/jet/tree/develop)
[![codecov](https://codecov.io/gh/go-jet/jet/branch/master/graph/badge.svg)](https://codecov.io/gh/go-jet/jet)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-jet/jet)](https://goreportcard.com/report/github.com/go-jet/jet)
[![Documentation](https://godoc.org/github.com/go-jet/jet?status.svg)](http://godoc.org/github.com/go-jet/jet)
[![GitHub release](https://img.shields.io/github/release/go-jet/jet.svg)](https://github.com/go-jet/jet/v2/releases)
[![Gitter](https://badges.gitter.im/go-jet/community.svg)](https://gitter.im/go-jet/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-jet/jet)](https://goreportcard.com/report/github.com/go-jet/jet/v2)
[![Documentation](https://godoc.org/github.com/go-jet/jet?status.svg)](http://godoc.org/github.com/go-jet/jet/v2)
[![GitHub release](https://img.shields.io/github/release/go-jet/jet.svg)](https://github.com/go-jet/jet/releases)

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.
Expand Down
30 changes: 17 additions & 13 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down
78 changes: 2 additions & 76 deletions generator/template/file_templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 19 additions & 15 deletions generator/template/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,29 @@ func processTableSQLBuilder(fileTypes, dirPath string,

for _, tableMetaData := range tablesMetaData {

var tableSQLBuilderTemplate TableSQLBuilder
var tableSQLBuilder TableSQLBuilder

if fileTypes == "view" {
tableSQLBuilderTemplate = sqlBuilderTemplate.View(tableMetaData)
tableSQLBuilder = sqlBuilderTemplate.View(tableMetaData)
} else {
tableSQLBuilderTemplate = sqlBuilderTemplate.Table(tableMetaData)
tableSQLBuilder = sqlBuilderTemplate.Table(tableMetaData)
}

if tableSQLBuilderTemplate.Skip {
if tableSQLBuilder.Skip {
continue
}

tableSQLBuilderPath := path.Join(dirPath, tableSQLBuilderTemplate.Path)
tableSQLBuilderPath := path.Join(dirPath, tableSQLBuilder.Path)

err := utils.EnsureDirPath(tableSQLBuilderPath)
throw.OnError(err)

text, err := generateTemplate(
autoGenWarningTemplate+getTableSQLBuilderTemplate(dialect),
autoGenWarningTemplate+tableSQLBuilderTemplate,
tableMetaData,
template.FuncMap{
"package": func() string {
return tableSQLBuilderTemplate.PackageName()
return tableSQLBuilder.PackageName()
},
"dialect": func() jet.Dialect {
return dialect
Expand All @@ -151,29 +151,33 @@ func processTableSQLBuilder(fileTypes, dirPath string,
return schemaMetaData.Name
},
"tableTemplate": func() TableSQLBuilder {
return tableSQLBuilderTemplate
return tableSQLBuilder
},
"structImplName": func() string { // postgres only
structName := tableSQLBuilderTemplate.TypeName
structName := tableSQLBuilder.TypeName
return string(strings.ToLower(structName)[0]) + structName[1:]
},
"columnField": func(columnMetaData metadata.Column) TableSQLBuilderColumn {
return tableSQLBuilderTemplate.Column(columnMetaData)
return tableSQLBuilder.Column(columnMetaData)
},
"toUpper": strings.ToUpper,
"insertedRowAlias": func() string {
return insertedRowAlias(dialect)
},
})
throw.OnError(err)

err = utils.SaveGoFile(tableSQLBuilderPath, tableSQLBuilderTemplate.FileName, text)
err = utils.SaveGoFile(tableSQLBuilderPath, tableSQLBuilder.FileName, text)
throw.OnError(err)
}
}

func getTableSQLBuilderTemplate(dialect jet.Dialect) string {
if dialect.Name() == "PostgreSQL" || dialect.Name() == "SQLite" {
return tableSQLBuilderTemplateWithEXCLUDED
func insertedRowAlias(dialect jet.Dialect) string {
if dialect.Name() == "MySQL" {
return "new"
}

return tableSQLBuilderTemplate
return "excluded"
}

func processTableModels(fileTypes, modelDirPath string, tablesMetaData []metadata.Table, modelTemplate Model) {
Expand Down
6 changes: 1 addition & 5 deletions internal/jet/bool_expression.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jet

//BoolExpression interface
// BoolExpression interface
type BoolExpression interface {
Expression

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 24857bc

Please sign in to comment.