db

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2020 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EmailPending - status of a pending email
	EmailPending = 1
	// EmailFailed - status of a failed email
	EmailFailed = 0

	// KmlPlainTextContentType - content type used when
	// sighting_kml kml record is encoded in plain text KML
	KmlPlainTextContentType = 0
	// KmlGzipContentType - content type used when
	// sighting_kml kml record is gzipped KML
	KmlGzipContentType = 1
)

Variables

This section is empty.

Functions

func CheckRowsUpdated

func CheckRowsUpdated(res sql.Result, expectAffected int64) error

CheckRowsUpdated verifies the sql.Result affected the expected number of rows. Returns an error a different number of rows was affected.

func IsUniqueConstraintViolation

func IsUniqueConstraintViolation(err error) (bool, error)

IsUniqueConstraintViolation returns whether the query failed due to violating a unique constraint. It returns an error if err is not one of the supported SQL driver error types.

Types

type Aircraft

type Aircraft struct {
	ID        uint64    `db:"id"`
	Icao      string    `db:"icao"`
	CreatedAt time.Time `db:"created_at"`
	UpdatedAt time.Time `db:"updated_at"`
}

Aircraft database record. There will be one of these per icao.

type Database

type Database interface {
	// Transaction takes a function to execute inside a transaction context.
	// If the function returns an error, the transaction is rolled back.
	Transaction(f func(tx *sqlx.Tx) error) error

	// CreateProject creates a Project record.
	CreateProject(projectName string, now time.Time) (sql.Result, error)
	// GetProject searches for a Project by its name. If the project exists
	// it will be returned. Otherwise an error will be returned.
	GetProject(projectName string) (*Project, error)

	// CreateSession creates a new Session for a particular project.
	CreateSession(project *Project, identifier string, withSquawks bool, withTxTypes bool, withCallSigns bool) (sql.Result, error)
	// GetSessionByIdentifier searches for a Session belonging to the provided project.
	// If the Session exists it will be returned. Otherwise an error is returned.
	GetSessionByIdentifier(project *Project, identifier string) (*Session, error)
	// CloseSession marks the Session as closed. The sql.Result is returned
	// if the query was successful, otherwise an error is returned.
	CloseSession(session *Session, closedAt time.Time) (sql.Result, error)

	// GetAircraftByIcao searches for an Aircraft using it's hex ICAO. If the
	// Aircraft exists it will be returned. Otherwise an error is returned.
	GetAircraftByIcao(icao string) (*Aircraft, error)
	// GetAircraftById searches for an Aircraft using it's ID. If the
	// Aircraft exists it will be returned. Otherwise an error is returned.
	GetAircraftByID(id uint64) (*Aircraft, error)
	// CreateAircraft creates an Aircraft for the specified icao.
	CreateAircraft(icao string, firstSeenTime time.Time) (sql.Result, error)

	// CreateSighting creates a sighting for ac on the provided Session. A sql.Result
	// is returned if the query was successful. Otherwise an error is returned.
	CreateSighting(session *Session, ac *Aircraft, firstSeenTime time.Time) (sql.Result, error)
	// CreateSightingTx creates a sighting for ac on the provided Session, executing
	// the query with the provided transaction. A sql.Result is returned if the query
	// was successful. Otherwise an error is returned.
	CreateSightingTx(tx *sqlx.Tx, session *Session, ac *Aircraft, firstSeenTime time.Time) (sql.Result, error)
	// GetSightingById searches for a Sighting with the provided ID. The Sighting is returned
	// if one was found. Otherwise an error is returned.
	GetSightingByID(sightingID uint64) (*Sighting, error)
	// GetSightingById searches for a Sighting with the provided ID, executing the query
	// with the provided transaction. The Sighting is returned if one was found. Otherwise
	// an error is returned.
	GetSightingByIDTx(tx *sqlx.Tx, sightingID uint64) (*Sighting, error)
	// GetLastSighting searches for a Sighting for ac in the provided Session. The Sighting
	// is returned if one was found. Otherwise an error is returned.
	GetLastSighting(session *Session, ac *Aircraft) (*Sighting, error)
	// GetLastSightingTx searches for a Sighting for ac in the provided Session, executing
	// the query with the provided transaction. The Sighting is returned if one was found.
	// Otherwise an error is returned.
	GetLastSightingTx(tx *sqlx.Tx, session *Session, ac *Aircraft) (*Sighting, error)
	// ReopenSighting updates the provided Sighting to mark it as open. A sql.Result
	// is returned if the query was successful. Otherwise an error is returned.
	ReopenSighting(sighting *Sighting) (sql.Result, error)
	// ReopenSightingTx updates the provided Sighting to mark it as open, executing
	// the query with the provided transaction. A sql.Result is returned if the query
	// was successful. Otherwise an error is returned.
	ReopenSightingTx(tx *sqlx.Tx, sighting *Sighting) (sql.Result, error)
	// CloseSightingBatch takes a list of sightings and marks them as closed in a batch.
	// If successful, nil is returned. Otherwise an error is returned.
	CloseSightingBatch(sightings []*Sighting, closedAt time.Time) error
	// UpdateSightingCallsignTx updates the Sighting.Callsign to the provided callsign.
	// A sql.Result is returned if the query is successful. Otherwise an error is returned.
	UpdateSightingCallsignTx(tx *sqlx.Tx, sighting *Sighting, callsign string) (sql.Result, error)
	// UpdateSightingSquawkTx updates the Sighting.Squawk to the provided squawk.
	// A sql.Result is returned if the query is successful. Otherwise an error is returned.
	UpdateSightingSquawkTx(tx *sqlx.Tx, sighting *Sighting, squawk string) (sql.Result, error)

	// CreateNewSightingCallSignTx inserts a new SightingCallSign for a sighting, executing
	// the query on the provided tx. A sql.Result is returned if the query was successful.
	// Otherwise an error is returned.
	CreateNewSightingCallSignTx(tx *sqlx.Tx, sighting *Sighting, callsign string, observedAt time.Time) (sql.Result, error)
	// CreateNewSightingSquawkTx inserts a new SightingSquaqk for a sighting, executing
	// the query on the provided tx. A sql.Result is returned if the query was successful.
	// Otherwise an error is returned.
	CreateNewSightingSquawkTx(tx *sqlx.Tx, sighting *Sighting, squawk string, observedAt time.Time) (sql.Result, error)

	// CreateSightingLocation inserts a new SightingCallSign for a sighting. A sql.Result is
	// returned if the query was successful. Otherwise an error is returned.
	CreateSightingLocation(sightingID uint64, t time.Time, altitude int64, lat float64, long float64) (sql.Result, error)
	// CreateSightingLocationTx inserts a new SightingLocation for a sighting, executing
	// the query on the provided tx. A sql.Result is returned if the query was successful.
	// Otherwise an error is returned.
	CreateSightingLocationTx(tx *sqlx.Tx, sightingID uint64, t time.Time, altitude int64, lat float64, long float64) (sql.Result, error)
	// LoadLocationHistory searches for SightingLocation records for the provided Sighting.
	// lastID should initially be zero, and in subsequent calls the ID of the last processed
	// row should be used instead. At most batchSize results will be returned. If the query
	// succeeds, the rows are returned (and must be closed by the caller). If unsuccessful
	// an error is returned.
	LoadLocationHistory(sighting *Sighting, lastID int64, batchSize int64) (*sqlx.Rows, error)
	// WalkLocationHistoryBatch searches for SightingLocation records by loading at most
	// batchSize results at a time, invoking f with each batch of results. An error is returned
	// if we were unsuccessful.
	WalkLocationHistoryBatch(sighting *Sighting, batchSize int64, f func([]SightingLocation)) error
	// GetFullLocationHistory queries for all SightingLocation records. Internally, use batchSize
	// to limit the number of rows returned by each query. If successful, the full location history
	// is returned. Otherwise, an error will be returned.
	GetFullLocationHistory(sighting *Sighting, batchSize int64) ([]SightingLocation, error)

	// GetSightingKml queries for a SightingKml record for the provided Sighting. The SightingKml
	// is returned if found, otherwise an error is returned.
	GetSightingKml(sighting *Sighting) (*SightingKml, error)
	// UpdateSightingKml commits an updated KML field to the database. A sql.Result is returned
	// if the query was successful, otherwise an error is returned.
	UpdateSightingKml(sightingKml *SightingKml) (sql.Result, error)
	// CreateSightingKmlContent creates a SightingKml record for the provided Sighting. A sql.Result is
	// returned if the query was successful, otherwise an error is returned.
	CreateSightingKmlContent(sighting *Sighting, kmlData []byte) (sql.Result, error)

	// CreateEmailJobTx inserts a new Email record, executing the query on the provided tx. A
	// sql.Result is returned if the query was successful, otherwise an error is returned.
	CreateEmailJobTx(tx *sqlx.Tx, createdAt time.Time, content []byte) (sql.Result, error)
	// GetPendingEmailJobs searches for non-failed emails with a retryTime less than or equal to
	// currentTime. A list of Email records is returned if successful, otherwise an error is
	// returned.
	GetPendingEmailJobs(currentTime time.Time) ([]Email, error)
	// DeleteCompletedEmailTx deletes the specified job, executing the query on the provided tx.
	// A sql.Result is returned if the query was successful, otherwise an error is returned.
	DeleteCompletedEmailTx(tx *sqlx.Tx, job Email) (sql.Result, error)
	// MarkEmailFailedTx sets job's status to failed, executing the query on the provided tx.
	// A sql.Result is returned if the query was successful, otherwise an error is returned.
	MarkEmailFailedTx(tx *sqlx.Tx, job *Email) (sql.Result, error)
	// RetryEmailAfterTx updates the job records retryAfter to the provided retryAfter value.
	// A sql.Result is returned if the query was successful, otherwise an error is returned.
	RetryEmailAfterTx(tx *sqlx.Tx, job *Email, retryAfter time.Time) (sql.Result, error)
}

