samples

package
v0.0.0-...-e005a71 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAlbumWithRandomTracks

func CreateAlbumWithRandomTracks(db *gorm.DB, albumTitle string, singerId int64, numTracks int) (int64, error)

CreateAlbumWithRandomTracks creates and stores a new Album in the database. Also generates numTracks random tracks for the Album. Returns the ID of the Album.

func CreateInterleavedTablesIfNotExist

func CreateInterleavedTablesIfNotExist(w io.Writer, db *gorm.DB) error

CreateInterleavedTablesIfNotExist creates all tables that are required for this sample if they do not yet exist.

func CreateRandomSingersAndAlbums

func CreateRandomSingersAndAlbums(w io.Writer, db *gorm.DB) error

CreateRandomSingersAndAlbums creates some random test records and stores these in the database.

func CreateSinger

func CreateSinger(db *gorm.DB, firstName, lastName string) (int64, error)

CreateSinger creates a new Singer and stores in the database. Returns the ID of the Singer.

func CreateVenueAndConcertInTransaction

func CreateVenueAndConcertInTransaction(w io.Writer, db *gorm.DB) error

CreateVenueAndConcertInTransaction creates a new Venue and a Concert in a read/write transaction.

func DeleteAllData

func DeleteAllData(db *gorm.DB) error

DeleteAllData deletes all existing records in the database.

func DeleteRandomAlbum

func DeleteRandomAlbum(w io.Writer, db *gorm.DB) error

DeleteRandomAlbum deletes a random Album. The Album could have one or more Tracks interleaved with it, but as the `INTERLEAVE IN PARENT` clause includes `ON DELETE CASCADE`, the child rows will be deleted along with the parent.

func DeleteRandomTrack

func DeleteRandomTrack(w io.Writer, db *gorm.DB) error

DeleteRandomTrack will delete a randomly chosen Track from the database. This function shows how to delete a record with a primary key consisting of more than one column.

func FirstOrCreateVenue

func FirstOrCreateVenue(w io.Writer, db *gorm.DB, name string) error

FirstOrCreateVenue tries to fetch an existing Venue from the database based on the name of the venue, and if not found, creates a new Venue record in the database.

func FirstOrInitVenue

func FirstOrInitVenue(w io.Writer, db *gorm.DB, name string) error

FirstOrInitVenue tries to fetch an existing Venue from the database based on the name of the venue, and if not found, initializes a Venue struct. This can then be used to create or update the record.

func PrintAlbumsFirstCharTitleAndFirstOrLastNameEqual

func PrintAlbumsFirstCharTitleAndFirstOrLastNameEqual(w io.Writer, db *gorm.DB) error

func PrintAlbumsReleaseBefore1900

func PrintAlbumsReleaseBefore1900(w io.Writer, db *gorm.DB) error

func PrintConcerts

func PrintConcerts(w io.Writer, db *gorm.DB) error

PrintConcerts prints the current concerts in the database to the console. It will preload all its associations, so it can directly print the properties of these as well.

func PrintSingersAlbumsAndTracks

func PrintSingersAlbumsAndTracks(w io.Writer, db *gorm.DB) error

PrintSingersAlbumsAndTracks queries and prints all Singers, Albums and Tracks in the database.

func PrintSingersWithLimitAndOffset

func PrintSingersWithLimitAndOffset(w io.Writer, db *gorm.DB) error

func QueryWithTimeout

func QueryWithTimeout(w io.Writer, db *gorm.DB) error

QueryWithTimeout will try to execute a query with a 1ms timeout. This will normally cause a Deadline Exceeded error to be returned.

func RunSample

func RunSample(w io.Writer, connString string) error

func SearchAlbumsUsingNamedArgument

func SearchAlbumsUsingNamedArgument(w io.Writer, db *gorm.DB, title string) error

SearchAlbumsUsingNamedArgument searches for Albums using a named argument.

func UpdateTracksInBatches

func UpdateTracksInBatches(w io.Writer, db *gorm.DB) error

UpdateTracksInBatches uses FindInBatches to iterate through a selection of Tracks in batches and updates each Track that it found.

func UpdateVenueDescription

func UpdateVenueDescription(w io.Writer, db *gorm.DB) error

UpdateVenueDescription updates the description of the 'Avenue Park' Venue.

Types

type Album

type Album struct {
	gorm.Model
	Title           string
	MarketingBudget sql.NullFloat64
	ReleaseDate     spanner.NullDate
	CoverPicture    []byte
	SingerId        int64
	Singer          Singer
	Tracks          []Track `gorm:"foreignKey:id"`
}

type Concert

type Concert struct {
	gorm.Model
	Name      string
	Venue     Venue
	VenueId   int64
	Singer    Singer
	SingerId  int64
	StartTime time.Time
	EndTime   time.Time
}

type Singer

type Singer struct {
	gorm.Model
	FirstName sql.NullString
	LastName  string
	// FullName is generated by the database. The '->' marks this a read-only field.
	FullName string `gorm:"->;type:STRING(MAX) AS (ARRAY_TO_STRING([first_name, last_name], \" \")) STORED;"`
	Active   bool
	Albums   []Album
}

type Track

type Track struct {
	gorm.Model
	TrackNumber int64 `gorm:"primaryKey;autoIncrement:false"`
	Title       string
	SampleRate  float64
	Album       Album `gorm:"foreignKey:id"`
}

Track is interleaved in Album. The ID column is both the first part of the primary key of Track, and a reference to the Album that owns the Track.

type Venue

type Venue struct {
	gorm.Model
	Name        string
	Description string
}

Jump to

Keyboard shortcuts

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