Add Model files wiki page.

This commit is contained in:
go-jet 2019-07-04 14:12:59 +02:00
parent 950663dadb
commit 471bddcaf5
6 changed files with 149 additions and 92 deletions

View file

@ -181,91 +181,3 @@ var MpaaRating = &struct {
}
```
#### Model files
Sample model file for table `film`:
```
package model
import (
"time"
)
type Film struct {
FilmID int32 `sql:"primary_key"`
Title string
Description *string
ReleaseYear *int32
LanguageID int16
RentalDuration int16
RentalRate float64
Length *int16
ReplacementCost float64
Rating *MpaaRating
LastUpdate time.Time
SpecialFeatures *string
Fulltext string
}
```
For every column of table `film` there is appropriate field in model type.
Fields corresponding to primary key columns are tagged with `sql:"primary_key"`.
This tag is used during query execution to group row results into desired arbitrary structure. See more at TODO:
Fields are pointer types, if they relate to column that can be NULL.
Mappings of database types to Go types:
| Database type(postgres) | Go type |
| ----------------------------------------------- | -------------------------------------------------- |
| boolean | bool |
| smallint | int16 |
| integer | int32 |
| bigint | int64 |
| real | float32 |
| numeric, decimal, double precision | float64 |
| date, timestamp, time(with or without timezone) | time.Time |
| bytea | []byte |
| uuid | uuid.UUID |
| enum | enum name |
| text, character, character varying, | |
| and all remaining types | string |
Part of the sample model file for enum `mpaa_rating`:
```
package model
import "errors"
type MpaaRating string
const (
MpaaRating_G MpaaRating = "G"
MpaaRating_PG MpaaRating = "PG"
MpaaRating_PG13 MpaaRating = "PG-13"
MpaaRating_R MpaaRating = "R"
MpaaRating_NC17 MpaaRating = "NC-17"
)
```
For a reference SQL table definition of table `film`:
```
CREATE TABLE dvds.film (
film_id integer DEFAULT nextval('dvds.film_film_id_seq'::regclass) NOT NULL,
title character varying(255) NOT NULL,
description text,
release_year dvds.year,
language_id smallint NOT NULL,
rental_duration smallint DEFAULT 3 NOT NULL,
rental_rate numeric(4,2) DEFAULT 4.99 NOT NULL,
length smallint,
replacement_cost numeric(5,2) DEFAULT 19.99 NOT NULL,
rating dvds.mpaa_rating DEFAULT 'G'::dvds.mpaa_rating,
last_update timestamp without time zone DEFAULT now() NOT NULL,
special_features text[],
fulltext tsvector NOT NULL
);
```