Database - interface for the database queries

type DatabaseImpl

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

DatabaseImpl - Implements Database.

func NewDatabase

func NewDatabase(db *sqlx.DB, dialect goqu.DialectWrapper) *DatabaseImpl

NewDatabase creates a DatabaseImpl with the provided db and dialect.

func (*DatabaseImpl) CloseSession

func (d *DatabaseImpl) CloseSession(session *Session, closedAt time.Time) (sql.Result, error)

CloseSession - see Database.CloseSession

func (*DatabaseImpl) CloseSightingBatch

func (d *DatabaseImpl) CloseSightingBatch(sightings []*Sighting, closedAt time.Time) error

CloseSightingBatch - see Database.CloseSightingBatch

func (*DatabaseImpl) CreateAircraft

func (d *DatabaseImpl) CreateAircraft(icao string, firstSeenTime time.Time) (sql.Result, error)

CreateAircraft - see Database.CreateAircraft

func (*DatabaseImpl) CreateEmailJobTx

func (d *DatabaseImpl) CreateEmailJobTx(tx *sqlx.Tx, createdAt time.Time, content []byte) (sql.Result, error)

CreateEmailJobTx - see Database.CreateEmailJobTx

func (*DatabaseImpl) CreateNewSightingCallSignTx

func (d *DatabaseImpl) CreateNewSightingCallSignTx(tx *sqlx.Tx, sighting *Sighting, callsign string, observedAt time.Time) (sql.Result, error)

