Add SQL-Builder.md wiki page.

This commit is contained in:
go-jet 2019-07-04 19:10:31 +02:00
parent 55e8c3bbb1
commit 1ab3ee4be6
5 changed files with 128 additions and 93 deletions

View file

@ -97,87 +97,5 @@ Generated files folder structure will look like this:
```
Table and enums from database schema are used as a template to generate two types of Go files:
* SQL builder files: Files used to write type safe SQL statements in Go (enum and table package)
* Model files: Files used to store result from database queries (model package)
#### SQL builder files
Part of the sample sql builder file for table `film`.
```
package table
import (
"github.com/go-jet/jet"
)
var Film = newFilmTable()
type FilmTable struct {
jet.Table
//Columns
FilmID jet.ColumnInteger
Title jet.ColumnString
Description jet.ColumnString
ReleaseYear jet.ColumnInteger
LanguageID jet.ColumnInteger
RentalDuration jet.ColumnInteger
RentalRate jet.ColumnFloat
Length jet.ColumnInteger
ReplacementCost jet.ColumnFloat
Rating jet.ColumnString
LastUpdate jet.ColumnTimestamp
SpecialFeatures jet.ColumnString
Fulltext jet.ColumnString
AllColumns jet.ColumnList
}
// creates new FilmTable with assigned alias
func (a *FilmTable) AS(alias string) *FilmTable {
aliasTable := newFilmTable()
aliasTable.Table.AS(alias)
return aliasTable
}
```
Table name and column names are camelized inside sql builder type.
`AllColumns` is used as shorthand notation for list of all columns `FilmID, Title, Description,...`.
Mappings of database types to sql builder column types are following:
| Database type(postgres) | Sql builder column type |
| ----------------------------------------------- | -------------------------------------------------- |
| boolean | ColumnBool |
| smallint, integer, bigint | ColumnInteger |
| real, numeric, decimal, double precision | ColumnFloat |
| date | ColumnDate |
| timestamp without time zone | ColumnTimestamp |
| timestamp with time zone | ColumnTimestampz |
| time without time zone | ColumnTime |
| time with time zone | ColumnTimez |
| enums, text, character, character varying | |
| bytea, uuid | |
| and all remaining types | ColumnString |
Sql builder file for enum type `mpaa_rating`:
```
package enum
import "github.com/go-jet/jet"
var MpaaRating = &struct {
G jet.StringExpression
PG jet.StringExpression
PG13 jet.StringExpression
R jet.StringExpression
NC17 jet.StringExpression
}{
G: jet.NewEnumValue("G"),
PG: jet.NewEnumValue("PG"),
PG13: jet.NewEnumValue("PG-13"),
R: jet.NewEnumValue("R"),
NC17: jet.NewEnumValue("NC-17"),
}
```
* SQL Builder files - used to write type safe SQL statements in Go (`enum` and `table` package)
* Model files - used to store result from database queries (`model` package)