fisch/types.go
2025-03-18 21:19:18 +01:00

161 lines
2.5 KiB
Go
Executable file

package main
import (
"database/sql"
"fmt"
)
// Web
type NavItem struct {
Caption string
Destination string
}
func NewNavItem(caption, destination string) NavItem {
return NavItem{caption, destination}
}
type Stylename string
func NewStyleItemList(stylenames ...Stylename) []Stylename {
return stylenames
}
type TableColumns []string
type TableRow struct {
Columns TableColumns
Id int
}
type Table struct {
Collumns TableColumns
Rows []TableRow
}
// Database
type Location struct {
Id sql.NullInt64
Parent sql.NullInt64
Name sql.NullString
Description sql.NullString
Connection *Connection
}
func (l *Location) ToTableRow() TableRow {
columns := make(TableColumns, 3)
tr := TableRow{}
var path string
if l.Id.Valid {
columns[0] = fmt.Sprintf("%d", l.Id.Int64)
tr.Id = int(l.Id.Int64)
path, _ = l.Connection.QueryLocationFullPath(l.Id.Int64)
} else {
columns[0] = "NULL"
tr.Id = -1
}
columns[1] = path
if l.Description.Valid {
columns[2] = l.Description.String
} else {
columns[2] = "NULL"
}
tr.Columns = columns
return tr
}
func (l *Location) GetParent() *Location {
return nil
}
type Container struct {
Id sql.NullInt64
Name sql.NullString
}
func (c *Container) ToTableRow() TableRow {
columns := make(TableColumns, 2)
tr := TableRow{}
if c.Id.Valid {
columns[0] = fmt.Sprintf("%d", c.Id.Int64)
tr.Id = int(c.Id.Int64)
} else {
columns[0] = "NULL"
tr.Id = -1
}
if c.Name.Valid {
columns[1] = c.Name.String
} else {
columns[1] = "NULL"
}
tr.Columns = columns
return tr
}
type Part struct {
Id sql.NullInt64
Name sql.NullString
Tags sql.NullString
Location Location
Container Container
Connection *Connection
}
func (p *Part) ToTableRow() TableRow {
columns := make(TableColumns, 5)
tr := TableRow{}
if p.Id.Valid {
columns[0] = fmt.Sprintf("%d", p.Id.Int64)
tr.Id = int(p.Id.Int64)
} else {
columns[0] = "NULL"
tr.Id = -1
}
if p.Name.Valid {
columns[1] = p.Name.String
} else {
columns[1] = "NULL"
}
if p.Tags.Valid {
columns[2] = p.Tags.String
} else {
columns[2] = "NULL"
}
if p.Location.Id.Valid {
fullPath, err := p.Connection.QueryLocationFullPath(p.Location.Id.Int64)
if err != nil {
fmt.Println(err)
}
columns[3] = fullPath
} else {
columns[3] = "NULL"
}
if p.Container.Name.Valid {
columns[4] = p.Container.Name.String
} else {
columns[4] = "NULL"
}
tr.Columns = columns
return tr
}
// Interfaces
type DatabaseType interface {
ToTableRow() TableRow
}