CreateNewSightingCallSignTx - see Database.CreateNewSightingCallSignTx

func (*DatabaseImpl) CreateNewSightingSquawkTx

func (d *DatabaseImpl) CreateNewSightingSquawkTx(tx *sqlx.Tx, sighting *Sighting, squawk string, observedAt time.Time) (sql.Result, error)

CreateNewSightingSquawkTx - see Database.CreateNewSightingSquawkTx

func (*DatabaseImpl) CreateProject

func (d *DatabaseImpl) CreateProject(name string, now time.Time) (sql.Result, error)

CreateProject - see Database.CreateProject

func (*DatabaseImpl) CreateSession

func (d *DatabaseImpl) CreateSession(project *Project, identifier string, withSquawks bool, withTxTypes bool, withCallSigns bool) (sql.Result, error)

CreateSession - see Database.CreateSession

func (*DatabaseImpl) CreateSighting

func (d *DatabaseImpl) CreateSighting(session *Session, ac *Aircraft, firstSeenTime time.Time) (sql.Result, error)

CreateSighting - see Database.CreateSighting

func (*DatabaseImpl) CreateSightingKmlContent

func (d *DatabaseImpl) CreateSightingKmlContent(sighting *Sighting, kmlData []byte) (sql.Result, error)

CreateSightingKmlContent - see Database.CreateSightingKmlContent

