mygorm

package
v0.0.0-...-6858033 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const MateTableCount = 10

MateTableCount is the number of tables used for mating. Counting starts at 1.

View Source
const UnknownHD = "--"

UnknownHD is the display and DB value for an unknown HD.

Variables

View Source
var (
	TypeUnknown    = TypeEnum{""}
	TypeString     = TypeEnum{"string"}
	TypeText       = TypeEnum{"text"}
	TypeCheckbox   = TypeEnum{"checkbox"}
	TypeInt        = TypeEnum{"integer"}
	TypeFloat      = TypeEnum{"floating point"}
	TypeDate       = TypeEnum{"date"}
	TypeDateTime   = TypeEnum{"timestamp"}
	TypeSelectOne  = TypeEnum{"select one"}
	TypeSelectMany = TypeEnum{"select many"}
)
View Source
var AllTypeEnums = []string{
	TypeUnknown.slug,
	TypeString.slug,
	TypeText.slug,
	TypeCheckbox.slug,
	TypeInt.slug,
	TypeFloat.slug,
	TypeDate.slug,
	TypeDateTime.slug,
	TypeSelectOne.slug,
	TypeSelectMany.slug,
}

Functions

func AfterDelete

func AfterDelete(tx *gorm.DB, tableIdx int) (bool, error)

AfterDelete deletes the chick associated with the mate table after the last mate has been deleted.

func CheckDoubleChick

func CheckDoubleChick(tx *gorm.DB, id uint, name string) error

CheckDoubleChick checks if the chick is already mating.

func ClearMateTable

func ClearMateTable(tx *gorm.DB, tableIdx int) error

ClearMateTable deletes all mates from the chosen mate table.

func CombineHD

func CombineHD(hd1, hd2 string) string

CombineHD combines the two given HD values in a predictable way.

func ComputeALC

func ComputeALC(tx *gorm.DB, dog *Dog) (float64, error)

ComputeALC calculates the correct ALC for 6 generations. WARNING: All generations have to be present!

func CreateChick

func CreateChick(tx *gorm.DB, chick *Chick, name string) error

CreateChick first checks if the chick is already mating and creates it only if not.

func FillMateTable

func FillMateTable(tx *gorm.DB, tableIdx int, chickID uint, chickName string) error

FillMateTable fills the chosen mate table with male dogs that are candidates for mating.

func FindFreeMateTable

func FindFreeMateTable(tx *gorm.DB) int

FindFreeMateTable returns the number (between 1 and 10) of the first currently unused mate table.

func Init

func Init(dbFname string) (*gorm.DB, error)

Init is initializing the DB.

Types

type BaseMetaFeature

type BaseMetaFeature struct {
	gorm.Model
	Name      string `gorm:"unique"`
	ShortName string `gorm:"unique"`
	Type      TypeEnum
	GroupID   uint
	Group     FeatureGroup `gorm:"foreignkey:GroupID;association_autocreate:false;association_autoupdate:false"`
}

type Chick

type Chick struct {
	ID        uint `gorm:"primary_key"`
	MateALC   Percentage
	MateHD    string `gorm:"size:8"`
	MateTable int    `gorm:"unique;not null"` // we allow up to 9 tables for male partners (mate1 ... mate9)
}

Chick is a female dog chosen for mating (opposite of Mate). The technically correct term for this ('Bitch') sounds insulting for many so I chose 'Chick' instead. Deleting a record from the corresponding table should not just fill a deleted_at column but really delete the DB row or the same dog couldn't mate twice. Because of that we don't use gorm.Model.

func ChickForMate

func ChickForMate(tx *gorm.DB, result interface{}) (*Chick, error)

func GetChickForTable

func GetChickForTable(tx *gorm.DB, tableNumber string) (*Chick, error)

GetChickForTable returns the current chick for the given mating table number.

type Color

type Color struct {
	gorm.Model
	Name     string `gorm:"unique"`
	HexValue string `gorm:"unique"`
}

type Dog

type Dog struct {
	gorm.Model
	Name      string `gorm:"size:32;unique;not null"`
	BirthDate *time.Time
	Gender    string `gorm:"size:16"`
	Star      Star
	ALC       Percentage
	HD        string `gorm:"size:8"`
	MateCount int
	FatherID  uint
	Father    MaleDog `gorm:"foreignkey:FatherID;association_autocreate:false;association_autoupdate:false"`
	MotherID  uint
	Mother    FemaleDog `gorm:"foreignkey:MotherID;association_autocreate:false;association_autoupdate:false"`
	Remark    string
}

Dog has got Mother and Father parents. This is the central data structure of the whole application.

func FindAncestorsForDog

