-
Notifications
You must be signed in to change notification settings - Fork 130
SQL Builder
go-jet edited this page Sep 21, 2019
·
14 revisions
SQL Builder files are Go files, containing types necessary to write type safe SQL queries in Go. They are autogenerated from database tables, views and enums.
Following rules are applied to generate table/views SQL Builder files:
- file names is always snake case of the table/view name.
- for every table/view there is one Go SQL Builder file generated.
- every file contains one type - struct with nested jet.Table.
- for every column of table/view there is a field column in SQL Builder table type. Field name is camel case of column name. See below table for type mapping.
-
AllColumns
is used as shorthand notation for list of all columns. -
MutableColumns
are all columns minus primary key columns (Useful in INSERT or UPDATE statements).
PostgreSQL:
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 |
MySQL and MariaDB:
Database type(postgres) | Sql builder column type |
---|---|
boolean | ColumnBool |
tinyint, smallint, mediumint, integer, bigint | ColumnInteger |
real, numeric, decimal, double precision | ColumnFloat |
date | ColumnDate |
timestamp, datetime | ColumnTimestamp |
time | ColumnTime |
enums, text, character, character varying | |
blob and all remaining types | ColumnString |
PostgreSQL table address
:
CREATE TABLE dvds.address
(
address_id serial NOT NULL DEFAULT,
address character varying(50) NOT NULL,
address2 character varying(50),
district character varying(20) NOT NULL,
city_id smallint NOT NULL,
postal_code character varying(10),
phone character varying(20) NOT NULL,
last_update timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT address_pkey PRIMARY KEY (address_id)
)
Part of the table sql builder file for table address
.
package table
import (
"github.com/go-jet/jet"
)
var Address = newAddressTable()
type AddressTable struct {
postgres.Table
//Columns
AddressID postgres.ColumnInteger
Address postgres.ColumnString
Address2 postgres.ColumnString
District postgres.ColumnString
CityID postgres.ColumnInteger
PostalCode postgres.ColumnString
Phone postgres.ColumnString
LastUpdate postgres.ColumnTimestamp
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
}
Following rules are applied to generate enum SQL Builder files:
- file names is always snake case of the enum name.
- for every enum there is one Go SQL Builder file generated.
- every file contains one type.
- PostgreSQL: File name is a snake case of
enum name
. - MySQL or MariaDB: File name is snake case of
table/view name
+enum name
.
- PostgreSQL: File name is a snake case of
- for every enum value there is a field in SQL Builder enum struct. Field name is camel case of enum value. Type is jet.StringExpression, meaning it can be used by string expressions methods.
PostgreSQL enum mpaa_rating
:
CREATE TYPE dvds.mpaa_rating AS ENUM
('G', 'PG', 'PG-13', 'R', 'NC-17');
Enum SQL Builder file for mpaa_rating
:
package enum
import "github.com/go-jet/jet"
var MpaaRating = &struct {
G postgres.StringExpression
PG postgres.StringExpression
PG13 postgres.StringExpression
R postgres.StringExpression
NC17 postgres.StringExpression
}{
G: postgres.NewEnumValue("G"),
PG: postgres.NewEnumValue("PG"),
PG13: postgres.NewEnumValue("PG-13"),
R: postgres.NewEnumValue("R"),
NC17: postgres.NewEnumValue("NC-17"),
}
- Home
- Generator
- Model
- SQL Builder
- Query Result Mapping (QRM)
-
FAQ
- How to execute jet statement in SQL transaction?
- How to construct dynamic projection list?
- How to construct dynamic condition?
- How to use jet in multi-tenant environment?
- How to change model field type?
- How to use custom(or currently unsupported) functions and operators?
- How to use IN/NOT_IN with dynamic list of values?
- Scan stopped working after naming a destination type