func (*DatabaseImpl) CreateSightingLocation

func (d *DatabaseImpl) CreateSightingLocation(sightingID uint64, t time.Time, altitude int64, lat float64, long float64) (sql.Result, error)

CreateSightingLocation - see Database.CreateSightingLocation

func (*DatabaseImpl) CreateSightingLocationTx

func (d *DatabaseImpl) CreateSightingLocationTx(tx *sqlx.Tx, sightingID uint64, t time.Time, altitude int64, lat float64, long float64) (sql.Result, error)

CreateSightingLocationTx - see Database.CreateSightingLocationTx

func (*DatabaseImpl) CreateSightingTx

func (d *DatabaseImpl) CreateSightingTx(tx *sqlx.Tx, session *Session, ac *Aircraft, firstSeenTime time.Time) (sql.Result, error)

CreateSightingTx - see Database.CreateSightingTx

func (*DatabaseImpl) DeleteCompletedEmailTx

func (d *DatabaseImpl) DeleteCompletedEmailTx(tx *sqlx.Tx, job Email) (sql.Result, error)

DeleteCompletedEmailTx - see Database.DeleteCompletedEmailTx

func (*DatabaseImpl) GetAircraftByID

func (d *DatabaseImpl) GetAircraftByID(id uint64) (*Aircraft, error)

GetAircraftByID - see Database.GetAircraftByID

func (*DatabaseImpl) GetAircraftByIcao

func (d *DatabaseImpl) GetAircraftByIcao(icao string) (*Aircraft, error)

GetAircraftByIcao - see Database.GetAircraftByIcao

func (*DatabaseImpl) GetFullLocationHistory

func (d *DatabaseImpl) GetFullLocationHistory(sighting *Sighting, batchSize int64) ([]SightingLocation, error)

GetFullLocationHistory - see Database.GetFullLocationHistory

func (*DatabaseImpl) GetLastSighting

func (d *DatabaseImpl) GetLastSighting(session *Session, ac *Aircraft) (*Sighting, error)

GetLastSighting - see Database.GetLastSighting

func (*DatabaseImpl) GetLastSightingCallSignTx

func (d *DatabaseImpl) GetLastSightingCallSignTx(tx *sqlx.Tx, sighting *Sighting) (*SightingCallSign, error)

GetLastSightingCallSignTx - see Database.GetLastSightingCallSignTx

func (*DatabaseImpl) GetLastSightingSquawkTx

func (d *DatabaseImpl) GetLastSightingSquawkTx(tx *sqlx.Tx, sighting *Sighting) (*SightingSquawk, error)

GetLastSightingSquawkTx - see Database.GetLastSightingSquawkTx

func (*DatabaseImpl) GetLastSightingTx

func (d *DatabaseImpl) GetLastSightingTx(tx *sqlx.Tx, session *Session, ac *Aircraft) (*Sighting, error)

GetLastSightingTx - see Database.GetLastSightingTx

func (*DatabaseImpl) GetPendingEmailJobs

func (d *DatabaseImpl) GetPendingEmailJobs(now time.Time) ([]Email, error)

GetPendingEmailJobs - see Database.GetPendingEmailJobs Does not return sql.ErrNoRows

func (*DatabaseImpl) GetProject

func (d *DatabaseImpl) GetProject(name string) (*Project, error)

GetProject - see Database.GetProject

func (*DatabaseImpl) GetSchemaMigration

func (d *DatabaseImpl) GetSchemaMigration() (*SchemaMigrations, error)