func FindAncestorsForDog(tx *gorm.DB, dog *Dog, generations int) ([]*Dog, error)

FindAncestorsForDog finds all ancestors up to the given generation.

func FindAncestorsForID

func FindAncestorsForID(tx *gorm.DB, id int, generations int) ([]*Dog, error)

FindAncestorsForID finds all ancestors up to the given generation.

func (*Dog) BeforeDelete

func (d *Dog) BeforeDelete(tx *gorm.DB) error

BeforeDelete checks if the dog is currently mating and returns an error if this is the case.

func (*Dog) BeforeSave

func (d *Dog) BeforeSave(tx *gorm.DB) error

BeforeSave is initializing the new dogs HD value as soon as both parents are known.

type FeatureGroup

type FeatureGroup struct {
	gorm.Model
	Name      string `gorm:"unique"`
	ShortName string `gorm:"unique"`
	ColorID   uint
	Color     Color `gorm:"foreignkey:ColorID;association_autocreate:false;association_autoupdate:false"`
}

type FemaleDog

type FemaleDog struct {
	gorm.Model
	Name string
}

FemaleDog is a view of female dogs (rows in the dogs table with gender set to 'F'). A FemaleDog belongs to a Dog (it's child) as Mother.

type Litter

type Litter struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	Name      string
	ALC       Percentage
	HD        string `gorm:"size:8"`
	FatherID  uint
	Father    MaleDog `gorm:"foreignkey:FatherID;association_autocreate:false;association_autoupdate:false"`
	MotherID  uint
	Mother    FemaleDog `gorm:"foreignkey:MotherID;association_autocreate:false;association_autoupdate:false"`
	Remark    string
}

Litter is the result of a successful mating action.

func (*Litter) BeforeSave

func (p *Litter) BeforeSave(tx *gorm.DB) error

BeforeSave is initializing the new litters HD value as soon as both parents are known.

type MaleDog

type MaleDog struct {
	gorm.Model
	Name string
}

MaleDog is a view of male dogs (rows in the dogs table with gender set to 'M'). A MaleDog belongs to a Dog (it's child) as Father.

type Mate

type Mate struct {
	ID        uint `gorm:"primary_key"`
	Name      string
	BirthDate time.Time
	ALC       Percentage
	HD        string `gorm:"size:8"`
	MateCount int
	FatherID  uint
	Father    MaleDog `gorm:"foreignkey:FatherID;association_autocreate:false;association_autoupdate:false"`
	MotherID  uint
	Mother    FemaleDog `gorm:"foreignkey:MotherID;association_autocreate:false;association_autoupdate:false"`
	ChildALC  Percentage
	Remark    string
}

Mate is a male dog chosen as a candidate for mating. Mate is like Dog but doesn't need Star or Gender because it is always true and male. Deleting a record from the corresponding table should not just fill a deleted_at column but really delete the DB row or the same dog couldn't mate twice. Because of that we don't use gorm.Model.

func GenericMate

func GenericMate(result interface{}) *Mate

GenericMate returns the Mate common to all mate tables.

type Mate1

type Mate1 struct {
	Mate
}

Mate1 is the 1. mate table.

type Mate10

type Mate10 struct {
	Mate
}

Mate10 is the 10. mate table.

type Mate2

type Mate2 struct {
	Mate
}

Mate2 is the 2. mate table.

type Mate3

type Mate3 struct {
	Mate
}

Mate3 is the 3. mate table.

type Mate4

type Mate4 struct {
	Mate
}

Mate4 is the 4. mate table.

type Mate5

type Mate5 struct {
	Mate
}

Mate5 is the 5. mate table.

type Mate6

type Mate6 struct {
	Mate
}

Mate6 is the 6. mate table.

type Mate7

type Mate7 struct {
	Mate
}

Mate7 is the 7. mate table.

type Mate8

type Mate8 struct {
	Mate
}

Mate8 is the 8. mate table.

type Mate9

type Mate9 struct {
	Mate
}

Mate9 is the 9. mate table.

type Percentage

type Percentage float64

Percentage can be stored in the DB and displayed nicely (with only 2 decimal places).

func (Percentage) String

func (p Percentage) String() string

String is implemented for nicer looking numbers.

type Star

type Star bool

Star can be stored in the DB and displayed nicely (as a star).

func (*Star) Scan

func (s *Star) Scan(src interface{}) error

Scan is needed for sqlite3.

func (Star) String

func (s Star) String() string

String is implemented for nicer looking numbers.

type TypeEnum

type TypeEnum struct {
	// contains filtered or unexported fields
}

func NewTypeEnum

func NewTypeEnum(s string) TypeEnum

func (TypeEnum) String

func (te TypeEnum) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL