Add wiki Expressions page.

This commit is contained in:
go-jet 2019-06-27 19:55:21 +02:00
parent 3516d2861c
commit 6a6ee0561d
17 changed files with 399 additions and 109 deletions

229
wiki/Expressions.md Normal file
View file

@ -0,0 +1,229 @@
Jet sql builder supports following expression types:
- Bool expressions
- Integer expressions
- Float expressions
- String expressions
- Date expressions
- Time expressions
- Timez expressions (with time zone)
- Timestamp expressions
- Timestampz expressions (with time zone)
_This list might be extended with feature Jet releases._
### Literal type
For every expression type there is a method to create one literal type expressions.
Literal type examples:
```
Bool(true)
Integer(11)
Float(23.44)
String("John Doe")
Date(2010, 12, 3)
Time(23, 6, 6, 1)
Timez(23, 6, 6, 222, +200)
Timestamp(2010, 10, 21, 15, 30, 12, 333)
Timestampz(2010, 10, 21, 15, 30, 12, 444, 0)
NULL
STAR (alias for *)
```
### Column types
Every sql builder table column belongs to one expression type. There are following column types:
```
ColumnBool
ColumnInteger
ColumnFloat
ColumnString
ColumnDate
ColumnTime
ColumnTimez
ColumnTimestamp
ColumnTimestampz
```
Columns and literals can form arbitrary expressions but have to follow valid SQL expression syntax.
For instance valid expressions are:
```
Bool(true).AND(Bool(false)).IS_FALSE()
(Film.Length.GT(Int(100)).AND(Film.Length.LT(Int(200))).IS_TRUE() // table 'film', integer column 'length'
```
Some of the invalid expressions. These expressions will cause go build to break.
```
Bool(true).AND(Int(11)) // can't compare bool and
Int(11).ADD(Float(22.2)) // can't add integer and floats, but
// using cast it is possible: Int(11).TO_FLOAT().ADD(Float(22.2))
```
## Common expression operators
```go
type Expression interface {
clause
projection
groupByClause
OrderByClause
// Test expression whether it is a NULL value.
IS_NULL() BoolExpression
// Test expression whether it is a non-NULL value.
IS_NOT_NULL() BoolExpression
// Check if this expressions matches any in expressions list
IN(expressions ...Expression) BoolExpression
// Check if this expressions is different of all expressions in expressions list
NOT_IN(expressions ...Expression) BoolExpression
// The temporary alias name to assign to the expression
AS(alias string) projection
// Expression will be used to sort query result in ascending order
ASC() OrderByClause
// Expression will be used to sort query result in ascending order
DESC() OrderByClause
// Cast expression to dbType
TO(dbType string) Expression
// Cast expression to bool type
TO_BOOL() BoolExpression
// Cast expression to smallint type
TO_SMALLINT() IntegerExpression
// Cast expression to integer type
TO_INTEGER() IntegerExpression
// Cast expression to bigint type
TO_BIGINT() IntegerExpression
// Cast expression to numeric type, using precision and optionally scale
TO_NUMERIC(precision int, scale ...int) FloatExpression
// Cast expression to real type
TO_REAL() FloatExpression
// Cast expression to double precision type
TO_DOUBLE() FloatExpression
// Cast expression to text type
TO_TEXT() StringExpression
// Cast expression to date type
TO_DATE() DateExpression
// Cast expression to time type
TO_TIME() TimeExpression
// Cast expression to time with time timezone type
TO_TIMEZ() TimezExpression
// Cast expression to timestamp type
TO_TIMESTAMP() TimestampExpression
// Cast expression to timestamp with timezone type
TO_TIMESTAMPZ() TimestampzExpression
}
```
Examples:
```go
Film.Description.IS_NULL()
Int(1).ADD(Int(2)).AS("1+2")
(Film.Duration.ADD(Int(3))).ASC()
String("1999-01-08 13:05:06 +0100 CET").TO_TIMESTAMPZ()
```
### Bool expression operators:
```go
type BoolExpression interface {
Expression
// Check if this expression is equal to rhs
EQ(rhs BoolExpression) BoolExpression
// Check if this expression is not equal to rhs
NOT_EQ(rhs BoolExpression) BoolExpression
// Check if this expression is distinct to rhs
IS_DISTINCT_FROM(rhs BoolExpression) BoolExpression
// Check if this expression is not distinct to rhs
IS_NOT_DISTINCT_FROM(rhs BoolExpression) BoolExpression
// Check if this expression is true
IS_TRUE() BoolExpression
// Check if this expression is not true
IS_NOT_TRUE() BoolExpression
// Check if this expression is false
IS_FALSE() BoolExpression
// Check if this expression is not false
IS_NOT_FALSE() BoolExpression
// Check if this expression is unknown
IS_UNKNOWN() BoolExpression
// Check if this expression is not unknown
IS_NOT_UNKNOWN() BoolExpression
// expression AND operator rhs
AND(rhs BoolExpression) BoolExpression
// expression OR operator rhs
OR(rhs BoolExpression) BoolExpression
}
```
Examples:
```
Staff.Active.EQ(Bool(true))
Staff.Active.IS_TRUE()
Bool(true).AND(Staff.Active).OR(Bool(false))
```
### Integer expression operators
```go
type IntegerExpression interface {
Expression
numericExpression
// Check if expression is equal to rhs
EQ(rhs IntegerExpression) BoolExpression
// Check if expression is not equal to rhs
NOT_EQ(rhs IntegerExpression) BoolExpression
// Check if expression is distinct from rhs
IS_DISTINCT_FROM(rhs IntegerExpression) BoolExpression
// Check if expression is not distinct from rhs
IS_NOT_DISTINCT_FROM(rhs IntegerExpression) BoolExpression
// Check if expression is less then rhs
LT(rhs IntegerExpression) BoolExpression
// Check if expression is less then equal rhs
LT_EQ(rhs IntegerExpression) BoolExpression
// Check if expression is greater then rhs
GT(rhs IntegerExpression) BoolExpression
// Check if expression is greater then equal rhs
GT_EQ(rhs IntegerExpression) BoolExpression
// expression + rhs
ADD(rhs IntegerExpression) IntegerExpression
// expression - rhs
SUB(rhs IntegerExpression) IntegerExpression
// expression * rhs
MUL(rhs IntegerExpression) IntegerExpression
// expression / rhs
DIV(rhs IntegerExpression) IntegerExpression
// expression % rhs
MOD(rhs IntegerExpression) IntegerExpression
// expression ^ rhs
POW(rhs IntegerExpression) IntegerExpression
// expression & rhs
BIT_AND(rhs IntegerExpression) IntegerExpression
// expression | rhs
BIT_OR(rhs IntegerExpression) IntegerExpression
// expression # rhs
BIT_XOR(rhs IntegerExpression) IntegerExpression
// ~expression
BIT_NOT() IntegerExpression
// expression << rhs
BIT_SHIFT_LEFT(shift IntegerExpression) IntegerExpression
// expression >> rhs
BIT_SHIFT_RIGHT(shift IntegerExpression) IntegerExpression
}
```
Examples:
```go
(Film.Length.ADD(Int(20))).LT(Int(200))
Film.LanguageID.EQ(Int(2))
Film.LanguageID.BIT_SHIFT_LEFT(Int(2))
```