GetSchemaMigration - see Database.GetSchemaMigration

func (*DatabaseImpl) GetSessionByIdentifier

func (d *DatabaseImpl) GetSessionByIdentifier(project *Project, identifier string) (*Session, error)

GetSessionByIdentifier - see Database.GetSessionByIdentifier

func (*DatabaseImpl) GetSightingByID

func (d *DatabaseImpl) GetSightingByID(sightingID uint64) (*Sighting, error)

GetSightingByID - see Database.GetSightingByID

func (*DatabaseImpl) GetSightingByIDTx

func (d *DatabaseImpl) GetSightingByIDTx(tx *sqlx.Tx, sightingID uint64) (*Sighting, error)

GetSightingByIDTx - see Database.GetSightingByIDTx

func (*DatabaseImpl) GetSightingKml

func (d *DatabaseImpl) GetSightingKml(sighting *Sighting) (*SightingKml, error)

GetSightingKml - see Database.GetSightingKml

func (*DatabaseImpl) LoadLocationHistory

func (d *DatabaseImpl) LoadLocationHistory(sighting *Sighting, lastID int64, batchSize int64) (*sqlx.Rows, error)

LoadLocationHistory - see Database.LoadLocationHistory

func (*DatabaseImpl) MarkEmailFailedTx

func (d *DatabaseImpl) MarkEmailFailedTx(tx *sqlx.Tx, job *Email) (sql.Result, error)

MarkEmailFailedTx - see Database.MarkEmailFailedTx

func (*DatabaseImpl) ReopenSighting

func (d *DatabaseImpl) ReopenSighting(sighting *Sighting) (sql.Result, error)

ReopenSighting - see Database.ReopenSighting

func (*DatabaseImpl) ReopenSightingTx

func (d *DatabaseImpl) ReopenSightingTx(tx *sqlx.Tx, sighting *Sighting) (sql.Result, error)

ReopenSightingTx - see Database.ReopenSightingTx

func (*DatabaseImpl) RetryEmailAfterTx

func (d *DatabaseImpl) RetryEmailAfterTx(tx *sqlx.Tx, job *Email, retryAfter time.Time) (sql.Result, error)

RetryEmailAfterTx - see Database.RetryEmailAfterTx

func (*DatabaseImpl) Transaction

func (d *DatabaseImpl) Transaction(f func(tx *sqlx.Tx) error) error

Transaction - see Database.Transaction

func (*DatabaseImpl) UpdateSightingCallsignTx

func (d *DatabaseImpl) UpdateSightingCallsignTx(tx *sqlx.Tx, sighting *Sighting, callsign string) (sql.Result, error)

UpdateSightingCallsignTx - see Database.UpdateSightingCallsignTx

func (*DatabaseImpl) UpdateSightingKml

func (d *DatabaseImpl) UpdateSightingKml(sightingKml *SightingKml) (sql.Result, error)

UpdateSightingKml - see Database.UpdateSightingKml

func (*DatabaseImpl) UpdateSightingSquawkTx

func (d *DatabaseImpl) UpdateSightingSquawkTx(tx *sqlx.Tx, sighting *Sighting, squawk string) (sql.Result, error)

UpdateSightingSquawkTx - see Database.UpdateSightingSquawkTx

func (*DatabaseImpl) WalkLocationHistoryBatch

func (d *DatabaseImpl) WalkLocationHistoryBatch(sighting *Sighting, batchSize int64, f func([]SightingLocation)) error

WalkLocationHistoryBatch - see Database.WalkLocationHistoryBatch

type Email

type Email struct {
	ID         uint64     `db:"id"`
	Status     int32      `db:"status"`
	Retries    int32      `db:"retries"`
	RetryAfter *time.Time `db:"retry_after"`
	CreatedAt  time.Time  `db:"created_at"`
	UpdatedAt  time.Time  `db:"updated_at"`
	Job        []byte
}

Email database record. Contains the encoded job, as well as information relating to it's pending status. Will be deleted if successfully processed, otherwise will be left in the failed state.

type Project

