func (t *Territory) Save(ctx context.Context, db DB) error {
	if t.Exists() {
		return t.Update(ctx, db)
	}
	return t.Insert(ctx, db)
}
Save saves the Territory to the database.
package models
import (
	"context"
)
type Territory struct {
	TerritoryID          string `json:"territory_id"`           
	TerritoryDescription string `json:"territory_description"`  
	RegionID             int16  `json:"region_id"`              
	_exists, _deleted bool
}
func (t *Territory) Exists() bool {
	return t._exists
}
func (t *Territory) Deleted() bool {
	return t._deleted
}
func (t *Territory) Insert(ctx context.Context, db DB) error {
	switch {
	case t._exists:  
		return logerror(&ErrInsertFailed{ErrAlreadyExists})
	case t._deleted:  
		return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
	}
	const sqlstr = `INSERT INTO public.territories (` +
		`territory_id, territory_description, region_id` +
		`) VALUES (` +
		`$1, $2, $3` +
		`)`
	logf(sqlstr, t.TerritoryID, t.TerritoryDescription, t.RegionID)
	if _, err := db.ExecContext(ctx, sqlstr, t.TerritoryID, t.TerritoryDescription, t.RegionID); err != nil {
		return logerror(err)
	}
	t._exists = true
	return nil
}
func (t *Territory) Update(ctx context.Context, db DB) error {
	switch {
	case !t._exists:  
		return logerror(&ErrUpdateFailed{ErrDoesNotExist})
	case t._deleted:  
		return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
	}
	const sqlstr = `UPDATE public.territories SET (` +
		`territory_description, region_id` +
		`) = ( ` +
		`$1, $2` +
		`) WHERE territory_id = $3`
	logf(sqlstr, t.TerritoryDescription, t.RegionID, t.TerritoryID)
	if _, err := db.ExecContext(ctx, sqlstr, t.TerritoryDescription, t.RegionID, t.TerritoryID); err != nil {
		return logerror(err)
	}
	return nil
}