View file

@ -4,7 +4,7 @@ To generate files we need running database instance containing already defined d
This files can be generated in two ways:
#### Generating from command line
#### 1) Generating from command line
Install jetgen to GOPATH bin folder. This will allow generating jet files from the command line.
@ -53,7 +53,7 @@ Generating enum sql builder files...
Generating enum model files...
Done
```
#### Generating from code
#### 2) Generating from code
```
import "github.com/go-jet/jet/generator/postgresgen"
@ -77,7 +77,7 @@ In both ways, generator will:
- generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`.
Generated files folder structure will look like this:
```sh
```
|-- gen # destination folder
| `-- jetdb # database name
| `-- dvds # schema name
@ -252,7 +252,7 @@ const (
For a reference SQL table definition of table `film`:
```sql
```
CREATE TABLE dvds.film (
film_id integer DEFAULT nextval('dvds.film_film_id_seq'::regclass) NOT NULL,
title character varying(255) NOT NULL,

11
wiki/_Sidebar.md Normal file
View file

@ -0,0 +1,11 @@
* [Installation](https://github.com/go-jet/jet/wiki/Installation)
* [Generator](https://github.com/go-jet/jet/wiki/Generator)
* [Writing SQL in Go]()
* [Expressions](https://github.com/go-jet/jet/wiki/Expressions)
* [Statements]()
* [SELECT]()
* [INSERT]()
* [UPDATE]()
* [DELETE]()
* [LOCK]()