type Project struct {
	ID         uint64    `db:"id"`
	Identifier string    `db:"identifier"`
	Label      *string   `db:"label"`
	CreatedAt  time.Time `db:"created_at"`
	UpdatedAt  time.Time `db:"updated_at"`
	// todo: why is project here? should it be deleted?
	DeletedAt *time.Time `db:"deleted_at"`
}

Project database record. Created the first time a project is used.

type SchemaMigrations

type SchemaMigrations struct {
	Version uint64 `db:"version"`
	Dirty   bool   `db:"dirty"`
}

SchemaMigrations database record. Contains information about state of database migrations.

type Session

type Session struct {
	ID                    uint64     `db:"id"`
	Identifier            string     `db:"identifier"`
	ProjectID             uint64     `db:"project_id"`
	ClosedAt              *time.Time `db:"closed_at"`
	CreatedAt             time.Time  `db:"created_at"`
	UpdatedAt             time.Time  `db:"updated_at"`
	DeletedAt             *time.Time `db:"deleted_at"`
	WithSquawks           bool       `db:"with_squawks"`
	WithTransmissionTypes bool       `db:"with_transmission_types"`
	WithCallSigns         bool       `db:"with_callsigns"`
}

Session database record. Created each time a project is used.

type Sighting

type Sighting struct {
	ID         uint64  `db:"id"`
	ProjectID  uint64  `db:"project_id"`
	SessionID  uint64  `db:"session_id"`
	AircraftID uint64  `db:"aircraft_id"`
	CallSign   *string `db:"callsign"`

	CreatedAt time.Time  `db:"created_at"`
	UpdatedAt time.Time  `db:"updated_at"`
	ClosedAt  *time.Time `db:"closed_at"`

	TransmissionTypes uint8   `db:"transmission_types"`
	Squawk            *string `db:"squawk"`
}

Sighting database record. Created every time an aircraft is watched by a project in a session.

type SightingCallSign

type SightingCallSign struct {
	ID         uint64    `db:"id"`
	SightingID uint64    `db:"sighting_id"`
	CallSign   string    `db:"callsign"`
	ObservedAt time.Time `db:"observed_at"`
}

SightingCallSign database record. Created for the first callsign and for newly adopted callsign.

type SightingKml

type SightingKml struct {
	ID          uint64 `db:"id"`
	SightingID  uint64 `db:"sighting_id"`
	ContentType int32  `db:"content_type"`
	Kml         []byte `db:"kml"`
}

SightingKml database record. Created once per sighting (as it closes). May be updated in future if the sighting is reopened.

func (*SightingKml) DecodedKml

func (k *SightingKml) DecodedKml() ([]byte, error)

DecodedKml - decodes SightingKml.Kml based on SightingKml.ContentType. Returns the KML file, or an error if one occurred.

func (*SightingKml) UpdateKml

func (k *SightingKml) UpdateKml(kml []byte) error

UpdateKml - updates the sighting_kml record

type SightingLocation

type SightingLocation struct {
	ID         uint64    `db:"id"`
	SightingID uint64    `db:"sighting_id"`
	TimeStamp  time.Time `db:"timestamp"`
	Altitude   int64     `db:"altitude"`
	Latitude   float64   `db:"latitude"`
	Longitude  float64   `db:"longitude"`
}

SightingLocation database record. Created each time a sightings location is updated.

type SightingSquawk

type SightingSquawk struct {
	ID         uint64    `db:"id"`
	SightingID uint64    `db:"sighting_id"`
	Squawk     string    `db:"squawk"`
	ObservedAt time.Time `db:"observed_at"`
}

SightingSquawk database record. Created for the first squawk, and for newly adopted squawks.

type TxExecer

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

TxExecer performs the task exec in a transaction against db.

func NewTxExecer

func NewTxExecer(db *sqlx.DB, f func(tx *sqlx.Tx) error) *TxExecer

NewTxExecer creates a TxExecer from db and f.

func (*TxExecer) Exec

func (e *TxExecer) Exec() error

Exec creates a transaction on db and runs the task. It takes care of BEGIN/COMMIT/ROLLBACK and will rollback if an error occurs or is returned after invoking exec.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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